Remove Manual option from configure script.
[maemo-rb.git] / apps / playback.c
blob2775e8a95bd8e1a61736986059ac17083b9b7dc6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005-2007 Miika Pekkarinen
11 * Copyright (C) 2007-2008 Nicolas Pennequin
12 * Copyright (C) 2011 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
24 #include "system.h"
25 #include "kernel.h"
26 #include "panic.h"
27 #include "buffer.h"
28 #include "sound.h"
29 #include "ata.h"
30 #include "usb.h"
31 #include "codecs.h"
32 #include "codec_thread.h"
33 #include "voice_thread.h"
34 #include "metadata.h"
35 #include "cuesheet.h"
36 #include "buffering.h"
37 #include "talk.h"
38 #include "playlist.h"
39 #include "abrepeat.h"
40 #include "pcmbuf.h"
41 #include "playback.h"
43 #ifdef HAVE_TAGCACHE
44 #include "tagcache.h"
45 #endif
47 #ifdef AUDIO_HAVE_RECORDING
48 #include "pcm_record.h"
49 #endif
51 #ifdef HAVE_LCD_BITMAP
52 #ifdef HAVE_ALBUMART
53 #include "albumart.h"
54 #endif
55 #endif
57 /* TODO: The audio thread really is doing multitasking of acting like a
58 consumer and producer of tracks. It may be advantageous to better
59 logically separate the two functions. I won't go that far just yet. */
61 /* Internal support for voice playback */
62 #define PLAYBACK_VOICE
64 #if CONFIG_PLATFORM & PLATFORM_NATIVE
65 /* Application builds don't support direct code loading */
66 #define HAVE_CODEC_BUFFERING
67 #endif
69 /* Amount of guess-space to allow for codecs that must hunt and peck
70 * for their correct seek target, 32k seems a good size */
71 #define AUDIO_REBUFFER_GUESS_SIZE (1024*32)
73 /* Define LOGF_ENABLE to enable logf output in this file */
74 /* #define LOGF_ENABLE */
75 #include "logf.h"
77 /* Macros to enable logf for queues
78 logging on SYS_TIMEOUT can be disabled */
79 #ifdef SIMULATOR
80 /* Define this for logf output of all queuing except SYS_TIMEOUT */
81 #define PLAYBACK_LOGQUEUES
82 /* Define this to logf SYS_TIMEOUT messages */
83 /*#define PLAYBACK_LOGQUEUES_SYS_TIMEOUT*/
84 #endif
86 #ifdef PLAYBACK_LOGQUEUES
87 #define LOGFQUEUE logf
88 #else
89 #define LOGFQUEUE(...)
90 #endif
92 #ifdef PLAYBACK_LOGQUEUES_SYS_TIMEOUT
93 #define LOGFQUEUE_SYS_TIMEOUT logf
94 #else
95 #define LOGFQUEUE_SYS_TIMEOUT(...)
96 #endif
98 /* Variables are commented with the threads that use them:
99 * A=audio, C=codec, O=other. A suffix of "-" indicates that the variable is
100 * read but not updated on that thread. Audio is the only user unless otherwise
101 * specified.
104 /** Miscellaneous **/
105 bool audio_is_initialized = false; /* (A,O-) */
106 extern struct codec_api ci; /* (A,C) */
108 /** Possible arrangements of the main buffer **/
109 static enum audio_buffer_state
111 AUDIOBUF_STATE_TRASHED = -1, /* trashed; must be reset */
112 AUDIOBUF_STATE_INITIALIZED = 0, /* voice+audio OR audio-only */
113 AUDIOBUF_STATE_VOICED_ONLY = 1, /* voice-only */
114 } buffer_state = AUDIOBUF_STATE_TRASHED; /* (A,O) */
116 /** Main state control **/
117 static bool ff_rw_mode SHAREDBSS_ATTR = false; /* Pre-ff-rewind mode (A,O-) */
119 enum play_status
121 PLAY_STOPPED = 0,
122 PLAY_PLAYING = AUDIO_STATUS_PLAY,
123 PLAY_PAUSED = AUDIO_STATUS_PLAY | AUDIO_STATUS_PAUSE,
124 } play_status = PLAY_STOPPED;
126 /* Sizeable things that only need exist during playback and not when stopped */
127 static struct audio_scratch_memory
129 struct mp3entry codec_id3; /* (A,C) */
130 struct mp3entry unbuffered_id3;
131 struct cuesheet *curr_cue; /* Will follow this structure */
132 } * audio_scratch_memory = NULL;
134 /* These are used to store the current, next and optionally the peek-ahead
135 * mp3entry's - this guarantees that the pointer returned by audio_current/
136 * next_track will be valid for the full duration of the currently playing
137 * track */
138 enum audio_id3_types
140 /* These are allocated statically */
141 PLAYING_ID3 = 0,
142 NEXTTRACK_ID3,
143 #ifdef AUDIO_FAST_SKIP_PREVIEW
144 /* The real playing metadata must has to be protected since it contains
145 critical info for other features */
146 PLAYING_PEEK_ID3,
147 #endif
148 ID3_TYPE_NUM_STATIC,
149 /* These go in the scratch memory */
150 UNBUFFERED_ID3 = ID3_TYPE_NUM_STATIC,
151 CODEC_ID3,
153 static struct mp3entry static_id3_entries[ID3_TYPE_NUM_STATIC]; /* (A,O) */
155 /* Peeking functions can yield and mess us up */
156 static struct mutex id3_mutex SHAREDBSS_ATTR; /* (A,O)*/
159 /** For Scrobbler support **/
161 /* Previous track elapsed time */
162 static unsigned long prev_track_elapsed = 0; /* (A,O-) */
165 /** For album art support **/
166 #define MAX_MULTIPLE_AA SKINNABLE_SCREENS_COUNT
167 #ifdef HAVE_ALBUMART
169 static struct albumart_slot
171 struct dim dim; /* Holds width, height of the albumart */
172 int used; /* Counter; increments if something uses it */
173 } albumart_slots[MAX_MULTIPLE_AA]; /* (A,O) */
175 #define FOREACH_ALBUMART(i) for(i = 0;i < MAX_MULTIPLE_AA; i++)
176 #endif /* HAVE_ALBUMART */
179 /** Information used for tracking buffer fills **/
181 /* Buffer and thread state tracking */
182 static enum filling_state
184 STATE_BOOT = 0, /* audio thread is not ready yet */
185 STATE_IDLE, /* audio is stopped: nothing to do */
186 STATE_FILLING, /* adding tracks to the buffer */
187 STATE_FULL, /* can't add any more tracks */
188 STATE_END_OF_PLAYLIST, /* all remaining tracks have been added */
189 STATE_FINISHED, /* all remaining tracks are fully buffered */
190 STATE_ENDING, /* audio playback is ending */
191 STATE_ENDED, /* audio playback is done */
192 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
193 STATE_USB, /* USB mode, ignore most messages */
194 #endif
195 } filling = STATE_BOOT;
197 /* Track info - holds information about each track in the buffer */
198 struct track_info
200 /* In per-track allocated order: */
201 int id3_hid; /* Metadata handle ID */
202 int cuesheet_hid; /* Parsed cuesheet handle ID */
203 #ifdef HAVE_ALBUMART
204 int aa_hid[MAX_MULTIPLE_AA];/* Album art handle IDs */
205 #endif
206 #ifdef HAVE_CODEC_BUFFERING
207 int codec_hid; /* Buffered codec handle ID */
208 #endif
209 int audio_hid; /* Main audio data handle ID */
210 size_t filesize; /* File total length on disk
211 TODO: This should be stored
212 in the handle or the
213 id3 and would use less
214 ram */
217 /* Track list - holds info about all buffered tracks */
218 #if MEMORYSIZE >= 32
219 #define TRACK_LIST_LEN 128 /* Must be 2^int(+n) */
220 #elif MEMORYSIZE >= 16
221 #define TRACK_LIST_LEN 64
222 #elif MEMORYSIZE >= 8
223 #define TRACK_LIST_LEN 32
224 #else
225 #define TRACK_LIST_LEN 16
226 #endif
228 #define TRACK_LIST_MASK (TRACK_LIST_LEN-1)
230 static struct
232 /* read, write and current are maintained unwrapped, limited only by the
233 unsigned int range and wrap-safe comparisons are used */
235 /* NOTE: there appears to be a bug in arm-elf-eabi-gcc 4.4.4 for ARMv4 where
236 if 'end' follows 'start' in this structure, track_list_count performs
237 'start - end' rather than 'end - start', giving negative count values...
238 so leave it this way for now! */
239 unsigned int end; /* Next open position */
240 unsigned int start; /* First track in list */
241 unsigned int current; /* Currently decoding track */
242 struct track_info tracks[TRACK_LIST_LEN]; /* Buffered track information */
243 } track_list; /* (A, O-) */
246 /* Playlist steps from playlist position to next track to be buffered */
247 static int playlist_peek_offset = 0;
249 /* Metadata handle of track load in progress (meaning all handles have not
250 yet been opened for the track, id3 always exists or the track does not)
252 Tracks are keyed by their metadata handles if track list pointers are
253 insufficient to make comparisons */
254 static int in_progress_id3_hid = ERR_HANDLE_NOT_FOUND;
256 #ifdef HAVE_DISK_STORAGE
257 /* Buffer margin A.K.A. anti-skip buffer (in seconds) */
258 static size_t buffer_margin = 5;
259 #endif
261 /* Values returned for track loading */
262 enum track_load_status
264 LOAD_TRACK_ERR_START_CODEC = -6,
265 LOAD_TRACK_ERR_FINISH_FAILED = -5,
266 LOAD_TRACK_ERR_FINISH_FULL = -4,
267 LOAD_TRACK_ERR_BUSY = -3,
268 LOAD_TRACK_ERR_NO_MORE = -2,
269 LOAD_TRACK_ERR_FAILED = -1,
270 LOAD_TRACK_OK = 0,
271 LOAD_TRACK_READY = 1,
274 /** Track change controls **/
276 /* What sort of skip is pending globally? */
277 enum track_skip_type
279 /* Relative to what user is intended to see: */
280 /* Codec: +0, Track List: +0, Playlist: +0 */
281 TRACK_SKIP_NONE = 0, /* no track skip */
282 /* Codec: +1, Track List: +1, Playlist: +0 */
283 TRACK_SKIP_AUTO, /* codec-initiated skip */
284 /* Codec: +1, Track List: +1, Playlist: +1 */
285 TRACK_SKIP_AUTO_NEW_PLAYLIST, /* codec-initiated skip is new playlist */
286 /* Codec: xx, Track List: +0, Playlist: +0 */
287 TRACK_SKIP_AUTO_END_PLAYLIST, /* codec-initiated end of playlist */
288 /* Manual skip: Never pends */
289 TRACK_SKIP_MANUAL, /* manual track skip */
290 /* Manual skip: Never pends */
291 TRACK_SKIP_DIR_CHANGE, /* manual directory skip */
292 } skip_pending = TRACK_SKIP_NONE;
294 /* Note about TRACK_SKIP_AUTO_NEW_PLAYLIST:
295 Fixing playlist code to be able to peek into the first song of
296 the next playlist would fix any issues and this wouldn't need
297 to be a special case since pre-advancing the playlist would be
298 unneeded - it could be much more like TRACK_SKIP_AUTO and all
299 actions that require reversal during an in-progress transition
300 would work as expected */
302 /* Used to indicate status for the events. Must be separate to satisfy all
303 clients so the correct metadata is read when sending the change events
304 and also so that it is read correctly outside the events. */
305 static bool automatic_skip = false; /* (A, O-) */
307 /* Pending manual track skip offset */
308 static int skip_offset = 0; /* (A, O) */
310 /* Track change notification */
311 static struct
313 unsigned int in; /* Number of pcmbuf posts (audio isr) */
314 unsigned int out; /* Number of times audio has read the difference */
315 } track_change = { 0, 0 };
317 /** Codec status **/
318 /* Did the codec notify us it finished while we were paused or while still
319 in an automatic transition?
321 If paused, it is necessary to defer a codec-initiated skip until resuming
322 or else the track will move forward while not playing audio!
324 If in-progress, skips should not build-up ahead of where the WPS is when
325 really short tracks finish decoding.
327 If it is forgotten, it will be missed altogether and playback will just sit
328 there looking stupid and comatose until the user does something */
329 static bool codec_skip_pending = false;
330 static int codec_skip_status;
331 static bool codec_seeking = false; /* Codec seeking ack expected? */
334 /* Event queues */
335 static struct event_queue audio_queue SHAREDBSS_ATTR;
337 /* Audio thread */
338 static struct queue_sender_list audio_queue_sender_list SHAREDBSS_ATTR;
339 static long audio_stack[(DEFAULT_STACK_SIZE + 0x1000)/sizeof(long)];
340 static const char audio_thread_name[] = "audio";
341 static unsigned int audio_thread_id = 0;
343 /* Forward declarations */
344 enum audio_start_playback_flags
346 AUDIO_START_RESTART = 0x1, /* "Restart" playback (flush _all_ tracks) */
347 AUDIO_START_NEWBUF = 0x2, /* Mark the audiobuffer as invalid */
350 static void audio_start_playback(size_t offset, unsigned int flags);
351 static void audio_stop_playback(void);
352 static void buffer_event_buffer_low_callback(void *data);
353 static void buffer_event_rebuffer_callback(void *data);
354 static void buffer_event_finished_callback(void *data);
357 /**************************************/
359 /** --- audio_queue helpers --- **/
361 /* codec thread needs access */
362 void audio_queue_post(long id, intptr_t data)
364 queue_post(&audio_queue, id, data);
367 static intptr_t audio_queue_send(long id, intptr_t data)
369 return queue_send(&audio_queue, id, data);
373 /** --- MP3Entry --- **/
375 /* Does the mp3entry have enough info for us to use it? */
376 static struct mp3entry * valid_mp3entry(const struct mp3entry *id3)
378 return id3 && (id3->length != 0 || id3->filesize != 0) &&
379 id3->codectype != AFMT_UNKNOWN ? (struct mp3entry *)id3 : NULL;
382 /* Return a pointer to an mp3entry on the buffer, as it is */
383 static struct mp3entry * bufgetid3(int handle_id)
385 if (handle_id < 0)
386 return NULL;
388 struct mp3entry *id3;
389 ssize_t ret = bufgetdata(handle_id, 0, (void *)&id3);
391 if (ret != sizeof(struct mp3entry))
392 return NULL;
394 return id3;
397 /* Read an mp3entry from the buffer, adjusted */
398 static bool bufreadid3(int handle_id, struct mp3entry *id3out)
400 struct mp3entry *id3 = bufgetid3(handle_id);
402 if (id3)
404 copy_mp3entry(id3out, id3);
405 return true;
408 return false;
411 /* Lock the id3 mutex */
412 static void id3_mutex_lock(void)
414 mutex_lock(&id3_mutex);
417 /* Unlock the id3 mutex */
418 static void id3_mutex_unlock(void)
420 mutex_unlock(&id3_mutex);
423 /* Return one of the collection of mp3entry pointers - collect them all here */
424 static inline struct mp3entry * id3_get(enum audio_id3_types id3_num)
426 switch (id3_num)
428 case UNBUFFERED_ID3:
429 return &audio_scratch_memory->unbuffered_id3;
430 case CODEC_ID3:
431 return &audio_scratch_memory->codec_id3;
432 default:
433 return &static_id3_entries[id3_num];
437 /* Copy an mp3entry into one of the mp3 entries */
438 static void id3_write(enum audio_id3_types id3_num,
439 const struct mp3entry *id3_src)
441 struct mp3entry *dest_id3 = id3_get(id3_num);
443 if (id3_src)
444 copy_mp3entry(dest_id3, id3_src);
445 else
446 wipe_mp3entry(dest_id3);
449 /* Call id3_write "safely" because peek aheads can yield, even if the fast
450 preview isn't enabled */
451 static void id3_write_locked(enum audio_id3_types id3_num,
452 const struct mp3entry *id3_src)
454 id3_mutex_lock();
455 id3_write(id3_num, id3_src);
456 id3_mutex_unlock();
460 /** --- Track info --- **/
462 /* Close a handle and mark it invalid */
463 static void track_info_close_handle(int *hid_p)
465 int hid = *hid_p;
467 /* bufclose returns true if the handle is not found, or if it is closed
468 * successfully, so these checks are safe on non-existant handles */
469 if (hid >= 0)
470 bufclose(hid);
472 /* Always reset to "no handle" in case it was something else */
473 *hid_p = ERR_HANDLE_NOT_FOUND;
476 /* Close all handles in a struct track_info and clear it */
477 static void track_info_close(struct track_info *info)
479 /* Close them in the order they are allocated on the buffer to speed up
480 the handle searching */
481 track_info_close_handle(&info->id3_hid);
482 track_info_close_handle(&info->cuesheet_hid);
483 #ifdef HAVE_ALBUMART
484 int i;
485 FOREACH_ALBUMART(i)
486 track_info_close_handle(&info->aa_hid[i]);
487 #endif
488 #ifdef HAVE_CODEC_BUFFERING
489 track_info_close_handle(&info->codec_hid);
490 #endif
491 track_info_close_handle(&info->audio_hid);
492 info->filesize = 0;
495 /* Invalidate all members to initial values - does not close handles */
496 static void track_info_wipe(struct track_info * info)
498 info->id3_hid = ERR_HANDLE_NOT_FOUND;
499 info->cuesheet_hid = ERR_HANDLE_NOT_FOUND;
500 #ifdef HAVE_ALBUMART
501 int i;
502 FOREACH_ALBUMART(i)
503 info->aa_hid[i] = ERR_HANDLE_NOT_FOUND;
504 #endif
505 #ifdef HAVE_CODEC_BUFFERING
506 info->codec_hid = ERR_HANDLE_NOT_FOUND;
507 #endif
508 info->audio_hid = ERR_HANDLE_NOT_FOUND;
509 info->filesize = 0;
513 /** --- Track list --- **/
515 /* Initialize the track list */
516 static void track_list_init(void)
518 int i;
519 for (i = 0; i < TRACK_LIST_LEN; i++)
520 track_info_wipe(&track_list.tracks[i]);
522 track_list.start = track_list.end = track_list.current;
525 /* Return number of items allocated in the list */
526 static unsigned int track_list_count(void)
528 return track_list.end - track_list.start;
531 /* Return true if the list is empty */
532 static inline bool track_list_empty(void)
534 return track_list.end == track_list.start;
537 /* Returns true if the list is holding the maximum number of items */
538 static bool track_list_full(void)
540 return track_list.end - track_list.start >= TRACK_LIST_LEN;
543 /* Test if the index is within the allocated range */
544 static bool track_list_in_range(int pos)
546 return (int)(pos - track_list.start) >= 0 &&
547 (int)(pos - track_list.end) < 0;
550 static struct track_info * track_list_entry(int pos)
552 return &track_list.tracks[pos & TRACK_LIST_MASK];
555 /* Return the info of the last allocation plus an offset, NULL if result is
556 out of bounds */
557 static struct track_info * track_list_last(int offset)
559 /* Last is before the end since the end isn't inclusive */
560 unsigned int pos = track_list.end + offset - 1;
562 if (!track_list_in_range(pos))
563 return NULL;
565 return track_list_entry(pos);
568 /* Allocate space at the end for another track if not full */
569 static struct track_info * track_list_alloc_track(void)
571 if (track_list_full())
572 return NULL;
574 return track_list_entry(track_list.end++);
577 /* Remove the last track entry allocated in order to support backing out
578 of a track load */
579 static void track_list_unalloc_track(void)
581 if (track_list_empty())
582 return;
584 track_list.end--;
586 if (track_list.current == track_list.end &&
587 track_list.current != track_list.start)
589 /* Current _must_ remain within bounds */
590 track_list.current--;
594 /* Return current track plus an offset, NULL if result is out of bounds */
595 static struct track_info * track_list_current(int offset)
597 unsigned int pos = track_list.current + offset;
599 if (!track_list_in_range(pos))
600 return NULL;
602 return track_list_entry(pos);
605 /* Return current based upon what's intended that the user sees - not
606 necessarily where decoding is taking place */
607 static struct track_info * track_list_user_current(int offset)
609 if (skip_pending == TRACK_SKIP_AUTO ||
610 skip_pending == TRACK_SKIP_AUTO_NEW_PLAYLIST)
612 offset--;
615 return track_list_current(offset);
618 /* Advance current track by an offset, return false if result is out of
619 bounds */
620 static struct track_info * track_list_advance_current(int offset)
622 unsigned int pos = track_list.current + offset;
624 if (!track_list_in_range(pos))
625 return NULL;
627 track_list.current = pos;
628 return track_list_entry(pos);
631 /* Clear tracks in the list, optionally preserving the current track -
632 returns 'false' if the operation was changed */
633 enum track_clear_action
635 TRACK_LIST_CLEAR_ALL = 0, /* Clear all tracks */
636 TRACK_LIST_KEEP_CURRENT, /* Keep current only; clear before + after */
637 TRACK_LIST_KEEP_NEW /* Keep current and those that follow */
640 static void track_list_clear(enum track_clear_action action)
642 logf("%s(%d)", __func__, (int)action);
644 /* Don't care now since rebuffering is imminent */
645 buf_set_watermark(0);
647 if (action != TRACK_LIST_CLEAR_ALL)
649 struct track_info *cur = track_list_current(0);
651 if (!cur || cur->id3_hid < 0)
652 action = TRACK_LIST_CLEAR_ALL; /* Nothing worthwhile keeping */
655 /* Noone should see this progressing */
656 int start = track_list.start;
657 int current = track_list.current;
658 int end = track_list.end;
660 track_list.start = current;
662 switch (action)
664 case TRACK_LIST_CLEAR_ALL:
665 /* Result: .start = .current, .end = .current */
666 track_list.end = current;
667 break;
669 case TRACK_LIST_KEEP_CURRENT:
670 /* Result: .start = .current, .end = .current + 1 */
671 track_list.end = current + 1;
672 break;
674 case TRACK_LIST_KEEP_NEW:
675 /* Result: .start = .current, .end = .end */
676 end = current;
677 break;
680 /* Close all open handles in the range except the for the current track
681 if preserving that */
682 while (start != end)
684 if (action != TRACK_LIST_KEEP_CURRENT || start != current)
686 struct track_info *info =
687 &track_list.tracks[start & TRACK_LIST_MASK];
689 /* If this is the in-progress load, abort it */
690 if (in_progress_id3_hid >= 0 &&
691 info->id3_hid == in_progress_id3_hid)
693 in_progress_id3_hid = ERR_HANDLE_NOT_FOUND;
696 track_info_close(info);
699 start++;
704 /** --- Audio buffer -- **/
706 /* What size is needed for the scratch buffer? */
707 static size_t scratch_mem_size(void)
709 size_t size = sizeof (struct audio_scratch_memory);
711 if (global_settings.cuesheet)
712 size += sizeof (struct cuesheet);
714 return size;
717 /* Initialize the memory area where data is stored that is only used when
718 playing audio and anything depending upon it */
719 static void scratch_mem_init(void *mem)
721 audio_scratch_memory = (struct audio_scratch_memory *)mem;
722 id3_write_locked(UNBUFFERED_ID3, NULL);
723 id3_write(CODEC_ID3, NULL);
724 ci.id3 = id3_get(CODEC_ID3);
725 audio_scratch_memory->curr_cue = NULL;
727 if (global_settings.cuesheet)
729 audio_scratch_memory->curr_cue =
730 SKIPBYTES((struct cuesheet *)audio_scratch_memory,
731 sizeof (struct audio_scratch_memory));
735 /* Set up the audio buffer for playback */
736 static void audio_reset_buffer(void)
739 * Layout audio buffer as follows:
740 * [[|TALK]|SCRATCH|BUFFERING|PCM|]
743 /* see audio_get_recording_buffer if this is modified */
744 logf("%s()", __func__);
746 /* If the setup of anything allocated before the file buffer is
747 changed, do check the adjustments after the buffer_alloc call
748 as it will likely be affected and need sliding over */
750 /* Initially set up file buffer as all space available */
751 unsigned char *filebuf = audiobuf + talk_get_bufsize();
752 size_t filebuflen = audiobufend - filebuf;
753 size_t allocsize;
755 ALIGN_BUFFER(filebuf, filebuflen, sizeof (intptr_t));
757 /* Subtract whatever the pcm buffer says it used plus the guard buffer */
758 allocsize = pcmbuf_init(filebuf + filebuflen);
760 /* Make sure filebuflen is a pointer sized multiple after adjustment */
761 allocsize = ALIGN_UP(allocsize, sizeof (intptr_t));
762 if (allocsize > filebuflen)
763 goto bufpanic;
765 filebuflen -= allocsize;
767 /* Scratch memory */
768 allocsize = scratch_mem_size();
769 if (allocsize > filebuflen)
770 goto bufpanic;
772 scratch_mem_init(filebuf);
773 filebuf += allocsize;
774 filebuflen -= allocsize;
776 buffering_reset(filebuf, filebuflen);
778 /* Clear any references to the file buffer */
779 buffer_state = AUDIOBUF_STATE_INITIALIZED;
781 #if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
782 /* Make sure everything adds up - yes, some info is a bit redundant but
783 aids viewing and the summation of certain variables should add up to
784 the location of others. */
786 size_t pcmbufsize;
787 const unsigned char *pcmbuf = pcmbuf_get_meminfo(&pcmbufsize);
788 logf("fbuf: %08X", (unsigned)filebuf);
789 logf("fbufe: %08X", (unsigned)(filebuf + filebuflen));
790 logf("sbuf: %08X", (unsigned)audio_scratch_memory);
791 logf("sbufe: %08X", (unsigned)(audio_scratch_memory + allocsize));
792 logf("pcmb: %08X", (unsigned)pcmbuf);
793 logf("pcmbe: %08X", (unsigned)(pcmbuf + pcmbufsize));
795 #endif
797 return;
799 bufpanic:
800 panicf("%s(): EOM (%zu > %zu)", __func__, allocsize, filebuflen);
803 /* Set the buffer margin to begin rebuffering when 'seconds' from empty */
804 static void audio_update_filebuf_watermark(int seconds)
806 size_t bytes = 0;
808 #ifdef HAVE_DISK_STORAGE
809 int spinup = ata_spinup_time();
811 if (seconds == 0)
813 /* By current setting */
814 seconds = buffer_margin;
816 else
818 /* New setting */
819 buffer_margin = seconds;
821 if (buf_get_watermark() == 0)
823 /* Write a watermark only if the audio thread already did so for
824 itself or it will fail to set the event and the watermark - if
825 it hasn't yet, it will use the new setting when it does */
826 return;
830 if (spinup)
831 seconds += (spinup / HZ) + 1;
832 else
833 seconds += 5;
835 seconds += buffer_margin;
836 #else
837 /* flash storage */
838 seconds = 1;
839 #endif
841 /* Watermark is a function of the bitrate of the last track in the buffer */
842 struct mp3entry *id3 = NULL;
843 struct track_info *info = track_list_last(0);
845 if (info)
846 id3 = valid_mp3entry(bufgetid3(info->id3_hid));
848 if (id3)
850 if (get_audio_base_data_type(id3->codectype) == TYPE_PACKET_AUDIO)
852 bytes = id3->bitrate * (1000/8) * seconds;
854 else
856 /* Bitrate has no meaning to buffering margin for atomic audio -
857 rebuffer when it's the only track left unless it's the only
858 track that fits, in which case we should avoid constant buffer
859 low events */
860 if (track_list_count() > 1)
861 bytes = info->filesize + 1;
864 else
866 /* Then set the minimum - this should not occur anyway */
867 logf("fwmark: No id3 for last track (s%u/c%u/e%u)",
868 track_list.start, track_list.current, track_list.end);
871 /* Actually setting zero disables the notification and we use that
872 to detect that it has been reset */
873 buf_set_watermark(MAX(bytes, 1));
874 logf("fwmark: %lu", (unsigned long)bytes);
878 /** -- Track change notification -- **/
880 /* Check the pcmbuf track changes and return write the message into the event
881 if there are any */
882 static inline bool audio_pcmbuf_track_change_scan(void)
884 if (track_change.out != track_change.in)
886 track_change.out++;
887 return true;
890 return false;
893 /* Clear outstanding track change posts */
894 static inline void audio_pcmbuf_track_change_clear(void)
896 track_change.out = track_change.in;
899 /* Post a track change notification - called by audio ISR */
900 static inline void audio_pcmbuf_track_change_post(void)
902 track_change.in++;
906 /** --- Helper functions --- **/
908 /* Removes messages that might end up in the queue before or while processing
909 a manual track change. Responding to them would be harmful since they
910 belong to a previous track's playback period. Anything that would generate
911 the stale messages must first be put into a state where it will not do so.
913 static void audio_clear_track_notifications(void)
915 static const long filter_list[][2] =
917 /* codec messages */
918 { Q_AUDIO_CODEC_SEEK_COMPLETE, Q_AUDIO_CODEC_COMPLETE },
919 /* track change messages */
920 { Q_AUDIO_TRACK_CHANGED, Q_AUDIO_TRACK_CHANGED },
923 const int filter_count = ARRAYLEN(filter_list) - 1;
925 /* Remove any pcmbuf notifications */
926 pcmbuf_monitor_track_change(false);
927 audio_pcmbuf_track_change_clear();
929 /* Scrub the audio queue of the old mold */
930 while (queue_peek_ex(&audio_queue, NULL,
931 filter_count | QPEEK_REMOVE_EVENTS,
932 filter_list))
934 yield(); /* Not strictly needed, per se, ad infinitum, ra, ra */
938 /* Takes actions based upon track load status codes */
939 static void audio_handle_track_load_status(int trackstat)
941 switch (trackstat)
943 case LOAD_TRACK_ERR_NO_MORE:
944 if (track_list_count() > 0)
945 break;
947 case LOAD_TRACK_ERR_START_CODEC:
948 audio_queue_post(Q_AUDIO_CODEC_COMPLETE, CODEC_ERROR);
949 break;
951 default:
952 break;
956 /* Announce the end of playing the current track */
957 static void audio_playlist_track_finish(void)
959 struct mp3entry *id3 = valid_mp3entry(id3_get(PLAYING_ID3));
961 playlist_update_resume_info(filling == STATE_ENDED ? NULL : id3);
963 if (id3)
965 send_event(PLAYBACK_EVENT_TRACK_FINISH, id3);
966 prev_track_elapsed = id3->elapsed;
968 else
970 prev_track_elapsed = 0;
974 /* Announce the beginning of the new track */
975 static void audio_playlist_track_change(void)
977 struct mp3entry *id3 = valid_mp3entry(id3_get(PLAYING_ID3));
979 if (id3)
980 send_event(PLAYBACK_EVENT_TRACK_CHANGE, id3);
982 playlist_update_resume_info(id3);
985 /* Change the data for the next track and send the event */
986 static void audio_update_and_announce_next_track(const struct mp3entry *id3_next)
988 id3_write_locked(NEXTTRACK_ID3, id3_next);
989 send_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE,
990 id3_get(NEXTTRACK_ID3));
993 /* Bring the user current mp3entry up to date and set a new offset for the
994 buffered metadata */
995 static void playing_id3_sync(struct track_info *user_info, size_t offset)
997 id3_mutex_lock();
999 struct mp3entry *id3 = bufgetid3(user_info->id3_hid);
1001 if (offset == (size_t)-1)
1003 struct mp3entry *ply_id3 = id3_get(PLAYING_ID3);
1004 size_t play_offset = ply_id3->offset;
1005 long play_elapsed = ply_id3->elapsed;
1006 id3_write(PLAYING_ID3, id3);
1007 ply_id3->offset = play_offset;
1008 ply_id3->elapsed = play_elapsed;
1009 offset = 0;
1011 else
1013 id3_write(PLAYING_ID3, id3);
1016 if (id3)
1017 id3->offset = offset;
1019 id3_mutex_unlock();
1022 /* Wipe-out track metadata - current is optional */
1023 static void wipe_track_metadata(bool current)
1025 id3_mutex_lock();
1027 if (current)
1028 id3_write(PLAYING_ID3, NULL);
1030 id3_write(NEXTTRACK_ID3, NULL);
1031 id3_write(UNBUFFERED_ID3, NULL);
1033 id3_mutex_unlock();
1036 /* Called when buffering is completed on the last track handle */
1037 static void filling_is_finished(void)
1039 logf("last track finished buffering");
1041 /* There's no more to load or watch for */
1042 buf_set_watermark(0);
1043 filling = STATE_FINISHED;
1046 /* Stop the codec decoding or waiting for its data to be ready - returns
1047 'false' if the codec ended up stopped */
1048 static bool halt_decoding_track(bool stop)
1050 /* If it was waiting for us to clear the buffer to make a rebuffer
1051 happen, it should cease otherwise codec_stop could deadlock waiting
1052 for the codec to go to its main loop - codec's request will now
1053 force-fail */
1054 bool retval = false;
1056 buf_signal_handle(ci.audio_hid, true);
1058 if (stop)
1059 codec_stop();
1060 else
1061 retval = codec_pause();
1063 audio_clear_track_notifications();
1065 /* We now know it's idle and not waiting for buffered data */
1066 buf_signal_handle(ci.audio_hid, false);
1068 codec_skip_pending = false;
1069 codec_seeking = false;
1071 return retval;
1074 /* Clear the PCM on a manual skip */
1075 static void audio_clear_paused_pcm(void)
1077 if (play_status == PLAY_PAUSED && !pcmbuf_is_crossfade_active())
1078 pcmbuf_play_stop();
1081 /* End the ff/rw mode */
1082 static void audio_ff_rewind_end(void)
1084 /* A seamless seek (not calling audio_pre_ff_rewind) skips this
1085 section */
1086 if (ff_rw_mode)
1088 ff_rw_mode = false;
1090 if (codec_seeking)
1092 /* Clear the buffer */
1093 pcmbuf_play_stop();
1096 if (play_status != PLAY_PAUSED)
1098 /* Seeking-while-playing, resume PCM playback */
1099 pcmbuf_pause(false);
1104 /* Complete the codec seek */
1105 static void audio_complete_codec_seek(void)
1107 /* If a seek completed while paused, 'paused' is true.
1108 * If seeking from seek mode, 'ff_rw_mode' is true. */
1109 if (codec_seeking)
1111 audio_ff_rewind_end();
1112 codec_seeking = false; /* set _after_ the call! */
1114 /* else it's waiting and we must repond */
1117 /* Get the current cuesheet pointer */
1118 static inline struct cuesheet * get_current_cuesheet(void)
1120 return audio_scratch_memory->curr_cue;
1123 /* Read the cuesheet from the buffer */
1124 static void buf_read_cuesheet(int handle_id)
1126 struct cuesheet *cue = get_current_cuesheet();
1128 if (!cue || handle_id < 0)
1129 return;
1131 bufread(handle_id, sizeof (struct cuesheet), cue);
1134 /* Backend to peek/current/next track metadata interface functions -
1135 fill in the mp3entry with as much information as we may obtain about
1136 the track at the specified offset from the user current track -
1137 returns false if no information exists with us */
1138 static bool audio_get_track_metadata(int offset, struct mp3entry *id3)
1140 if (play_status == PLAY_STOPPED)
1141 return false;
1143 if (id3->path[0] != '\0')
1144 return true; /* Already filled */
1146 struct track_info *info = track_list_user_current(offset);
1148 if (!info)
1150 struct mp3entry *ub_id3 = id3_get(UNBUFFERED_ID3);
1152 if (offset > 0 && track_list_user_current(offset - 1))
1154 /* Try the unbuffered id3 since we're moving forward */
1155 if (ub_id3->path[0] != '\0')
1157 copy_mp3entry(id3, ub_id3);
1158 return true;
1162 else if (bufreadid3(info->id3_hid, id3))
1164 return true;
1167 /* We didn't find the ID3 metadata, so we fill it with the little info we
1168 have and return that */
1170 char path[MAX_PATH+1];
1171 if (playlist_peek(offset, path, sizeof (path)))
1173 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
1174 /* Try to get it from the database */
1175 if (!tagcache_fill_tags(id3, path))
1176 #endif
1178 /* By now, filename is the only source of info */
1179 fill_metadata_from_path(id3, path);
1182 return true;
1185 wipe_mp3entry(id3);
1187 return false;
1190 /* Get a resume rewind adjusted offset from the ID3 */
1191 unsigned long resume_rewind_adjusted_offset(const struct mp3entry *id3)
1193 unsigned long offset = id3->offset;
1194 size_t resume_rewind = global_settings.resume_rewind *
1195 id3->bitrate * (1000/8);
1197 if (offset < resume_rewind)
1198 offset = 0;
1199 else
1200 offset -= resume_rewind;
1202 return offset;
1205 /* Get the codec into ram and initialize it - keep it if it's ready */
1206 static bool audio_init_codec(struct track_info *track_info,
1207 struct mp3entry *track_id3)
1209 int codt_loaded = get_audio_base_codec_type(codec_loaded());
1210 int hid = ERR_HANDLE_NOT_FOUND;
1212 if (codt_loaded != AFMT_UNKNOWN)
1214 int codt = get_audio_base_codec_type(track_id3->codectype);
1216 if (codt == codt_loaded)
1218 /* Codec is the same base type */
1219 logf("Reusing prev. codec: %d", track_id3->codectype);
1220 #ifdef HAVE_CODEC_BUFFERING
1221 /* Close any buffered codec (we could have skipped directly to a
1222 format transistion that is the same format as the current track
1223 and the buffered one is no longer needed) */
1224 track_info_close_handle(&track_info->codec_hid);
1225 #endif
1226 return true;
1228 else
1230 /* New codec - first make sure the old one's gone */
1231 logf("Removing prev. codec: %d", codt_loaded);
1232 codec_unload();
1236 logf("New codec: %d/%d", track_id3->codectype, codec_loaded());
1238 #ifdef HAVE_CODEC_BUFFERING
1239 /* Codec thread will close the handle even if it fails and will load from
1240 storage if hid is not valid or the buffer load fails */
1241 hid = track_info->codec_hid;
1242 track_info->codec_hid = ERR_HANDLE_NOT_FOUND;
1243 #endif
1245 return codec_load(hid, track_id3->codectype);
1246 (void)track_info; /* When codec buffering isn't supported */
1249 /* Start the codec for the current track scheduled to be decoded */
1250 static bool audio_start_codec(bool auto_skip)
1252 struct track_info *info = track_list_current(0);
1253 struct mp3entry *cur_id3 = valid_mp3entry(bufgetid3(info->id3_hid));
1255 if (!cur_id3)
1256 return false;
1258 buf_pin_handle(info->id3_hid, true);
1260 if (!audio_init_codec(info, cur_id3))
1262 buf_pin_handle(info->id3_hid, false);
1263 return false;
1266 #ifdef HAVE_TAGCACHE
1267 bool autoresume_enable = global_settings.autoresume_enable;
1269 if (autoresume_enable && !cur_id3->offset)
1271 /* Resume all manually selected tracks */
1272 bool resume = !auto_skip;
1274 /* Send the "buffer" event to obtain the resume position for the codec */
1275 send_event(PLAYBACK_EVENT_TRACK_BUFFER, cur_id3);
1277 if (!resume)
1279 /* Automatic skip - do further tests to see if we should just
1280 ignore any autoresume position */
1281 int autoresume_automatic = global_settings.autoresume_automatic;
1283 switch (autoresume_automatic)
1285 case AUTORESUME_NEXTTRACK_ALWAYS:
1286 /* Just resume unconditionally */
1287 resume = true;
1288 break;
1289 case AUTORESUME_NEXTTRACK_NEVER:
1290 /* Force-rewind it */
1291 break;
1292 default:
1293 /* Not "never resume" - pass resume filter? */
1294 resume = autoresumable(cur_id3);
1298 if (!resume)
1299 cur_id3->offset = 0;
1301 logf("%s: Set offset for %s to %lX\n", __func__,
1302 cur_id3->title, cur_id3->offset);
1304 #endif /* HAVE_TAGCACHE */
1306 /* Rewind the required amount - if an autoresume was done, this also rewinds
1307 that by the setting's amount
1309 It would be best to have bookkeeping about whether or not the track
1310 sounded or not since skipping to it or else skipping to it while paused
1311 and back again will cause accumulation of silent rewinds - that's not
1312 our job to track directly nor could it be in any reasonable way
1314 cur_id3->offset = resume_rewind_adjusted_offset(cur_id3);
1316 /* Update the codec API with the metadata and track info */
1317 id3_write(CODEC_ID3, cur_id3);
1319 ci.audio_hid = info->audio_hid;
1320 ci.filesize = info->filesize;
1321 buf_set_base_handle(info->audio_hid);
1323 /* All required data is now available for the codec */
1324 codec_go();
1326 #ifdef HAVE_TAGCACHE
1327 if (!autoresume_enable || cur_id3->offset)
1328 #endif
1330 /* Send the "buffer" event now */
1331 send_event(PLAYBACK_EVENT_TRACK_BUFFER, cur_id3);
1334 buf_pin_handle(info->id3_hid, false);
1335 return true;
1337 (void)auto_skip; /* ifndef HAVE_TAGCACHE */
1341 /** --- Audio thread --- **/
1343 /* Load and parse a cuesheet for the file - returns false if the buffer
1344 is full */
1345 static bool audio_load_cuesheet(struct track_info *info,
1346 struct mp3entry *track_id3)
1348 struct cuesheet *cue = get_current_cuesheet();
1349 track_id3->cuesheet = NULL;
1351 if (cue && info->cuesheet_hid == ERR_HANDLE_NOT_FOUND)
1353 /* If error other than a full buffer, then mark it "unsupported" to
1354 avoid reloading attempt */
1355 int hid = ERR_UNSUPPORTED_TYPE;
1356 char cuepath[MAX_PATH];
1358 #ifdef HAVE_IO_PRIORITY
1359 buf_back_off_storage(true);
1360 #endif
1361 if (look_for_cuesheet_file(track_id3->path, cuepath))
1363 hid = bufalloc(NULL, sizeof (struct cuesheet), TYPE_CUESHEET);
1365 if (hid >= 0)
1367 void *cuesheet = NULL;
1368 bufgetdata(hid, sizeof (struct cuesheet), &cuesheet);
1370 if (parse_cuesheet(cuepath, (struct cuesheet *)cuesheet))
1372 /* Indicate cuesheet is present (while track remains
1373 buffered) */
1374 track_id3->cuesheet = cue;
1376 else
1378 bufclose(hid);
1379 hid = ERR_UNSUPPORTED_TYPE;
1384 #ifdef HAVE_IO_PRIORITY
1385 buf_back_off_storage(false);
1386 #endif
1387 if (hid == ERR_BUFFER_FULL)
1389 logf("buffer is full for now (%s)", __func__);
1390 return false;
1392 else
1394 if (hid < 0)
1395 logf("Cuesheet loading failed");
1397 info->cuesheet_hid = hid;
1401 return true;
1404 #ifdef HAVE_ALBUMART
1405 /* Load any album art for the file - returns false if the buffer is full */
1406 static bool audio_load_albumart(struct track_info *info,
1407 struct mp3entry *track_id3)
1409 int i;
1410 FOREACH_ALBUMART(i)
1412 struct bufopen_bitmap_data user_data;
1413 int *aa_hid = &info->aa_hid[i];
1414 int hid = ERR_UNSUPPORTED_TYPE;
1416 /* albumart_slots may change during a yield of bufopen,
1417 * but that's no problem */
1418 if (*aa_hid >= 0 || *aa_hid == ERR_UNSUPPORTED_TYPE ||
1419 !albumart_slots[i].used)
1420 continue;
1422 memset(&user_data, 0, sizeof(user_data));
1423 user_data.dim = &albumart_slots[i].dim;
1425 #ifdef HAVE_IO_PRIORITY
1426 buf_back_off_storage(true);
1427 #endif
1429 /* We can only decode jpeg for embedded AA */
1430 if (track_id3->embed_albumart && track_id3->albumart.type == AA_TYPE_JPG)
1432 user_data.embedded_albumart = &track_id3->albumart;
1433 hid = bufopen(track_id3->path, 0, TYPE_BITMAP, &user_data);
1436 if (hid < 0 && hid != ERR_BUFFER_FULL)
1438 /* No embedded AA or it couldn't be loaded - try other sources */
1439 char path[MAX_PATH];
1441 if (find_albumart(track_id3, path, sizeof(path),
1442 &albumart_slots[i].dim))
1444 user_data.embedded_albumart = NULL;
1445 hid = bufopen(path, 0, TYPE_BITMAP, &user_data);
1449 #ifdef HAVE_IO_PRIORITY
1450 buf_back_off_storage(false);
1451 #endif
1452 if (hid == ERR_BUFFER_FULL)
1454 logf("buffer is full for now (%s)", __func__);
1455 return false;
1457 else
1459 /* If error other than a full buffer, then mark it "unsupported"
1460 to avoid reloading attempt */
1461 if (hid < 0)
1463 logf("Album art loading failed");
1464 hid = ERR_UNSUPPORTED_TYPE;
1467 *aa_hid = hid;
1471 return true;
1473 #endif /* HAVE_ALBUMART */
1475 #ifdef HAVE_CODEC_BUFFERING
1476 /* Load a codec for the file onto the buffer - assumes we're working from the
1477 currently loading track - not called for the current track */
1478 static bool audio_buffer_codec(struct track_info *track_info,
1479 struct mp3entry *track_id3)
1481 /* This will not be the current track -> it cannot be the first and the
1482 current track cannot be ahead of buffering -> there is a previous
1483 track entry which is either current or ahead of the current */
1484 struct track_info *prev_info = track_list_last(-1);
1485 struct mp3entry *prev_id3 = bufgetid3(prev_info->id3_hid);
1487 /* If the previous codec is the same as this one, there is no need to
1488 put another copy of it on the file buffer (in other words, only
1489 buffer codecs at format transitions) */
1490 if (prev_id3)
1492 int codt = get_audio_base_codec_type(track_id3->codectype);
1493 int prev_codt = get_audio_base_codec_type(prev_id3->codectype);
1495 if (codt == prev_codt)
1497 logf("Reusing prev. codec: %d", prev_id3->codectype);
1498 return true;
1501 /* else just load it (harmless) */
1503 /* Load the codec onto the buffer if possible */
1504 const char *codec_fn = get_codec_filename(track_id3->codectype);
1505 if (!codec_fn)
1506 return false;
1508 char codec_path[MAX_PATH+1]; /* Full path to codec */
1509 codec_get_full_path(codec_path, codec_fn);
1511 track_info->codec_hid = bufopen(codec_path, 0, TYPE_CODEC, NULL);
1513 if (track_info->codec_hid >= 0)
1515 logf("Buffered codec: %d", afmt);
1516 return true;
1519 return false;
1521 #endif /* HAVE_CODEC_BUFFERING */
1523 /* Load metadata for the next track (with bufopen). The rest of the track
1524 loading will be handled by audio_finish_load_track once the metadata has
1525 been actually loaded by the buffering thread.
1527 Each track is arranged in the buffer as follows:
1528 <id3|[cuesheet|][album art|][codec|]audio>
1530 The next will not be loaded until the previous succeeds if the buffer was
1531 full at the time. To put any metadata after audio would make those handles
1532 unmovable.
1534 static int audio_load_track(void)
1536 if (in_progress_id3_hid >= 0)
1538 /* There must be an info pointer if the in-progress id3 is even there */
1539 struct track_info *info = track_list_last(0);
1541 if (info->id3_hid == in_progress_id3_hid)
1543 if (filling == STATE_FILLING)
1545 /* Haven't finished the metadata but the notification is
1546 anticipated to come soon */
1547 logf("%s(): in progress ok: %d". __func__, info->id3_hid);
1548 return LOAD_TRACK_OK;
1550 else if (filling == STATE_FULL)
1552 /* Buffer was full trying to complete the load after the
1553 metadata finished, so attempt to continue - older handles
1554 should have been cleared already */
1555 logf("%s(): finishing load: %d". __func__, info->id3_hid);
1556 filling = STATE_FILLING;
1557 buffer_event_finished_callback(&info->id3_hid);
1558 return LOAD_TRACK_OK;
1562 /* Some old, stray buffering message */
1563 logf("%s(): already in progress: %d". __func__, info->id3_hid);
1564 return LOAD_TRACK_ERR_BUSY;
1567 filling = STATE_FILLING;
1569 struct track_info *info = track_list_alloc_track();
1570 if (info == NULL)
1572 /* List is full so stop buffering tracks - however, attempt to obtain
1573 metadata as the unbuffered id3 */
1574 logf("No free tracks");
1575 filling = STATE_FULL;
1578 playlist_peek_offset++;
1580 logf("Buffering track: s%u/c%u/e%u/p%d",
1581 track_list.start, track_list.current, track_list.end,
1582 playlist_peek_offset);
1584 /* Get track name from current playlist read position */
1585 int fd = -1;
1586 char name_buf[MAX_PATH + 1];
1587 const char *trackname;
1589 while (1)
1592 trackname = playlist_peek(playlist_peek_offset, name_buf,
1593 sizeof (name_buf));
1595 if (!trackname)
1596 break;
1598 /* Test for broken playlists by probing for the files */
1599 fd = open(trackname, O_RDONLY);
1600 if (fd >= 0)
1601 break;
1603 logf("Open failed");
1604 /* Skip invalid entry from playlist */
1605 playlist_skip_entry(NULL, playlist_peek_offset);
1607 /* Sync the playlist if it isn't finished */
1608 if (playlist_peek(playlist_peek_offset, NULL, 0))
1609 playlist_next(0);
1612 if (!trackname)
1614 /* No track - exhausted the playlist entries */
1615 logf("End-of-playlist");
1616 id3_write_locked(UNBUFFERED_ID3, NULL);
1618 if (filling != STATE_FULL)
1619 track_list_unalloc_track(); /* Free this entry */
1621 playlist_peek_offset--; /* Maintain at last index */
1623 /* We can end up here after the real last track signals its completion
1624 and miss the transition to STATE_FINISHED esp. if dropping the last
1625 songs of a playlist late in their load (2nd stage) */
1626 info = track_list_last(0);
1628 if (info && buf_handle_remaining(info->audio_hid) == 0)
1629 filling_is_finished();
1630 else
1631 filling = STATE_END_OF_PLAYLIST;
1633 return LOAD_TRACK_ERR_NO_MORE;
1636 /* Successfully opened the file - get track metadata */
1637 if (filling == STATE_FULL ||
1638 (info->id3_hid = bufopen(trackname, 0, TYPE_ID3, NULL)) < 0)
1640 /* Buffer or track list is full */
1641 struct mp3entry *ub_id3;
1643 playlist_peek_offset--;
1645 /* Load the metadata for the first unbuffered track */
1646 ub_id3 = id3_get(UNBUFFERED_ID3);
1647 id3_mutex_lock();
1648 get_metadata(ub_id3, fd, trackname);
1649 id3_mutex_unlock();
1651 if (filling != STATE_FULL)
1653 track_list_unalloc_track();
1654 filling = STATE_FULL;
1657 logf("%s: buffer is full for now (%u tracks)", __func__,
1658 track_list_count());
1660 else
1662 /* Successful load initiation */
1663 info->filesize = filesize(fd);
1664 in_progress_id3_hid = info->id3_hid; /* Remember what's in-progress */
1667 close(fd);
1668 return LOAD_TRACK_OK;
1671 /* Second part of the track loading: We now have the metadata available, so we
1672 can load the codec, the album art and finally the audio data.
1673 This is called on the audio thread after the buffering thread calls the
1674 buffering_handle_finished_callback callback. */
1675 static int audio_finish_load_track(struct track_info *info)
1677 int trackstat = LOAD_TRACK_OK;
1679 if (info->id3_hid != in_progress_id3_hid)
1681 /* We must not be here if not! */
1682 logf("%s: wrong track %d/%d", __func__, info->id3_hid,
1683 in_progress_id3_hid);
1684 return LOAD_TRACK_ERR_BUSY;
1687 /* The current track for decoding (there is always one if the list is
1688 populated) */
1689 struct track_info *cur_info = track_list_current(0);
1690 struct mp3entry *track_id3 = valid_mp3entry(bufgetid3(info->id3_hid));
1692 if (!track_id3)
1694 /* This is an error condition. Track cannot be played without valid
1695 metadata; skip the track. */
1696 logf("No metadata for: %s", track_id3->path);
1697 trackstat = LOAD_TRACK_ERR_FINISH_FAILED;
1698 goto audio_finish_load_track_exit;
1701 /* Try to load a cuesheet for the track */
1702 if (!audio_load_cuesheet(info, track_id3))
1704 /* No space for cuesheet on buffer, not an error */
1705 filling = STATE_FULL;
1706 goto audio_finish_load_track_exit;
1709 #ifdef HAVE_ALBUMART
1710 /* Try to load album art for the track */
1711 if (!audio_load_albumart(info, track_id3))
1713 /* No space for album art on buffer, not an error */
1714 filling = STATE_FULL;
1715 goto audio_finish_load_track_exit;
1717 #endif
1719 /* All handles available to external routines are ready - audio and codec
1720 information is private */
1722 if (info == track_list_user_current(0))
1724 /* Send only when the track handles could not all be opened ahead of
1725 time for the user's current track - otherwise everything is ready
1726 by the time PLAYBACK_EVENT_TRACK_CHANGE is sent */
1727 send_event(PLAYBACK_EVENT_CUR_TRACK_READY, id3_get(PLAYING_ID3));
1730 #ifdef HAVE_CODEC_BUFFERING
1731 /* Try to buffer a codec for the track */
1732 if (info != cur_info && !audio_buffer_codec(info, track_id3))
1734 if (info->codec_hid == ERR_BUFFER_FULL)
1736 /* No space for codec on buffer, not an error */
1737 filling = STATE_FULL;
1738 logf("buffer is full for now (%s)", __func__);
1740 else
1742 /* This is an error condition, either no codec was found, or
1743 reading the codec file failed part way through, either way,
1744 skip the track */
1745 logf("No codec for: %s", track_id3->path);
1746 trackstat = LOAD_TRACK_ERR_FINISH_FAILED;
1749 goto audio_finish_load_track_exit;
1751 #endif /* HAVE_CODEC_BUFFERING */
1753 /** Finally, load the audio **/
1754 size_t file_offset = 0;
1755 track_id3->elapsed = 0;
1757 if (track_id3->offset >= info->filesize)
1758 track_id3->offset = 0;
1760 logf("%s: set offset for %s to %lu\n", __func__,
1761 id3->title, (unsigned long)offset);
1763 /* Adjust for resume rewind so we know what to buffer - starting the codec
1764 calls it again, so we don't save it (and they shouldn't accumulate) */
1765 size_t offset = resume_rewind_adjusted_offset(track_id3);
1767 enum data_type audiotype = get_audio_base_data_type(track_id3->codectype);
1769 if (audiotype == TYPE_ATOMIC_AUDIO)
1770 logf("Loading atomic %d", track_id3->codectype);
1772 if (format_buffers_with_offset(track_id3->codectype))
1774 /* This format can begin buffering from any point */
1775 file_offset = offset;
1778 logf("load track: %s", track_id3->path);
1780 if (file_offset > AUDIO_REBUFFER_GUESS_SIZE)
1782 /* We can buffer later in the file, adjust the hunt-and-peck margin */
1783 file_offset -= AUDIO_REBUFFER_GUESS_SIZE;
1785 else
1787 /* No offset given or it is very minimal - begin at the first frame
1788 according to the metadata */
1789 file_offset = track_id3->first_frame_offset;
1792 int hid = bufopen(track_id3->path, file_offset, audiotype, NULL);
1794 if (hid >= 0)
1796 info->audio_hid = hid;
1798 if (info == cur_info)
1800 /* This is the current track to decode - should be started now */
1801 trackstat = LOAD_TRACK_READY;
1804 else
1806 /* Buffer could be full but not properly so if this is the only
1807 track! */
1808 if (hid == ERR_BUFFER_FULL && audio_track_count() > 1)
1810 filling = STATE_FULL;
1811 logf("Buffer is full for now (%s)", __func__);
1813 else
1815 /* Nothing to play if no audio handle - skip this */
1816 logf("Could not add audio data handle");
1817 trackstat = LOAD_TRACK_ERR_FINISH_FAILED;
1821 audio_finish_load_track_exit:
1822 if (trackstat < LOAD_TRACK_OK)
1824 playlist_skip_entry(NULL, playlist_peek_offset);
1825 track_info_close(info);
1826 track_list_unalloc_track();
1828 if (playlist_peek(playlist_peek_offset, NULL, 0))
1829 playlist_next(0);
1831 playlist_peek_offset--;
1834 if (filling != STATE_FULL)
1836 /* Load next track - error or not */
1837 in_progress_id3_hid = ERR_HANDLE_NOT_FOUND;
1838 LOGFQUEUE("audio > audio Q_AUDIO_FILL_BUFFER");
1839 audio_queue_post(Q_AUDIO_FILL_BUFFER, 0);
1841 else
1843 /* Full */
1844 trackstat = LOAD_TRACK_ERR_FINISH_FULL;
1847 return trackstat;
1850 /* Start a new track load */
1851 static int audio_fill_file_buffer(void)
1853 if (play_status == PLAY_STOPPED)
1854 return LOAD_TRACK_ERR_FAILED;
1856 trigger_cpu_boost();
1858 /* Must reset the buffer before use if trashed or voice only - voice
1859 file size shouldn't have changed so we can go straight from
1860 AUDIOBUF_STATE_VOICED_ONLY to AUDIOBUF_STATE_INITIALIZED */
1861 if (buffer_state != AUDIOBUF_STATE_INITIALIZED)
1862 audio_reset_buffer();
1864 logf("Starting buffer fill");
1866 int trackstat = audio_load_track();
1868 if (trackstat >= LOAD_TRACK_OK)
1870 if (track_list_current(0) == track_list_user_current(0))
1871 playlist_next(0);
1873 if (filling == STATE_FULL && !track_list_user_current(1))
1875 /* There are no user tracks on the buffer after this therefore
1876 this is the next track */
1877 audio_update_and_announce_next_track(id3_get(UNBUFFERED_ID3));
1881 return trackstat;
1884 /* Discard unwanted tracks and start refill from after the specified playlist
1885 offset */
1886 static int audio_reset_and_rebuffer(
1887 enum track_clear_action action, int peek_offset)
1889 logf("Forcing rebuffer: 0x%X, %d", flags, peek_offset);
1891 id3_write_locked(UNBUFFERED_ID3, NULL);
1893 /* Remove unwanted tracks - caller must have ensured codec isn't using
1894 any */
1895 track_list_clear(action);
1897 /* Refill at specified position (-1 starts at index offset 0) */
1898 playlist_peek_offset = peek_offset;
1900 /* Fill the buffer */
1901 return audio_fill_file_buffer();
1904 /* Handle buffering events
1905 (Q_AUDIO_BUFFERING) */
1906 static void audio_on_buffering(int event)
1908 enum track_clear_action action;
1909 int peek_offset;
1911 if (track_list_empty())
1912 return;
1914 switch (event)
1916 case BUFFER_EVENT_BUFFER_LOW:
1917 if (filling != STATE_FULL && filling != STATE_END_OF_PLAYLIST)
1918 return; /* Should be nothing left to fill */
1920 /* Clear old tracks and continue buffering where it left off */
1921 action = TRACK_LIST_KEEP_NEW;
1922 peek_offset = playlist_peek_offset;
1923 break;
1925 case BUFFER_EVENT_REBUFFER:
1926 /* Remove all but the currently decoding track and redo buffering
1927 after that */
1928 action = TRACK_LIST_KEEP_CURRENT;
1929 peek_offset = (skip_pending == TRACK_SKIP_AUTO) ? 1 : 0;
1930 break;
1932 default:
1933 return;
1936 switch (skip_pending)
1938 case TRACK_SKIP_NONE:
1939 case TRACK_SKIP_AUTO:
1940 case TRACK_SKIP_AUTO_NEW_PLAYLIST:
1941 audio_reset_and_rebuffer(action, peek_offset);
1942 break;
1944 case TRACK_SKIP_AUTO_END_PLAYLIST:
1945 /* Already finished */
1946 break;
1948 default:
1949 /* Invalid */
1950 logf("Buffering call, inv. state: %d", (int)skip_pending);
1954 /* Handle starting the next track load
1955 (Q_AUDIO_FILL_BUFFER) */
1956 static void audio_on_fill_buffer(void)
1958 audio_handle_track_load_status(audio_fill_file_buffer());
1961 /* Handle posted load track finish event
1962 (Q_AUDIO_FINISH_LOAD_TRACK) */
1963 static void audio_on_finish_load_track(int id3_hid)
1965 struct track_info *info = track_list_last(0);
1967 if (!info || !buf_is_handle(id3_hid))
1968 return;
1970 if (info == track_list_user_current(1))
1972 /* Just loaded the metadata right after the current position */
1973 audio_update_and_announce_next_track(bufgetid3(info->id3_hid));
1976 if (audio_finish_load_track(info) != LOAD_TRACK_READY)
1977 return; /* Not current track */
1979 bool is_user_current = info == track_list_user_current(0);
1981 if (is_user_current)
1983 /* Copy cuesheet */
1984 buf_read_cuesheet(info->cuesheet_hid);
1987 if (audio_start_codec(automatic_skip))
1989 if (is_user_current)
1991 /* Be sure all tagtree info is synchronized; it will be needed for the
1992 track finish event - the sync will happen when finalizing a track
1993 change otherwise */
1994 bool was_valid = valid_mp3entry(id3_get(PLAYING_ID3));
1996 playing_id3_sync(info, -1);
1998 if (!was_valid)
2000 /* Playing id3 hadn't been updated yet because no valid track
2001 was yet available - treat like the first track */
2002 audio_playlist_track_change();
2006 else
2008 audio_handle_track_load_status(LOAD_TRACK_ERR_START_CODEC);
2012 /* Called when handles other than metadata handles have finished buffering
2013 (Q_AUDIO_HANDLE_FINISHED) */
2014 static void audio_on_handle_finished(int hid)
2016 /* Right now, only audio handles should end up calling this */
2017 if (filling == STATE_END_OF_PLAYLIST)
2019 struct track_info *info = track_list_last(0);
2021 /* Really we don't know which order the handles will actually complete
2022 to zero bytes remaining since another thread is doing it - be sure
2023 it's the right one */
2024 if (info && info->audio_hid == hid)
2026 /* This was the last track in the playlist and we now have all the
2027 data we need */
2028 filling_is_finished();
2033 /* Called to make an outstanding track skip the current track and to send the
2034 transition events */
2035 static void audio_finalise_track_change(bool delayed)
2037 switch (skip_pending)
2039 case TRACK_SKIP_NONE: /* Manual skip */
2040 break;
2042 case TRACK_SKIP_AUTO:
2043 case TRACK_SKIP_AUTO_NEW_PLAYLIST:
2045 int playlist_delta = skip_pending == TRACK_SKIP_AUTO ? 1 : 0;
2046 audio_playlist_track_finish();
2048 if (!playlist_peek(playlist_delta, NULL, 0))
2050 /* Track ended up rejected - push things ahead like the codec blew
2051 it (because it was never started and now we're here where it
2052 should have been decoding the next track by now) - next, a
2053 directory change or end of playback will most likely happen */
2054 skip_pending = TRACK_SKIP_NONE;
2055 audio_handle_track_load_status(LOAD_TRACK_ERR_START_CODEC);
2056 return;
2059 if (!playlist_delta)
2060 break;
2062 playlist_peek_offset -= playlist_delta;
2063 if (playlist_next(playlist_delta) >= 0)
2064 break;
2065 /* What!? Disappear? Hopeless bleak despair */
2067 /* Fallthrough */
2068 case TRACK_SKIP_AUTO_END_PLAYLIST:
2069 default: /* Invalid */
2070 filling = STATE_ENDED;
2071 audio_stop_playback();
2072 return;
2075 struct track_info *info = track_list_current(0);
2076 struct mp3entry *track_id3 = NULL;
2078 id3_mutex_lock();
2080 /* Update the current cuesheet if any and enabled */
2081 if (info)
2083 buf_read_cuesheet(info->cuesheet_hid);
2084 track_id3 = bufgetid3(info->id3_hid);
2087 id3_write(PLAYING_ID3, track_id3);
2089 if (delayed)
2091 /* Delayed skip where codec is ahead of user's current track */
2092 struct mp3entry *ci_id3 = id3_get(CODEC_ID3);
2093 struct mp3entry *ply_id3 = id3_get(PLAYING_ID3);
2094 ply_id3->elapsed = ci_id3->elapsed;
2095 ply_id3->offset = ci_id3->offset;
2098 /* The skip is technically over */
2099 skip_pending = TRACK_SKIP_NONE;
2101 /* Sync the next track information */
2102 info = track_list_current(1);
2104 id3_write(NEXTTRACK_ID3, info ? bufgetid3(info->id3_hid) :
2105 id3_get(UNBUFFERED_ID3));
2107 id3_mutex_unlock();
2109 audio_playlist_track_change();
2112 /* Actually begin a transition and take care of the codec change - may complete
2113 it now or ask pcmbuf for notification depending on the type and what pcmbuf
2114 has to say */
2115 static void audio_begin_track_change(bool auto_skip, int trackstat)
2117 /* Even if the new track is bad, the old track must be finished off */
2118 bool finalised = pcmbuf_start_track_change(auto_skip);
2120 if (finalised)
2122 /* pcmbuf says that the transition happens now - complete it */
2123 audio_finalise_track_change(false);
2125 if (play_status == PLAY_STOPPED)
2126 return; /* Stopped us */
2129 if (!auto_skip)
2130 audio_clear_paused_pcm();
2132 if (trackstat >= LOAD_TRACK_OK)
2134 struct track_info *info = track_list_current(0);
2136 if (info->audio_hid < 0)
2137 return;
2139 /* Everything needed for the codec is ready - start it */
2140 if (audio_start_codec(auto_skip))
2142 if (finalised)
2143 playing_id3_sync(info, -1);
2144 return;
2147 trackstat = LOAD_TRACK_ERR_START_CODEC;
2150 audio_handle_track_load_status(trackstat);
2153 /* Transition to end-of-playlist state and begin wait for PCM to finish */
2154 static void audio_monitor_end_of_playlist(void)
2156 skip_pending = TRACK_SKIP_AUTO_END_PLAYLIST;
2157 filling = STATE_ENDING;
2158 pcmbuf_monitor_track_change(true);
2161 /* Codec has completed decoding the track
2162 (usually Q_AUDIO_CODEC_COMPLETE) */
2163 static void audio_on_codec_complete(int status)
2165 logf("%s(%d)", __func__, status);
2167 if (play_status == PLAY_STOPPED)
2168 return;
2170 /* If it didn't notify us first, don't expect "seek complete" message
2171 since the codec can't post it now - do things like it would have
2172 done */
2173 audio_complete_codec_seek();
2175 if (play_status == PLAY_PAUSED || skip_pending != TRACK_SKIP_NONE)
2177 /* Old-hay on the ip-skay - codec has completed decoding
2179 Paused: We're not sounding it, so just remember that it happened
2180 and the resume will begin the transition
2182 Skipping: There was already a skip in progress, remember it and
2183 allow no further progress until the PCM from the previous
2184 song has finished
2186 codec_skip_pending = true;
2187 codec_skip_status = status;
2188 return;
2191 codec_skip_pending = false;
2193 #ifdef AB_REPEAT_ENABLE
2194 if (status >= 0)
2196 /* Normal automatic skip */
2197 ab_end_of_track_report();
2199 #endif
2201 int trackstat = LOAD_TRACK_OK;
2203 automatic_skip = true;
2204 skip_pending = TRACK_SKIP_AUTO;
2206 /* Does this track have an entry allocated? */
2207 struct track_info *info = track_list_advance_current(1);
2209 if (!info || info->audio_hid < 0)
2211 bool end_of_playlist = false;
2213 if (info)
2215 /* Track load is not complete - it might have stopped on a
2216 full buffer without reaching the audio handle or we just
2217 arrived at it early
2219 If this type is atomic and we couldn't get the audio,
2220 perhaps it would need to wrap to make the allocation and
2221 handles are in the way - to maximize the liklihood it can
2222 be allocated, clear all handles to reset the buffer and
2223 its indexes to 0 - for packet audio, this should not be an
2224 issue and a pointless full reload of all the track's
2225 metadata may be avoided */
2227 struct mp3entry *track_id3 = bufgetid3(info->id3_hid);
2229 if (track_id3 &&
2230 get_audio_base_data_type(track_id3->codectype)
2231 == TYPE_PACKET_AUDIO)
2233 /* Continue filling after this track */
2234 audio_reset_and_rebuffer(TRACK_LIST_KEEP_CURRENT, 1);
2235 audio_begin_track_change(true, trackstat);
2236 return;
2238 /* else rebuffer at this track; status applies to the track we
2239 want */
2241 else if (!playlist_peek(1, NULL, 0))
2243 /* Play sequence is complete - directory change or other playlist
2244 resequencing - the playlist must now be advanced in order to
2245 continue since a peek ahead to the next track is not possible */
2246 skip_pending = TRACK_SKIP_AUTO_NEW_PLAYLIST;
2247 end_of_playlist = playlist_next(1) < 0;
2250 if (!end_of_playlist)
2252 trackstat = audio_reset_and_rebuffer(TRACK_LIST_CLEAR_ALL,
2253 skip_pending == TRACK_SKIP_AUTO ? 0 : -1);
2255 if (trackstat == LOAD_TRACK_ERR_NO_MORE)
2257 /* Failed to find anything after all - do playlist switchover
2258 instead */
2259 skip_pending = TRACK_SKIP_AUTO_NEW_PLAYLIST;
2260 end_of_playlist = playlist_next(1) < 0;
2264 if (end_of_playlist)
2266 audio_monitor_end_of_playlist();
2267 return;
2271 audio_begin_track_change(true, trackstat);
2274 /* Called when codec completes seek operation
2275 (usually Q_AUDIO_CODEC_SEEK_COMPLETE) */
2276 static void audio_on_codec_seek_complete(void)
2278 logf("%s()", __func__);
2279 audio_complete_codec_seek();
2280 codec_go();
2283 /* Called when PCM track change has completed
2284 (Q_AUDIO_TRACK_CHANGED) */
2285 static void audio_on_track_changed(void)
2287 /* Finish whatever is pending so that the WPS is in sync */
2288 audio_finalise_track_change(true);
2290 if (codec_skip_pending)
2292 /* Codec got ahead completing a short track - complete the
2293 codec's skip and begin the next */
2294 codec_skip_pending = false;
2295 audio_on_codec_complete(codec_skip_status);
2299 /* Begin playback from an idle state, transition to a new playlist or
2300 invalidate the buffer and resume (if playing).
2301 (usually Q_AUDIO_PLAY, Q_AUDIO_REMAKE_AUDIO_BUFFER) */
2302 static void audio_start_playback(size_t offset, unsigned int flags)
2304 enum play_status old_status = play_status;
2306 if (flags & AUDIO_START_NEWBUF)
2308 /* Mark the buffer dirty - if not playing, it will be reset next
2309 time */
2310 if (buffer_state == AUDIOBUF_STATE_INITIALIZED)
2311 buffer_state = AUDIOBUF_STATE_VOICED_ONLY;
2314 if (old_status != PLAY_STOPPED)
2316 logf("%s(%lu): skipping", __func__, (unsigned long)offset);
2318 halt_decoding_track(true);
2320 automatic_skip = false;
2321 ff_rw_mode = false;
2323 if (flags & AUDIO_START_RESTART)
2325 /* Clear out some stuff to resume the current track where it
2326 left off */
2327 pcmbuf_play_stop();
2328 offset = id3_get(PLAYING_ID3)->offset;
2329 track_list_clear(TRACK_LIST_CLEAR_ALL);
2331 else
2333 /* This is more-or-less treated as manual track transition */
2334 /* Save resume information for current track */
2335 audio_playlist_track_finish();
2336 track_list_clear(TRACK_LIST_CLEAR_ALL);
2338 /* Indicate manual track change */
2339 pcmbuf_start_track_change(false);
2340 audio_clear_paused_pcm();
2341 wipe_track_metadata(true);
2344 /* Set after track finish event in case skip was in progress */
2345 skip_pending = TRACK_SKIP_NONE;
2347 else
2349 if (flags & AUDIO_START_RESTART)
2350 return; /* Must already be playing */
2352 /* Cold playback start from a stopped state */
2353 logf("%s(%lu): starting", __func__, offset);
2355 /* Set audio parameters */
2356 #if INPUT_SRC_CAPS != 0
2357 audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
2358 audio_set_output_source(AUDIO_SRC_PLAYBACK);
2359 #endif
2360 #ifndef PLATFORM_HAS_VOLUME_CHANGE
2361 sound_set_volume(global_settings.volume);
2362 #endif
2363 /* Update our state */
2364 play_status = PLAY_PLAYING;
2367 /* Start fill from beginning of playlist */
2368 playlist_peek_offset = -1;
2369 buf_set_base_handle(-1);
2371 /* Officially playing */
2372 queue_reply(&audio_queue, 1);
2374 /* Add these now - finish event for the first id3 will most likely be sent
2375 immediately */
2376 add_event(BUFFER_EVENT_REBUFFER, false, buffer_event_rebuffer_callback);
2377 add_event(BUFFER_EVENT_FINISHED, false, buffer_event_finished_callback);
2379 if (old_status == PLAY_STOPPED)
2381 /* Send coldstart event */
2382 send_event(PLAYBACK_EVENT_START_PLAYBACK, NULL);
2385 /* Fill the buffer */
2386 int trackstat = audio_fill_file_buffer();
2388 if (trackstat >= LOAD_TRACK_OK)
2390 /* This is the currently playing track - get metadata, stat */
2391 playing_id3_sync(track_list_current(0), offset);
2393 if (valid_mp3entry(id3_get(PLAYING_ID3)))
2395 /* Only if actually changing tracks... */
2396 if (!(flags & AUDIO_START_RESTART))
2397 audio_playlist_track_change();
2400 else
2402 /* Found nothing playable */
2403 audio_handle_track_load_status(trackstat);
2407 /* Stop playback and enter an idle state
2408 (usually Q_AUDIO_STOP) */
2409 static void audio_stop_playback(void)
2411 logf("%s()", __func__);
2413 if (play_status == PLAY_STOPPED)
2414 return;
2416 /* Stop the codec and unload it */
2417 halt_decoding_track(true);
2418 pcmbuf_play_stop();
2419 codec_unload();
2421 /* Save resume information - "filling" might have been set to
2422 "STATE_ENDED" by caller in order to facilitate end of playlist */
2423 audio_playlist_track_finish();
2425 skip_pending = TRACK_SKIP_NONE;
2426 automatic_skip = false;
2428 /* Close all tracks and mark them NULL */
2429 remove_event(BUFFER_EVENT_REBUFFER, buffer_event_rebuffer_callback);
2430 remove_event(BUFFER_EVENT_FINISHED, buffer_event_finished_callback);
2431 remove_event(BUFFER_EVENT_BUFFER_LOW, buffer_event_buffer_low_callback);
2433 track_list_clear(TRACK_LIST_CLEAR_ALL);
2435 /* Update our state */
2436 ff_rw_mode = false;
2437 play_status = PLAY_STOPPED;
2439 wipe_track_metadata(true);
2441 /* Go idle */
2442 filling = STATE_IDLE;
2443 cancel_cpu_boost();
2446 /* Pause the playback of the current track
2447 (Q_AUDIO_PAUSE) */
2448 static void audio_on_pause(bool pause)
2450 logf("%s(%s)", __func__, pause ? "true" : "false");
2452 if (play_status == PLAY_STOPPED || pause == (play_status == PLAY_PAUSED))
2453 return;
2455 if (!ff_rw_mode)
2457 /* Not in ff/rw mode - may set the state (otherwise this could make
2458 old data play because seek hasn't completed and cleared it) */
2459 pcmbuf_pause(pause);
2462 play_status = pause ? PLAY_PAUSED : PLAY_PLAYING;
2464 if (!pause && codec_skip_pending)
2466 /* Actually do the skip that is due - resets the status flag */
2467 audio_on_codec_complete(codec_skip_status);
2471 /* Skip a certain number of tracks forwards or backwards
2472 (Q_AUDIO_SKIP) */
2473 static void audio_on_skip(void)
2475 id3_mutex_lock();
2477 /* Eat the delta to keep it synced, even if not playing */
2478 int toskip = skip_offset;
2479 skip_offset = 0;
2481 logf("%s(): %d", __func__, toskip);
2483 id3_mutex_unlock();
2485 if (play_status == PLAY_STOPPED)
2486 return;
2488 /* Force codec to abort this track */
2489 halt_decoding_track(true);
2491 /* Kill the ff/rw halt */
2492 ff_rw_mode = false;
2494 /* Manual skip */
2495 automatic_skip = false;
2497 /* If there was an auto skip in progress, there will be residual
2498 advancement of the playlist and/or track list so compensation will be
2499 required in order to end up in the right spot */
2500 int track_list_delta = toskip;
2501 int playlist_delta = toskip;
2503 if (skip_pending != TRACK_SKIP_NONE)
2505 if (skip_pending != TRACK_SKIP_AUTO_END_PLAYLIST)
2506 track_list_delta--;
2508 if (skip_pending == TRACK_SKIP_AUTO_NEW_PLAYLIST)
2509 playlist_delta--;
2512 audio_playlist_track_finish();
2513 skip_pending = TRACK_SKIP_NONE;
2515 /* Update the playlist current track now */
2516 while (playlist_next(playlist_delta) < 0)
2518 /* Manual skip out of range (because the playlist wasn't updated
2519 yet by us and so the check in audio_skip returned 'ok') - bring
2520 back into range */
2521 int d = toskip < 0 ? 1 : -1;
2523 while (!playlist_check(playlist_delta))
2525 if (playlist_delta == d)
2527 /* Had to move the opposite direction to correct, which is
2528 wrong - this is the end */
2529 filling = STATE_ENDED;
2530 audio_stop_playback();
2531 return;
2534 playlist_delta += d;
2535 track_list_delta += d;
2539 /* Adjust things by how much the playlist was manually moved */
2540 playlist_peek_offset -= playlist_delta;
2542 struct track_info *info = track_list_advance_current(track_list_delta);
2543 int trackstat = LOAD_TRACK_OK;
2545 if (!info || info->audio_hid < 0)
2547 /* We don't know the next track thus we know we don't have it */
2548 trackstat = audio_reset_and_rebuffer(TRACK_LIST_CLEAR_ALL, -1);
2551 audio_begin_track_change(false, trackstat);
2554 /* Skip to the next/previous directory
2555 (Q_AUDIO_DIR_SKIP) */
2556 static void audio_on_dir_skip(int direction)
2558 logf("%s(%d)", __func__, direction);
2560 id3_mutex_lock();
2561 skip_offset = 0;
2562 id3_mutex_unlock();
2564 if (play_status == PLAY_STOPPED)
2565 return;
2567 /* Force codec to abort this track */
2568 halt_decoding_track(true);
2570 /* Kill the ff/rw halt */
2571 ff_rw_mode = false;
2573 /* Manual skip */
2574 automatic_skip = false;
2576 audio_playlist_track_finish();
2578 /* Unless automatic and gapless, skips do not pend */
2579 skip_pending = TRACK_SKIP_NONE;
2581 /* Regardless of the return value we need to rebuffer. If it fails the old
2582 playlist will resume, else the next dir will start playing. */
2583 playlist_next_dir(direction);
2585 wipe_track_metadata(false);
2587 int trackstat = audio_reset_and_rebuffer(TRACK_LIST_CLEAR_ALL, -1);
2589 if (trackstat == LOAD_TRACK_ERR_NO_MORE)
2591 /* The day the music died - finish-off whatever is playing and call it
2592 quits */
2593 audio_monitor_end_of_playlist();
2594 return;
2597 audio_begin_track_change(false, trackstat);
2600 /* Enter seek mode in order to start a seek
2601 (Q_AUDIO_PRE_FF_REWIND) */
2602 static void audio_on_pre_ff_rewind(void)
2604 logf("%s()", __func__);
2606 if (play_status == PLAY_STOPPED || ff_rw_mode)
2607 return;
2609 ff_rw_mode = true;
2611 if (play_status == PLAY_PAUSED)
2612 return;
2614 pcmbuf_pause(true);
2617 /* Seek the playback of the current track to the specified time
2618 (Q_AUDIO_FF_REWIND) */
2619 static void audio_on_ff_rewind(long time)
2621 logf("%s(%ld)", __func__, time);
2623 if (play_status == PLAY_STOPPED)
2624 return;
2626 enum track_skip_type pending = skip_pending;
2628 switch (pending)
2630 case TRACK_SKIP_NONE: /* The usual case */
2631 case TRACK_SKIP_AUTO: /* Have to back it out (fun!) */
2632 case TRACK_SKIP_AUTO_END_PLAYLIST: /* Still have the last codec used */
2634 struct mp3entry *id3 = id3_get(PLAYING_ID3);
2635 struct mp3entry *ci_id3 = id3_get(CODEC_ID3);
2637 automatic_skip = false;
2639 /* Send event before clobbering the time */
2640 /* FIXME: Nasty, but the tagtree expects this so that rewinding and
2641 then skipping back to this track resumes properly. Something else
2642 should be sent. We're not _really_ finishing the track are we? */
2643 if (time == 0)
2644 send_event(PLAYBACK_EVENT_TRACK_FINISH, id3);
2646 /* Prevent user codec time update - coerce to something that is
2647 innocuous concerning lookaheads */
2648 if (pending == TRACK_SKIP_NONE)
2649 skip_pending = TRACK_SKIP_AUTO_END_PLAYLIST;
2651 id3->elapsed = time;
2652 queue_reply(&audio_queue, 1);
2654 bool haltres = halt_decoding_track(pending == TRACK_SKIP_AUTO);
2656 /* Need this set in case ff/rw mode + error but _after_ the codec
2657 halt that will reset it */
2658 codec_seeking = true;
2660 if (pending == TRACK_SKIP_AUTO)
2662 if (!track_list_advance_current(-1))
2664 /* Not in list - must rebuffer at the current playlist index */
2665 if (audio_reset_and_rebuffer(TRACK_LIST_CLEAR_ALL, -1)
2666 < LOAD_TRACK_OK)
2668 /* Codec is stopped */
2669 break;
2674 /* Set after audio_fill_file_buffer to disable playing id3 clobber if
2675 rebuffer is needed */
2676 skip_pending = TRACK_SKIP_NONE;
2677 struct track_info *cur_info = track_list_current(0);
2679 /* Track must complete the loading _now_ since a codec and audio
2680 handle are needed in order to do the seek */
2681 if (cur_info->audio_hid < 0 &&
2682 audio_finish_load_track(cur_info) != LOAD_TRACK_READY)
2684 /* Call above should push any load sequence - no need for
2685 halt_decoding_track here if no skip was pending here because
2686 there would not be a codec started if no audio handle was yet
2687 opened */
2688 break;
2691 if (pending == TRACK_SKIP_AUTO)
2693 if (!bufreadid3(cur_info->id3_hid, ci_id3) ||
2694 !audio_init_codec(cur_info, ci_id3))
2696 /* We should have still been able to get it - skip it and move
2697 onto the next one - like it or not this track is broken */
2698 break;
2701 /* Set the codec API to the correct metadata and track info */
2702 ci.audio_hid = cur_info->audio_hid;
2703 ci.filesize = cur_info->filesize;
2704 buf_set_base_handle(cur_info->audio_hid);
2707 if (!haltres)
2709 /* If codec must be (re)started, reset the offset */
2710 ci_id3->offset = 0;
2713 codec_seek(time);
2714 return;
2717 case TRACK_SKIP_AUTO_NEW_PLAYLIST:
2719 /* We cannot do this because the playlist must be reversed by one
2720 and it doesn't always return the same song when going backwards
2721 across boundaries as forwards (either because of randomization
2722 or inconsistency in deciding what the previous track should be),
2723 therefore the whole operation would often end up as nonsense -
2724 lock out seeking for a couple seconds */
2726 /* Sure as heck cancel seek mode too! */
2727 audio_ff_rewind_end();
2728 return;
2731 default:
2732 /* Won't see this */
2733 return;
2736 if (play_status == PLAY_STOPPED)
2738 /* Playback ended because of an error completing a track load */
2739 return;
2742 /* Always fake it as a codec start error which will handle mode
2743 cancellations and skip to the next track */
2744 audio_handle_track_load_status(LOAD_TRACK_ERR_START_CODEC);
2747 /* Invalidates all but currently playing track
2748 (Q_AUDIO_FLUSH) */
2749 static void audio_on_audio_flush(void)
2751 logf("%s", __func__);
2753 if (track_list_empty())
2754 return; /* Nothing to flush out */
2756 switch (skip_pending)
2758 case TRACK_SKIP_NONE:
2759 case TRACK_SKIP_AUTO_END_PLAYLIST:
2760 /* Remove all but the currently playing track from the list and
2761 refill after that */
2762 track_list_clear(TRACK_LIST_KEEP_CURRENT);
2763 playlist_peek_offset = 0;
2764 id3_write_locked(UNBUFFERED_ID3, NULL);
2765 audio_update_and_announce_next_track(NULL);
2767 /* Ignore return since it's about the next track, not this one */
2768 audio_fill_file_buffer();
2770 if (skip_pending == TRACK_SKIP_NONE)
2771 break;
2773 /* There's now a track after this one now - convert to auto skip -
2774 no skip should pend right now because multiple flush messages can
2775 be fired which would cause a restart in the below cases */
2776 skip_pending = TRACK_SKIP_NONE;
2777 audio_clear_track_notifications();
2778 audio_queue_post(Q_AUDIO_CODEC_COMPLETE, CODEC_OK);
2779 break;
2781 case TRACK_SKIP_AUTO:
2782 case TRACK_SKIP_AUTO_NEW_PLAYLIST:
2783 /* Precisely removing what it already decoded for the next track is
2784 not possible so a restart is required in order to continue the
2785 currently playing track without the now invalid future track
2786 playing */
2787 audio_start_playback(0, AUDIO_START_RESTART);
2788 break;
2790 default: /* Nothing else is a state */
2791 break;
2795 #ifdef AUDIO_HAVE_RECORDING
2796 /* Load the requested encoder type
2797 (Q_AUDIO_LOAD_ENCODER) */
2798 static void audio_on_load_encoder(int afmt)
2800 bool res = true;
2802 if (play_status != PLAY_STOPPED)
2803 audio_stop_playback(); /* Can't load both types at once */
2804 else
2805 codec_unload(); /* Encoder still loaded, stop and unload it */
2807 if (afmt != AFMT_UNKNOWN)
2809 res = codec_load(-1, afmt | CODEC_TYPE_ENCODER);
2810 if (res)
2811 codec_go(); /* These are run immediately */
2814 queue_reply(&audio_queue, res);
2816 #endif /* AUDIO_HAVE_RECORDING */
2818 static void audio_thread(void)
2820 struct queue_event ev;
2822 pcm_postinit();
2824 filling = STATE_IDLE;
2826 while (1)
2828 switch (filling)
2830 /* Active states */
2831 case STATE_FULL:
2832 case STATE_END_OF_PLAYLIST:
2833 if (buf_get_watermark() == 0)
2835 /* End of buffering for now, let's calculate the watermark,
2836 register for a low buffer event and unboost */
2837 audio_update_filebuf_watermark(0);
2838 add_event(BUFFER_EVENT_BUFFER_LOW, true,
2839 buffer_event_buffer_low_callback);
2841 /* Fall-through */
2842 case STATE_FINISHED:
2843 /* All data was buffered */
2844 cancel_cpu_boost();
2845 /* Fall-through */
2846 case STATE_FILLING:
2847 case STATE_ENDING:
2848 if (audio_pcmbuf_track_change_scan())
2850 /* Transfer notification to audio queue event */
2851 ev.id = Q_AUDIO_TRACK_CHANGED;
2852 ev.data = 1;
2854 else
2856 /* If doing auto skip, poll pcmbuf track notifications a bit
2857 faster to promply detect the transition */
2858 queue_wait_w_tmo(&audio_queue, &ev,
2859 skip_pending == TRACK_SKIP_NONE ?
2860 HZ/2 : HZ/10);
2862 break;
2864 /* Idle states */
2865 default:
2866 queue_wait(&audio_queue, &ev);
2868 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
2869 switch (ev.id)
2871 #ifdef AUDIO_HAVE_RECORDING
2872 /* Must monitor the encoder message for recording so it can remove
2873 it if we process the insertion before it does. It cannot simply
2874 be removed from under recording however. */
2875 case Q_AUDIO_LOAD_ENCODER:
2876 break;
2877 #endif
2878 case SYS_USB_DISCONNECTED:
2879 filling = STATE_IDLE;
2880 break;
2882 default:
2883 if (filling == STATE_USB)
2884 continue;
2886 #endif /* CONFIG_PLATFORM */
2889 switch (ev.id)
2891 /** Codec and track change messages **/
2892 case Q_AUDIO_CODEC_COMPLETE:
2893 /* Codec is done processing track and has gone idle */
2894 LOGFQUEUE("audio < Q_AUDIO_CODEC_COMPLETE: %ld", (long)ev.data);
2895 audio_on_codec_complete(ev.data);
2896 break;
2898 case Q_AUDIO_CODEC_SEEK_COMPLETE:
2899 /* Codec is done seeking */
2900 LOGFQUEUE("audio < Q_AUDIO_SEEK_COMPLETE");
2901 audio_on_codec_seek_complete();
2902 break;
2904 case Q_AUDIO_TRACK_CHANGED:
2905 /* PCM track change done */
2906 LOGFQUEUE("audio < Q_AUDIO_TRACK_CHANGED");
2907 audio_on_track_changed();
2908 break;
2910 /** Control messages **/
2911 case Q_AUDIO_PLAY:
2912 LOGFQUEUE("audio < Q_AUDIO_PLAY");
2913 audio_start_playback(ev.data, 0);
2914 break;
2916 case Q_AUDIO_STOP:
2917 LOGFQUEUE("audio < Q_AUDIO_STOP");
2918 audio_stop_playback();
2919 if (ev.data != 0)
2920 queue_clear(&audio_queue);
2921 break;
2923 case Q_AUDIO_PAUSE:
2924 LOGFQUEUE("audio < Q_AUDIO_PAUSE");
2925 audio_on_pause(ev.data);
2926 break;
2928 case Q_AUDIO_SKIP:
2929 LOGFQUEUE("audio < Q_AUDIO_SKIP");
2930 audio_on_skip();
2931 break;
2933 case Q_AUDIO_DIR_SKIP:
2934 LOGFQUEUE("audio < Q_AUDIO_DIR_SKIP");
2935 audio_on_dir_skip(ev.data);
2936 break;
2938 case Q_AUDIO_PRE_FF_REWIND:
2939 LOGFQUEUE("audio < Q_AUDIO_PRE_FF_REWIND");
2940 audio_on_pre_ff_rewind();
2941 break;
2943 case Q_AUDIO_FF_REWIND:
2944 LOGFQUEUE("audio < Q_AUDIO_FF_REWIND");
2945 audio_on_ff_rewind(ev.data);
2946 break;
2948 case Q_AUDIO_FLUSH:
2949 LOGFQUEUE("audio < Q_AUDIO_FLUSH: %d", (int)ev.data);
2950 audio_on_audio_flush();
2951 break;
2953 /** Buffering messages **/
2954 case Q_AUDIO_BUFFERING:
2955 /* some buffering event */
2956 LOGFQUEUE("audio < Q_AUDIO_BUFFERING: %d", (int)ev.data);
2957 audio_on_buffering(ev.data);
2958 break;
2960 case Q_AUDIO_FILL_BUFFER:
2961 /* continue buffering next track */
2962 LOGFQUEUE("audio < Q_AUDIO_FILL_BUFFER");
2963 audio_on_fill_buffer();
2964 break;
2966 case Q_AUDIO_FINISH_LOAD_TRACK:
2967 /* metadata is buffered */
2968 LOGFQUEUE("audio < Q_AUDIO_FINISH_LOAD_TRACK");
2969 audio_on_finish_load_track(ev.data);
2970 break;
2972 case Q_AUDIO_HANDLE_FINISHED:
2973 /* some other type is buffered */
2974 LOGFQUEUE("audio < Q_AUDIO_HANDLE_FINISHED");
2975 audio_on_handle_finished(ev.data);
2976 break;
2978 /** Miscellaneous messages **/
2979 case Q_AUDIO_REMAKE_AUDIO_BUFFER:
2980 /* buffer needs to be reinitialized */
2981 LOGFQUEUE("audio < Q_AUDIO_REMAKE_AUDIO_BUFFER");
2982 audio_start_playback(0, AUDIO_START_RESTART | AUDIO_START_NEWBUF);
2983 break;
2985 #ifdef HAVE_DISK_STORAGE
2986 case Q_AUDIO_UPDATE_WATERMARK:
2987 /* buffering watermark needs updating */
2988 LOGFQUEUE("audio < Q_AUDIO_UPDATE_WATERMARK: %d", (int)ev.data);
2989 audio_update_filebuf_watermark(ev.data);
2990 break;
2991 #endif /* HAVE_DISK_STORAGE */
2993 #ifdef AUDIO_HAVE_RECORDING
2994 case Q_AUDIO_LOAD_ENCODER:
2995 /* load an encoder for recording */
2996 LOGFQUEUE("audio < Q_AUDIO_LOAD_ENCODER: %d", (int)ev.data);
2997 audio_on_load_encoder(ev.data);
2998 break;
2999 #endif /* AUDIO_HAVE_RECORDING */
3001 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
3002 case SYS_USB_CONNECTED:
3003 LOGFQUEUE("audio < SYS_USB_CONNECTED");
3004 audio_stop_playback();
3005 #ifdef PLAYBACK_VOICE
3006 voice_stop();
3007 #endif
3008 filling = STATE_USB;
3009 usb_acknowledge(SYS_USB_CONNECTED_ACK);
3010 break;
3011 #endif /* (CONFIG_PLATFORM & PLATFORM_NATIVE) */
3013 case SYS_TIMEOUT:
3014 LOGFQUEUE_SYS_TIMEOUT("audio < SYS_TIMEOUT");
3015 break;
3017 default:
3018 /* LOGFQUEUE("audio < default : %08lX", ev.id); */
3019 break;
3020 } /* end switch */
3021 } /* end while */
3025 /* --- Buffering callbacks --- */
3027 /* Called when fullness is below the watermark level */
3028 static void buffer_event_buffer_low_callback(void *data)
3030 logf("low buffer callback");
3031 LOGFQUEUE("buffering > audio Q_AUDIO_BUFFERING: buffer low");
3032 audio_queue_post(Q_AUDIO_BUFFERING, BUFFER_EVENT_BUFFER_LOW);
3033 (void)data;
3036 /* Called when handles must be discarded in order to buffer new data */
3037 static void buffer_event_rebuffer_callback(void *data)
3039 logf("rebuffer callback");
3040 LOGFQUEUE("buffering > audio Q_AUDIO_BUFFERING: rebuffer");
3041 audio_queue_post(Q_AUDIO_BUFFERING, BUFFER_EVENT_REBUFFER);
3042 (void)data;
3045 /* A handle has completed buffering and all required data is available */
3046 static void buffer_event_finished_callback(void *data)
3048 int hid = *(const int *)data;
3049 const enum data_type htype = buf_handle_data_type(hid);
3051 logf("handle %d finished buffering (type:%u)", hid, (unsigned)htype);
3053 /* Limit queue traffic */
3054 switch (htype)
3056 case TYPE_ID3:
3057 /* The metadata handle for the last loaded track has been buffered.
3058 We can ask the audio thread to load the rest of the track's data. */
3059 LOGFQUEUE("buffering > audio Q_AUDIO_FINISH_LOAD_TRACK: %d", hid);
3060 audio_queue_post(Q_AUDIO_FINISH_LOAD_TRACK, hid);
3061 break;
3063 case TYPE_PACKET_AUDIO:
3064 /* Strip any useless trailing tags that are left. */
3065 strip_tags(hid);
3066 /* Fall-through */
3067 case TYPE_ATOMIC_AUDIO:
3068 LOGFQUEUE("buffering > audio Q_AUDIO_HANDLE_FINISHED: %d", hid);
3069 audio_queue_post(Q_AUDIO_HANDLE_FINISHED, hid);
3070 break;
3072 default:
3073 /* Don't care to know about these */
3074 break;
3079 /** -- Codec callbacks -- **/
3081 /* Update elapsed times with latency-adjusted values */
3082 void audio_codec_update_elapsed(unsigned long value)
3084 #ifdef AB_REPEAT_ENABLE
3085 ab_position_report(value);
3086 #endif
3088 unsigned long latency = pcmbuf_get_latency();
3090 if (LIKELY(value >= latency))
3092 unsigned long elapsed = value - latency;
3094 if (elapsed > value || elapsed < value - 2)
3095 value = elapsed;
3097 else
3099 value = 0;
3102 /* Track codec: used later when updating the playing at the user
3103 transition */
3104 id3_get(CODEC_ID3)->elapsed = value;
3106 /* If a skip is pending, the PCM buffer is updating the time on the
3107 previous song */
3108 if (LIKELY(skip_pending == TRACK_SKIP_NONE))
3109 id3_get(PLAYING_ID3)->elapsed = value;
3112 /* Update offsets with latency-adjusted values */
3113 void audio_codec_update_offset(size_t value)
3115 struct mp3entry *ci_id3 = id3_get(CODEC_ID3);
3116 unsigned long latency = pcmbuf_get_latency() * ci_id3->bitrate / 8;
3118 if (LIKELY(value >= latency))
3120 value -= latency;
3122 else
3124 value = 0;
3127 /* Track codec: used later when updating the playing id3 at the user
3128 transition */
3129 ci_id3->offset = value;
3131 /* If a skip is pending, the PCM buffer is updating the time on the
3132 previous song */
3133 if (LIKELY(skip_pending == TRACK_SKIP_NONE))
3134 id3_get(PLAYING_ID3)->offset = value;
3138 /** --- Pcmbuf callbacks --- **/
3140 /* Between the codec and PCM track change, we need to keep updating the
3141 * "elapsed" value of the previous (to the codec, but current to the
3142 * user/PCM/WPS) track, so that the progressbar reaches the end. */
3143 void audio_pcmbuf_position_callback(unsigned int time)
3145 struct mp3entry *id3 = id3_get(PLAYING_ID3);
3147 time += id3->elapsed;
3149 id3->elapsed = MIN(time, id3->length);
3152 /* Post message from pcmbuf that the end of the previous track has just
3153 * been played */
3154 void audio_pcmbuf_track_change(bool pcmbuf)
3156 if (pcmbuf)
3158 /* Notify of the change in special-purpose semaphore object */
3159 LOGFQUEUE("pcmbuf > pcmbuf Q_AUDIO_TRACK_CHANGED");
3160 audio_pcmbuf_track_change_post();
3162 else
3164 /* Safe to post directly to the queue */
3165 LOGFQUEUE("pcmbuf > audio Q_AUDIO_TRACK_CHANGED");
3166 audio_queue_post(Q_AUDIO_TRACK_CHANGED, 0);
3170 /* May pcmbuf start PCM playback when the buffer is full enough? */
3171 bool audio_pcmbuf_may_play(void)
3173 return play_status != PLAY_PAUSED && !ff_rw_mode;
3177 /** -- External interfaces -- **/
3179 /* Return the playback and recording status */
3180 int audio_status(void)
3182 unsigned int ret = play_status;
3184 #ifdef AUDIO_HAVE_RECORDING
3185 /* Do this here for constitency with mpeg.c version */
3186 ret |= pcm_rec_status();
3187 #endif
3189 return (int)ret;
3192 /* Clear all accumulated audio errors for playback and recording */
3193 void audio_error_clear(void)
3195 #ifdef AUDIO_HAVE_RECORDING
3196 pcm_rec_error_clear();
3197 #endif
3200 /* Get a copy of the id3 data for the for current track + offset + skip delta */
3201 bool audio_peek_track(struct mp3entry *id3, int offset)
3203 bool retval = false;
3205 id3_mutex_lock();
3207 if (play_status != PLAY_STOPPED)
3209 id3->path[0] = '\0'; /* Null path means it should be filled now */
3210 retval = audio_get_track_metadata(offset + skip_offset, id3) &&
3211 id3->path[0] != '\0';
3214 id3_mutex_unlock();
3216 return retval;
3219 /* Return the mp3entry for the currently playing track */
3220 struct mp3entry * audio_current_track(void)
3222 struct mp3entry *id3;
3224 id3_mutex_lock();
3226 #ifdef AUDIO_FAST_SKIP_PREVIEW
3227 if (skip_offset != 0)
3229 /* This is a peekahead */
3230 id3 = id3_get(PLAYING_PEEK_ID3);
3231 audio_peek_track(id3, 0);
3233 else
3234 #endif
3236 /* Normal case */
3237 id3 = id3_get(PLAYING_ID3);
3238 audio_get_track_metadata(0, id3);
3241 id3_mutex_unlock();
3243 return id3;
3246 /* Obtains the mp3entry for the next track from the current */
3247 struct mp3entry * audio_next_track(void)
3249 struct mp3entry *id3 = id3_get(NEXTTRACK_ID3);
3251 id3_mutex_lock();
3253 #ifdef AUDIO_FAST_SKIP_PREVIEW
3254 if (skip_offset != 0)
3256 /* This is a peekahead */
3257 if (!audio_peek_track(id3, 1))
3258 id3 = NULL;
3260 else
3261 #endif
3263 /* Normal case */
3264 if (!audio_get_track_metadata(1, id3))
3265 id3 = NULL;
3268 id3_mutex_unlock();
3270 return id3;
3273 /* Start playback at the specified offset */
3274 void audio_play(long offset)
3276 logf("audio_play");
3278 #ifdef PLAYBACK_VOICE
3279 /* Truncate any existing voice output so we don't have spelling
3280 * etc. over the first part of the played track */
3281 talk_force_shutup();
3282 #endif
3284 LOGFQUEUE("audio >| audio Q_AUDIO_PLAY: %ld", offset);
3285 audio_queue_send(Q_AUDIO_PLAY, offset);
3288 /* Stop playback if playing */
3289 void audio_stop(void)
3291 LOGFQUEUE("audio >| audio Q_AUDIO_STOP");
3292 audio_queue_send(Q_AUDIO_STOP, 0);
3295 /* Pause playback if playing */
3296 void audio_pause(void)
3298 LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE");
3299 audio_queue_send(Q_AUDIO_PAUSE, true);
3302 /* This sends a stop message and the audio thread will dump all its
3303 subsequent messages */
3304 void audio_hard_stop(void)
3306 /* Stop playback */
3307 LOGFQUEUE("audio >| audio Q_AUDIO_STOP: 1");
3308 audio_queue_send(Q_AUDIO_STOP, 1);
3309 #ifdef PLAYBACK_VOICE
3310 voice_stop();
3311 #endif
3314 /* Resume playback if paused */
3315 void audio_resume(void)
3317 LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE resume");
3318 audio_queue_send(Q_AUDIO_PAUSE, false);
3321 /* Skip the specified number of tracks forward or backward from the current */
3322 void audio_skip(int offset)
3324 id3_mutex_lock();
3326 /* If offset has to be backed-out to stay in range, no skip is done */
3327 int accum = skip_offset + offset;
3329 while (offset != 0 && !playlist_check(accum))
3331 offset += offset < 0 ? 1 : -1;
3332 accum = skip_offset + offset;
3335 if (offset != 0)
3337 /* Accumulate net manual skip count since the audio thread last
3338 processed one */
3339 skip_offset = accum;
3341 if (global_settings.beep)
3342 pcmbuf_beep(2000, 100, 2500*global_settings.beep);
3344 LOGFQUEUE("audio > audio Q_AUDIO_SKIP %d", offset);
3346 #ifdef AUDIO_FAST_SKIP_PREVIEW
3347 /* Do this before posting so that the audio thread can correct us
3348 when things settle down - additionally, if audio gets a message
3349 and the delta is zero, the Q_AUDIO_SKIP handler (audio_on_skip)
3350 handler a skip event with the correct info but doesn't skip */
3351 send_event(PLAYBACK_EVENT_TRACK_SKIP, NULL);
3352 #endif /* AUDIO_FAST_SKIP_PREVIEW */
3354 /* Playback only needs the final state even if more than one is
3355 processed because it wasn't removed in time */
3356 queue_remove_from_head(&audio_queue, Q_AUDIO_SKIP);
3357 audio_queue_post(Q_AUDIO_SKIP, 0);
3359 else
3361 /* No more tracks */
3362 if (global_settings.beep)
3363 pcmbuf_beep(1000, 100, 1500*global_settings.beep);
3366 id3_mutex_unlock();
3369 /* Skip one track forward from the current */
3370 void audio_next(void)
3372 audio_skip(1);
3375 /* Skip one track backward from the current */
3376 void audio_prev(void)
3378 audio_skip(-1);
3381 /* Move one directory forward */
3382 void audio_next_dir(void)
3384 LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP 1");
3385 audio_queue_post(Q_AUDIO_DIR_SKIP, 1);
3388 /* Move one directory backward */
3389 void audio_prev_dir(void)
3391 LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP -1");
3392 audio_queue_post(Q_AUDIO_DIR_SKIP, -1);
3395 /* Pause playback in order to start a seek that flushes the old audio */
3396 void audio_pre_ff_rewind(void)
3398 LOGFQUEUE("audio > audio Q_AUDIO_PRE_FF_REWIND");
3399 audio_queue_post(Q_AUDIO_PRE_FF_REWIND, 0);
3402 /* Seek to the new time in the current track */
3403 void audio_ff_rewind(long time)
3405 LOGFQUEUE("audio > audio Q_AUDIO_FF_REWIND");
3406 audio_queue_post(Q_AUDIO_FF_REWIND, time);
3409 /* Clear all but the currently playing track then rebuffer */
3410 void audio_flush_and_reload_tracks(void)
3412 LOGFQUEUE("audio > audio Q_AUDIO_FLUSH");
3413 audio_queue_post(Q_AUDIO_FLUSH, 0);
3416 /* Return the pointer to the main audio buffer, optionally preserving
3417 voicing */
3418 unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size)
3420 unsigned char *buf, *end;
3422 if (audio_is_initialized)
3424 audio_hard_stop();
3426 /* else buffer_state will be AUDIOBUF_STATE_TRASHED at this point */
3428 if (buffer_size == NULL)
3430 /* Special case for talk_init to use since it already knows it's
3431 trashed */
3432 buffer_state = AUDIOBUF_STATE_TRASHED;
3433 return NULL;
3436 if (talk_buf || buffer_state == AUDIOBUF_STATE_TRASHED
3437 || !talk_voice_required())
3439 logf("get buffer: talk, audio");
3440 /* Ok to use everything from audiobuf to audiobufend - voice is loaded,
3441 the talk buffer is not needed because voice isn't being used, or
3442 could be AUDIOBUF_STATE_TRASHED already. If state is
3443 AUDIOBUF_STATE_VOICED_ONLY, no problem as long as memory isn't
3444 written without the caller knowing what's going on. Changing certain
3445 settings may move it to a worse condition but the memory in use by
3446 something else will remain undisturbed.
3448 if (buffer_state != AUDIOBUF_STATE_TRASHED)
3450 talk_buffer_steal();
3451 buffer_state = AUDIOBUF_STATE_TRASHED;
3454 buf = audiobuf;
3455 end = audiobufend;
3457 else
3459 /* Safe to just return this if already AUDIOBUF_STATE_VOICED_ONLY or
3460 still AUDIOBUF_STATE_INITIALIZED */
3461 /* Skip talk buffer and move pcm buffer to end to maximize available
3462 contiguous memory - no audio running means voice will not need the
3463 swap space */
3464 logf("get buffer: audio");
3465 buf = audiobuf + talk_get_bufsize();
3466 end = audiobufend - pcmbuf_init(audiobufend);
3467 buffer_state = AUDIOBUF_STATE_VOICED_ONLY;
3470 *buffer_size = end - buf;
3472 return buf;
3475 #ifdef HAVE_RECORDING
3476 /* Stop audio, voice and obtain all available buffer space */
3477 unsigned char * audio_get_recording_buffer(size_t *buffer_size)
3479 audio_hard_stop();
3480 talk_buffer_steal();
3482 unsigned char *end = audiobufend;
3483 buffer_state = AUDIOBUF_STATE_TRASHED;
3484 *buffer_size = end - audiobuf;
3486 return (unsigned char *)audiobuf;
3488 #endif /* HAVE_RECORDING */
3490 /* Restore audio buffer to a particular state (one more valid than the current
3491 state) */
3492 bool audio_restore_playback(int type)
3494 switch (type)
3496 case AUDIO_WANT_PLAYBACK:
3497 if (buffer_state != AUDIOBUF_STATE_INITIALIZED)
3498 audio_reset_buffer();
3499 return true;
3500 case AUDIO_WANT_VOICE:
3501 if (buffer_state == AUDIOBUF_STATE_TRASHED)
3502 audio_reset_buffer();
3503 return true;
3504 default:
3505 return false;
3509 /* Has the playback buffer been completely claimed? */
3510 bool audio_buffer_state_trashed(void)
3512 return buffer_state == AUDIOBUF_STATE_TRASHED;
3516 /** --- Miscellaneous public interfaces --- **/
3518 #ifdef HAVE_ALBUMART
3519 /* Return which album art handle is current for the user in the given slot */
3520 int playback_current_aa_hid(int slot)
3522 if ((unsigned)slot < MAX_MULTIPLE_AA)
3524 struct track_info *info = track_list_user_current(skip_offset);
3526 if (!info && abs(skip_offset) <= 1)
3528 /* Give the actual position a go */
3529 info = track_list_user_current(0);
3532 if (info)
3533 return info->aa_hid[slot];
3536 return ERR_HANDLE_NOT_FOUND;
3539 /* Find an album art slot that doesn't match the dimensions of another that
3540 is already claimed - increment the use count if it is */
3541 int playback_claim_aa_slot(struct dim *dim)
3543 int i;
3545 /* First try to find a slot already having the size to reuse it since we
3546 don't want albumart of the same size buffered multiple times */
3547 FOREACH_ALBUMART(i)
3549 struct albumart_slot *slot = &albumart_slots[i];
3551 if (slot->dim.width == dim->width &&
3552 slot->dim.height == dim->height)
3554 slot->used++;
3555 return i;
3559 /* Size is new, find a free slot */
3560 FOREACH_ALBUMART(i)
3562 if (!albumart_slots[i].used)
3564 albumart_slots[i].used++;
3565 albumart_slots[i].dim = *dim;
3566 return i;
3570 /* Sorry, no free slot */
3571 return -1;
3574 /* Invalidate the albumart_slot - decrement the use count if > 0 */
3575 void playback_release_aa_slot(int slot)
3577 if ((unsigned)slot < MAX_MULTIPLE_AA)
3579 struct albumart_slot *aa_slot = &albumart_slots[slot];
3581 if (aa_slot->used > 0)
3582 aa_slot->used--;
3585 #endif /* HAVE_ALBUMART */
3588 #ifdef HAVE_RECORDING
3589 /* Load an encoder and run it */
3590 bool audio_load_encoder(int afmt)
3592 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
3593 LOGFQUEUE("audio >| Q_AUDIO_LOAD_ENCODER: %d", afmt);
3594 return audio_queue_send(Q_AUDIO_LOAD_ENCODER, afmt) != 0;
3595 #else
3596 (void)afmt;
3597 return true;
3598 #endif
3601 /* Stop an encoder and unload it */
3602 void audio_remove_encoder(void)
3604 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
3605 LOGFQUEUE("audio >| Q_AUDIO_LOAD_ENCODER: NULL");
3606 audio_queue_send(Q_AUDIO_LOAD_ENCODER, AFMT_UNKNOWN);
3607 #endif
3609 #endif /* HAVE_RECORDING */
3611 /* Is an automatic skip in progress? If called outside transition callbacks,
3612 indicates the last skip type at the time it was processed and isn't very
3613 meaningful. */
3614 bool audio_automatic_skip(void)
3616 return automatic_skip;
3619 /* Would normally calculate byte offset from an elapsed time but is not
3620 used on SWCODEC */
3621 int audio_get_file_pos(void)
3623 return 0;
3626 /* Return the elapsed time of the track previous to the current */
3627 unsigned long audio_prev_elapsed(void)
3629 return prev_track_elapsed;
3632 /* Is the audio thread ready to accept commands? */
3633 bool audio_is_thread_ready(void)
3635 return filling != STATE_BOOT;
3638 /* Return total file buffer length after accounting for the talk buf */
3639 size_t audio_get_filebuflen(void)
3641 return buf_length();
3644 /* How many tracks exist on the buffer - full or partial */
3645 int audio_track_count(void)
3646 __attribute__((alias("track_list_count")));
3648 /* Return total ringbuffer space occupied - ridx to widx */
3649 long audio_filebufused(void)
3651 return buf_used();
3655 /** -- Settings -- **/
3657 /* Enable or disable cuesheet support and allocate/don't allocate the
3658 extra associated resources */
3659 void audio_set_cuesheet(int enable)
3661 if (play_status == PLAY_STOPPED || !enable != !get_current_cuesheet())
3663 LOGFQUEUE("audio >| audio Q_AUDIO_REMAKE_AUDIO_BUFFER");
3664 audio_queue_send(Q_AUDIO_REMAKE_AUDIO_BUFFER, 0);
3668 #ifdef HAVE_DISK_STORAGE
3669 /* Set the audio antiskip buffer margin by index */
3670 void audio_set_buffer_margin(int setting)
3672 static const unsigned short lookup[] =
3673 { 5, 15, 30, 60, 120, 180, 300, 600 };
3675 if ((unsigned)setting >= ARRAYLEN(lookup))
3676 setting = 0;
3678 logf("buffer margin: %u", (unsigned)lookup[setting]);
3680 LOGFQUEUE("audio > audio Q_AUDIO_UPDATE_WATERMARK: %u",
3681 (unsigned)lookup[setting]);
3682 audio_queue_post(Q_AUDIO_UPDATE_WATERMARK, lookup[setting]);
3684 #endif /* HAVE_DISK_STORAGE */
3686 #ifdef HAVE_CROSSFADE
3687 /* Take necessary steps to enable or disable the crossfade setting */
3688 void audio_set_crossfade(int enable)
3690 /* Tell it the next setting to use */
3691 pcmbuf_request_crossfade_enable(enable);
3693 /* Return if size hasn't changed or this is too early to determine
3694 which in the second case there's no way we could be playing
3695 anything at all */
3696 if (!pcmbuf_is_same_size())
3698 LOGFQUEUE("audio >| audio Q_AUDIO_REMAKE_AUDIO_BUFFER");
3699 audio_queue_send(Q_AUDIO_REMAKE_AUDIO_BUFFER, 0);
3702 #endif /* HAVE_CROSSFADE */
3705 /** -- Startup -- **/
3707 /* Initialize the audio system - called from init() in main.c */
3708 void audio_init(void)
3710 /* Can never do this twice */
3711 if (audio_is_initialized)
3713 logf("audio: already initialized");
3714 return;
3717 logf("audio: initializing");
3719 /* Initialize queues before giving control elsewhere in case it likes
3720 to send messages. Thread creation will be delayed however so nothing
3721 starts running until ready if something yields such as talk_init. */
3722 queue_init(&audio_queue, true);
3724 mutex_init(&id3_mutex);
3726 pcm_init();
3728 codec_init_codec_api();
3730 make_codec_thread();
3732 /* This thread does buffer, so match its priority */
3733 audio_thread_id = create_thread(audio_thread, audio_stack,
3734 sizeof(audio_stack), CREATE_THREAD_FROZEN,
3735 audio_thread_name
3736 IF_PRIO(, MIN(PRIORITY_BUFFERING, PRIORITY_USER_INTERFACE))
3737 IF_COP(, CPU));
3739 queue_enable_queue_send(&audio_queue, &audio_queue_sender_list,
3740 audio_thread_id);
3742 #ifdef PLAYBACK_VOICE
3743 voice_thread_init();
3744 #endif
3746 /* audio_reset_buffer must know the size of voice buffer so init
3747 talk first */
3748 talk_init();
3750 #ifdef HAVE_CROSSFADE
3751 /* Set crossfade setting for next buffer init which should be about... */
3752 pcmbuf_request_crossfade_enable(global_settings.crossfade);
3753 #endif
3755 /* Initialize the buffering system */
3756 track_list_init();
3757 buffering_init();
3758 /* ...now! Set up the buffers */
3759 audio_reset_buffer();
3761 /* Probably safe to say */
3762 audio_is_initialized = true;
3764 sound_settings_apply();
3765 #ifdef HAVE_DISK_STORAGE
3766 audio_set_buffer_margin(global_settings.buffer_margin);
3767 #endif
3769 /* It's safe to let the threads run now */
3770 #ifdef PLAYBACK_VOICE
3771 voice_thread_resume();
3772 #endif
3773 codec_thread_resume();
3774 thread_thaw(audio_thread_id);