From 8a0c48e61e51ed700ec2e286bd20ad4f1551a843 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Thu, 27 Dec 2012 00:30:03 +0100 Subject: [PATCH] Add timestamp and duration to all spotify buffers. This fixes the issue where pausing playback would show the time of the last timestamped buffer instead of the current time. We also make sure to reset the time when we start a new track. This was done by overriding the play method on the session manager as it is also used for pausing, resuming and stopping. Ideally this should probably be reworked to avoid the gst import in mopidy.backends.spotify.playback, but for now this should do. --- mopidy/backends/spotify/playback.py | 7 ++++++- mopidy/backends/spotify/session_manager.py | 10 ++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mopidy/backends/spotify/playback.py b/mopidy/backends/spotify/playback.py index 9069ce7e..d7e622fb 100644 --- a/mopidy/backends/spotify/playback.py +++ b/mopidy/backends/spotify/playback.py @@ -1,5 +1,9 @@ from __future__ import unicode_literals +import pygst +pygst.require('0.10') +import gst + import logging import functools @@ -35,6 +39,7 @@ class SpotifyPlaybackProvider(base.BasePlaybackProvider): self.backend.spotify.session.load( Link.from_string(track.uri).as_track()) self.backend.spotify.session.play(1) + self.backend.spotify.buffer_timestamp = 0 self.audio.prepare_change() self.audio.set_appsrc( @@ -54,5 +59,5 @@ class SpotifyPlaybackProvider(base.BasePlaybackProvider): def on_seek_data(self, time_position): logger.debug('playback.on_seek_data(%d) called', time_position) - self.backend.spotify.next_buffer_timestamp = time_position + self.backend.spotify.buffer_timestamp = time_position * gst.MSECOND self.backend.spotify.session.seek(time_position) diff --git a/mopidy/backends/spotify/session_manager.py b/mopidy/backends/spotify/session_manager.py index ad0a806e..d372bfa4 100644 --- a/mopidy/backends/spotify/session_manager.py +++ b/mopidy/backends/spotify/session_manager.py @@ -46,7 +46,7 @@ class SpotifySessionManager(process.BaseThread, PyspotifySessionManager): self.backend_ref = backend_ref self.connected = threading.Event() - self.next_buffer_timestamp = None + self.buffer_timestamp = 0 self.container_manager = None self.playlist_manager = None @@ -121,11 +121,13 @@ class SpotifySessionManager(process.BaseThread, PyspotifySessionManager): 'sample_rate': sample_rate, 'channels': channels, } + buffer_ = gst.Buffer(bytes(frames)) buffer_.set_caps(gst.caps_from_string(capabilites)) - if self.next_buffer_timestamp is not None: - buffer_.timestamp = self.next_buffer_timestamp * gst.MSECOND - self.next_buffer_timestamp = None + buffer_.timestamp = self.buffer_timestamp + buffer_.duration = num_frames * gst.SECOND / sample_rate + + self.buffer_timestamp += buffer_.duration if self.audio.emit_data(buffer_).get(): return num_frames