2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "output_thread.h"
22 #include "output_api.h"
23 #include "output_internal.h"
26 #include "player_control.h"
27 #include "filter_plugin.h"
28 #include "filter/convert_filter_plugin.h"
29 #include "filter/replay_gain_filter_plugin.h"
38 #define G_LOG_DOMAIN "output"
40 static void ao_command_finished(struct audio_output
*ao
)
42 assert(ao
->command
!= AO_COMMAND_NONE
);
43 ao
->command
= AO_COMMAND_NONE
;
45 g_mutex_unlock(ao
->mutex
);
46 notify_signal(&audio_output_client_notify
);
47 g_mutex_lock(ao
->mutex
);
51 ao_enable(struct audio_output
*ao
)
56 if (ao
->really_enabled
)
59 g_mutex_unlock(ao
->mutex
);
60 success
= ao_plugin_enable(ao
->plugin
, ao
->data
, &error
);
61 g_mutex_lock(ao
->mutex
);
63 g_warning("Failed to enable \"%s\" [%s]: %s\n",
64 ao
->name
, ao
->plugin
->name
, error
->message
);
69 ao
->really_enabled
= true;
74 ao_close(struct audio_output
*ao
, bool drain
);
77 ao_disable(struct audio_output
*ao
)
82 if (ao
->really_enabled
) {
83 ao
->really_enabled
= false;
85 g_mutex_unlock(ao
->mutex
);
86 ao_plugin_disable(ao
->plugin
, ao
->data
);
87 g_mutex_lock(ao
->mutex
);
92 ao_open(struct audio_output
*ao
)
96 const struct audio_format
*filter_audio_format
;
97 struct audio_format_string af_string
;
100 assert(ao
->fail_timer
== NULL
);
101 assert(ao
->pipe
!= NULL
);
102 assert(ao
->chunk
== NULL
);
104 /* enable the device (just in case the last enable has failed) */
110 /* open the filter */
112 filter_audio_format
= filter_open(ao
->filter
, &ao
->in_audio_format
,
114 if (filter_audio_format
== NULL
) {
115 g_warning("Failed to open filter for \"%s\" [%s]: %s",
116 ao
->name
, ao
->plugin
->name
, error
->message
);
119 ao
->fail_timer
= g_timer_new();
123 ao
->out_audio_format
= *filter_audio_format
;
124 audio_format_mask_apply(&ao
->out_audio_format
,
125 &ao
->config_audio_format
);
127 g_mutex_unlock(ao
->mutex
);
128 success
= ao_plugin_open(ao
->plugin
, ao
->data
,
129 &ao
->out_audio_format
,
131 g_mutex_lock(ao
->mutex
);
136 g_warning("Failed to open \"%s\" [%s]: %s",
137 ao
->name
, ao
->plugin
->name
, error
->message
);
140 filter_close(ao
->filter
);
141 ao
->fail_timer
= g_timer_new();
145 convert_filter_set(ao
->convert_filter
, &ao
->out_audio_format
);
149 g_debug("opened plugin=%s name=\"%s\" "
151 ao
->plugin
->name
, ao
->name
,
152 audio_format_to_string(&ao
->out_audio_format
, &af_string
));
154 if (!audio_format_equals(&ao
->in_audio_format
,
155 &ao
->out_audio_format
))
156 g_debug("converting from %s",
157 audio_format_to_string(&ao
->in_audio_format
,
162 ao_close(struct audio_output
*ao
, bool drain
)
171 g_mutex_unlock(ao
->mutex
);
174 ao_plugin_drain(ao
->plugin
, ao
->data
);
176 ao_plugin_cancel(ao
->plugin
, ao
->data
);
178 ao_plugin_close(ao
->plugin
, ao
->data
);
179 filter_close(ao
->filter
);
181 g_mutex_lock(ao
->mutex
);
183 g_debug("closed plugin=%s name=\"%s\"", ao
->plugin
->name
, ao
->name
);
187 ao_reopen_filter(struct audio_output
*ao
)
189 const struct audio_format
*filter_audio_format
;
190 GError
*error
= NULL
;
192 filter_close(ao
->filter
);
193 filter_audio_format
= filter_open(ao
->filter
, &ao
->in_audio_format
,
195 if (filter_audio_format
== NULL
) {
196 g_warning("Failed to open filter for \"%s\" [%s]: %s",
197 ao
->name
, ao
->plugin
->name
, error
->message
);
200 /* this is a little code duplication fro ao_close(),
201 but we cannot call this function because we must
202 not call filter_close(ao->filter) again */
208 ao
->fail_timer
= g_timer_new();
210 g_mutex_unlock(ao
->mutex
);
211 ao_plugin_close(ao
->plugin
, ao
->data
);
212 g_mutex_lock(ao
->mutex
);
217 convert_filter_set(ao
->convert_filter
, &ao
->out_audio_format
);
221 ao_reopen(struct audio_output
*ao
)
223 if (!audio_format_fully_defined(&ao
->config_audio_format
)) {
225 const struct music_pipe
*mp
= ao
->pipe
;
230 /* no audio format is configured: copy in->out, let
231 the output's open() method determine the effective
233 ao
->out_audio_format
= ao
->in_audio_format
;
234 audio_format_mask_apply(&ao
->out_audio_format
,
235 &ao
->config_audio_format
);
239 /* the audio format has changed, and all filters have
240 to be reconfigured */
241 ao_reopen_filter(ao
);
247 ao_play_chunk(struct audio_output
*ao
, const struct music_chunk
*chunk
)
249 const char *data
= chunk
->data
;
250 size_t size
= chunk
->length
;
251 GError
*error
= NULL
;
254 assert(ao
->filter
!= NULL
);
255 assert(!music_chunk_is_empty(chunk
));
256 assert(music_chunk_check_format(chunk
, &ao
->in_audio_format
));
257 assert(size
% audio_format_frame_size(&ao
->in_audio_format
) == 0);
259 if (chunk
->tag
!= NULL
) {
260 g_mutex_unlock(ao
->mutex
);
261 ao_plugin_send_tag(ao
->plugin
, ao
->data
, chunk
->tag
);
262 g_mutex_lock(ao
->mutex
);
265 /* update replay gain */
267 if (ao
->replay_gain_filter
!= NULL
&&
268 chunk
->replay_gain_serial
!= ao
->replay_gain_serial
) {
269 replay_gain_filter_set_info(ao
->replay_gain_filter
,
270 chunk
->replay_gain_serial
!= 0
271 ? &chunk
->replay_gain_info
273 ao
->replay_gain_serial
= chunk
->replay_gain_serial
;
279 data
= filter_filter(ao
->filter
, data
, size
, &size
, &error
);
281 g_warning("\"%s\" [%s] failed to filter: %s",
282 ao
->name
, ao
->plugin
->name
, error
->message
);
287 /* don't automatically reopen this device for 10
289 ao
->fail_timer
= g_timer_new();
293 while (size
> 0 && ao
->command
== AO_COMMAND_NONE
) {
296 g_mutex_unlock(ao
->mutex
);
297 nbytes
= ao_plugin_play(ao
->plugin
, ao
->data
, data
, size
,
299 g_mutex_lock(ao
->mutex
);
301 /* play()==0 means failure */
302 g_warning("\"%s\" [%s] failed to play: %s",
303 ao
->name
, ao
->plugin
->name
, error
->message
);
308 /* don't automatically reopen this device for
310 ao
->fail_timer
= g_timer_new();
314 assert(nbytes
<= size
);
315 assert(nbytes
% audio_format_frame_size(&ao
->out_audio_format
) == 0);
324 static const struct music_chunk
*
325 ao_next_chunk(struct audio_output
*ao
)
327 return ao
->chunk
!= NULL
328 /* continue the previous play() call */
330 /* get the first chunk from the pipe */
331 : music_pipe_peek(ao
->pipe
);
335 * Plays all remaining chunks, until the tail of the pipe has been
336 * reached (and no more chunks are queued), or until a command is
339 * @return true if at least one chunk has been available, false if the
340 * tail of the pipe was already reached
343 ao_play(struct audio_output
*ao
)
346 const struct music_chunk
*chunk
;
348 assert(ao
->pipe
!= NULL
);
350 chunk
= ao_next_chunk(ao
);
352 /* no chunk available */
355 ao
->chunk_finished
= false;
357 while (chunk
!= NULL
&& ao
->command
== AO_COMMAND_NONE
) {
358 assert(!ao
->chunk_finished
);
362 success
= ao_play_chunk(ao
, chunk
);
364 assert(ao
->chunk
== NULL
);
368 assert(ao
->chunk
== chunk
);
372 ao
->chunk_finished
= true;
374 g_mutex_unlock(ao
->mutex
);
375 player_lock_signal();
376 g_mutex_lock(ao
->mutex
);
381 static void ao_pause(struct audio_output
*ao
)
385 g_mutex_unlock(ao
->mutex
);
386 ao_plugin_cancel(ao
->plugin
, ao
->data
);
387 g_mutex_lock(ao
->mutex
);
390 ao_command_finished(ao
);
393 g_mutex_unlock(ao
->mutex
);
394 ret
= ao_plugin_pause(ao
->plugin
, ao
->data
);
395 g_mutex_lock(ao
->mutex
);
401 } while (ao
->command
== AO_COMMAND_NONE
);
406 static gpointer
audio_output_task(gpointer arg
)
408 struct audio_output
*ao
= arg
;
410 g_mutex_lock(ao
->mutex
);
413 switch (ao
->command
) {
414 case AO_COMMAND_NONE
:
417 case AO_COMMAND_ENABLE
:
419 ao_command_finished(ao
);
422 case AO_COMMAND_DISABLE
:
424 ao_command_finished(ao
);
427 case AO_COMMAND_OPEN
:
429 ao_command_finished(ao
);
432 case AO_COMMAND_REOPEN
:
434 ao_command_finished(ao
);
437 case AO_COMMAND_CLOSE
:
439 assert(ao
->pipe
!= NULL
);
442 ao_command_finished(ao
);
445 case AO_COMMAND_PAUSE
:
447 /* the output has failed after
448 audio_output_all_pause() has
449 submitted the PAUSE command; bail
451 ao_command_finished(ao
);
456 /* don't "break" here: this might cause
457 ao_play() to be called when command==CLOSE
458 ends the paused state - "continue" checks
459 the new command first */
462 case AO_COMMAND_DRAIN
:
464 assert(ao
->chunk
== NULL
);
465 assert(music_pipe_peek(ao
->pipe
) == NULL
);
467 g_mutex_unlock(ao
->mutex
);
468 ao_plugin_drain(ao
->plugin
, ao
->data
);
469 g_mutex_lock(ao
->mutex
);
472 ao_command_finished(ao
);
475 case AO_COMMAND_CANCEL
:
478 ao_plugin_cancel(ao
->plugin
, ao
->data
);
479 ao_command_finished(ao
);
481 /* the player thread will now clear our music
482 pipe - wait for a notify, to give it some
484 if (ao
->command
== AO_COMMAND_NONE
)
485 g_cond_wait(ao
->cond
, ao
->mutex
);
488 case AO_COMMAND_KILL
:
490 ao_command_finished(ao
);
491 g_mutex_unlock(ao
->mutex
);
495 if (ao
->open
&& ao_play(ao
))
496 /* don't wait for an event if there are more
497 chunks in the pipe */
500 if (ao
->command
== AO_COMMAND_NONE
)
501 g_cond_wait(ao
->cond
, ao
->mutex
);
505 void audio_output_thread_start(struct audio_output
*ao
)
509 assert(ao
->command
== AO_COMMAND_NONE
);
511 if (!(ao
->thread
= g_thread_create(audio_output_task
, ao
, true, &e
)))
512 g_error("Failed to spawn output task: %s\n", e
->message
);