-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradioFunctions.py
executable file
·1129 lines (854 loc) · 38.1 KB
/
radioFunctions.py
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
description_text = """
"""
import sys, io, os, datetime, time, random
import re, codecs, locale, pwd
import socket, urllib2, sys
from urllib2 import urlopen
from stat import *
from xml.etree import cElementTree as ET
from threading import Thread
try:
from subprocess32 import *
except:
from subprocess import *
class FunctionConfig:
def __init__(self):
self.name = 'radioFunctions.py'
self.major = 0
self.minor = 2
self.patch = 1
self.beta = True
self.functioncalls = {u'poweroff' :u'PowerOff',
u'reboot' :u'Reboot',
u'hibernate' :u'Hibernate',
u'suspend' :u'Suspend',
u'play_radio' :u'PlayRadio',
u'stop_radio' :u'StopRadio',
u'start_stop_radio' :u'ToggleStartStop',
u'Ch+' :u'ChannelUp',
u'Ch-' :u'ChannelDown',
u'V+' :u'VolumeUp',
u'V-' :u'VolumeDown',
u'mute' :u'Mute',
u'create_mythfmmenu':u'CreateMythfmMenu'}
#~ u'':u'',
self.call_list = {}
for v in self.functioncalls.values():
self.call_list[v.lower()] = v
self.mutetext = {}
self.mutetext[0] = 'Unmuting'
self.mutetext[1] = 'Muting'
self.channels = {}
self.frequencies = {}
self.opt_dict = {}
self.opt_dict['verbose'] = True
self.max_logsize = 1048576
self.max_logcount = 5
self.log_level = 1
self.log_file = ''
self.log_output = None
self.disable_alsa = False
self.disable_radio = False
try:
global alsaaudio
import alsaaudio
try:
x = alsaaudio.pcms()
self.alsa_version = '0.8'
except:
self.alsa_version = '0.7'
except:
print 'You need to install the pyalsaaudio module\n'
print 'Alsa (and radio) support will be disabled\n'
self.disable_alsa = True
self.alsa_cards = {}
self.alsa_names = {}
self.get_alsa()
self.active_channel = 1
self.new_channel = 0
self.radio_pid = None
self.play_pcm = None
self.mixer = None
self.audio_out = None
# end Init()
def version(self, as_string = False):
if as_string and self.beta:
return u'%s Version: %s.%s.%s-beta' % (self.name, self.major, self.minor, self.patch)
if as_string and not self.beta:
return u'%s Version: %s.%s.%s' % (self.name, self.major, self.minor, self.patch)
else:
return (self.name, self.major, self.minor, self.patch, self.beta)
# end version()
def retrieve_value(self, name, default):
# Retrieve old values
if os.access('%s/%s' % (self.ivtv_dir, name), os.F_OK):
f = io.open('%s/%s' % (self.ivtv_dir, name), 'rb')
value = int(re.sub('\n','', f.readline()).strip())
f.close()
return value
return default
# end retrieve_value()
def save_value(self, name, value):
# save a value for later
if os.access('%s/%s' % (self.ivtv_dir, name), os.F_OK) and not os.access('%s/%s' % (self.ivtv_dir, name), os.W_OK):
log('Can not save %s/%s. Check access rights' % (self.ivtv_dir, name) )
try:
f = io.open('%s/%s' % (self.ivtv_dir, name), 'wb')
f.write('%s\n' % value)
f.close()
except:
log('Can not save %s/%s. Check access rights' % (self.ivtv_dir, name) )
# end save_value()
def check_dependencies(self, ivtv_dir):
def check_path(name, use_sudo = False):
if use_sudo:
try:
path = check_output(['sudo', 'which', name], stderr = None)
return re.sub('\n', '',path)
except:
log('%s not Found!\n' % (name))
return None
else:
try:
path = check_output(['which', name], stderr = None)
return re.sub('\n', '',path)
except:
log('%s not Found!\n' % (name))
return None
self.ivtv_dir = ivtv_dir
self.active_channel = int(self.retrieve_value('LastChannel', 1))
self.udevadm = check_path("udevadm")
self.ivtv_radio = check_path("ivtv-radio")
self.ivtv_tune = check_path("ivtv-tune")
self.v4l2_ctl = check_path("v4l2-ctl")
if not self.disable_alsa:
if self.udevadm == None:
log('I can not find udevadm, so unable to identify devices.\n')
log('Disabling both alsa and radiofunctionality.\n')
self.disable_alsa = True
elif self.ivtv_radio == None or self.ivtv_tune == None or self.v4l2_ctl == None:
log('I can not find ivtv-tools and/or v4l-tools, so unable to play radio.\n')
log('Disabling radiofunctionality.\n')
self.disable_radio = True
poweroff = check_path("poweroff", True)
reboot = check_path("reboot", True)
hibernate_ram = check_path("hibernate-ram", True)
hibernate = check_path("hibernate", True)
pm_suspend = check_path("pm-suspend", True)
pm_hibernate =check_path("pm-hibernate", True)
# Checking for the presence of Commands.sh
if os.access(self.ivtv_dir + '/Commands.sh', os.F_OK):
if not os.access(self.ivtv_dir + '/Commands.sh', os.X_OK):
os.chmod(self.ivtv_dir + '/Commands.sh', 0750)
self.command_name = self.ivtv_dir + '/Commands.sh'
elif os.access('/usr/bin/Commands.sh', os.X_OK):
self.command_name = '/usr/bin/Commands.sh'
else:
f = io.open(self.ivtv_dir + '/Commands.sh', 'wb')
f.write('#!/bin/bash\n')
f.write('\n')
f.write('Command=${1:-""}\n')
f.write('\n')
f.write('case $Command in\n')
f.write(' "poweroff")\n')
f.write(' # The command to execute on poweroff\n')
if poweroff != None:
f.write(' sudo %s\n' % poweroff)
else:
f.write('# sudo poweroff\n')
f.write(' ;;\n')
f.write(' "reboot")\n')
f.write(' # The command to execute on reboot\n')
if reboot != None:
f.write(' sudo %s\n' % reboot)
else:
f.write('# sudo reboot\n')
f.write(' ;;\n')
f.write(' "suspend")\n')
f.write(' # The command to execute on suspend\n')
if hibernate_ram != None:
f.write(' sudo %s\n' % hibernate_ram)
if pm_suspend != None:
f.write('# sudo %s\n' % pm_suspend)
elif pm_suspend != None:
f.write(' sudo %s\n' % pm_suspend)
else:
f.write('# sudo pm_suspend\n')
f.write(' ;;\n')
f.write(' "hibernate")\n')
f.write(' # The command to execute on hibernate\n')
if hibernate != None:
f.write(' sudo %s\n' % hibernate)
if pm_hibernate != None:
f.write('# sudo %s\n' % pm_hibernate)
elif pm_hibernate != None:
f.write(' sudo %s\n' % pm_hibernate)
else:
f.write('# sudo pm_hibernate\n')
f.write(' ;;\n')
f.write('esac \n')
f.write('\n')
f.close()
self.command_name = self.ivtv_dir + '/Commands.sh'
os.chmod(self.command_name, 0750)
# end check_dependencies()
def get_alsa(self):
if self.disable_alsa:
return
for cid in range(len(alsaaudio.cards())):
self.alsa_names[alsaaudio.cards()[cid]] = cid
self.alsa_cards[cid] = {}
self.alsa_cards[cid]['name'] = alsaaudio.cards()[cid]
self.alsa_cards[cid]['id'] = cid
self.alsa_cards[cid]['mixers'] = {}
for name in alsaaudio.mixers(cid):
while True:
if name in self.alsa_cards[cid]['mixers'].keys():
mid +=1
else:
self.alsa_cards[cid]['mixers'][name] = {}
mid = 0
try:
mixer = alsaaudio.Mixer(control = name, id = mid, cardindex = cid)
except:
continue
self.alsa_cards[cid]['mixers'][name][mid] = {}
self.alsa_cards[cid]['mixers'][name][mid]['mixer'] = mixer
self.alsa_cards[cid]['mixers'][name][mid]['controls'] = []
if len(mixer.switchcap()) > 0:
try:
x = mixer.getmute()
self.alsa_cards[cid]['mixers'][name][mid]['controls'].append('mute')
self.alsa_cards[cid]['mixers'][name][mid]['mute'] = x
except:
pass
try:
x = mixer.getrec()
self.alsa_cards[cid]['mixers'][name][mid]['controls'].append('rec')
self.alsa_cards[cid]['mixers'][name][mid]['rec'] = x
except:
pass
if len(mixer.volumecap()) > 0:
try:
if self.alsa_version == '0.7':
x = mixer.getvolume('playback')
elif self.alsa_version == '0.8':
x = mixer.getvolume(alsaaudio.PCM_PLAYBACK)
self.alsa_cards[cid]['mixers'][name][mid]['controls'].append('volume')
self.alsa_cards[cid]['mixers'][name][mid]['volume'] = x
except:
pass
try:
if self.alsa_version == '0.7':
x = mixer.getvolume('capture')
elif self.alsa_version == '0.8':
x = mixer.getvolume(alsaaudio.PCM_CAPTURE)
self.alsa_cards[cid]['mixers'][name][mid]['controls'].append('capture')
self.alsa_cards[cid]['mixers'][name][mid]['capture'] = x
except:
pass
if len(mixer.getenum()) > 0:
self.alsa_cards[cid]['mixers'][name][mid]['controls'].append('enum')
self.alsa_cards[cid]['mixers'][name][mid]['value'] = mixer.getenum()[0]
self.alsa_cards[cid]['mixers'][name][mid]['values'] = mixer.getenum()[1]
break
# end get_alsa()
def set_mixer(self):
if self.disable_alsa:
return False
if self.mixer != None:
return True
cid = RadioFunctions().get_cardid()
if not cid in self.alsa_cards:
return False
if not self.opt_dict['audio_mixer'] in self.alsa_cards[cid]['mixers']:
return False
for id in self.alsa_cards[cid]['mixers'][self.opt_dict['audio_mixer']].keys():
if not 'volume' in self.alsa_cards[cid]['mixers'][self.opt_dict['audio_mixer']][id]['controls']:
log('The mixer %s is not a playback volume control' % self.opt_dict['audio_mixer'])
return False
if not 'mute' in self.alsa_cards[cid]['mixers'][self.opt_dict['audio_mixer']][id]['controls']:
log('The mixer %s is not a playback mute control' % self.opt_dict['audio_mixer'])
return False
self.mixer = self.alsa_cards[cid]['mixers'][self.opt_dict['audio_mixer']][id]['mixer']
return True
return False
# end set_mixer()
def rotate_log(self):
if self.log_output == None or self.log_file == '':
return
self.log_output.flush()
if os.stat(self.log_file).st_size < self.max_logsize:
return
self.log_output.close()
if os.access('%s.%s' % (self.log_file, self.max_logcount), os.F_OK):
os.remove('%s.%s' % (self.log_file, self.max_logcount))
for i in range(self.max_logcount - 1, 0, -1):
if os.access('%s.%s' % (self.log_file, i), os.F_OK):
os.rename('%s.%s' % (self.log_file, i), '%s.%s' % (self.log_file, i + 1))
os.rename(self.log_file, '%s.1' % (self.log_file))
self.log_output = io.open(self.log_file, mode = 'ab', encoding = 'utf-8')
sys.stderr = self.log_output
# end rotate_log()
def close(self):
# close everything neatly
if self.play_pcm != None:
self.play_pcm.close()
self.play_pcm = None
if self.radio_pid != None:
self.radio_pid.kill()
self.radio_pid = None
self.mixer = None
for c in self.alsa_cards.values():
c['name'] = None
for m in c['mixers'].values():
for i in m.values():
m['mixer'] = None
try:
self.log_output.close()
except:
pass
# end close()
# end FunctionConfig()
config = FunctionConfig()
def log(message, log_level = 1, log_target = 3):
"""
Log messages to log and/or screen
"""
def now():
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z') + ': '
if type(message) != unicode:
message = unicode(message)
# Log to the screen
if log_level == 0 or ((config.opt_dict['verbose']) and (log_level & config.log_level) and (log_target & 1)):
sys.stdout.write(message.encode("utf-8"))
# Log to the log-file
if (log_level == 0 or ((log_level & config.log_level) and (log_target & 2))) and config.log_output != None:
message = u'%s%s\n' % (now(), message.replace('\n',''))
sys.stderr.write(message.encode("utf-8"))
config.rotate_log()
# end log()
class AudioPCM(Thread):
def __init__(self, card = None, capture = None):
if config.disable_alsa:
return
Thread.__init__(self)
self.quit = False
if card == None:
self.card = config.opt_dict['audio_card']
else:
self.card = card
if capture == None:
self.capture = config.opt_dict['radio_out']
else:
self.capture = capture
def run(self):
if config.disable_alsa or not config.opt_dict['radio_cardtype'] in (0, 1):
return
log('Starting Radioplayback from %s on %s.' % (config.opt_dict['radio_out'], self.card), 8)
try:
if config.opt_dict['radio_cardtype'] == 0:
PCM = alsaaudio.PCM(type = alsaaudio.PCM_PLAYBACK, mode = alsaaudio.PCM_NONBLOCK, card = self.card)
PCM.setformat(alsaaudio.PCM_FORMAT_S16_LE)
PCM.setrate(48000)
PCM.setchannels(2)
PCM.setperiodsize(160)
out = io.open(self.capture, 'rb')
while True:
if self.quit:
log('Stoping Radioplayback from %s on %s.' % (config.opt_dict['radio_out'], self.card), 8)
out.close()
out = None
PCM = None
return
data = out.read(320)
PCM.write(data)
elif config.opt_dict['radio_cardtype'] == 1:
return
#~ PCM = alsaaudio.PCM(type = alsaaudio.PCM_PLAYBACK, mode = alsaaudio.PCM_NONBLOCK, card = self.card)
#~ PCM.setformat(alsaaudio.PCM_FORMAT_U8)
#~ PCM.setrate(8000)
#~ PCM.setchannels(2)
#~ PCM.setperiodsize(160)
out = alsaaudio.PCM(type = alsaaudio.PCM_CAPTURE, mode = alsaaudio.PCM_NONBLOCK, card = self.capture)
#~ out.setformat(alsaaudio.PCM_FORMAT_U8)
#~ out.setrate(8000)
out.setformat(alsaaudio.PCM_FORMAT_S16_LE)
out.setrate(48000)
out.setchannels(2)
out.setperiodsize(160)
f = io.open('/home/mythtv/.ivtv/test.wav', 'wb')
while True:
if self.quit:
log('Stoping Radioplayback from %s on %s.' % (config.opt_dict['radio_out'], self.card), 8)
f.close()
out = None
PCM = None
return
l, data = out.read()
f.write(data)
#~ if l:
#~ PCM.write(data)
#~ time.sleep(.001)
except:
log('Error: %s Playing radio on %s' % (sys.exc_info()[1], self.card))
out = None
PCM = None
return
def close(self):
self.quit = True
# end AudioPCM()
class RadioFunctions:
"""
All functions to manipulate the radio and others
"""
def rf_function_call(self, rf_call_id, command = None):
if rf_call_id == 'Command'and config.command_name != None and command != None:
log('Executing %s %s' % (config.command_name, command), 32)
call([config.command_name, command])
elif rf_call_id == 'PowerOff'and config.command_name != None:
log('Executing %s %s' % (config.command_name, 'poweroff'), 32)
call([config.command_name,'poweroff'])
elif rf_call_id == 'Reboot'and config.command_name != None:
log('Executing %s %s' % (config.command_name, 'reboot'), 32)
call([config.command_name,'reboot'])
elif rf_call_id == 'Hibernate'and config.command_name != None:
log('Executing %s %s' % (config.command_name, 'hibernate'), 32)
if config.play_pcm != None or config.radio_pid != None:
self.stop_radio()
time.sleep(1)
call([config.command_name,'hibernate'])
elif rf_call_id == 'Suspend'and config.command_name != None:
log('Executing %s %s' % (config.command_name, 'suspend'), 32)
if config.play_pcm != None or config.radio_pid != None:
self.stop_radio()
time.sleep(1)
call([config.command_name,'suspend'])
elif rf_call_id == 'PlayRadio':
self.start_radio()
elif rf_call_id == 'StopRadio':
self.stop_radio()
elif rf_call_id == 'ToggleStartStop':
if config.radio_pid != None:
self.stop_radio()
else:
self.start_radio()
elif rf_call_id == 'ChannelUp':
self.channel_up()
elif rf_call_id == 'ChannelDown':
self.channel_down()
elif rf_call_id == 'VolumeUp':
self.radio_volume_up()
elif rf_call_id == 'VolumeDown':
self.radio_volume_down()
elif rf_call_id == 'Mute':
self.toggle_radio_mute()
elif rf_call_id == 'CreateMythfmMenu':
self.create_fm_menu_file(command[0], command[1])
# end rf_function_call ()
def query_backend(self, backend = None):
"""
Check a MythTV backend for reaction
"""
if backend == None:
backend = config.opt_dict['myth_backend']
URL0 = 'http://%s:6544//Myth/GetHostName' % (backend)
try:
response = ET.parse(urlopen(URL0))
root = response.getroot()
return root.text
except:
log('GetHostName failed, is the backend running?\n')
return(-2)
# end querybackend()
def query_tuner(self, backend = None, video_device = None):
"""
Query tunerstate with the backend
Returns -2 Error
-1 Not Connected
0 Inactive
1 Watching LiveTV
7 Recording
"""
if backend == None:
backend = config.opt_dict['myth_backend']
if video_device == None:
video_device = config.opt_dict['video_device']
hostname = self.query_backend(backend)
if hostname == -2:
return(-2)
URL1 = 'http://%s:6544/Capture/GetCaptureCardList?HostName=%s' % (backend, hostname)
try:
response = ET.parse(urlopen(URL1))
except:
log('GetCaptureCardList failed, is the backend running?')
return(-2)
for element1 in response.findall('CaptureCards/CaptureCard'):
if element1.findtext('VideoDevice') == video_device:
URL2 = 'http://%s:6544/Dvr/GetEncoderList' % (backend)
try:
response = ET.parse(urlopen(URL2))
except:
log('GetEncoderList failed, is the backend running?')
return(-2)
for element2 in response.findall('Encoders/Encoder'):
if element2.findtext('Id') == element1.findtext('CardId'):
return(int(element2.findtext('State')))
print(element2.findtext('State'))
break
break
log('The VideoCard is unknown to the MythBackend!')
return(-1)
# end querytuner()
def query_udev_path(self, device, enddir = None):
if not os.access(device, os.F_OK) or config.udevadm == None:
return None
if not device[:5] == '/dev/':
return None
udev_path = check_output([config.udevadm, 'info', '--query', 'path', '--name=%s' % device])
if enddir == None:
return udev_path
else:
returnpath = ''
udev_path = re.split('/', udev_path)
for dir in udev_path:
if dir == enddir:
return returnpath
returnpath += '/%s' % dir
return returnpath
# end query_udev_path()
def get_device_ids(self, device):
devpath = self.query_udev_path(device)
if devpath == None:
return None
subsystem = ''
name = ''
driver = ''
path = ''
vendor = ''
device = ''
subvendor = ''
subdevice = ''
output = check_output([config.udevadm, 'info', '--attribute-walk', '--path=%s' % devpath])
output = re.split('\n', output)
for line in output:
if line[:11] == 'SUBSYSTEM==':
subsystem = re.sub('"', '', line[11:])
elif line[:12] == 'ATTR{name}==':
name = re.sub('"', '', line[12:])
elif line[:9] == 'KERNELS==' and path == '' and line[10:13] != 'card':
path = re.sub('"', '', line[9:])
elif line[:9] == 'DRIVERS==' and driver == '':
driver = re.sub('"', '', line[9:])
elif line[:15] == 'ATTRS{vendor}==' and vendor == '':
vendor = re.sub('"', '', line[15:])
elif line[:15] == 'ATTRS{device}==' and device == '':
device = re.sub('"', '', line[15:])
elif line[:25] == 'ATTRS{subsystem_vendor}==' and subvendor == '':
subvendor = re.sub('"', '', line[25:])
elif line[:25] == 'ATTRS{subsystem_device}==' and subdevice == '':
subdevice = re.sub('"', '', line[25:])
return (subsystem, name, path, driver, vendor, device, subvendor, subdevice)
# end get_device_id()
def start_radio(self):
log('Executing start_radio', 32)
if config.disable_alsa or config.disable_radio:
log('Alsa support disabled. Install the pyalsaaudio module')
return
tunerstatus = self.query_tuner()
if tunerstatus > 0:
log('MythTV is using the tuner!')
return
if config.radio_pid != None:
return
log('Starting ivtv-radio channel %s on %s.' % (config.channels[config.active_channel]['title'], config.opt_dict['radio_device']), 8)
try:
config.radio_pid = Popen(executable = config.ivtv_radio, stderr = config.log_output, \
args = ['-d %s' % config.opt_dict['radio_device'], '-j', '-f %s' % config.channels[config.active_channel]['frequency']])
except:
log('Error: %s Starting %s' % (sys.exc_info()[1], config.ivtv_radio))
if config.opt_dict['radio_cardtype'] in (0, 1):
try:
config.play_pcm = AudioPCM()
config.play_pcm.start()
except:
log('Error: %s, Starting Playback' % (sys.exc_info()[1]))
elif config.opt_dict['radio_cardtype'] == 2:
log('%s sset "%s" %s' % (self.get_cardid(), config.opt_dict['source_switch'], config.opt_dict['source']))
try:
check_call(['amixer', '--quiet', '--card=%s' % self.get_cardid(), 'sset', '"%s"' % (config.opt_dict['source_switch']), config.opt_dict['source']])
except:
log('Error: %s, Selecting Source' % (sys.exc_info()[1]))
config.mixer.setvolume(config.retrieve_value('RadioVolume',70))
config.mixer.setmute(0)
# end start_radio()
def stop_radio(self):
log('Executing stop_radio', 32)
if config.disable_alsa or config.disable_radio:
log('Alsa support disabled. Install the pyalsaaudio module')
return
if config.play_pcm != None:
config.play_pcm.close()
config.play_pcm = None
if config.radio_pid != None:
config.radio_pid.terminate()
config.radio_pid = None
# end stop_radio()
def select_channel(self, channel):
if 0 < channel <= len(config.channels):
# It's a Radio Channelnumber
config.new_channel = channel
self.set_channel()
elif 800 <channel < 1100:
# It's a radio frequency *10
frequency = float(channel)/10
if frequency in config.frequencies.keys():
config.new_channel = config.frequencies[frequency]
self.set_channel()
elif channel > 1100:
# It's a TV Channel *1000
self.tune_tv(int(channel/1000))
# end select_channel()
def channel_up(self):
log('Executing channel_up', 32)
config.new_channel = config.active_channel + 1
if config.new_channel > len(config.channels):
config.new_channel = 1
self.set_channel()
# end channel_up()
def channel_down(self):
log('Executing channel_down', 32)
config.new_channel = config.active_channel - 1
if config.new_channel < 1:
config.new_channel = len(config.channels)
self.set_channel()
# end channel_down()
def get_active_frequency(self):
if config.disable_radio:
log('Radio support disabled. Install the ivtv/v4l Utilities')
return
try:
freq = check_output([config.v4l2_ctl, '--device=%s' % config.opt_dict['radio_device'], '--get-freq'])
freq = re.search('.*?\((.*?) MHz', freq)
if freq != None:
return float(freq.group(1))
else:
log('Error retreiving frequency from %s' % config.opt_dict['radio_device'])
except:
log('Error retreiving frequency from %s' % config.opt_dict['radio_device'])
# end get_active_frequency()
def set_channel(self):
if config.disable_radio:
log('Radio support disabled. Install the ivtv/v4l Utilities')
return
if config.radio_pid == None:
self.start_radio()
if config.active_channel == config.new_channel:
return
try:
chanid = config.channels[config.new_channel]
log('Setting frequency for %s to %3.1f MHz(%s)' % (config.opt_dict['radio_device'], chanid['frequency'], chanid['title']), 8)
check_call([config.v4l2_ctl, '--device=%s' % config.opt_dict['radio_device'], '--set-freq=%s' % chanid['frequency']])
config.active_channel = config.new_channel
config.save_value('LastChannel', config.active_channel)
except:
log('Error setting frequency for %s' % config.opt_dict['radio_device'])
# end set_channel()
def tune_tv(self, frequency):
if config.disable_radio:
log('Radio support disabled. Install the ivtv/v4l Utilities')
return
tunerstatus = query_tuner()
if tunerstatus < 0:
return
if tunerstatus > 0:
log('The videotuner is busy!')
return
try:
check_call([config.ivtv_tune, '--device=%s' % config.opt_dict['video_device'], '--frequency=%s' % frequency])
time.sleep(1)
check_call([config.ivtv_tune, '--device=%s' % config.opt_dict['video_device'], '--frequency=%s' % frequency])
log('Setting frequency for %s to %3.1f KHz' % (config.opt_dict['video_device'], frequency), 8)
except:
log('Error setting frequency for %s' % config.opt_dict['video_device'])
# end tune_tv()
def detect_channels(self, radio_dev = None):
if config.disable_radio:
return []
freq_list = []
if radio_dev == None and 'radio_device' in config.opt_dict:
radio_dev = config.opt_dict['radio_device']
try:
read_list = check_output([config.ivtv_radio, '-d', radio_dev, '-s'])
read_list = re.split('\n',read_list)
for freq in read_list:
if freq == '':
continue
freq = re.split(' ', freq)
freq_list.append(float(freq[1]))
return freq_list
except:
return []
# end detect_channels()
def get_alsa_cards(self, cardid = None):
if config.disable_alsa:
return []
if cardid == None:
return alsaaudio.cards()
elif cardid < len(alsaaudio.cards()):
return alsaaudio.cards()[cardid]
# end get_alsa_cards()
def get_alsa_mixers(self, cardid = 0, mixerid = None):
if config.disable_alsa:
return []
if cardid < len(alsaaudio.cards()):
if mixerid == None:
return alsaaudio.mixers(cardid)
elif mixerid < len(alsaaudio.mixers(cardid)):
return alsaaudio.mixers(cardid)[mixerid]
# end get_alsa_mixers()
def get_cardid(self, audiocard = None):
if config.disable_alsa:
return -1
if audiocard == None:
audiocard = config.opt_dict['audio_card']
for id in range(len(alsaaudio.cards())):
if alsaaudio.cards()[id] == audiocard:
return id
return -1
# end get_cardid()
def get_volume(self, cardnr = 0, mixer_ctrl = 'Master', id = 0, playback = True):
if config.disable_alsa:
return
if playback and 'volume' in config.alsa_cards[cardnr]['mixers'][mixer_ctrl][id]['controls']:
if config.alsa_version == '0.7':
return alsaaudio.Mixer(mixer_ctrl, id, cardnr).getvolume('playback')
elif config.alsa_version == '0.8':
return alsaaudio.Mixer(mixer_ctrl, id, cardnr).getvolume(alsaaudio.PCM_PLAYBACK)
if (not playback) and 'capture' in config.alsa_cards[cardnr]['mixers'][mixer_ctrl][id]['controls']:
if config.alsa_version == '0.7':
return alsaaudio.Mixer(mixer_ctrl, id, cardnr).getvolume('capture')
elif config.alsa_version == '0.8':
return alsaaudio.Mixer(mixer_ctrl, id, cardnr).getvolume(alsaaudio.PCM_CAPTURE)
return None
# end get_volume()
def set_volume(self, cardnr = 0, mixer_ctrl = 'Master', id = 0, playback = True, volume = 0):
if config.disable_alsa:
return
if playback and 'volume' in config.alsa_cards[cardnr]['mixers'][mixer_ctrl][id]['controls']:
log('Setting playbackvolume for %s on %s to %s.' % (mixer_ctrl, config.alsa_cards[cardnr]['name'], volume), 16)
if config.alsa_version == '0.7':
alsaaudio.Mixer(mixer_ctrl, id, cardnr).setvolume(volume, direction = 'playback')
elif config.alsa_version == '0.8':
alsaaudio.Mixer(mixer_ctrl, id, cardnr).setvolume(volume, direction = alsaaudio.PCM_PLAYBACK)
config.save_value('%s_%s_Volume' % (config.alsa_cards[cardnr]['name'], mixer_ctrl),volume)
elif (not playback) and 'capture' in config.alsa_cards[cardnr]['mixers'][mixer_ctrl][id]['controls']:
log('Setting capturevolume for %s on %s to %s.' % (mixer_ctrl, config.alsa_cards[cardnr]['name'], volume), 16)
if config.alsa_version == '0.7':
alsaaudio.Mixer(mixer_ctrl, id, cardnr).setvolume(volume, direction = 'capture')
elif config.alsa_version == '0.8':
alsaaudio.Mixer(mixer_ctrl, id, cardnr).setvolume(volume, direction = alsaaudio.PCM_CAPTURE)
config.save_value('%s_%s_Volume' % (config.alsa_cards[cardnr]['name'], mixer_ctrl),volume)
# end set_volume()
def get_mute(self, cardnr = 0, mixer_ctrl = 'Master', id = 0, playback = True):