-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGstHandler.py
109 lines (93 loc) · 3.43 KB
/
GstHandler.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
import gst
import logging
import datetime
from collections import deque
from pithos.pandora import *
class GstHandler:
def __init__(self):
self.player = gst.element_factory_make("playbin", "player")
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.onMessage)
self.time_format = gst.Format(gst.FORMAT_TIME)
self.currentSong = None
self.playlist = None
self.playing = False
self.station = None
def playStation(self, station):
if not self.station or station.id != self.station.id:
self.station = station
self.playlist = deque(self.station.get_playlist())
self.nextSong()
def nextSong(self):
logging.info('Changing songs')
prev = self.currentSong
self.stop()
try:
self.currentSong = self.playlist.popleft()
except IndexError:
self.playlist = deque(self.station.get_playlist())
self.currentSong = self.playlist.popleft()
if not self.currentSong.is_still_valid():
return self.nextSong()
if self.currentSong.tired or self.currentSong.rating == RATE_BAN:
return self.nextSong()
self.player.set_property("uri", self.currentSong.audioUrl)
self.play()
def getCurrentSongAsDict(self):
if self.currentSong:
songDict = self.currentSong.getDict()
songDict['duration'] = self.getDuration()
songDict['position'] = self.getPosition()
return songDict
else:
return None
def getCurrentSongObj(self):
return self.currentSong
def play(self):
if not self.playing:
logging.info('Starting to play ' + self.currentSong.title)
self.playing = True
self.player.set_state(gst.STATE_PLAYING)
def pause(self):
if self.playing:
self.playing = False
self.player.set_state(gst.STATE_PAUSED)
def stop(self):
prev = self.currentSong
if prev and prev.start_time:
prev.finished = True
try:
prev.duration = self.getDuration()
prev.position = self.getPosition()
except gst.QueryError:
prev.duration = prev.position = None
self.playing = False
self.player.set_state(gst.STATE_NULL)
def getDuration(self):
try:
return self.player.query_duration(self.time_format, None)[0] / 1000000000
except gst.QueryError:
return 0
def getPosition(self):
try:
return self.player.query_position(self.time_format, None)[0] / 1000000000
except gst.QueryError:
return 0
def onMessage(self, bus, message):
t = message.type
logging.debug('Received message ' + str(t))
if t == gst.MESSAGE_EOS:
self.nextSong()
elif message.type == gst.MESSAGE_BUFFERING:
percent = message.parse_buffering()
if percent < 100:
self.player.set_state(gst.STATE_PAUSED)
elif self.playing:
self.player.set_state(gst.STATE_PLAYING)
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
logging.error("Gstreamer error: %s, %s" % (err, debug))
self.gstreamer_error = str(err)
self.gstreamer_errorcount_1 += 1
self.nextSong()