$OpenBSD: patch-src_player_c,v 1.19 2015/03/20 14:59:36 dcoppa Exp $

commit 310900e4be52d11388792d776d9f6b89380bbecd
Author: Lars-Dominik Braun <lars@6xq.net>
Date:   Sat Mar 7 16:20:26 2015 +0100

player: Ignore volume change before playback started

commit d888d0ac2aeb233deeb70aaca196453f8195e433
Author: Cody <codyschuyler@gmail.com>
Date:   Sat Oct 25 11:22:33 2014 -0700

Use default channel layout if zero
libav 11 reports an invalid channel layout for mp3 files. This is a
work-around. The problem is fixed with libav 11.1.

commit 0005ea3873202ddefaae6466a932c159c08fd1c3
Author: Lars-Dominik Braun <lars@6xq.net>
Date:   Wed Mar 11 20:23:02 2015 +0100

player: Fix initial track volume

--- src/player.c.orig	Sun Sep 28 08:17:29 2014
+++ src/player.c	Fri Mar 20 09:46:17 2015
@@ -85,8 +85,11 @@ void BarPlayerDestroy () {
  */
 void BarPlayerSetVolume (player_t * const player) {
 	assert (player != NULL);
-	assert (player->fvolume != NULL);
 
+	if (player->mode != PLAYER_PLAYING) {
+		return;
+	}
+
 	int ret;
 #ifdef HAVE_AVFILTER_GRAPH_SEND_COMMAND
 	/* ffmpeg and libav disagree on the type of this option (string vs. double)
@@ -94,6 +97,7 @@ void BarPlayerSetVolume (player_t * const player) {
 	char strbuf[16];
 	snprintf (strbuf, sizeof (strbuf), "%fdB",
 			player->settings->volume + player->gain);
+	assert (player->fgraph != NULL);
 	if ((ret = avfilter_graph_send_command (player->fgraph, "volume", "volume",
 					strbuf, NULL, 0, 0)) < 0) {
 #else
@@ -101,6 +105,7 @@ void BarPlayerSetVolume (player_t * const player) {
 	const double volume = pow (10, (player->settings->volume + player->gain) / 20);
 	/* libav does not provide other means to set this right now. it might not
 	 * even work everywhere. */
+	assert (player->fvolume != NULL);
 	if ((ret = av_opt_set_double (player->fvolume->priv, "volume", volume,
 			0)) != 0) {
 #endif
@@ -197,7 +202,6 @@ static bool openStream (player_t * const player) {
 	player->songPlayed = 0;
 	player->songDuration = av_q2d (player->st->time_base) *
 			(double) player->st->duration;
-	player->mode = PLAYER_PLAYING;
 
 	return true;
 }
@@ -216,6 +220,13 @@ static bool openFilter (player_t * const player) {
 
 	/* abuffer */
 	AVRational time_base = player->st->time_base;
+
+	/* Workaround for a bug in libav-11, which reports an invalid channel
+	 * layout mp3 files */
+	if (cctx->channel_layout == 0) {
+		cctx->channel_layout = av_get_default_channel_layout (cctx->channels);
+	}
+
 	snprintf (strbuf, sizeof (strbuf),
 			"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64, 
 			time_base.num, time_base.den, cctx->sample_rate,
@@ -233,7 +244,6 @@ static bool openFilter (player_t * const player) {
 			player->fgraph)) < 0) {
 		softfail ("create_filter volume");
 	}
-	BarPlayerSetVolume (player);
 
 	/* aformat: convert float samples into something more usable */
 	AVFilterContext *fafmt = NULL;
@@ -419,6 +429,8 @@ void *BarPlayerThread (void *data) {
 		retry = false;
 		if (openStream (player)) {
 			if (openFilter (player) && openDevice (player)) {
+				player->mode = PLAYER_PLAYING;
+				BarPlayerSetVolume (player);
 				retry = play (player) == AVERROR_INVALIDDATA;
 			} else {
 				/* filter missing or audio device busy */
@@ -428,6 +440,7 @@ void *BarPlayerThread (void *data) {
 			/* stream not found */
 			pret = PLAYER_RET_SOFTFAIL;
 		}
+		player->mode = PLAYER_WAITING;
 		finish (player);
 	} while (retry);
 
