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_all.h"
22 #include "output_internal.h"
23 #include "output_control.h"
28 #include "player_control.h"
38 #define G_LOG_DOMAIN "output"
40 static struct audio_format input_audio_format
;
42 static struct audio_output
*audio_outputs
;
43 static unsigned int num_audio_outputs
;
46 * The #music_buffer object where consumed chunks are returned.
48 static struct music_buffer
*g_music_buffer
;
51 * The #music_pipe object which feeds all audio outputs. It is filled
52 * by audio_output_all_play().
54 static struct music_pipe
*g_mp
;
57 * The "elapsed_time" stamp of the most recently finished chunk.
59 static float audio_output_all_elapsed_time
= -1.0;
61 unsigned int audio_output_count(void)
63 return num_audio_outputs
;
67 audio_output_get(unsigned i
)
69 assert(i
< num_audio_outputs
);
71 return &audio_outputs
[i
];
75 audio_output_find(const char *name
)
77 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
) {
78 struct audio_output
*ao
= audio_output_get(i
);
80 if (strcmp(ao
->name
, name
) == 0)
89 audio_output_config_count(void)
92 const struct config_param
*param
= NULL
;
94 while ((param
= config_get_next_param(CONF_AUDIO_OUTPUT
, param
)))
97 nr
= 1; /* we'll always have at least one device */
102 audio_output_all_init(void)
104 const struct config_param
*param
= NULL
;
106 GError
*error
= NULL
;
108 notify_init(&audio_output_client_notify
);
110 num_audio_outputs
= audio_output_config_count();
111 audio_outputs
= g_new(struct audio_output
, num_audio_outputs
);
113 for (i
= 0; i
< num_audio_outputs
; i
++)
115 struct audio_output
*output
= &audio_outputs
[i
];
118 param
= config_get_next_param(CONF_AUDIO_OUTPUT
, param
);
120 /* only allow param to be NULL if there just one audioOutput */
121 assert(param
|| (num_audio_outputs
== 1));
123 if (!audio_output_init(output
, param
, &error
)) {
125 g_error("line %i: %s",
126 param
->line
, error
->message
);
128 g_error("%s", error
->message
);
131 /* require output names to be unique: */
132 for (j
= 0; j
< i
; j
++) {
133 if (!strcmp(output
->name
, audio_outputs
[j
].name
)) {
134 g_error("output devices with identical "
135 "names: %s\n", output
->name
);
142 audio_output_all_finish(void)
146 for (i
= 0; i
< num_audio_outputs
; i
++) {
147 audio_output_finish(&audio_outputs
[i
]);
150 g_free(audio_outputs
);
151 audio_outputs
= NULL
;
152 num_audio_outputs
= 0;
154 notify_deinit(&audio_output_client_notify
);
158 audio_output_all_enable_disable(void)
160 for (unsigned i
= 0; i
< num_audio_outputs
; i
++) {
161 struct audio_output
*ao
= &audio_outputs
[i
];
164 g_mutex_lock(ao
->mutex
);
165 enabled
= ao
->really_enabled
;
166 g_mutex_unlock(ao
->mutex
);
168 if (ao
->enabled
!= enabled
) {
170 audio_output_enable(ao
);
172 audio_output_disable(ao
);
178 * Determine if all (active) outputs have finished the current
182 audio_output_all_finished(void)
184 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
) {
185 struct audio_output
*ao
= &audio_outputs
[i
];
188 g_mutex_lock(ao
->mutex
);
189 not_finished
= audio_output_is_open(ao
) &&
190 !audio_output_command_is_finished(ao
);
191 g_mutex_unlock(ao
->mutex
);
200 static void audio_output_wait_all(void)
202 while (!audio_output_all_finished())
203 notify_wait(&audio_output_client_notify
);
207 * Signals the audio output if it is open. This function locks the
211 audio_output_lock_signal(struct audio_output
*ao
)
213 g_mutex_lock(ao
->mutex
);
214 if (audio_output_is_open(ao
))
215 g_cond_signal(ao
->cond
);
216 g_mutex_unlock(ao
->mutex
);
220 * Signals all audio outputs which are open.
223 audio_output_signal_all(void)
225 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
)
226 audio_output_lock_signal(&audio_outputs
[i
]);
230 audio_output_reset_reopen(struct audio_output
*ao
)
232 g_mutex_lock(ao
->mutex
);
234 if (!ao
->open
&& ao
->fail_timer
!= NULL
) {
235 g_timer_destroy(ao
->fail_timer
);
236 ao
->fail_timer
= NULL
;
239 g_mutex_unlock(ao
->mutex
);
243 * Resets the "reopen" flag on all audio devices. MPD should
244 * immediately retry to open the device instead of waiting for the
245 * timeout when the user wants to start playback.
248 audio_output_all_reset_reopen(void)
250 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
) {
251 struct audio_output
*ao
= &audio_outputs
[i
];
253 audio_output_reset_reopen(ao
);
258 * Opens all output devices which are enabled, but closed.
260 * @return true if there is at least open output device which is open
263 audio_output_all_update(void)
268 if (!audio_format_defined(&input_audio_format
))
271 for (i
= 0; i
< num_audio_outputs
; ++i
)
272 ret
= audio_output_update(&audio_outputs
[i
],
273 &input_audio_format
, g_mp
) || ret
;
279 audio_output_all_play(struct music_chunk
*chunk
)
284 assert(g_music_buffer
!= NULL
);
285 assert(g_mp
!= NULL
);
286 assert(chunk
!= NULL
);
287 assert(music_chunk_check_format(chunk
, &input_audio_format
));
289 ret
= audio_output_all_update();
293 music_pipe_push(g_mp
, chunk
);
295 for (i
= 0; i
< num_audio_outputs
; ++i
)
296 audio_output_play(&audio_outputs
[i
]);
302 audio_output_all_open(const struct audio_format
*audio_format
,
303 struct music_buffer
*buffer
)
305 bool ret
= false, enabled
= false;
308 assert(audio_format
!= NULL
);
309 assert(buffer
!= NULL
);
310 assert(g_music_buffer
== NULL
|| g_music_buffer
== buffer
);
311 assert((g_mp
== NULL
) == (g_music_buffer
== NULL
));
313 g_music_buffer
= buffer
;
315 /* the audio format must be the same as existing chunks in the
317 assert(g_mp
== NULL
|| music_pipe_check_format(g_mp
, audio_format
));
320 g_mp
= music_pipe_new();
322 /* if the pipe hasn't been cleared, the the audio
323 format must not have changed */
324 assert(music_pipe_size(g_mp
) == 0 ||
325 audio_format_equals(audio_format
,
326 &input_audio_format
));
328 input_audio_format
= *audio_format
;
330 audio_output_all_reset_reopen();
331 audio_output_all_enable_disable();
332 audio_output_all_update();
334 for (i
= 0; i
< num_audio_outputs
; ++i
) {
335 if (audio_outputs
[i
].enabled
)
338 if (audio_outputs
[i
].open
)
343 g_warning("All audio outputs are disabled");
346 /* close all devices if there was an error */
347 audio_output_all_close();
353 * Has the specified audio output already consumed this chunk?
356 chunk_is_consumed_in(const struct audio_output
*ao
,
357 const struct music_chunk
*chunk
)
362 if (ao
->chunk
== NULL
)
365 assert(chunk
== ao
->chunk
|| music_pipe_contains(g_mp
, ao
->chunk
));
367 if (chunk
!= ao
->chunk
) {
368 assert(chunk
->next
!= NULL
);
372 return ao
->chunk_finished
&& chunk
->next
== NULL
;
376 * Has this chunk been consumed by all audio outputs?
379 chunk_is_consumed(const struct music_chunk
*chunk
)
381 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
) {
382 const struct audio_output
*ao
= &audio_outputs
[i
];
385 g_mutex_lock(ao
->mutex
);
386 consumed
= chunk_is_consumed_in(ao
, chunk
);
387 g_mutex_unlock(ao
->mutex
);
397 * There's only one chunk left in the pipe (#g_mp), and all audio
398 * outputs have consumed it already. Clear the reference.
401 clear_tail_chunk(G_GNUC_UNUSED
const struct music_chunk
*chunk
, bool *locked
)
403 assert(chunk
->next
== NULL
);
404 assert(music_pipe_contains(g_mp
, chunk
));
406 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
) {
407 struct audio_output
*ao
= &audio_outputs
[i
];
409 /* this mutex will be unlocked by the caller when it's
411 g_mutex_lock(ao
->mutex
);
412 locked
[i
] = ao
->open
;
415 g_mutex_unlock(ao
->mutex
);
419 assert(ao
->chunk
== chunk
);
420 assert(ao
->chunk_finished
);
426 audio_output_all_check(void)
428 const struct music_chunk
*chunk
;
430 struct music_chunk
*shifted
;
431 bool locked
[num_audio_outputs
];
433 assert(g_music_buffer
!= NULL
);
434 assert(g_mp
!= NULL
);
436 while ((chunk
= music_pipe_peek(g_mp
)) != NULL
) {
437 assert(music_pipe_size(g_mp
) > 0);
439 if (!chunk_is_consumed(chunk
))
440 /* at least one output is not finished playing
442 return music_pipe_size(g_mp
);
444 if (chunk
->length
> 0 && chunk
->times
>= 0.0)
445 /* only update elapsed_time if the chunk
446 provides a defined value */
447 audio_output_all_elapsed_time
= chunk
->times
;
449 is_tail
= chunk
->next
== NULL
;
451 /* this is the tail of the pipe - clear the
452 chunk reference in all outputs */
453 clear_tail_chunk(chunk
, locked
);
455 /* remove the chunk from the pipe */
456 shifted
= music_pipe_shift(g_mp
);
457 assert(shifted
== chunk
);
460 /* unlock all audio outputs which were locked
461 by clear_tail_chunk() */
462 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
)
464 g_mutex_unlock(audio_outputs
[i
].mutex
);
466 /* return the chunk to the buffer */
467 music_buffer_return(g_music_buffer
, shifted
);
474 audio_output_all_wait(unsigned threshold
)
478 if (audio_output_all_check() < threshold
) {
486 return audio_output_all_check() < threshold
;
490 audio_output_all_pause(void)
494 audio_output_all_update();
496 for (i
= 0; i
< num_audio_outputs
; ++i
)
497 audio_output_pause(&audio_outputs
[i
]);
499 audio_output_wait_all();
503 audio_output_all_drain(void)
505 for (unsigned i
= 0; i
< num_audio_outputs
; ++i
)
506 audio_output_drain_async(&audio_outputs
[i
]);
508 audio_output_wait_all();
512 audio_output_all_cancel(void)
516 /* send the cancel() command to all audio outputs */
518 for (i
= 0; i
< num_audio_outputs
; ++i
)
519 audio_output_cancel(&audio_outputs
[i
]);
521 audio_output_wait_all();
523 /* clear the music pipe and return all chunks to the buffer */
526 music_pipe_clear(g_mp
, g_music_buffer
);
528 /* the audio outputs are now waiting for a signal, to
529 synchronize the cleared music pipe */
531 audio_output_signal_all();
533 /* invalidate elapsed_time */
535 audio_output_all_elapsed_time
= -1.0;
539 audio_output_all_close(void)
543 for (i
= 0; i
< num_audio_outputs
; ++i
)
544 audio_output_close(&audio_outputs
[i
]);
547 assert(g_music_buffer
!= NULL
);
549 music_pipe_clear(g_mp
, g_music_buffer
);
550 music_pipe_free(g_mp
);
554 g_music_buffer
= NULL
;
556 audio_format_clear(&input_audio_format
);
558 audio_output_all_elapsed_time
= -1.0;
562 audio_output_all_release(void)
566 for (i
= 0; i
< num_audio_outputs
; ++i
)
567 audio_output_release(&audio_outputs
[i
]);
570 assert(g_music_buffer
!= NULL
);
572 music_pipe_clear(g_mp
, g_music_buffer
);
573 music_pipe_free(g_mp
);
577 g_music_buffer
= NULL
;
579 audio_format_clear(&input_audio_format
);
581 audio_output_all_elapsed_time
= -1.0;
585 audio_output_all_song_border(void)
587 /* clear the elapsed_time pointer at the beginning of a new
589 audio_output_all_elapsed_time
= 0.0;
593 audio_output_all_get_elapsed_time(void)
595 return audio_output_all_elapsed_time
;