summaryrefslogtreecommitdiffstats
path: root/pulsecaster/pulseaudio/PulseObj.py
blob: ce6edad02e7afd5220cddc3da994cdf6541117b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# vi: et sw=2
#
# PulseObj.py
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#

from lib_pulseaudio import *
from PulseSink      import PulseSinkInputInfo, PulseSinkInfo
from PulseSource    import PulseSourceOutputInfo, PulseSourceInfo
from PulseClient    import PulseClientCtypes
#from PulseStream    import PulseStream

################################################################################
#
# Classes
#
################################################################################

class PulseObj:
  "Basic PulseAudio object"

  ##############################################################################
  #
  # Init
  #
  ##############################################################################

  def __init__(self, server = None, retry = False, clientName = 'Unknown (Python)'):
    "Initialise pulseaudio connection"

    # Variables
    self.server       = server
    self.mainloop     = None
    self.mainloop_api = None
    self.context      = None
    self.ret          = None
    self.retry        = retry
    self.operation    = None
    self.connected    = False
    self.action_done  = False
    self.data         = None

    # Init

    #
    # Callbacks
    #
    self.PA_SIGNAL_CB = PA_SIGNAL_CB_T(self.py_signal_cb)

    self.PA_STATE_CB = PA_STATE_CB_T(self.py_state_cb)
    #
    # Mainloop init
    #
    self.mainloop = pa_mainloop_new()

    self.mainloop_api = pa_mainloop_get_api(self.mainloop)

    #
    # Signal binding
    #
    r = pa_signal_init(self.mainloop_api)

    if r != 0:
      # FIXME
      print "FIXME Do something. Something is wrong"

    # SIGINT
    pa_signal_new(2, self.PA_SIGNAL_CB, None)

    # SIGTERM
    pa_signal_new(15, self.PA_SIGNAL_CB, None)

    #
    # Context creating
    #
    self.context = pa_context_new(self.mainloop_api, clientName)

    pa_context_set_state_callback(self.context, self.PA_STATE_CB, None)

    self.start_action()

    #
    # Connect
    #
    if 0 > pa_context_connect(self.context,
                              self.server,
                              0,
                              None):
        if self.retry:
          pa_context_disconnect(self.context)
          return
        self.pulse_context_error()

    self.pulse_iterate()

    return

  ##############################################################################
  #
  # Callback methods
  #
  # FIXME: rename methods better
  #
  ##############################################################################

  def py_signal_cb(self, api, e, sig, userdata):
    #print "py_signal_cb:", api, e, sig, userdata

    if sig == 2:
      self.pulse_disconnect()
    elif sig == 15:
      self.pulse_disconnect()

    return 0

  ###

  def py_state_cb(self, c, b):
    #print "py_state_cb:", c[0]._opaque_struct, b
    state = pa_context_get_state(c);


    if state == 0:
      None
      #print "py_state_cb: Unconnected"

    elif state == 1:
      None
      #print "py_state_cb: Connecting"

    elif state == 2:
      None
      #print "py_state_cb: Authorizing"

    elif state == 3:
      None
      #print "py_state_cb: Setting name"

    elif state == 4:
      #print "py_state_cb: Ready"
      self.complete_action()
      self.connected = True

    elif state == 5:
      None
      #print "py_state_cb: Failed"

    elif state == 6:
      None
      #print "py_state_cb: Terminated"
      if not self.retry:
        import sys
        sys.exit(pa_context_errno(c))

      self.complete_action()

    else:
      None
      #print "py_state_cb: Unknown state", state


    #print "py_state_cb:", pa_strerror(pa_context_errno(c))
    return 0

  ###

  def py_client_cb(self, c, client_info, endofdata, userdata):
    "Sink callback"
    #print "py_client_cb:", c, client_info, endofdata, userdata

    if (endofdata):
      self.complete_action()
      return 0

    if self.data == None:
      self.data = [ PulseClientCtypes(client_info[0]) ]
    else:
      self.data.append(PulseClientCtypes(client_info[0]))

    return 0

  ###

  def py_sink_input_cb(self, c, sink_input_info, endofdata, userdata):
    "Sink input callback"
    #print "py_sink_input_cb:", c, sink_input_info, endofdata, userdata

    if (endofdata):
      self.complete_action()
      return 0

    if self.data == None:
      self.data = [ PulseSinkInputInfo(sink_input_info[0]) ]
    else:
      self.data.append(PulseSinkInputInfo(sink_input_info[0]))

    return 0

  ###

  def py_sink_cb(self, c, sink_info, endofdata, userdata):
    "Sink callback"
    #print "py_sink_cb:", c, sink_info, endofdata, userdata

    if (endofdata):
      self.complete_action()
      return 0

    if self.data == None:
      self.data = [ PulseSinkInfo(sink_info[0]) ]
    else:
      self.data.append(PulseSinkInfo(sink_info[0]))

    return 0
  ###

  def py_source_output_cb(self, c, source_output_info, endofdata, userdata):
    "Source output callback"
    #print "py_source_output_cb:", c, source_output_info, endofdata, userdata

    if (endofdata):
      self.complete_action()
      return 0

    if self.data == None:
      self.data = [ PulseSourceOutputInfo(source_output_info[0]) ]
    else:
      self.data.append(PulseSourceOutputInfo(source_output_info[0]))

    return 0

  ###

  def py_source_cb(self, c, source_info, endofdata, userdata):
    "Source callback"
    #print "py_source_cb:", c, source_info, endofdata, userdata

    if (endofdata):
      self.complete_action()
      return 0

    if self.data == None:
      self.data = [ PulseSourceInfo(source_info[0]) ]
    else:
      self.data.append(PulseSourceInfo(source_info[0]))

    return 0
  ###

  def py_drain_cb(self, c, userdata):
    #print "py_drain_cb: called"
    return

  ###

  def py_subscribe_cb(self, c, event, index, userdata):
    print 'py_subscribe_cb: called'
    print 'py_subscribe_cb:', c, event, index, userdata
    self.complete_action()
    return 0

  def py_context_success(self, c, success, userdata):
    if success == 0:
      None
      #print "py_context_success: Failed"
    else:
      None
      #print "py_context_success: Success"

    self.complete_action()
    return 0

  ##############################################################################
  #
  ##############################################################################

  def complete_action(self):
    "Completed action"
    #print "complete_action: Called"
    self.action_done = True
    return

  ###

  def start_action(self):
    "Call every time when starting action"
    #print "start_action: Called"
    self.action_done = False
    return

  ###

  def pulse_disconnect(self):
    "Call when disconnect object"

    #print "pulse_disconnect: Disconnecting"
    pa_context_disconnect(self.context)
    pa_mainloop_free(self.mainloop)
    return

  ###

  def pulse_context_error(self):
    "Print context error msg"

    #print "pulse_context_error:", pa_strerror(pa_context_errno(self.context))
    self.pulse_disconnect()
    return

  ###

  def pulse_sink_input_list(self):
    "List all sink input"
    #print "pulse_sink_input_list: Called";

    return_data = None

    self.start_action()

    # Callback function
    SINK_INPUT_LIST_CB = PA_SINK_INPUT_INFO_CB_T(self.py_sink_input_cb)

    self.operation = pa_context_get_sink_input_info_list(self.context,
                                                         SINK_INPUT_LIST_CB,
                                                         None)
    self.pulse_iterate()
                        
    #print "pulse_sink_input_list:", self.data
    return_data = self.data
    self.data = None

    return return_data

  ###

  def pulse_sink_list(self):
    "List all sinks"
    #print "pulse_sink_list: Called";

    return_data = None

    self.start_action()

    # Callback function
    SINK_LIST_CB = PA_SINK_INFO_CB_T(self.py_sink_cb)

    self.operation = pa_context_get_sink_info_list(self.context,
                                                   SINK_LIST_CB,
                                                   None)
    self.pulse_iterate()
                        
    #print "pulse_sink_list:", self.data
    return_data = self.data
    self.data = None

    return return_data

  ###

  def pulse_source_output_list(self):
    "List all source outputs"
    #print "pulse_source_output_list: Called";

    return_data = None

    self.start_action()

    # Callback function
    SOURCE_OUTPUT_LIST_CB = PA_SOURCE_OUTPUT_INFO_CB_T(self.py_source_output_cb)

    self.operation = pa_context_get_source_output_info_list(self.context,
                                                            SOURCE_OUTPUT_LIST_CB,
                                                            None)
    self.pulse_iterate()
                        
    #print "pulse_source_output_list:", self.data
    return_data = self.data
    self.data = None

    return return_data

  ###

  def pulse_source_list(self):
    "List all sources"
    #print "pulse_source_list: Called";

    return_data = None

    self.start_action()

    # Callback function
    SOURCE_LIST_CB = PA_SOURCE_INFO_CB_T(self.py_source_cb)

    self.operation = pa_context_get_source_info_list(self.context,
                                                     SOURCE_LIST_CB,
                                                     None)
    self.pulse_iterate()
                        
    #print "pulse_source_list:", self.data
    return_data = self.data
    self.data = None

    return return_data

  ###
  def pulse_client_list(self):
    "Fetch all clients"

    self.start_action()

    CLIENT_INFO_CB = PA_CLIENT_INFO_CB_T(self.py_client_cb)

    self.operation = pa_context_get_client_info_list(self.context,
                                                     CLIENT_INFO_CB,
                                                     None)

    self.pulse_iterate()

    #print "pulse_client_list:", self.data
    return_data = self.data
    self.data = None

    return return_data

  ###

  def pulse_sink_input_mute(self, index, mute):
    "Mute one stream by index"

    self.start_action()

    CONTEXT_SUCCESS = PA_CONTEXT_SUCCESS_CB_T(self.py_context_success)

    self.operation = pa_context_set_sink_input_mute(self.context,
                                                    index,
                                                    mute, # Mute = 1
                                                    CONTEXT_SUCCESS,
                                                    None)

    self.pulse_iterate()

    return

  ###

  def pulse_sink_mute(self, index, mute):
    "Mute sink by index"

    self.start_action()

    CONTEXT_SUCCESS = PA_CONTEXT_SUCCESS_CB_T(self.py_context_success)

    self.operation = pa_context_set_sink_mute_by_index(self.context,
                                                       index,
                                                       mute, # Mute = 1
                                                       CONTEXT_SUCCESS,
                                                       None)

    self.pulse_iterate()

    return

  ###

  def pulse_mute_stream(self, index):
    self.pulse_sink_input_mute(index, 1)
    return

  ###

  def pulse_unmute_stream(self, index):
    self.pulse_sink_input_mute(index, 0)
    return

  ###

  def pulse_mute_sink(self, index):
    self.pulse_sink_mute(index, 1)
    return

  ###

  def pulse_unmute_sink(self, index):
    self.pulse_sink_mute(index, 0)
    return

  ###

  def pulse_set_sink_input_volume(self, index, vol):
    "Set volume by index"
    self.start_action()

    #print "pulse_set_sink_input_volume:", index, "Vol:", vol

    #print vol.values
    #for a in vol.toCtypes().values:
    #  print a
    #return

    PA_CONTEXT_SUCCESS_CB = PA_CONTEXT_SUCCESS_CB_T(self.py_context_success)

    self.operation = pa_context_set_sink_input_volume(self.context,
                                                       index,
                                                       vol.toCtypes(),
                                                       PA_CONTEXT_SUCCESS_CB,
                                                       None)

    self.pulse_iterate()

    return

  ###

  def pulse_set_sink_volume(self, index, vol):
    "Set volume by index"
    self.start_action()

    #print "pulse_set_sink_volume:", index, "Vol:", vol

    PA_CONTEXT_SUCCESS_CB = PA_CONTEXT_SUCCESS_CB_T(self.py_context_success)

    self.operation = pa_context_set_sink_volume_by_index(self.context,
                                                          index,
                                                          vol.toCtypes(),
                                                          PA_CONTEXT_SUCCESS_CB,
                                                          None)

    self.pulse_iterate()

    return

  ###

  def pulse_context_subscribe(self, mask):
    "Subscribe to event"
    self.start_action()
    print "pulse_context_subscribe:", mask
    CONTEXT_SUCCESS = PA_CONTEXT_SUCCESS_CB_T(self.py_context_success)
    
    self.operation = pa_context_subscribe(self.context,
                                          mask,
                                          CONTEXT_SUCCESS,
                                          None)
    self.pulse_iterate()
    return

  ###

  def pulse_context_set_subscribe_callback(self, callback):
    "Set subscribe callback"
    print "py_context_set_subscribe_callback:", callback
    PA_CONTEXT_SUBSCRIBE_CB = PA_CONTEXT_SUBSCRIBE_CB_T(callback)

    # This returns a void, not a PA_OPERATION
    pa_context_set_subscribe_callback(self.context,
                                      PA_CONTEXT_SUBSCRIBE_CB,
                                      None)
    return

  ###

  def reconnect(self):
    self.context = pa_context_new(self.mainloop_api, clientName)

    pa_context_set_state_callback(self.context, self.PA_STATE_CB, None)

    self.start_action()


    if 0 > pa_context_connect(self.context,
                              self.server,
                              0,
                              None):
        if self.retry:
          pa_context_disconnect(self.context)
          #print "bar"
          return
        self.pulse_context_error()
        #print "foo"

    self.pulse_iterate()

    return

  ###

  def pulse_iterate(self, times = 1):
    "Runs queries"
    #print "pulse_iterate: Called"
    self.ret = pointer(c_int())

    pa_mainloop_iterate(self.mainloop, times, self.ret)

    while not self.action_done:
      pa_mainloop_iterate(self.mainloop, times, self.ret)

    return

  ###

  def pulse_run(self):
    self.ret = pointer(c_int(0))

    #pa_mainloop_iterate(self.mainloop, 11, self.ret)
    pa_mainloop_run(self.mainloop, self.ret)
    return