2018-01-24 17:43:35 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-03-04 12:29:25 -06:00
|
|
|
from __future__ import unicode_literals # at top of module
|
2018-02-25 04:53:39 -06:00
|
|
|
import os
|
2018-01-24 17:43:35 -06:00
|
|
|
import random
|
2018-02-19 11:45:57 -06:00
|
|
|
import vlc
|
2018-01-24 17:43:35 -06:00
|
|
|
import logging
|
2018-10-10 11:46:35 -05:00
|
|
|
import config
|
2018-01-24 17:43:35 -06:00
|
|
|
from pubsub import pub
|
2018-02-22 13:57:51 -06:00
|
|
|
from utils import call_threaded
|
2018-01-24 17:43:35 -06:00
|
|
|
|
|
|
|
player = None
|
2018-03-12 17:11:05 -06:00
|
|
|
log = logging.getLogger("controller.player")
|
2018-01-24 17:43:35 -06:00
|
|
|
|
|
|
|
def setup():
|
|
|
|
global player
|
|
|
|
if player == None:
|
|
|
|
player = audioPlayer()
|
|
|
|
|
|
|
|
class audioPlayer(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.is_playing = False
|
2018-10-10 11:46:35 -05:00
|
|
|
self.vol = config.app["main"]["volume"]
|
2018-01-24 17:43:35 -06:00
|
|
|
self.is_working = False
|
|
|
|
self.queue = []
|
|
|
|
self.stopped = True
|
2018-01-26 11:52:49 -06:00
|
|
|
self.queue_pos = 0
|
2018-01-26 12:03:07 -06:00
|
|
|
self.shuffle = False
|
2018-02-19 11:45:57 -06:00
|
|
|
self.instance = vlc.Instance()
|
|
|
|
self.player = self.instance.media_player_new()
|
2018-03-12 17:11:05 -06:00
|
|
|
log.debug("Media player instantiated.")
|
2018-02-22 13:57:51 -06:00
|
|
|
self.event_manager = self.player.event_manager()
|
|
|
|
self.event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, self.end_callback)
|
2018-03-02 14:04:20 -06:00
|
|
|
self.event_manager.event_attach(vlc.EventType.MediaPlayerEncounteredError, self.playback_error)
|
2018-03-12 17:11:05 -06:00
|
|
|
log.debug("Bound media playback events.")
|
2018-01-24 17:43:35 -06:00
|
|
|
|
2018-01-25 17:18:51 -06:00
|
|
|
def play(self, item):
|
2018-02-19 11:45:57 -06:00
|
|
|
self.stopped = True
|
2018-01-24 17:43:35 -06:00
|
|
|
if self.is_working == False:
|
|
|
|
self.is_working = True
|
2018-01-26 11:52:49 -06:00
|
|
|
if item.download_url == "":
|
|
|
|
item.get_download_url()
|
2018-03-12 17:11:05 -06:00
|
|
|
log.debug("playing {0}...".format(item.download_url,))
|
2018-02-19 11:45:57 -06:00
|
|
|
self.stream_new = self.instance.media_new(item.download_url)
|
|
|
|
self.player.set_media(self.stream_new)
|
|
|
|
if self.player.play() == -1:
|
2018-01-25 17:18:51 -06:00
|
|
|
log.debug("Error when playing the file {0}".format(item.title,))
|
2018-01-26 11:52:49 -06:00
|
|
|
pub.sendMessage("change_status", status=_("Error playing {0}. {1}.").format(item.title, e.description))
|
|
|
|
self.stopped = True
|
|
|
|
self.is_working = False
|
|
|
|
self.next()
|
2018-01-24 17:43:35 -06:00
|
|
|
return
|
2018-02-19 11:45:57 -06:00
|
|
|
self.player.audio_set_volume(self.vol)
|
2018-01-25 17:18:51 -06:00
|
|
|
pub.sendMessage("change_status", status=_("Playing {0}.").format(item.title))
|
2018-01-24 17:43:35 -06:00
|
|
|
self.stopped = False
|
|
|
|
self.is_working = False
|
|
|
|
|
2018-01-26 11:52:49 -06:00
|
|
|
def next(self):
|
|
|
|
if len(self.queue) > 0:
|
|
|
|
if self.shuffle:
|
|
|
|
self.queue_pos = random.randint(0, len(self.queue)-1)
|
|
|
|
else:
|
|
|
|
if self.queue_pos < len(self.queue)-1:
|
|
|
|
self.queue_pos += 1
|
|
|
|
else:
|
|
|
|
self.queue_pos = 0
|
|
|
|
self.play(self.queue[self.queue_pos])
|
|
|
|
|
|
|
|
def previous(self):
|
|
|
|
if len(self.queue) > 0:
|
|
|
|
if self.shuffle:
|
|
|
|
self.queue_pos = random.randint(0, len(self.queue)-1)
|
|
|
|
else:
|
|
|
|
if self.queue_pos > 0:
|
|
|
|
self.queue_pos -= 1
|
|
|
|
else:
|
|
|
|
self.queue_pos = len(self.queue)-1
|
|
|
|
self.play(self.queue[self.queue_pos])
|
|
|
|
|
2018-01-24 17:43:35 -06:00
|
|
|
def stop(self):
|
2018-02-19 11:45:57 -06:00
|
|
|
self.player.stop()
|
|
|
|
self.stopped = True
|
2018-01-24 17:43:35 -06:00
|
|
|
|
|
|
|
def pause(self):
|
2018-02-19 11:45:57 -06:00
|
|
|
self.player.pause()
|
|
|
|
if self.stopped == True:
|
|
|
|
self.stopped = False
|
|
|
|
else:
|
|
|
|
self.stopped = True
|
2018-01-24 17:43:35 -06:00
|
|
|
|
|
|
|
@property
|
|
|
|
def volume(self):
|
2018-01-25 17:18:51 -06:00
|
|
|
return self.vol
|
2018-01-24 17:43:35 -06:00
|
|
|
|
|
|
|
@volume.setter
|
|
|
|
def volume(self, vol):
|
|
|
|
if vol <= 100 and vol >= 0:
|
2018-10-10 11:46:35 -05:00
|
|
|
config.app["main"]["volume"] = vol
|
2018-01-24 17:43:35 -06:00
|
|
|
self.vol = vol
|
2018-02-19 11:45:57 -06:00
|
|
|
self.player.audio_set_volume(self.vol)
|
2018-01-24 17:43:35 -06:00
|
|
|
|
2018-01-26 11:52:49 -06:00
|
|
|
def play_all(self, list_of_items, playing=0, shuffle=False):
|
|
|
|
if list_of_items != self.queue:
|
|
|
|
self.queue = list_of_items
|
|
|
|
self.shuffle = shuffle
|
|
|
|
self.queue_pos = playing
|
|
|
|
self.play(self.queue[self.queue_pos])
|
2018-01-24 17:43:35 -06:00
|
|
|
|
2018-02-22 13:57:51 -06:00
|
|
|
def end_callback(self, event, *args, **kwargs):
|
|
|
|
#https://github.com/ZeBobo5/Vlc.DotNet/issues/4
|
|
|
|
call_threaded(self.next)
|
|
|
|
|
2019-06-20 17:24:56 -05:00
|
|
|
def transcode_audio(self, item, path, _format="mp3", bitrate=320):
|
2018-02-25 04:53:39 -06:00
|
|
|
""" Converts given item to mp3. This method will be available when needed automatically."""
|
|
|
|
if item.download_url == "":
|
|
|
|
item.get_download_url()
|
2018-03-12 17:11:05 -06:00
|
|
|
log.debug("Download started: filename={0}, url={1}".format(path, item.download_url))
|
2018-02-25 04:53:39 -06:00
|
|
|
temporary_filename = "chunk_{0}".format(random.randint(0,2000000))
|
|
|
|
temporary_path = os.path.join(os.path.dirname(path), temporary_filename)
|
|
|
|
# Let's get a new VLC instance for transcoding this file.
|
2019-06-20 17:24:56 -05:00
|
|
|
transcoding_instance = vlc.Instance(*["--sout=#transcode{acodec=%s,ab=%d}:file{mux=raw,dst=\"%s\"}"% (_format, bitrate, temporary_path,)])
|
2018-02-25 04:53:39 -06:00
|
|
|
transcoder = transcoding_instance.media_player_new()
|
|
|
|
transcoder.set_mrl(item.download_url)
|
|
|
|
pub.sendMessage("change_status", status=_(u"Downloading {0}.").format(item.title,))
|
|
|
|
media = transcoder.get_media()
|
|
|
|
transcoder.play()
|
|
|
|
while True:
|
|
|
|
state = media.get_state()
|
|
|
|
pub.sendMessage("change_status", status=_("Downloading {0} ({1}%).").format(item.title, int(transcoder.get_position()*100)))
|
|
|
|
if str(state) == 'State.Ended':
|
|
|
|
break
|
|
|
|
elif str(state) == 'state.error':
|
|
|
|
os.remove(temporary_path)
|
|
|
|
break
|
|
|
|
transcoder.release()
|
|
|
|
os.rename(temporary_path, path)
|
2018-03-12 17:11:05 -06:00
|
|
|
log.debug("Download finished sucsessfully.")
|
2018-02-28 17:44:18 -06:00
|
|
|
pub.sendMessage("download_finished", file=os.path.basename(path))
|
2018-03-02 14:04:20 -06:00
|
|
|
|
|
|
|
def playback_error(self, event):
|
|
|
|
pub.sendMessage("notify", title=_("Error"), message=_("There was an error while trying to access the file you have requested."))
|
2018-02-25 04:53:39 -06:00
|
|
|
|
2018-02-22 13:57:51 -06:00
|
|
|
def __del__(self):
|
2018-03-02 14:04:20 -06:00
|
|
|
self.event_manager.event_detach(vlc.EventType.MediaPlayerEndReached)
|
|
|
|
self.event_manager.event_detach(vlc.EventType.MediaPlayerEncounteredError, self.playback_error)
|