1 /*
2 * MOC - music on console
3 * Copyright (C) 2004-2006 Damian Pietras <daper@daper.net>
4 *
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.
9 *
10 * Contributors:
11 * - Kamil Tarkowski <kamilt@interia.pl> - "previous" request
12 *
13 */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #include <stdio.h>
20 #include <pthread.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <errno.h>
24 #include <assert.h>
25
26 #define DEBUG
27
28 #include "common.h"
29 #include "server.h"
30 #include "decoder.h"
31 #include "playlist.h"
32 #include "log.h"
33 #include "lists.h"
34
35 #ifdef HAVE_OSS
36 # include "oss.h"
37 #endif
38 #ifdef HAVE_SNDIO
39 # include "sndio_out.h"
40 #endif
41 #ifdef HAVE_ALSA
42 # include "alsa.h"
43 #endif
44 #ifndef NDEBUG
45 # include "null_out.h"
46 #endif
47 #ifdef HAVE_JACK
48 # include "jack.h"
49 #endif
50
51 #include "softmixer.h"
52 #include "equalizer.h"
53
54 #include "out_buf.h"
55 #include "protocol.h"
56 #include "options.h"
57 #include "player.h"
58 #include "audio.h"
59 #include "files.h"
60 #include "io.h"
61 #include "audio_conversion.h"
62
63 static pthread_t playing_thread = 0; /* tid of play thread */
64 static int play_thread_running = 0;
65
66 /* currently played file */
67 static int curr_playing = -1;
68 /* file we played before playing songs from queue */
69 static char *before_queue_fname = NULL;
70 static char *curr_playing_fname = NULL;
71 /* This flag is set 1 if audio_play() was called with nonempty queue,
72 * so we know that when the queue is empty, we should play the regular
73 * playlist from the beginning. */
74 static int started_playing_in_queue = 0;
75 static pthread_mutex_t curr_playing_mut = PTHREAD_MUTEX_INITIALIZER;
76
77 static struct out_buf out_buf;
78 static struct hw_funcs hw;
79 static struct output_driver_caps hw_caps; /* capabilities of the output
80 driver */
81
82 /* Player state. */
83 static int state = STATE_STOP;
84 static int prev_state = STATE_STOP;
85
86 /* requests for playing thread */
87 static int stop_playing = 0;
88 static int play_next = 0;
89 static int play_prev = 0;
90 static pthread_mutex_t request_mut = PTHREAD_MUTEX_INITIALIZER;
91
92 /* Playlists. */
93 static struct plist playlist;
94 static struct plist shuffled_plist;
95 static struct plist queue;
96 static struct plist *curr_plist; /* currently used playlist */
97 static pthread_mutex_t plist_mut = PTHREAD_MUTEX_INITIALIZER;
98
99 /* Is the audio device opened? */
100 static int audio_opened = 0;
101
102 /* Current sound parameters (with which the device is opened). */
103 static struct sound_params driver_sound_params = { 0, 0, 0 };
104
105 /* Sound parameters requested by the decoder. */
106 static struct sound_params req_sound_params = { 0, 0, 0 };
107
108 static struct audio_conversion sound_conv;
109 static int need_audio_conversion = 0;
110
111 /* URL of the last played stream. Used to fake pause/unpause of internet
112 * streams. Protected by curr_playing_mut. */
113 static char *last_stream_url = NULL;
114
115 static int current_mixer = 0;
116
117 /* Check if the two sample rates don't differ so much that we can't play. */
118 #define sample_rate_compat(sound, device) ((device) * 1.05 >= sound \
119 && (device) * 0.95 <= sound)
120
121 /* Make a human readable description of the sound sample format(s).
122 * Put the description in msg which is of size buf_size.
123 * Return msg. */
sfmt_str(const long format,char * msg,const size_t buf_size)124 char *sfmt_str (const long format, char *msg, const size_t buf_size)
125 {
126 assert (sound_format_ok(format));
127
128 assert (buf_size > 0);
129 msg[0] = 0;
130
131 if (format & SFMT_S8)
132 strncat (msg, ", 8-bit signed", buf_size - strlen(msg) - 1);
133 if (format & SFMT_U8)
134 strncat (msg, ", 8-bit unsigned", buf_size - strlen(msg) - 1);
135 if (format & SFMT_S16)
136 strncat (msg, ", 16-bit signed", buf_size - strlen(msg) - 1);
137 if (format & SFMT_U16)
138 strncat (msg, ", 16-bit unsigned", buf_size - strlen(msg) - 1);
139 if (format & SFMT_S32)
140 strncat (msg, ", 24-bit signed (as 32-bit samples)",
141 buf_size - strlen(msg) - 1);
142 if (format & SFMT_U32)
143 strncat (msg, ", 24-bit unsigned (as 32-bit samples)",
144 buf_size - strlen(msg) - 1);
145 if (format & SFMT_FLOAT)
146 strncat (msg, ", float",
147 buf_size - strlen(msg) - 1);
148
149 if (format & SFMT_LE)
150 strncat (msg, " little-endian", buf_size - strlen(msg) - 1);
151 else if (format & SFMT_BE)
152 strncat (msg, " big-endian", buf_size - strlen(msg) - 1);
153 if (format & SFMT_NE)
154 strncat (msg, " (native)", buf_size - strlen(msg) - 1);
155
156 /* skip first ", " */
157 if (msg[0])
158 memmove (msg, msg + 2, strlen(msg) + 1);
159
160 return msg;
161 }
162
163 /* Return != 0 if fmt1 and fmt2 have the same sample width. */
sfmt_same_bps(const long fmt1,const long fmt2)164 int sfmt_same_bps (const long fmt1, const long fmt2)
165 {
166 if (fmt1 & (SFMT_S8 | SFMT_U8)
167 && fmt2 & (SFMT_S8 | SFMT_U8))
168 return 1;
169 if (fmt1 & (SFMT_S16 | SFMT_U16)
170 && fmt2 & (SFMT_S16 | SFMT_U16))
171 return 1;
172 if (fmt1 & (SFMT_S32 | SFMT_U32)
173 && fmt2 & (SFMT_S32 | SFMT_U32))
174 return 1;
175 if (fmt1 & fmt2 & SFMT_FLOAT)
176 return 1;
177
178 return 0;
179 }
180
181 /* Return the best matching sample format for the requested format and
182 * available format mask. */
sfmt_best_matching(const long formats_with_endian,const long req_with_endian)183 static long sfmt_best_matching (const long formats_with_endian,
184 const long req_with_endian)
185 {
186 long formats = formats_with_endian & SFMT_MASK_FORMAT;
187 long req = req_with_endian & SFMT_MASK_FORMAT;
188 long best = 0;
189
190 #ifdef DEBUG
191 char fmt_name1[SFMT_STR_MAX];
192 char fmt_name2[SFMT_STR_MAX];
193 #endif
194
195 if (formats & req)
196 best = req;
197 else if (req == SFMT_S8 || req == SFMT_U8) {
198 if (formats & SFMT_S8)
199 best = SFMT_S8;
200 else if (formats & SFMT_U8)
201 best = SFMT_U8;
202 else if (formats & SFMT_S16)
203 best = SFMT_S16;
204 else if (formats & SFMT_U16)
205 best = SFMT_U16;
206 else if (formats & SFMT_S32)
207 best = SFMT_S32;
208 else if (formats & SFMT_U32)
209 best = SFMT_U32;
210 else if (formats & SFMT_FLOAT)
211 best = SFMT_FLOAT;
212 }
213 else if (req == SFMT_S16 || req == SFMT_U16) {
214 if (formats & SFMT_S16)
215 best = SFMT_S16;
216 else if (formats & SFMT_U16)
217 best = SFMT_U16;
218 else if (formats & SFMT_S32)
219 best = SFMT_S32;
220 else if (formats & SFMT_U32)
221 best = SFMT_U32;
222 else if (formats & SFMT_FLOAT)
223 best = SFMT_FLOAT;
224 else if (formats & SFMT_S8)
225 best = SFMT_S8;
226 else if (formats & SFMT_U8)
227 best = SFMT_U8;
228 }
229 else if (req == SFMT_S32 || req == SFMT_U32 || req == SFMT_FLOAT) {
230 if (formats & SFMT_S32)
231 best = SFMT_S32;
232 else if (formats & SFMT_U32)
233 best = SFMT_U32;
234 else if (formats & SFMT_S16)
235 best = SFMT_S16;
236 else if (formats & SFMT_U16)
237 best = SFMT_U16;
238 else if (formats & SFMT_FLOAT)
239 best = SFMT_FLOAT;
240 else if (formats & SFMT_S8)
241 best = SFMT_S8;
242 else if (formats & SFMT_U8)
243 best = SFMT_U8;
244 }
245
246 assert (best != 0);
247
248 if (!(best & (SFMT_S8 | SFMT_U8))) {
249 if ((formats_with_endian & SFMT_LE)
250 && (formats_with_endian & SFMT_BE))
251 best |= SFMT_NE;
252 else
253 best |= formats_with_endian & SFMT_MASK_ENDIANNESS;
254 }
255
256 #ifdef DEBUG
257 debug ("Chose %s as the best matching %s",
258 sfmt_str(best, fmt_name1, sizeof(fmt_name1)),
259 sfmt_str(req_with_endian, fmt_name2, sizeof(fmt_name2)));
260 #endif
261
262 return best;
263 }
264
265 /* Return the number of bytes per sample for the given format. */
sfmt_Bps(const long format)266 int sfmt_Bps (const long format)
267 {
268 int Bps = -1;
269
270 switch (format & SFMT_MASK_FORMAT) {
271 case SFMT_S8:
272 case SFMT_U8:
273 Bps = 1;
274 break;
275 case SFMT_S16:
276 case SFMT_U16:
277 Bps = 2;
278 break;
279 case SFMT_S32:
280 case SFMT_U32:
281 Bps = 4;
282 break;
283 case SFMT_FLOAT:
284 Bps = sizeof (float);
285 break;
286 }
287
288 assert (Bps > 0);
289
290 return Bps;
291 }
292
293 /* Move to the next file depending on the options set, the user
294 * request and whether or not there are files in the queue. */
go_to_another_file()295 static void go_to_another_file ()
296 {
297 int shuffle = options_get_int ("Shuffle");
298 int go_next = (play_next || options_get_int("AutoNext"));
299 int curr_playing_curr_pos;
300 /* XXX: Shouldn't play_next be protected by mutex? */
301
302 LOCK (curr_playing_mut);
303 LOCK (plist_mut);
304
305 /* If we move forward in the playlist and there are some songs in
306 * the queue, then play them. */
307 if (plist_count(&queue) && go_next) {
308 logit ("Playing file from queue");
309
310 if (!before_queue_fname && curr_playing_fname)
311 before_queue_fname = xstrdup (curr_playing_fname);
312
313 curr_plist = &queue;
314 curr_playing = plist_next (&queue, -1);
315
316 server_queue_pop (queue.items[curr_playing].file);
317 plist_delete (&queue, curr_playing);
318 }
319 else {
320 /* If we just finished playing files from the queue and the
321 * appropriate option is set, continue with the file played
322 * before playing the queue. */
323 if (before_queue_fname && options_get_int("QueueNextSongReturn")) {
324 free (curr_playing_fname);
325 curr_playing_fname = before_queue_fname;
326 before_queue_fname = NULL;
327 }
328
329 if (shuffle) {
330 curr_plist = &shuffled_plist;
331
332 if (plist_count(&playlist)
333 && !plist_count(&shuffled_plist)) {
334 plist_cat (&shuffled_plist, &playlist);
335 plist_shuffle (&shuffled_plist);
336
337 if (curr_playing_fname)
338 plist_swap_first_fname (&shuffled_plist,
339 curr_playing_fname);
340 }
341 }
342 else
343 curr_plist = &playlist;
344
345 curr_playing_curr_pos = plist_find_fname (curr_plist,
346 curr_playing_fname);
347
348 /* If we came from the queue and the last file in
349 * queue wasn't in the playlist, we try to revert to
350 * the QueueNextSongReturn = 1 behaviour. */
351 if (curr_playing_curr_pos == -1 && before_queue_fname) {
352 curr_playing_curr_pos = plist_find_fname (curr_plist,
353 before_queue_fname);
354 }
355
356 if (play_prev && plist_count(curr_plist)) {
357 logit ("Playing previous...");
358
359 if (curr_playing_curr_pos == -1
360 || started_playing_in_queue) {
361 curr_playing = plist_prev (curr_plist, -1);
362 started_playing_in_queue = 0;
363 }
364 else
365 curr_playing = plist_prev (curr_plist,
366 curr_playing_curr_pos);
367
368 if (curr_playing == -1) {
369 if (options_get_int("Repeat"))
370 curr_playing = plist_last (curr_plist);
371 logit ("Beginning of the list.");
372 }
373 else
374 logit ("Previous item.");
375 }
376 else if (go_next && plist_count(curr_plist)) {
377 logit ("Playing next...");
378
379 if (curr_playing_curr_pos == -1
380 || started_playing_in_queue) {
381 curr_playing = plist_next (curr_plist, -1);
382 started_playing_in_queue = 0;
383 }
384 else
385 curr_playing = plist_next (curr_plist,
386 curr_playing_curr_pos);
387
388 if (curr_playing == -1 && options_get_int("Repeat")) {
389 if (shuffle) {
390 plist_clear (&shuffled_plist);
391 plist_cat (&shuffled_plist, &playlist);
392 plist_shuffle (&shuffled_plist);
393 }
394 curr_playing = plist_next (curr_plist, -1);
395 logit ("Going back to the first item.");
396 }
397 else if (curr_playing == -1)
398 logit ("End of the list");
399 else
400 logit ("Next item");
401
402 }
403 else if (!options_get_int("Repeat")) {
404 curr_playing = -1;
405 }
406 else
407 debug ("Repeating file");
408
409 if (before_queue_fname)
410 free (before_queue_fname);
411 before_queue_fname = NULL;
412 }
413
414 UNLOCK (plist_mut);
415 UNLOCK (curr_playing_mut);
416 }
417
play_thread(void * unused ATTR_UNUSED)418 static void *play_thread (void *unused ATTR_UNUSED)
419 {
420 logit ("Entering playing thread");
421
422 while (curr_playing != -1) {
423 char *file;
424
425 LOCK (plist_mut);
426 file = plist_get_file (curr_plist, curr_playing);
427 UNLOCK (plist_mut);
428
429 play_next = 0;
430 play_prev = 0;
431
432 if (file) {
433 int next;
434 char *next_file;
435
436 LOCK (curr_playing_mut);
437 LOCK (plist_mut);
438 logit ("Playing item %d: %s", curr_playing, file);
439
440 if (curr_playing_fname)
441 free (curr_playing_fname);
442 curr_playing_fname = xstrdup (file);
443
444 out_buf_time_set (&out_buf, 0.0);
445
446 next = plist_next (curr_plist, curr_playing);
447 next_file = next != -1 ? plist_get_file (curr_plist, next) : NULL;
448 UNLOCK (plist_mut);
449 UNLOCK (curr_playing_mut);
450
451 player (file, next_file, &out_buf);
452 if (next_file)
453 free (next_file);
454
455 set_info_rate (0);
456 set_info_bitrate (0);
457 set_info_channels (1);
458 out_buf_time_set (&out_buf, 0.0);
459 free (file);
460 }
461
462 LOCK (curr_playing_mut);
463 if (last_stream_url) {
464 free (last_stream_url);
465 last_stream_url = NULL;
466 }
467 UNLOCK (curr_playing_mut);
468
469 if (stop_playing) {
470 LOCK (curr_playing_mut);
471 curr_playing = -1;
472 UNLOCK (curr_playing_mut);
473 logit ("stopped");
474 }
475 else
476 go_to_another_file ();
477
478 }
479
480 prev_state = state;
481 state = STATE_STOP;
482 state_change ();
483
484 if (curr_playing_fname) {
485 free (curr_playing_fname);
486 curr_playing_fname = NULL;
487 }
488
489 audio_close ();
490 logit ("Exiting");
491
492 return NULL;
493 }
494
audio_reset()495 void audio_reset ()
496 {
497 if (hw.reset)
498 hw.reset ();
499 }
500
audio_stop()501 void audio_stop ()
502 {
503 int rc;
504
505 if (play_thread_running) {
506 logit ("audio_stop()");
507 LOCK (request_mut);
508 stop_playing = 1;
509 UNLOCK (request_mut);
510 player_stop ();
511 logit ("pthread_join (playing_thread, NULL)");
512 rc = pthread_join (playing_thread, NULL);
513 if (rc != 0)
514 logit ("pthread_join() failed: %s", strerror (rc));
515 playing_thread = 0;
516 play_thread_running = 0;
517 stop_playing = 0;
518 logit ("done stopping");
519 }
520 else if (state == STATE_PAUSE) {
521
522 /* Paused internet stream - we are in fact stopped already. */
523 if (curr_playing_fname) {
524 free (curr_playing_fname);
525 curr_playing_fname = NULL;
526 }
527
528 prev_state = state;
529 state = STATE_STOP;
530 state_change ();
531 }
532 }
533
534 /* Start playing from the file fname. If fname is an empty string,
535 * start playing from the first file on the list. */
audio_play(const char * fname)536 void audio_play (const char *fname)
537 {
538 int rc;
539
540 audio_stop ();
541 player_reset ();
542
543 LOCK (curr_playing_mut);
544 LOCK (plist_mut);
545
546 /* If we have songs in the queue and fname is empty string, start
547 * playing file from the queue. */
548 if (plist_count(&queue) && !(*fname)) {
549 curr_plist = &queue;
550 curr_playing = plist_next (&queue, -1);
551
552 /* remove the file from queue */
553 server_queue_pop (queue.items[curr_playing].file);
554 plist_delete (curr_plist, curr_playing);
555
556 started_playing_in_queue = 1;
557 }
558 else if (options_get_int("Shuffle")) {
559 plist_clear (&shuffled_plist);
560 plist_cat (&shuffled_plist, &playlist);
561 plist_shuffle (&shuffled_plist);
562 plist_swap_first_fname (&shuffled_plist, fname);
563
564 curr_plist = &shuffled_plist;
565
566 if (*fname)
567 curr_playing = plist_find_fname (curr_plist, fname);
568 else if (plist_count(curr_plist)) {
569 curr_playing = plist_next (curr_plist, -1);
570 }
571 else
572 curr_playing = -1;
573 }
574 else {
575 curr_plist = &playlist;
576
577 if (*fname)
578 curr_playing = plist_find_fname (curr_plist, fname);
579 else if (plist_count(curr_plist))
580 curr_playing = plist_next (curr_plist, -1);
581 else
582 curr_playing = -1;
583 }
584
585 rc = pthread_create (&playing_thread, NULL, play_thread,
586 curr_playing != -1 ? NULL : (void *)fname);
587 if (rc != 0)
588 error ("Can't create thread: %s", strerror (rc));
589 play_thread_running = 1;
590
591 UNLOCK (plist_mut);
592 UNLOCK (curr_playing_mut);
593 }
594
audio_next()595 void audio_next ()
596 {
597 if (play_thread_running) {
598 play_next = 1;
599 player_stop ();
600 }
601 }
602
audio_prev()603 void audio_prev ()
604 {
605 if (play_thread_running) {
606 play_prev = 1;
607 player_stop ();
608 }
609 }
610
audio_pause()611 void audio_pause ()
612 {
613 LOCK (curr_playing_mut);
614 LOCK (plist_mut);
615
616 if (curr_playing != -1) {
617 char *sname = plist_get_file (curr_plist, curr_playing);
618
619 if (file_type(sname) == F_URL) {
620 UNLOCK (curr_playing_mut);
621 UNLOCK (plist_mut);
622 audio_stop ();
623 LOCK (curr_playing_mut);
624 LOCK (plist_mut);
625
626 if (last_stream_url)
627 free (last_stream_url);
628 last_stream_url = xstrdup (sname);
629
630 /* Pretend that we are paused on this. */
631 curr_playing_fname = xstrdup (sname);
632 }
633 else
634 out_buf_pause (&out_buf);
635
636 prev_state = state;
637 state = STATE_PAUSE;
638 state_change ();
639
640 free (sname);
641 }
642
643 UNLOCK (plist_mut);
644 UNLOCK (curr_playing_mut);
645 }
646
audio_unpause()647 void audio_unpause ()
648 {
649 LOCK (curr_playing_mut);
650 if (last_stream_url && file_type(last_stream_url) == F_URL) {
651 char *url = xstrdup (last_stream_url);
652
653 UNLOCK (curr_playing_mut);
654 audio_play (url);
655 free (url);
656 }
657 else if (curr_playing != -1) {
658 out_buf_unpause (&out_buf);
659 prev_state = state;
660 state = STATE_PLAY;
661 UNLOCK (curr_playing_mut);
662 state_change ();
663 }
664 else
665 UNLOCK (curr_playing_mut);
666 }
667
reset_sound_params(struct sound_params * params)668 static void reset_sound_params (struct sound_params *params)
669 {
670 params->rate = 0;
671 params->channels = 0;
672 params->fmt = 0;
673 }
674
675 /* Return 0 on error. If sound params == NULL, open the device using
676 * the previous parameters. */
audio_open(struct sound_params * sound_params)677 int audio_open (struct sound_params *sound_params)
678 {
679 int res;
680 static struct sound_params last_params = { 0, 0, 0 };
681
682 if (!sound_params)
683 sound_params = &last_params;
684 else
685 last_params = *sound_params;
686
687 assert (sound_format_ok(sound_params->fmt));
688
689 if (audio_opened) {
690 if (sound_params_eq(req_sound_params, *sound_params)) {
691 if (audio_get_bps() >= 88200) {
692 logit ("Audio device already opened with such parameters.");
693 return 1;
694 }
695
696 /* Not closing the device would cause that much
697 * sound from the previous file to stay in the buffer
698 * and the user will hear old data, so close it. */
699 logit ("Reopening device due to low bps.");
700 }
701
702 audio_close ();
703 }
704
705 req_sound_params = *sound_params;
706
707 /* Set driver_sound_params to parameters supported by the driver that
708 * are nearly the requested parameters. */
709
710 if (options_get_int("ForceSampleRate")) {
711 driver_sound_params.rate = options_get_int("ForceSampleRate");
712 logit ("Setting forced driver sample rate to %dHz",
713 driver_sound_params.rate);
714 }
715 else
716 driver_sound_params.rate = req_sound_params.rate;
717
718 driver_sound_params.fmt = sfmt_best_matching (hw_caps.formats,
719 req_sound_params.fmt);
720
721 /* number of channels */
722 driver_sound_params.channels = CLAMP(hw_caps.min_channels,
723 req_sound_params.channels,
724 hw_caps.max_channels);
725
726 res = hw.open (&driver_sound_params);
727
728 if (res) {
729 char fmt_name[SFMT_STR_MAX];
730
731 driver_sound_params.rate = hw.get_rate ();
732 if (driver_sound_params.fmt != req_sound_params.fmt
733 || driver_sound_params.channels
734 != req_sound_params.channels
735 || (!sample_rate_compat(
736 req_sound_params.rate,
737 driver_sound_params.rate))) {
738 logit ("Conversion of the sound is needed.");
739 if (!audio_conv_new (&sound_conv, &req_sound_params,
740 &driver_sound_params)) {
741 hw.close ();
742 reset_sound_params (&req_sound_params);
743 return 0;
744 }
745 need_audio_conversion = 1;
746 }
747 audio_opened = 1;
748
749 logit ("Requested sound parameters: %s, %d channels, %dHz",
750 sfmt_str(req_sound_params.fmt, fmt_name, sizeof(fmt_name)),
751 req_sound_params.channels,
752 req_sound_params.rate);
753 logit ("Driver sound parameters: %s, %d channels, %dHz",
754 sfmt_str(driver_sound_params.fmt, fmt_name, sizeof(fmt_name)),
755 driver_sound_params.channels,
756 driver_sound_params.rate);
757 }
758
759 return res;
760 }
761
audio_send_buf(const char * buf,const size_t size)762 int audio_send_buf (const char *buf, const size_t size)
763 {
764 size_t out_data_len = size;
765 int res;
766 char *converted = NULL;
767
768 if (need_audio_conversion)
769 converted = audio_conv (&sound_conv, buf, size, &out_data_len);
770
771 if (need_audio_conversion && converted)
772 res = out_buf_put (&out_buf, converted, out_data_len);
773 else if (!need_audio_conversion)
774 res = out_buf_put (&out_buf, buf, size);
775 else
776 res = 0;
777
778 if (converted)
779 free (converted);
780
781 return res;
782 }
783
784 /* Get the current audio format bytes per frame value.
785 * May return 0 if the audio device is closed. */
audio_get_bpf()786 int audio_get_bpf ()
787 {
788 return driver_sound_params.channels
789 * (driver_sound_params.fmt ? sfmt_Bps(driver_sound_params.fmt)
790 : 0);
791 }
792
793 /* Get the current audio format bytes per second value.
794 * May return 0 if the audio device is closed. */
audio_get_bps()795 int audio_get_bps ()
796 {
797 return driver_sound_params.rate * audio_get_bpf ();
798 }
799
audio_get_buf_fill()800 int audio_get_buf_fill ()
801 {
802 return hw.get_buff_fill ();
803 }
804
audio_send_pcm(const char * buf,const size_t size)805 int audio_send_pcm (const char *buf, const size_t size)
806 {
807 char *softmixed = NULL;
808 char *equalized = NULL;
809
810 if (equalizer_is_active ())
811 {
812 equalized = xmalloc (size);
813 memcpy (equalized, buf, size);
814
815 equalizer_process_buffer (equalized, size, &driver_sound_params);
816
817 buf = equalized;
818 }
819
820 if (softmixer_is_active ())
821 {
822 if (equalized)
823 {
824 softmixed = equalized;
825 }
826 else
827 {
828 softmixed = xmalloc (size);
829 memcpy (softmixed, buf, size);
830 }
831
832 softmixer_process_buffer (softmixed, size, &driver_sound_params);
833
834 buf = softmixed;
835 }
836
837 int played;
838
839 played = hw.play (buf, size);
840
841 if (played < 0)
842 fatal ("Audio output error!");
843
844 if (softmixed && !equalized)
845 free (softmixed);
846
847 if (equalized)
848 free (equalized);
849
850 return played;
851 }
852
853 /* Get current time of the song in seconds. */
audio_get_time()854 int audio_get_time ()
855 {
856 return state != STATE_STOP ? out_buf_time_get (&out_buf) : 0;
857 }
858
audio_close()859 void audio_close ()
860 {
861 if (audio_opened) {
862 reset_sound_params (&req_sound_params);
863 reset_sound_params (&driver_sound_params);
864 hw.close ();
865 if (need_audio_conversion) {
866 audio_conv_destroy (&sound_conv);
867 need_audio_conversion = 0;
868 }
869 audio_opened = 0;
870 }
871 }
872
873 /* Try to initialize drivers from the list and fill funcs with
874 * those of the first working driver. */
find_working_driver(lists_t_strs * drivers,struct hw_funcs * funcs)875 static void find_working_driver (lists_t_strs *drivers, struct hw_funcs *funcs)
876 {
877 int ix;
878
879 memset (funcs, 0, sizeof(*funcs));
880
881 for (ix = 0; ix < lists_strs_size (drivers); ix += 1) {
882 const char *name;
883
884 name = lists_strs_at (drivers, ix);
885
886 #ifdef HAVE_SNDIO
887 if (!strcasecmp(name, "sndio")) {
888 sndio_funcs (funcs);
889 printf ("Trying SNDIO...\n");
890 if (funcs->init(&hw_caps))
891 return;
892 }
893 #endif
894
895 #ifdef HAVE_OSS
896 if (!strcasecmp(name, "oss")) {
897 oss_funcs (funcs);
898 printf ("Trying OSS...\n");
899 if (funcs->init(&hw_caps))
900 return;
901 }
902 #endif
903
904 #ifdef HAVE_ALSA
905 if (!strcasecmp(name, "alsa")) {
906 alsa_funcs (funcs);
907 printf ("Trying ALSA...\n");
908 if (funcs->init(&hw_caps))
909 return;
910 }
911 #endif
912
913 #ifdef HAVE_JACK
914 if (!strcasecmp(name, "jack")) {
915 moc_jack_funcs (funcs);
916 printf ("Trying JACK...\n");
917 if (funcs->init(&hw_caps))
918 return;
919 }
920 #endif
921
922 #ifndef NDEBUG
923 if (!strcasecmp(name, "null")) {
924 null_funcs (funcs);
925 printf ("Trying NULL...\n");
926 if (funcs->init(&hw_caps))
927 return;
928 }
929 #endif
930 }
931
932 fatal ("No valid sound driver!");
933 }
934
print_output_capabilities(const struct output_driver_caps * caps)935 static void print_output_capabilities (const struct output_driver_caps *caps)
936 {
937 char fmt_name[SFMT_STR_MAX];
938
939 logit ("Sound driver capabilities: channels %d - %d, formats: %s",
940 caps->min_channels, caps->max_channels,
941 sfmt_str(caps->formats, fmt_name, sizeof(fmt_name)));
942 }
943
audio_initialize()944 void audio_initialize ()
945 {
946 find_working_driver (options_get_list ("SoundDriver"), &hw);
947
948 assert (hw_caps.max_channels >= hw_caps.min_channels);
949 assert (sound_format_ok(hw_caps.formats));
950
951 print_output_capabilities (&hw_caps);
952 if (!options_get_int("Allow24bitOutput")
953 && hw_caps.formats & (SFMT_S32 | SFMT_U32)) {
954 logit ("Disabling 24bit modes because Allow24bitOutput is set to no.");
955 hw_caps.formats &= ~(SFMT_S32 | SFMT_U32);
956 }
957
958 out_buf_init (&out_buf, options_get_int("OutputBuffer") * 1024);
959
960 softmixer_init();
961 equalizer_init();
962
963 plist_init (&playlist);
964 plist_init (&shuffled_plist);
965 plist_init (&queue);
966 player_init ();
967 }
968
audio_exit()969 void audio_exit ()
970 {
971 int rc;
972
973 audio_stop ();
974 if (hw.shutdown)
975 hw.shutdown ();
976 out_buf_destroy (&out_buf);
977 plist_free (&playlist);
978 plist_free (&shuffled_plist);
979 plist_free (&queue);
980 player_cleanup ();
981 rc = pthread_mutex_destroy (&curr_playing_mut);
982 if (rc != 0)
983 logit ("Can't destroy curr_playing_mut: %s", strerror (rc));
984 rc = pthread_mutex_destroy (&plist_mut);
985 if (rc != 0)
986 logit ("Can't destroy plist_mut: %s", strerror (rc));
987 rc = pthread_mutex_destroy (&request_mut);
988 if (rc != 0)
989 logit ("Can't destroy request_mut: %s", strerror (rc));
990
991 if (last_stream_url)
992 free (last_stream_url);
993
994 softmixer_shutdown();
995 equalizer_shutdown();
996 }
997
audio_seek(const int sec)998 void audio_seek (const int sec)
999 {
1000 int playing;
1001
1002 LOCK (curr_playing_mut);
1003 playing = curr_playing;
1004 UNLOCK (curr_playing_mut);
1005
1006 if (playing != -1 && state == STATE_PLAY)
1007 player_seek (sec);
1008 else
1009 logit ("Seeking when nothing is played.");
1010 }
1011
audio_jump_to(const int sec)1012 void audio_jump_to (const int sec)
1013 {
1014 int playing;
1015
1016 LOCK (curr_playing_mut);
1017 playing = curr_playing;
1018 UNLOCK (curr_playing_mut);
1019
1020 if (playing != -1 && state == STATE_PLAY)
1021 player_jump_to (sec);
1022 else
1023 logit ("Jumping when nothing is played.");
1024 }
1025
audio_get_state()1026 int audio_get_state ()
1027 {
1028 return state;
1029 }
1030
audio_get_prev_state()1031 int audio_get_prev_state ()
1032 {
1033 return prev_state;
1034 }
1035
audio_plist_add(const char * file)1036 void audio_plist_add (const char *file)
1037 {
1038 LOCK (plist_mut);
1039 plist_clear (&shuffled_plist);
1040 if (plist_find_fname(&playlist, file) == -1)
1041 plist_add (&playlist, file);
1042 else
1043 logit ("Wanted to add a file already present: %s", file);
1044 UNLOCK (plist_mut);
1045 }
1046
audio_queue_add(const char * file)1047 void audio_queue_add (const char *file)
1048 {
1049 LOCK (plist_mut);
1050 if (plist_find_fname(&queue, file) == -1)
1051 plist_add (&queue, file);
1052 else
1053 logit ("Wanted to add a file already present: %s", file);
1054 UNLOCK (plist_mut);
1055 }
1056
audio_plist_clear()1057 void audio_plist_clear ()
1058 {
1059 LOCK (plist_mut);
1060 plist_clear (&shuffled_plist);
1061 plist_clear (&playlist);
1062 UNLOCK (plist_mut);
1063 }
1064
audio_queue_clear()1065 void audio_queue_clear ()
1066 {
1067 LOCK (plist_mut);
1068 plist_clear (&queue);
1069 UNLOCK (plist_mut);
1070 }
1071
1072 /* Returned memory is malloc()ed. */
audio_get_sname()1073 char *audio_get_sname ()
1074 {
1075 char *sname;
1076
1077 LOCK (curr_playing_mut);
1078 sname = xstrdup (curr_playing_fname);
1079 UNLOCK (curr_playing_mut);
1080
1081 return sname;
1082 }
1083
audio_get_mixer()1084 int audio_get_mixer ()
1085 {
1086 if (current_mixer == 2)
1087 return softmixer_get_value ();
1088
1089 return hw.read_mixer ();
1090 }
1091
audio_set_mixer(const int val)1092 void audio_set_mixer (const int val)
1093 {
1094 if (!RANGE(0, val, 100)) {
1095 logit ("Tried to set mixer to volume out of range.");
1096 return;
1097 }
1098
1099 if (current_mixer == 2)
1100 softmixer_set_value (val);
1101 else
1102 hw.set_mixer (val);
1103 }
1104
audio_plist_delete(const char * file)1105 void audio_plist_delete (const char *file)
1106 {
1107 int num;
1108
1109 LOCK (plist_mut);
1110 num = plist_find_fname (&playlist, file);
1111 if (num != -1)
1112 plist_delete (&playlist, num);
1113
1114 num = plist_find_fname (&shuffled_plist, file);
1115 if (num != -1)
1116 plist_delete (&shuffled_plist, num);
1117 UNLOCK (plist_mut);
1118 }
1119
audio_queue_delete(const char * file)1120 void audio_queue_delete (const char *file)
1121 {
1122 int num;
1123
1124 LOCK (plist_mut);
1125 num = plist_find_fname (&queue, file);
1126 if (num != -1)
1127 plist_delete (&queue, num);
1128 UNLOCK (plist_mut);
1129 }
1130
1131 /* Get the time of a file if the file is on the playlist and
1132 * the time is available. */
audio_get_ftime(const char * file)1133 int audio_get_ftime (const char *file)
1134 {
1135 int i;
1136 int time;
1137 time_t mtime;
1138
1139 mtime = get_mtime (file);
1140
1141 LOCK (plist_mut);
1142 i = plist_find_fname (&playlist, file);
1143 if (i != -1) {
1144 time = get_item_time (&playlist, i);
1145 if (time != -1) {
1146 if (playlist.items[i].mtime == mtime) {
1147 debug ("Found time for %s", file);
1148 UNLOCK (plist_mut);
1149 return time;
1150 }
1151 logit ("mtime for %s has changed", file);
1152 }
1153 }
1154 UNLOCK (plist_mut);
1155
1156 return -1;
1157 }
1158
1159 /* Set the time for a file on the playlist. */
audio_plist_set_time(const char * file,const int time)1160 void audio_plist_set_time (const char *file, const int time)
1161 {
1162 int i;
1163
1164 LOCK (plist_mut);
1165 if ((i = plist_find_fname(&playlist, file)) != -1) {
1166 plist_set_item_time (&playlist, i, time);
1167 playlist.items[i].mtime = get_mtime (file);
1168 debug ("Setting time for %s", file);
1169 }
1170 else
1171 logit ("Request for updating time for a file not present on the"
1172 " playlist!");
1173 UNLOCK (plist_mut);
1174 }
1175
1176 /* Notify that the state was changed (used by the player). */
audio_state_started_playing()1177 void audio_state_started_playing ()
1178 {
1179 prev_state = state;
1180 state = STATE_PLAY;
1181 state_change ();
1182 }
1183
audio_plist_get_serial()1184 int audio_plist_get_serial ()
1185 {
1186 int serial;
1187
1188 LOCK (plist_mut);
1189 serial = plist_get_serial (&playlist);
1190 UNLOCK (plist_mut);
1191
1192 return serial;
1193 }
1194
audio_plist_set_serial(const int serial)1195 void audio_plist_set_serial (const int serial)
1196 {
1197 LOCK (plist_mut);
1198 plist_set_serial (&playlist, serial);
1199 UNLOCK (plist_mut);
1200 }
1201
1202 /* Swap 2 files on the playlist. */
audio_plist_move(const char * file1,const char * file2)1203 void audio_plist_move (const char *file1, const char *file2)
1204 {
1205 LOCK (plist_mut);
1206 plist_swap_files (&playlist, file1, file2);
1207 UNLOCK (plist_mut);
1208 }
1209
audio_queue_move(const char * file1,const char * file2)1210 void audio_queue_move (const char *file1, const char *file2)
1211 {
1212 LOCK (plist_mut);
1213 plist_swap_files (&queue, file1, file2);
1214 UNLOCK (plist_mut);
1215 }
1216
1217 /* Return a copy of the song queue. We cannot just return constant
1218 * pointer, because it will be used in a different thread.
1219 * It obviously needs to be freed after use. */
audio_queue_get_contents()1220 struct plist* audio_queue_get_contents ()
1221 {
1222 struct plist *ret = (struct plist *)xmalloc (sizeof(struct plist));
1223 plist_init (ret);
1224
1225 LOCK (plist_mut);
1226 plist_cat (ret, &queue);
1227 UNLOCK (plist_mut);
1228
1229 return ret;
1230 }
1231
audio_get_curr_tags()1232 struct file_tags *audio_get_curr_tags ()
1233 {
1234 return player_get_curr_tags ();
1235 }
1236
audio_get_mixer_channel_name()1237 char *audio_get_mixer_channel_name ()
1238 {
1239 if (current_mixer == 2)
1240 return softmixer_name ();
1241
1242 return hw.get_mixer_channel_name ();
1243 }
1244
audio_toggle_mixer_channel()1245 void audio_toggle_mixer_channel ()
1246 {
1247 current_mixer = (current_mixer + 1) % 3;
1248 if (current_mixer < 2)
1249 hw.toggle_mixer_channel ();
1250 }
1251