From 62daa08d3b9e32f6d7f81bae4407faab4347c90d Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 10 Apr 2013 19:07:26 +0200 Subject: [PATCH] mplayer: keep volume persistent, even when using --volume Consider: mpv --volume 10 file1.mkv file2.mkv Before this commit, the volume was reset to 10 when playing file2.mkv. This was inconsistent to most other options. E.g. --brightness is a rather similar case. In general, settings should never be reset when playing the next file, unless the option was explicitly marked file-local. This commit corrects the behavior of the --volume and --mute options. File local --volume still works as expected: mpv --{ --volume 10 file1.mkv file2.mkv --} This sets the volume always to 10 on playback start. Move the m_config_leave_file_local() call down so that the mixer code in uninit_player() can set the option volume and mute variables without overwriting the global option values. Another subtle issue is that we don't want to set volume if there's no need to, which is why the user_set_volume/mute fields are introduced. This is important because setting the volume might change the system volume depending on other options. --- audio/mixer.c | 4 ++++ audio/mixer.h | 2 ++ core/mplayer.c | 17 +++++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/audio/mixer.c b/audio/mixer.c index d97391cf7e..b67e548a3c 100644 --- a/audio/mixer.c +++ b/audio/mixer.c @@ -130,6 +130,7 @@ void mixer_setvolume(mixer_t *mixer, float l, float r) if (!mixer->ao || mixer->muted_using_volume) return; setvolume_internal(mixer, mixer->vol_l, mixer->vol_r); + mixer->user_set_volume = true; } void mixer_getbothvolume(mixer_t *mixer, float *b) @@ -152,6 +153,7 @@ void mixer_setmute(struct mixer *mixer, bool mute) } mixer->muted = mute; mixer->muted_by_us = mute; + mixer->user_set_mute = true; } } @@ -264,6 +266,8 @@ void mixer_reinit(struct mixer *mixer, struct ao *ao) mixer_setmute(mixer, true); if (mixer->balance != 0) mixer_setbalance(mixer, mixer->balance); + mixer->user_set_mute = false; + mixer->user_set_volume = false; } /* Called before uninitializing the audio output. The main purpose is to diff --git a/audio/mixer.h b/audio/mixer.h index 3de92e1e03..3160c20cfe 100644 --- a/audio/mixer.h +++ b/audio/mixer.h @@ -41,6 +41,8 @@ typedef struct mixer { * and needs to be restored after the driver is reinitialized. */ const char *restore_volume; float balance; + bool user_set_mute; + bool user_set_volume; } mixer_t; void mixer_reinit(struct mixer *mixer, struct ao *ao); diff --git a/core/mplayer.c b/core/mplayer.c index ee1df36654..0a108cb514 100644 --- a/core/mplayer.c +++ b/core/mplayer.c @@ -450,6 +450,8 @@ static void uninit_subs(struct demuxer *demuxer) void uninit_player(struct MPContext *mpctx, unsigned int mask) { + struct MPOpts *opts = &mpctx->opts; + mask &= mpctx->initialized_flags; mp_msg(MSGT_CPLAYER, MSGL_DBG2, "\n*** uninit(0x%X)\n", mask); @@ -542,8 +544,15 @@ void uninit_player(struct MPContext *mpctx, unsigned int mask) if (mask & INITIALIZED_AO) { mpctx->initialized_flags &= ~INITIALIZED_AO; - if (mpctx->mixer.ao) + if (mpctx->mixer.ao) { + // Normally the mixer remembers volume, but do it even if the + // volume is set explicitly with --volume=... + if (opts->mixer_init_volume >= 0 && mpctx->mixer.user_set_volume) + mixer_getbothvolume(&mpctx->mixer, &opts->mixer_init_volume); + if (opts->mixer_init_mute >= 0 && mpctx->mixer.user_set_mute) + opts->mixer_init_mute = mixer_getmute(&mpctx->mixer); mixer_uninit(&mpctx->mixer); + } if (mpctx->ao) ao_uninit(mpctx->ao, mpctx->stop_play != AT_END_OF_FILE); mpctx->ao = NULL; @@ -4148,9 +4157,6 @@ terminate_playback: // don't jump here after ao/vo/getch initialization! mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n"); - // xxx handle this as INITIALIZED_CONFIG? - m_config_leave_file_local(mpctx->mconfig); - // time to uninit all, except global stuff: int uninitialize_parts = INITIALIZED_ALL; if (opts->fixed_vo) @@ -4160,6 +4166,9 @@ terminate_playback: // don't jump here after ao/vo/getch initialization! uninitialize_parts -= INITIALIZED_AO; uninit_player(mpctx, uninitialize_parts); + // xxx handle this as INITIALIZED_CONFIG? + m_config_leave_file_local(mpctx->mconfig); + mpctx->filename = NULL; mpctx->file_format = DEMUXER_TYPE_UNKNOWN; talloc_free(mpctx->resolve_result);