Skip to content

Commit

Permalink
Merge pull request #699 from philippe44/8.3-misc
Browse files Browse the repository at this point in the history
Player restart proposal
  • Loading branch information
mherger committed Dec 3, 2021
2 parents b0b3034 + 732a8b9 commit e1219ef
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 60 deletions.
6 changes: 0 additions & 6 deletions Slim/Control/Commands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@ sub clientForgetCommand {
return;
}

# Persist playback state like we would do when turning off a player, that is, treat a vanishing
# player that's still playing the same way as a player that's turned off while still playing, so
# we can make it start playing again if it reappears and the user told us to resume playing
# when powering on.
$client->persistPlaybackStateForPowerOff();

$client->controller()->playerInactive($client);

$client->forgetClient();
Expand Down
13 changes: 13 additions & 0 deletions Slim/Networking/Slimproto.pm
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@ sub slimproto_close {

sub forget_disconnected_client {
my $client = shift;
my $cprefs = preferences('server')->client($client);

# Persist playback state like we would do when turning off a player, that is, treat a vanishing
# player that's still playing the same way as a player that's turned off while still playing, so
# we can make it start playing again if it reappears and the user told us to resume playing
# when powering on.
$cprefs->set('playingAtPowerOff', $client->isPlaying(1));

if ($client->isPlaying(1)) {
my $position = $client->controller->playingSongElapsed();
$cprefs->set('positionAtDisconnect', $position);
main::INFOLOG && $log->is_info && $log->info("disconnected player position $position secs");
}

main::INFOLOG && $log->info("forgetting disconnected client");

Expand Down
11 changes: 2 additions & 9 deletions Slim/Player/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,12 @@ sub forgetClient {
}
}

sub persistPlaybackStateForPowerOff {
my $client = shift;

if ($client->power()) {
my $playing = $client->controller()->isPlaying(1);
$prefs->client($client)->set('playingAtPowerOff', $playing);
}
}

sub startup {
my $client = shift;
my $syncgroupid = shift;

# I don't think that should be done here b/c Squeezebox::reconnect does it as
# well but by doing it here, $client is not activated as it is not connected
Slim::Player::Sync::restoreSync($client, $syncgroupid);

# restore the old playlist
Expand Down
56 changes: 33 additions & 23 deletions Slim/Player/Player.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ sub init {
Slim::Hardware::IR::initClient($client);
Slim::Buttons::Home::updateMenu($client);

# fire it up!
# Let reconnect() decide if we need to play or not
$client->power($prefs->client($client)->get('power'), 1);
$client->startup($syncgroupid);
$client->power($prefs->client($client)->get('power'), 0, 1);

return if $client->display->isa('Slim::Display::NoDisplay');

Expand Down Expand Up @@ -201,17 +201,13 @@ sub power {
my $client = shift;
my $on = shift;
my $noplay = shift;
my $force = shift;

my $currOn = $prefs->client($client)->get('power') || 0;

return $currOn unless defined $on;
return unless (!defined(Slim::Buttons::Common::mode($client)) || ($currOn != $on)) || $force;
return unless (!defined(Slim::Buttons::Common::mode($client)) || ($currOn != $on));

my $resume = $prefs->client($client)->get('powerOnResume');
$resume =~ /(.*)Off-(.*)On/;
my ($resumeOff, $resumeOn) = ($1,$2);

my ($resumeOff) = $prefs->client($client)->get('powerOnResume') =~ /(.*)Off/;
my $controller = $client->controller();

if (!$on) {
Expand Down Expand Up @@ -251,7 +247,7 @@ sub power {
$client->display->renderCache()->{defaultfont} = undef;

# Do now, not earlier so that playmode changes still work
$prefs->client($client)->set('power', $on); # Do now, not earlier so that
$prefs->client($client)->set('power', $on);

# turn off audio outputs
$client->audio_outputs_enable(0);
Expand Down Expand Up @@ -297,23 +293,37 @@ sub power {

$controller->playerActive($client);

if (!$controller->isPlaying() && !$noplay) {

if ($resumeOn =~ /Reset/) {
# reset playlist to start, but don't start the playback yet
$client->execute(["playlist","jump", 0, 1, 1]);
}

if ($resumeOn =~ /Play/ && Slim::Player::Playlist::track($client)
&& $prefs->client($client)->get('playingAtPowerOff')) {
# play even if current playlist item is a remote url (bug 7426)
# but only if we were playing at power-off (bug 7061)
$client->execute(["play"]); # will resume if paused
}
}
# Decide if we shall resume playing on power
$client->resumeOnPower() unless $noplay;
}
}

sub resumeOnPower {
my ($client, $connect) = @_;

if (!$client->controller->isPlaying()) {
my ($resumeOn) = $prefs->client($client)->get('powerOnResume') =~ /-(.*)On/;

if ($resumeOn =~ /Reset/) {
# reset playlist to start, but don't start the playback yet
$client->execute(["playlist","jump", 0, 1, 1]);
}

if ($resumeOn =~ /Play/ && Slim::Player::Playlist::track($client)
&& $prefs->client($client)->get('playingAtPowerOff')) {
# play even if current playlist item is a remote url (bug 7426)
# but only if we were playing at power-off (bug 7061)
if ($connect) {
my $index = Slim::Player::Source::playingSongIndex($client);
my $position = $prefs->client($client)->get('positionAtDisconnect');
$client->execute(["playlist","jump", $index, 1, 0, { timeOffset => $position}]);
} else {
$client->execute(["play"]); # will resume if paused
}
}
}
}

sub welcomeScreen {
my $client = shift;

Expand Down
1 change: 0 additions & 1 deletion Slim/Player/SqueezeSlave.pm
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ sub songElapsedSeconds {

if ($client->isPlaying(1)) {
my $timeDiff = Time::HiRes::time() - $client->jiffiesToTimestamp($jiffies);
#logBacktrace($client->id, ": songElapsed=$songElapsed, jiffies=$jiffies, timeDiff=$timeDiff");
$songElapsed += $timeDiff if ($timeDiff > 0);
}

Expand Down
47 changes: 29 additions & 18 deletions Slim/Player/Squeezebox.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,42 @@ sub reconnect {

my $controller = $client->controller();

if (!$reconnect) {
if (!defined $reconnect) {

# Reconnection of a forgotten client, need to take resume position from
# the preferences
if ($client->power()) {
# Don't try to resume if we are synced, we might confuse others who have
# moved on. I think playerActive is not need should Sync::restoreSync be
# removed from Client::startup.
$client->resumeOnPower(1) if $controller->onlyActivePlayer($client);
$controller->playerActive($client);
}

} else {

if ($client->power()) {
$client->resumeOnPower();
$controller->playerActive($client);
}

if ($controller->onlyActivePlayer($client)) {
main::INFOLOG && $sourcelog->is_info && $sourcelog->info($client->id . " restaring play on pseudo-reconnect at "
. ($bytes_received ? $bytes_received : 0));
$controller->playerReconnect($bytes_received);
}
# Disconnected but not forgotten clients may need a restart or a proper stop
if (!$reconnect) {
if ($controller->onlyActivePlayer($client)) {
main::INFOLOG && $sourcelog->is_info && $sourcelog->info($client->id . " restarting play on pseudo-reconnect at "
. ($bytes_received ? $bytes_received : 0));
$controller->playerReconnect($bytes_received);
}

if ($client->isStopped()) {
# Ensure that a new client is stopped, but only on sb2s
if ( $client->isa('Slim::Player::Squeezebox2') ) {
main::INFOLOG && $sourcelog->is_info && $sourcelog->info($client->id . " forcing stop on pseudo-reconnect");
$client->stop();
if ($client->isStopped()) {
# Ensure that a new client is stopped, but only on sb2s
if ( $client->isa('Slim::Player::Squeezebox2') ) {
main::INFOLOG && $sourcelog->is_info && $sourcelog->info($client->id . " forcing stop on pseudo-reconnect");
$client->stop();
}
}
}
} else {
# bug 16881: player in a sync-group may have been made inactive upon disconnect;
# make sure it is active now.
if ($client->power()) {
$controller->playerActive($client);
}
}

}

# reinitialize the irtime to the current time so that
Expand Down
4 changes: 3 additions & 1 deletion Slim/Player/Squeezebox2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@ sub songElapsedSeconds {
$songElapsed = $elapsedSeconds;
}

if ($client->isPlaying(1)) {
# If we are disconnected and the only player or not master, elapsed shall not progress
# anymore otherwise, extrapolate value to not confuse other players
if ($client->isPlaying(1) && (!$client->disconnected() || ($client->isSynced() && Slim::Player::Sync::isMaster($client)))) {
my $timeDiff = Time::HiRes::time() - $client->jiffiesToTimestamp($jiffies);
$songElapsed += $timeDiff if ($timeDiff > 0);
}
Expand Down
5 changes: 3 additions & 2 deletions Slim/Player/StreamingController.pm
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,6 @@ sub _RetryOrNext { # -> Idle; IF [shouldretry && canretry] THEN continue

_getNextTrack($self, $params, 1);
}


sub _Continue {
my ($self, $event, $params) = @_;
Expand All @@ -942,13 +941,15 @@ sub _Continue {
if ($seekdata && $seekdata->{'streamComplete'}) {
main::INFOLOG && $log->is_info && $log->info("stream already complete at offset $bytesReceived");
_Streamout($self);
} elsif (!$bytesReceived || $seekdata) {
} elsif ($seekdata && $bytesReceived) {
main::INFOLOG && $log->is_info && $log->info("Restarting stream at offset $bytesReceived");
_Stream($self, $event, {song => $song, seekdata => $seekdata, reconnect => 1});
if ($song == playingSong($self)) {
$song->setStatus(Slim::Player::Song::STATUS_PLAYING);
}
} else {
# This handles resuming after reboot with the caveat that if connection has been lost (no reboot)
# while playing and before reception of next song's 1st byte, we'll resume the current song
main::INFOLOG && $log->is_info && $log->info("Restarting playback at time offset: ". $self->playingSongElapsed());
_JumpToTime($self, $event, {newtime => $self->playingSongElapsed(), restartIfNoSeek => 1});
}
Expand Down

0 comments on commit e1219ef

Please sign in to comment.