configure.ac: Move OggVorbis Encoder to Encoder Plugins.
[mpd-mk.git] / src / output_thread.c
blob4bf0827f6e0cae047a6fa98f7b96504de4c7164a
1 /*
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.
20 #include "config.h"
21 #include "output_thread.h"
22 #include "output_api.h"
23 #include "output_internal.h"
24 #include "chunk.h"
25 #include "pipe.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"
31 #include <glib.h>
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <errno.h>
37 #undef G_LOG_DOMAIN
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);
50 static bool
51 ao_enable(struct audio_output *ao)
53 GError *error = NULL;
54 bool success;
56 if (ao->really_enabled)
57 return true;
59 g_mutex_unlock(ao->mutex);
60 success = ao_plugin_enable(ao->plugin, ao->data, &error);
61 g_mutex_lock(ao->mutex);
62 if (!success) {
63 g_warning("Failed to enable \"%s\" [%s]: %s\n",
64 ao->name, ao->plugin->name, error->message);
65 g_error_free(error);
66 return false;
69 ao->really_enabled = true;
70 return true;
73 static void
74 ao_close(struct audio_output *ao, bool drain);
76 static void
77 ao_disable(struct audio_output *ao)
79 if (ao->open)
80 ao_close(ao, false);
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);
91 static void
92 ao_open(struct audio_output *ao)
94 bool success;
95 GError *error = NULL;
96 const struct audio_format *filter_audio_format;
97 struct audio_format_string af_string;
99 assert(!ao->open);
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) */
106 if (!ao_enable(ao))
107 /* still no luck */
108 return;
110 /* open the filter */
112 filter_audio_format = filter_open(ao->filter, &ao->in_audio_format,
113 &error);
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);
117 g_error_free(error);
119 ao->fail_timer = g_timer_new();
120 return;
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,
130 &error);
131 g_mutex_lock(ao->mutex);
133 assert(!ao->open);
135 if (!success) {
136 g_warning("Failed to open \"%s\" [%s]: %s",
137 ao->name, ao->plugin->name, error->message);
138 g_error_free(error);
140 filter_close(ao->filter);
141 ao->fail_timer = g_timer_new();
142 return;
145 convert_filter_set(ao->convert_filter, &ao->out_audio_format);
147 ao->open = true;
149 g_debug("opened plugin=%s name=\"%s\" "
150 "audio_format=%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,
158 &af_string));
161 static void
162 ao_close(struct audio_output *ao, bool drain)
164 assert(ao->open);
166 ao->pipe = NULL;
168 ao->chunk = NULL;
169 ao->open = false;
171 g_mutex_unlock(ao->mutex);
173 if (drain)
174 ao_plugin_drain(ao->plugin, ao->data);
175 else
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);
186 static void
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,
194 &error);
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);
198 g_error_free(error);
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 */
204 ao->pipe = NULL;
206 ao->chunk = NULL;
207 ao->open = false;
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);
214 return;
217 convert_filter_set(ao->convert_filter, &ao->out_audio_format);
220 static void
221 ao_reopen(struct audio_output *ao)
223 if (!audio_format_fully_defined(&ao->config_audio_format)) {
224 if (ao->open) {
225 const struct music_pipe *mp = ao->pipe;
226 ao_close(ao, true);
227 ao->pipe = mp;
230 /* no audio format is configured: copy in->out, let
231 the output's open() method determine the effective
232 out_audio_format */
233 ao->out_audio_format = ao->in_audio_format;
234 audio_format_mask_apply(&ao->out_audio_format,
235 &ao->config_audio_format);
238 if (ao->open)
239 /* the audio format has changed, and all filters have
240 to be reconfigured */
241 ao_reopen_filter(ao);
242 else
243 ao_open(ao);
246 static bool
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;
253 assert(ao != 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
272 : NULL);
273 ao->replay_gain_serial = chunk->replay_gain_serial;
276 if (size == 0)
277 return true;
279 data = filter_filter(ao->filter, data, size, &size, &error);
280 if (data == NULL) {
281 g_warning("\"%s\" [%s] failed to filter: %s",
282 ao->name, ao->plugin->name, error->message);
283 g_error_free(error);
285 ao_close(ao, false);
287 /* don't automatically reopen this device for 10
288 seconds */
289 ao->fail_timer = g_timer_new();
290 return false;
293 while (size > 0 && ao->command == AO_COMMAND_NONE) {
294 size_t nbytes;
296 g_mutex_unlock(ao->mutex);
297 nbytes = ao_plugin_play(ao->plugin, ao->data, data, size,
298 &error);
299 g_mutex_lock(ao->mutex);
300 if (nbytes == 0) {
301 /* play()==0 means failure */
302 g_warning("\"%s\" [%s] failed to play: %s",
303 ao->name, ao->plugin->name, error->message);
304 g_error_free(error);
306 ao_close(ao, false);
308 /* don't automatically reopen this device for
309 10 seconds */
310 ao->fail_timer = g_timer_new();
311 return false;
314 assert(nbytes <= size);
315 assert(nbytes % audio_format_frame_size(&ao->out_audio_format) == 0);
317 data += nbytes;
318 size -= nbytes;
321 return true;
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 */
329 ? ao->chunk->next
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
337 * received.
339 * @return true if at least one chunk has been available, false if the
340 * tail of the pipe was already reached
342 static bool
343 ao_play(struct audio_output *ao)
345 bool success;
346 const struct music_chunk *chunk;
348 assert(ao->pipe != NULL);
350 chunk = ao_next_chunk(ao);
351 if (chunk == NULL)
352 /* no chunk available */
353 return false;
355 ao->chunk_finished = false;
357 while (chunk != NULL && ao->command == AO_COMMAND_NONE) {
358 assert(!ao->chunk_finished);
360 ao->chunk = chunk;
362 success = ao_play_chunk(ao, chunk);
363 if (!success) {
364 assert(ao->chunk == NULL);
365 break;
368 assert(ao->chunk == chunk);
369 chunk = chunk->next;
372 ao->chunk_finished = true;
374 g_mutex_unlock(ao->mutex);
375 player_lock_signal();
376 g_mutex_lock(ao->mutex);
378 return true;
381 static void ao_pause(struct audio_output *ao)
383 bool ret;
385 g_mutex_unlock(ao->mutex);
386 ao_plugin_cancel(ao->plugin, ao->data);
387 g_mutex_lock(ao->mutex);
389 ao->pause = true;
390 ao_command_finished(ao);
392 do {
393 g_mutex_unlock(ao->mutex);
394 ret = ao_plugin_pause(ao->plugin, ao->data);
395 g_mutex_lock(ao->mutex);
397 if (!ret) {
398 ao_close(ao, false);
399 break;
401 } while (ao->command == AO_COMMAND_NONE);
403 ao->pause = false;
406 static gpointer audio_output_task(gpointer arg)
408 struct audio_output *ao = arg;
410 g_mutex_lock(ao->mutex);
412 while (1) {
413 switch (ao->command) {
414 case AO_COMMAND_NONE:
415 break;
417 case AO_COMMAND_ENABLE:
418 ao_enable(ao);
419 ao_command_finished(ao);
420 break;
422 case AO_COMMAND_DISABLE:
423 ao_disable(ao);
424 ao_command_finished(ao);
425 break;
427 case AO_COMMAND_OPEN:
428 ao_open(ao);
429 ao_command_finished(ao);
430 break;
432 case AO_COMMAND_REOPEN:
433 ao_reopen(ao);
434 ao_command_finished(ao);
435 break;
437 case AO_COMMAND_CLOSE:
438 assert(ao->open);
439 assert(ao->pipe != NULL);
441 ao_close(ao, false);
442 ao_command_finished(ao);
443 break;
445 case AO_COMMAND_PAUSE:
446 if (!ao->open) {
447 /* the output has failed after
448 audio_output_all_pause() has
449 submitted the PAUSE command; bail
450 out */
451 ao_command_finished(ao);
452 break;
455 ao_pause(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 */
460 continue;
462 case AO_COMMAND_DRAIN:
463 if (ao->open) {
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);
473 continue;
475 case AO_COMMAND_CANCEL:
476 ao->chunk = NULL;
477 if (ao->open)
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
483 time */
484 if (ao->command == AO_COMMAND_NONE)
485 g_cond_wait(ao->cond, ao->mutex);
486 continue;
488 case AO_COMMAND_KILL:
489 ao->chunk = NULL;
490 ao_command_finished(ao);
491 g_mutex_unlock(ao->mutex);
492 return NULL;
495 if (ao->open && ao_play(ao))
496 /* don't wait for an event if there are more
497 chunks in the pipe */
498 continue;
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)
507 GError *e = NULL;
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);