Added the services for zukebox render
[zukebox] / zukebox_render / src / services / avtransport / av_transport.py
1
2 __all__ = ('AVTransport', )
3
4 import os.path
5 import platform
6
7 from brisa.core import log
8 from brisa.upnp.device import Service, ServiceController
9 from zukebox_renderer.services.gst_renderer import GSTRenderer
10
11 service_name = 'AVTransport'
12 service_type = 'urn:schemas-upnp-org:service:AVTransport:1'
13
14
15 class AVTransport(Service):
16
17     def __init__(self, xml_path):
18         Service.__init__(self, service_name, service_type, '',
19                          os.path.join(xml_path, 'render-transport-scpd.xml'),
20                          AVTransportControl())
21
22     def get_player(self):
23         return self.control_controller.get_player()
24
25
26 class AVTransportControl(ServiceController):
27
28     def __init__(self):
29         ServiceController.__init__(self, service_type)
30         self.av_transport_uri = ''
31         self.number_of_tracks = 0
32
33         self.gst_player = GSTRenderer()
34
35         self.urilist = {}
36         self.transport_state = 'NO_MEDIA_PRESENT'
37         self.transport_status= 'OK'
38         self.transport_speed = 1
39
40     def soap_SetAVTransportURI(self, *args, **kwargs):
41         """Specifies the URI of resource.
42
43         This action specifies the URI of the resource to be controlled
44         by the specified AVTransport instance. It is RECOMMENDED that the
45         AVTransport service checks the MIME-type of the specified resource
46         when executing this action"""
47
48         instance_id = kwargs['InstanceID']
49         current_uri = kwargs['CurrentURI']
50         current_uri_metadata = kwargs['CurrentURIMetaData']
51
52         self.av_transport_uri = current_uri
53         self.number_of_tracks = 1
54         self.gst_player.av_uri = current_uri
55         self.transport_state = 'STOPPED'
56
57         log.info('SetAVTransportURI()')
58
59         return {'SetAVTransportURI': {}}
60
61     def soap_GetMediaInfo(self, *args, **kwargs):
62         """Return information of current Media.
63
64         This action returns information associated with the current
65         media of the specified instance; it has no effect on state."""
66
67         log.info('GetMediaInfo()')
68
69         return {'GetMediaInfoResponse':
70                 {'NrTracks': str(self.number_of_tracks),
71                 'MediaDuration': str(self.gst_player.query_duration()[0]),
72                 'CurrentURI': self.av_transport_uri,
73                 'CurrentURIMetaData': '',
74                 'NextURI': '', 'NextURIMetaData': '',
75                 'PlayMedium': 'NETWORK',
76                 'RecordMedium': '',
77                 'WriteStatus': ''}}
78
79     def soap_GetMediaInfo_Ext(self, *args, **kwargs):
80         """Return information of current Media and CurrentType argment.
81
82         This action returns information associated with the current
83         media of the specified instance; it has no effect on state.The
84         information returned is identical to the information returned
85         by the GetMediaInfo() action, except for the additionally
86         returned CurrentType argument """
87
88         log.info('GetMediaInfo_Ext()')
89
90         return {'GetMediaInfo_ExtResponse': {'CurrentType': 'TRACK_UNAWARE',
91                 'NrTracks': str(self.number_of_tracks),
92                 'MediaDuration': str(self.gst_player.query_duration()[0]),
93                 'CurrentURI': self.av_transport_uri,
94                 'CurrentURIMetaData': '',
95                 'NextURI': '', 'NextURIMetaData': '',
96                 'PlayMedium': 'NETWORK',
97                 'RecordMedium': '',
98                 'WriteStatus': ''}}
99
100     def soap_GetTransportInfo(self, *args, **kwargs):
101         """Return informations os current Transport state.
102
103         This action returns information associated with the current
104         transport state of the specified instance; it has no effect on
105         state."""
106
107         self.transport_state = self.gst_player.get_state()
108
109         return {'GetTransportInfoResponse': {'CurrentTransportState':
110                                              self.transport_state,
111                                              'CurrentTransportStatus':
112                                              self.transport_status,
113                                              'CurrentSpeed':
114                                              str(self.transport_speed)}}
115
116     def soap_GetPositionInfo(self, *args, **kwargs):
117         """Return information of the Transporte of the specified instante.
118
119         This action returns information associated with the current
120         position of the transport of the specified instance; it has no
121         effect on state."""
122
123         dur = self.gst_player.query_duration()
124         pos = self.gst_player.query_position()
125
126         duration = dur[0]
127
128         if dur[1] == -1:
129             duration = pos[0]
130
131
132         abs_pos = pos[1]
133         rel_pos = pos[1]
134
135         if dur[1] > 2147483646:
136             abs_pos = pos[1]*2147483646/dur[1]
137
138         if pos[1] == -1:
139             abs_pos = 0
140             rel_pos = 0
141
142         return {'GetPositionInfoResponse': {'Track': '1',
143                                             'TrackDuration': \
144                                             '"'+str(duration)+'"',
145                                             'TrackMetaData': '',
146                                             'TrackURI': \
147                                             self.av_transport_uri,
148                                             'RelTime': '"'+str(pos[0])+'"',
149                                             'AbsTime': '"'+str(pos[0])+'"',
150                                             'RelCount': rel_pos,
151                                             'AbsCount': abs_pos}}
152
153     def soap_GetDeviceCapabilities(self, *args, **kwargs):
154         """Return information on device capabilities.
155
156         This action returns information on device capabilities of
157         the specified instance, such as the supported playback and
158         recording formats, and the supported quality levels for
159         recording. This action has no effect on state."""
160
161         log.info('GetDeviceCapabilities()')
162
163         return {'GetDeviceCapabilitiesResponse': {'PlayMedia': 'NONE',
164                                         'RecMedia': 'NOT_IMPLEMENTED',
165                                         'ReqQualityMode': 'NOT_IMPLEMENTED'}}
166
167     def soap_GetTransportSettings(self, *args, **kwargs):
168         """Return information on various settings of instance.
169
170         This action returns information on various settings of the
171         specified instance, such as the current play mode and the
172         current recording quality mode. This action has no effect on
173         state."""
174
175         log.info('GetTransportSettings()')
176
177         return {'GetTransportSettingsResponse': {'PlayMode': 'DIRECT_1',
178                                         'ReqQualityMode': 'NOT_IMPLEMENTED'}}
179
180     def soap_Stop(self, *args, **kwargs):
181         """Stop progression of current resource.
182
183         This action stops the progression of the current resource that
184         is associated with the specified instance."""
185
186         log.info('Stop()')
187         self.transport_state = 'TRANSITIONING'
188         self.gst_player.stop()
189         self.transport_state = 'STOPPED'
190
191         return {'StopResponse': {}}
192
193     def soap_Play(self, *args, **kwargs):
194         """Play the resource of instance.
195
196         This action starts playing the resource of the specified
197         instance, at the specified speed, starting at the current
198         position, according to the current play mode."""
199
200         log.info('Play()')
201         log.debug('Playing uri: %r', self.gst_player.av_uri)
202         self.transport_state = 'TRANSITIONING'
203
204         self.gst_player.play()
205
206         self.transport_state = 'PLAYING'
207
208         return {'PlayResponse': {}}
209
210     def soap_Pause(self, *args, **kwargs):
211         """Pause the resouce of instance.
212
213         This action starts playing the resource of the specified
214         instance, at the specified speed, starting at the current
215         position, according to the current play mode."""
216
217         log.info('Pause()')
218
219         self.transport_state = 'TRANSITIONING'
220
221         self.gst_player.pause()
222
223         self.transport_state = 'PAUSED_PLAYBACK'
224
225         return {'PauseResponse': {}}
226
227     def soap_Seek(self, *args, **kwargs):
228         """Seek through the resource controlled.
229
230         This action starts seeking through the resource controlled
231         by the specified instance - as fast as possible - to the position,
232         specified in the Target argument."""
233
234         (instance_id, unit, target) = args
235
236         self.gst_player.seek(unit, target)
237
238         log.info('Seek()')
239
240         return {'SeekResponse': {}}
241
242     def soap_Next(self, *args, **kwargs):
243         """Advance to the next track.
244
245         This is a convenient action to advance to the next track. This
246         action is functionally equivalent to Seek(TRACK_NR, CurrentTrackNr+1).
247         This action does not cycle back to the first track."""
248
249         log.info('Next()')
250
251     def soap_Previous(self, *args, **kwargs):
252         """Advance to the previous track.
253
254         This is a convenient action to advance to the previous track. This
255         action is functionally equivalent to Seek(TRACK_NR, CurrentTrackNr-1).
256         This action does not cycle back to the last track."""
257
258         log.info('Previous()')
259
260     def get_player(self):
261         return self.gst_player