scripts/mycroft/audio/services/chromecast/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import time
16 from mimetypes import guess_type
17 
18 import pychromecast
19 
20 from mycroft.audio.services import RemoteAudioBackend
21 from mycroft.messagebus.message import Message
22 from mycroft.util.log import LOG
23 
24 
26  """
27  Audio backend for playback on chromecast. Using the default media
28  playback controller included in pychromecast.
29  """
30  def _connect(self, message):
31  LOG.info('Trying to connect to chromecast')
32  casts = pychromecast.get_chromecasts()
33  if self.config is None or 'identifier' not in self.config:
34  LOG.error("Chromecast identifier not found!")
35  return # Can't connect since no id is specified
36  else:
37  identifier = self.config['identifier']
38  for c in casts:
39  if c.name == identifier:
40  self.cast = c
41  break
42  else:
43  LOG.info('Couldn\'t find chromecast ' + identifier)
44  self.connection_attempts += 1
45  time.sleep(10)
46  self.bus.emit(Message('ChromecastServiceConnect'))
47  return
48 
49  def __init__(self, config, bus, name='chromecast', cast=None):
50  super(ChromecastService, self).__init__(config, bus)
52  self.bus = bus
53  self.config = config
54  self.name = name
55 
56  self.tracklist = []
57 
58  if cast is not None:
59  self.cast = cast
60  else:
61  self.cast = None
62  self.bus.on('ChromecastServiceConnect', self._connect)
63  self.bus.emit(Message('ChromecastServiceConnect'))
64 
65  def supported_uris(self):
66  """ Return supported uris of chromecast. """
67  LOG.info("Chromecasts found: " + str(self.cast))
68  if self.cast:
69  return ['http', 'https']
70  else:
71  return []
72 
73  def clear_list(self):
74  """ Clear tracklist. """
75  self.tracklist = []
76 
77  def add_list(self, tracks):
78  """
79  Add list of tracks to chromecast playlist.
80 
81  Args:
82  tracks (list): list media to add to playlist.
83  """
84  self.tracklist = tracks
85  pass
86 
87  def play(self, repeat=False):
88  """ Start playback.
89 
90  TODO: add playlist support and repeat
91  """
92  self.cast.quit_app()
93 
94  track = self.tracklist[0]
95  # Report start of playback to audioservice
96  if self._track_start_callback:
97  self._track_start_callback(track)
98  LOG.debug('track: {}, type: {}'.format(track, guess_type(track)))
99  mime = guess_type(track)[0] or 'audio/mp3'
100  self.cast.play_media(track, mime)
101 
102  def stop(self):
103  """ Stop playback and quit app. """
104  if self.cast.media_controller.is_playing:
105  self.cast.media_controller.stop()
106  self.cast.quit_app()
107  return True
108  else:
109  return False
110 
111  def pause(self):
112  """ Pause current playback. """
113  if not self.cast.media_controller.is_paused:
114  self.cast.media_controller.pause()
115 
116  def resume(self):
117  if self.cast.media_controller.is_paused:
118  self.cast.media_controller.play()
119 
120  def next(self):
121  """ Skip current track. (Not implemented) """
122  pass
123 
124  def previous(self):
125  """ Return to previous track. (Not implemented) """
126  pass
127 
128  def lower_volume(self):
129  # self.cast.volume_down()
130  pass
131 
132  def restore_volume(self):
133  # self.cast.volume_up()
134  pass
135 
136  def track_info(self):
137  """ Return info about currently playing track. """
138  info = {}
139  ret = {}
140  ret['name'] = info.get('name', '')
141  if 'album' in info:
142  ret['artist'] = info['album']['artists'][0]['name']
143  ret['album'] = info['album'].get('name', '')
144  else:
145  ret['artist'] = ''
146  ret['album'] = ''
147  return ret
148 
149  def shutdown(self):
150  """ Disconnect from the device. """
151  self.cast.disconnect()
152 
153 
154 def autodetect(config, bus):
155  """
156  Autodetect chromecasts on the network and create backends for each
157  """
158  casts = pychromecast.get_chromecasts(timeout=5, tries=2, retry_wait=2)
159  ret = []
160  for c in casts:
161  LOG.info(c.name + " found.")
162  ret.append(ChromecastService(config, bus, c.name.lower(), c))
163 
164  return ret
def get(phrase, lang=None, context=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40