1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005-2007 Miika Pekkarinen
11 * Copyright (C) 2007-2008 Nicolas Pennequin
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 /* TODO: Pause should be handled in here, rather than PCMBUF so that voice can
24 * play whilst audio is paused */
28 #include "codec_thread.h"
31 #include "buffering.h"
32 #include "voice_thread.h"
42 #ifdef HAVE_LCD_BITMAP
53 #include "pcm_record.h"
56 #define PLAYBACK_VOICE
58 /* amount of guess-space to allow for codecs that must hunt and peck
59 * for their correct seeek target, 32k seems a good size */
60 #define AUDIO_REBUFFER_GUESS_SIZE (1024*32)
62 /* Define LOGF_ENABLE to enable logf output in this file */
63 /*#define LOGF_ENABLE*/
66 /* macros to enable logf for queues
67 logging on SYS_TIMEOUT can be disabled */
69 /* Define this for logf output of all queuing except SYS_TIMEOUT */
70 #define PLAYBACK_LOGQUEUES
71 /* Define this to logf SYS_TIMEOUT messages */
72 /*#define PLAYBACK_LOGQUEUES_SYS_TIMEOUT*/
75 #ifdef PLAYBACK_LOGQUEUES
76 #define LOGFQUEUE logf
78 #define LOGFQUEUE(...)
81 #ifdef PLAYBACK_LOGQUEUES_SYS_TIMEOUT
82 #define LOGFQUEUE_SYS_TIMEOUT logf
84 #define LOGFQUEUE_SYS_TIMEOUT(...)
88 static enum filling_state
{
89 STATE_IDLE
, /* audio is stopped: nothing to do */
90 STATE_FILLING
, /* adding tracks to the buffer */
91 STATE_FULL
, /* can't add any more tracks */
92 STATE_END_OF_PLAYLIST
, /* all remaining tracks have been added */
93 STATE_FINISHED
, /* all remaining tracks are fully buffered */
96 /* As defined in plugins/lib/xxx2wav.h */
97 #define GUARD_BUFSIZE (32*1024)
99 bool audio_is_initialized
= false;
100 static bool audio_thread_ready SHAREDBSS_ATTR
= false;
102 /* Variables are commented with the threads that use them: *
103 * A=audio, C=codec, V=voice. A suffix of - indicates that *
104 * the variable is read but not updated on that thread. */
105 /* TBD: Split out "audio" and "playback" (ie. calling) threads */
107 /* Main state control */
108 static volatile bool playing SHAREDBSS_ATTR
= false;/* Is audio playing? (A) */
109 static volatile bool paused SHAREDBSS_ATTR
= false; /* Is audio paused? (A/C-) */
110 extern volatile bool audio_codec_loaded
; /* Codec loaded? (C/A-) */
112 /* Ring buffer where compressed audio and codecs are loaded */
113 static unsigned char *filebuf
= NULL
; /* Start of buffer (A/C-) */
114 static unsigned char *malloc_buf
= NULL
; /* Start of malloc buffer (A/C-) */
115 static size_t filebuflen
= 0; /* Size of buffer (A/C-) */
116 /* FIXME: make buf_ridx (C/A-) */
118 /* Possible arrangements of the buffer */
119 static int buffer_state
= AUDIOBUF_STATE_TRASHED
; /* Buffer state */
121 /* These are used to store the current and next (or prev if the current is the last)
122 * mp3entry's in a round-robin system. This guarentees that the pointer returned
123 * by audio_current/next_track will be valid for the full duration of the
124 * currently playing track */
125 static struct mp3entry mp3entry_buf
[2];
126 struct mp3entry
*thistrack_id3
, /* the currently playing track */
127 *othertrack_id3
; /* prev track during track-change-transition, or end of playlist,
128 * next track otherwise */
129 static struct mp3entry unbuffered_id3
; /* the id3 for the first unbuffered track */
131 /* for cuesheet support */
132 static struct cuesheet
*curr_cue
= NULL
;
135 #define MAX_MULTIPLE_AA 2
138 static struct albumart_slot
{
139 struct dim dim
; /* holds width, height of the albumart */
140 int used
; /* counter, increments if something uses it */
141 } albumart_slots
[MAX_MULTIPLE_AA
];
143 #define FOREACH_ALBUMART(i) for(i = 0;i < MAX_MULTIPLE_AA; i++)
147 #define MAX_TRACK 128
148 #define MAX_TRACK_MASK (MAX_TRACK-1)
150 /* Track info structure about songs in the file buffer (A/C-) */
151 static struct track_info
{
152 int audio_hid
; /* The ID for the track's buffer handle */
153 int id3_hid
; /* The ID for the track's metadata handle */
154 int codec_hid
; /* The ID for the track's codec handle */
156 int aa_hid
[MAX_MULTIPLE_AA
];/* The ID for the track's album art handle */
158 int cuesheet_hid
; /* The ID for the track's parsed cueesheet handle */
160 size_t filesize
; /* File total length */
162 bool taginfo_ready
; /* Is metadata read */
166 static volatile int track_ridx
= 0; /* Track being decoded (A/C-) */
167 static int track_widx
= 0; /* Track being buffered (A) */
168 #define CUR_TI (&tracks[track_ridx]) /* Playing track info pointer (A/C-) */
170 static struct track_info
*prev_ti
= NULL
; /* Pointer to the previously played
173 /* Information used only for filling the buffer */
174 /* Playlist steps from playing track to next track to be buffered (A) */
175 static int last_peek_offset
= 0;
177 /* Scrobbler support */
178 static unsigned long prev_track_elapsed
= 0; /* Previous track elapsed time (C/A-)*/
180 /* Track change controls */
181 bool automatic_skip
= false; /* Who initiated in-progress skip? (C/A-) */
182 static bool dir_skip
= false; /* Is a directory skip pending? (A) */
183 static bool new_playlist
= false; /* Are we starting a new playlist? (A) */
184 static int wps_offset
= 0; /* Pending track change offset, to keep WPS responsive (A) */
185 static bool skipped_during_pause
= false; /* Do we need to clear the PCM buffer when playback resumes (A) */
187 static bool start_play_g
= false; /* Used by audio_load_track to notify
188 audio_finish_load_track about start_play */
190 /* True when a track load is in progress, i.e. audio_load_track() has returned
191 * but audio_finish_load_track() hasn't been called yet. Used to avoid allowing
192 * audio_load_track() to get called twice in a row, which would cause problems.
194 static bool track_load_started
= false;
196 #ifdef HAVE_DISK_STORAGE
197 static size_t buffer_margin
= 5; /* Buffer margin aka anti-skip buffer (A/C-) */
201 struct event_queue audio_queue SHAREDBSS_ATTR
;
202 struct event_queue codec_queue SHAREDBSS_ATTR
;
203 static struct event_queue pcmbuf_queue SHAREDBSS_ATTR
;
205 extern struct codec_api ci
;
206 extern unsigned int codec_thread_id
;
208 /* Multiple threads */
209 /* Set the watermark to trigger buffer fill (A/C) */
210 static void set_filebuf_watermark(void);
213 static struct queue_sender_list audio_queue_sender_list SHAREDBSS_ATTR
;
214 static long audio_stack
[(DEFAULT_STACK_SIZE
+ 0x1000)/sizeof(long)];
215 static const char audio_thread_name
[] = "audio";
217 static void audio_thread(void);
218 static void audio_initiate_track_change(long direction
);
219 static bool audio_have_tracks(void);
220 static void audio_reset_buffer(void);
221 static void audio_stop_playback(void);
224 /**************************************/
226 /* Post message from pcmbuf callback in the codec thread that
227 * the end of the previous track has just been played. */
228 void audio_post_track_change(void)
230 queue_post(&pcmbuf_queue
, Q_AUDIO_TRACK_CHANGED
, 0);
233 /* Scan the pcmbuf queue and return true if a message pulled.
234 * Permissible Context(s): Thread
236 static bool pcmbuf_queue_scan(struct queue_event
*ev
)
238 if (!queue_empty(&pcmbuf_queue
))
240 /* Transfer message to audio queue */
242 /* Pull message - never, ever any blocking call! */
243 queue_wait_w_tmo(&pcmbuf_queue
, ev
, 0);
251 /* Clear the pcmbuf queue of messages
252 * Permissible Context(s): Thread
254 static void pcmbuf_queue_clear(void)
257 queue_clear(&pcmbuf_queue
);
261 /* --- Helper functions --- */
263 static struct mp3entry
*bufgetid3(int handle_id
)
268 struct mp3entry
*id3
;
269 ssize_t ret
= bufgetdata(handle_id
, 0, (void *)&id3
);
271 if (ret
< 0 || ret
!= sizeof(struct mp3entry
))
277 static bool clear_track_info(struct track_info
*track
)
279 /* bufclose returns true if the handle is not found, or if it is closed
280 * successfully, so these checks are safe on non-existant handles */
284 if (track
->codec_hid
>= 0) {
285 if (bufclose(track
->codec_hid
))
286 track
->codec_hid
= -1;
291 if (track
->id3_hid
>= 0) {
292 if (bufclose(track
->id3_hid
))
298 if (track
->audio_hid
>= 0) {
299 if (bufclose(track
->audio_hid
))
300 track
->audio_hid
= -1;
310 if (track
->aa_hid
[i
] >= 0) {
311 if (bufclose(track
->aa_hid
[i
]))
312 track
->aa_hid
[i
] = -1;
320 if (track
->cuesheet_hid
>= 0) {
321 if (bufclose(track
->cuesheet_hid
))
322 track
->cuesheet_hid
= -1;
328 track
->taginfo_ready
= false;
333 /* --- External interfaces --- */
335 /* This sends a stop message and the audio thread will dump all it's
336 subsequenct messages */
337 void audio_hard_stop(void)
340 LOGFQUEUE("audio >| audio Q_AUDIO_STOP: 1");
341 queue_send(&audio_queue
, Q_AUDIO_STOP
, 1);
342 #ifdef PLAYBACK_VOICE
347 bool audio_restore_playback(int type
)
351 case AUDIO_WANT_PLAYBACK
:
352 if (buffer_state
!= AUDIOBUF_STATE_INITIALIZED
)
353 audio_reset_buffer();
355 case AUDIO_WANT_VOICE
:
356 if (buffer_state
== AUDIOBUF_STATE_TRASHED
)
357 audio_reset_buffer();
364 unsigned char *audio_get_buffer(bool talk_buf
, size_t *buffer_size
)
366 unsigned char *buf
, *end
;
368 if (audio_is_initialized
)
372 /* else buffer_state will be AUDIOBUF_STATE_TRASHED at this point */
374 /* Reset the buffering thread so that it doesn't try to use the data */
375 buffering_reset(filebuf
, filebuflen
);
377 if (buffer_size
== NULL
)
379 /* Special case for talk_init to use since it already knows it's
381 buffer_state
= AUDIOBUF_STATE_TRASHED
;
385 if (talk_buf
|| buffer_state
== AUDIOBUF_STATE_TRASHED
386 || !talk_voice_required())
388 logf("get buffer: talk, audio");
389 /* Ok to use everything from audiobuf to audiobufend - voice is loaded,
390 the talk buffer is not needed because voice isn't being used, or
391 could be AUDIOBUF_STATE_TRASHED already. If state is
392 AUDIOBUF_STATE_VOICED_ONLY, no problem as long as memory isn't written
393 without the caller knowing what's going on. Changing certain settings
394 may move it to a worse condition but the memory in use by something
395 else will remain undisturbed.
397 if (buffer_state
!= AUDIOBUF_STATE_TRASHED
)
400 buffer_state
= AUDIOBUF_STATE_TRASHED
;
408 /* Safe to just return this if already AUDIOBUF_STATE_VOICED_ONLY or
409 still AUDIOBUF_STATE_INITIALIZED */
410 /* Skip talk buffer and move pcm buffer to end to maximize available
411 contiguous memory - no audio running means voice will not need the
413 logf("get buffer: audio");
414 buf
= audiobuf
+ talk_get_bufsize();
415 end
= audiobufend
- pcmbuf_init(audiobufend
);
416 buffer_state
= AUDIOBUF_STATE_VOICED_ONLY
;
419 *buffer_size
= end
- buf
;
424 int audio_buffer_state(void)
429 #ifdef HAVE_RECORDING
430 unsigned char *audio_get_recording_buffer(size_t *buffer_size
)
432 /* Stop audio, voice and obtain all available buffer space */
436 unsigned char *end
= audiobufend
;
437 buffer_state
= AUDIOBUF_STATE_TRASHED
;
438 *buffer_size
= end
- audiobuf
;
440 return (unsigned char *)audiobuf
;
443 bool audio_load_encoder(int afmt
)
446 const char *enc_fn
= get_codec_filename(afmt
| CODEC_TYPE_ENCODER
);
450 audio_remove_encoder();
451 ci
.enc_codec_loaded
= 0; /* clear any previous error condition */
453 LOGFQUEUE("codec > Q_ENCODER_LOAD_DISK");
454 queue_post(&codec_queue
, Q_ENCODER_LOAD_DISK
, (intptr_t)enc_fn
);
456 while (ci
.enc_codec_loaded
== 0)
459 logf("codec loaded: %d", ci
.enc_codec_loaded
);
461 return ci
.enc_codec_loaded
> 0;
466 } /* audio_load_encoder */
468 void audio_remove_encoder(void)
471 /* force encoder codec unload (if currently loaded) */
472 if (ci
.enc_codec_loaded
<= 0)
475 ci
.stop_encoder
= true;
476 while (ci
.enc_codec_loaded
> 0)
479 } /* audio_remove_encoder */
481 #endif /* HAVE_RECORDING */
484 struct mp3entry
* audio_current_track(void)
486 const char *filename
;
487 struct playlist_track_info trackinfo
;
489 int offset
= ci
.new_track
+ wps_offset
;
490 struct mp3entry
*write_id3
;
492 cur_idx
= (track_ridx
+ offset
) & MAX_TRACK_MASK
;
494 if (cur_idx
== track_ridx
&& *thistrack_id3
->path
)
497 if (tracks
[cur_idx
].cuesheet_hid
>= 0 && !thistrack_id3
->cuesheet
)
499 bufread(tracks
[cur_idx
].cuesheet_hid
, sizeof(struct cuesheet
), curr_cue
);
500 thistrack_id3
->cuesheet
= curr_cue
;
501 cue_spoof_id3(thistrack_id3
->cuesheet
, thistrack_id3
);
503 return thistrack_id3
;
505 else if (automatic_skip
&& offset
== -1 && *othertrack_id3
->path
)
507 /* We're in a track transition. The codec has moved on to the next track,
508 but the audio being played is still the same (now previous) track.
509 othertrack_id3.elapsed is being updated in an ISR by
510 codec_pcmbuf_position_callback */
511 if (tracks
[cur_idx
].cuesheet_hid
>= 0 && !thistrack_id3
->cuesheet
)
513 bufread(tracks
[cur_idx
].cuesheet_hid
, sizeof(struct cuesheet
), curr_cue
);
514 othertrack_id3
->cuesheet
= curr_cue
;
515 cue_spoof_id3(othertrack_id3
->cuesheet
, othertrack_id3
);
517 return othertrack_id3
;
522 /* Codec may be using thistrack_id3, so it must not be overwritten.
523 If this is a manual skip, othertrack_id3 will become
524 thistrack_id3 in audio_check_new_track().
525 FIXME: If this is an automatic skip, it probably means multiple
526 short tracks fit in the PCM buffer. Overwriting othertrack_id3
527 can lead to an incorrect value later.
528 Note that othertrack_id3 may also be used for next track.
530 write_id3
= othertrack_id3
;
534 write_id3
= thistrack_id3
;
537 if (tracks
[cur_idx
].id3_hid
>= 0)
539 /* The current track's info has been buffered but not read yet, so get it */
540 if (bufread(tracks
[cur_idx
].id3_hid
, sizeof(struct mp3entry
), write_id3
)
541 == sizeof(struct mp3entry
))
545 /* We didn't find the ID3 metadata, so we fill temp_id3 with the little info
546 we have and return that. */
548 memset(write_id3
, 0, sizeof(struct mp3entry
));
550 playlist_get_track_info(NULL
, playlist_next(0)+wps_offset
, &trackinfo
);
551 filename
= trackinfo
.filename
;
553 filename
= "No file!";
555 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
556 if (tagcache_fill_tags(write_id3
, filename
))
560 strlcpy(write_id3
->path
, filename
, sizeof(write_id3
->path
));
561 write_id3
->title
= strrchr(write_id3
->path
, '/');
562 if (!write_id3
->title
)
563 write_id3
->title
= &write_id3
->path
[0];
570 struct mp3entry
* audio_next_track(void)
573 int offset
= ci
.new_track
+ wps_offset
;
575 if (!audio_have_tracks())
578 if (wps_offset
== -1 && *thistrack_id3
->path
)
580 /* We're in a track transition. The next track for the WPS is the one
581 currently being decoded. */
582 return thistrack_id3
;
585 next_idx
= (track_ridx
+ offset
+ 1) & MAX_TRACK_MASK
;
587 if (tracks
[next_idx
].id3_hid
>= 0)
589 if (bufread(tracks
[next_idx
].id3_hid
, sizeof(struct mp3entry
), othertrack_id3
)
590 == sizeof(struct mp3entry
))
591 return othertrack_id3
;
596 if (next_idx
== track_widx
)
598 /* The next track hasn't been buffered yet, so we return the static
599 version of its metadata. */
600 return &unbuffered_id3
;
607 int playback_current_aa_hid(int slot
)
612 int offset
= ci
.new_track
+ wps_offset
;
614 cur_idx
= track_ridx
+ offset
;
615 cur_idx
&= MAX_TRACK_MASK
;
617 return tracks
[cur_idx
].aa_hid
[slot
];
620 int playback_claim_aa_slot(struct dim
*dim
)
623 /* first try to find a slot already having the size to reuse it
624 * since we don't want albumart of the same size buffered multiple times */
627 struct albumart_slot
*slot
= &albumart_slots
[i
];
628 if (slot
->dim
.width
== dim
->width
629 && slot
->dim
.height
== dim
->height
)
635 /* size is new, find a free slot */
638 if (!albumart_slots
[i
].used
)
640 albumart_slots
[i
].used
++;
641 albumart_slots
[i
].dim
= *dim
;
645 /* sorry, no free slot */
649 void playback_release_aa_slot(int slot
)
651 /* invalidate the albumart_slot */
652 struct albumart_slot
*aa_slot
= &albumart_slots
[slot
];
653 if (aa_slot
->used
> 0)
658 void audio_play(long offset
)
662 #ifdef PLAYBACK_VOICE
663 /* Truncate any existing voice output so we don't have spelling
664 * etc. over the first part of the played track */
669 LOGFQUEUE("audio >| audio Q_AUDIO_PLAY: %ld", offset
);
670 /* Don't return until playback has actually started */
671 queue_send(&audio_queue
, Q_AUDIO_PLAY
, offset
);
674 void audio_stop(void)
677 LOGFQUEUE("audio >| audio Q_AUDIO_STOP");
678 /* Don't return until playback has actually stopped */
679 queue_send(&audio_queue
, Q_AUDIO_STOP
, 0);
682 void audio_pause(void)
684 LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE");
685 /* Don't return until playback has actually paused */
686 queue_send(&audio_queue
, Q_AUDIO_PAUSE
, true);
689 void audio_resume(void)
691 LOGFQUEUE("audio >| audio Q_AUDIO_PAUSE resume");
692 /* Don't return until playback has actually resumed */
693 queue_send(&audio_queue
, Q_AUDIO_PAUSE
, false);
696 void audio_skip(int direction
)
698 if (playlist_check(ci
.new_track
+ wps_offset
+ direction
))
700 if (global_settings
.beep
)
701 pcmbuf_beep(2000, 100, 2500*global_settings
.beep
);
703 LOGFQUEUE("audio > audio Q_AUDIO_SKIP %d", direction
);
704 queue_post(&audio_queue
, Q_AUDIO_SKIP
, direction
);
705 /* Update wps while our message travels inside deep playback queues. */
706 wps_offset
+= direction
;
710 /* No more tracks. */
711 if (global_settings
.beep
)
712 pcmbuf_beep(1000, 100, 1500*global_settings
.beep
);
716 void audio_next(void)
721 void audio_prev(void)
726 void audio_next_dir(void)
728 LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP 1");
729 queue_post(&audio_queue
, Q_AUDIO_DIR_SKIP
, 1);
732 void audio_prev_dir(void)
734 LOGFQUEUE("audio > audio Q_AUDIO_DIR_SKIP -1");
735 queue_post(&audio_queue
, Q_AUDIO_DIR_SKIP
, -1);
738 void audio_pre_ff_rewind(void)
740 LOGFQUEUE("audio > audio Q_AUDIO_PRE_FF_REWIND");
741 queue_post(&audio_queue
, Q_AUDIO_PRE_FF_REWIND
, 0);
744 void audio_ff_rewind(long newpos
)
746 LOGFQUEUE("audio > audio Q_AUDIO_FF_REWIND");
747 queue_post(&audio_queue
, Q_AUDIO_FF_REWIND
, newpos
);
750 void audio_flush_and_reload_tracks(void)
752 LOGFQUEUE("audio > audio Q_AUDIO_FLUSH");
753 queue_post(&audio_queue
, Q_AUDIO_FLUSH
, 0);
756 void audio_error_clear(void)
758 #ifdef AUDIO_HAVE_RECORDING
759 pcm_rec_error_clear();
763 int audio_status(void)
768 ret
|= AUDIO_STATUS_PLAY
;
771 ret
|= AUDIO_STATUS_PAUSE
;
773 #ifdef HAVE_RECORDING
774 /* Do this here for constitency with mpeg.c version */
775 /* FIXME: pcm_rec_status() is deprecated */
776 ret
|= pcm_rec_status();
782 int audio_get_file_pos(void)
787 #ifdef HAVE_DISK_STORAGE
788 void audio_set_buffer_margin(int setting
)
790 static const unsigned short lookup
[] = {5, 15, 30, 60, 120, 180, 300, 600};
791 buffer_margin
= lookup
[setting
];
792 logf("buffer margin: %ld", (long)buffer_margin
);
793 set_filebuf_watermark();
797 /* Take necessary steps to enable or disable the crossfade setting */
798 void audio_set_crossfade(int enable
)
804 /* Tell it the next setting to use */
805 pcmbuf_crossfade_enable(enable
);
807 /* Return if size hasn't changed or this is too early to determine
808 which in the second case there's no way we could be playing
810 if (pcmbuf_is_same_size())
812 /* This function is a copout and just syncs some variables -
813 to be removed at a later date */
814 pcmbuf_crossfade_enable_finished();
819 was_playing
= playing
;
821 /* Playback has to be stopped before changing the buffer size */
824 /* Store the track resume position */
825 offset
= thistrack_id3
->offset
;
828 /* Blast it - audio buffer will have to be setup again next time
830 audio_get_buffer(true, &size
);
832 /* Restart playback if audio was running previously */
837 /* --- Routines called from multiple threads --- */
839 static void set_filebuf_watermark(void)
842 return; /* Audio buffers not yet set up */
844 #ifdef HAVE_DISK_STORAGE
846 int spinup
= ata_spinup_time();
848 seconds
= (spinup
/ HZ
) + 1;
852 seconds
+= buffer_margin
;
858 /* bitrate of last track in buffer dictates watermark */
859 struct mp3entry
* id3
= NULL
;
860 if (tracks
[track_widx
].taginfo_ready
)
861 id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
863 id3
= bufgetid3(tracks
[track_widx
-1].id3_hid
);
865 logf("fwmark: No id3 for last track (r%d/w%d), aborting!", track_ridx
, track_widx
);
868 size_t bytes
= id3
->bitrate
* (1000/8) * seconds
;
869 buf_set_watermark(bytes
);
870 logf("fwmark: %d", bytes
);
873 /* --- Buffering callbacks --- */
875 static void buffering_low_buffer_callback(void *data
)
878 logf("low buffer callback");
880 if (filling
== STATE_FULL
|| filling
== STATE_END_OF_PLAYLIST
) {
882 LOGFQUEUE("buffering > audio Q_AUDIO_FILL_BUFFER");
883 queue_post(&audio_queue
, Q_AUDIO_FILL_BUFFER
, 0);
887 static void buffering_handle_rebuffer_callback(void *data
)
890 LOGFQUEUE("audio >| audio Q_AUDIO_FLUSH");
891 queue_post(&audio_queue
, Q_AUDIO_FLUSH
, 0);
894 static void buffering_handle_finished_callback(void *data
)
896 logf("handle %d finished buffering", *(int*)data
);
897 int hid
= (*(int*)data
);
899 if (hid
== tracks
[track_widx
].id3_hid
)
901 int offset
= ci
.new_track
+ wps_offset
;
902 int next_idx
= (track_ridx
+ offset
+ 1) & MAX_TRACK_MASK
;
903 /* The metadata handle for the last loaded track has been buffered.
904 We can ask the audio thread to load the rest of the track's data. */
905 LOGFQUEUE("audio >| audio Q_AUDIO_FINISH_LOAD");
906 queue_post(&audio_queue
, Q_AUDIO_FINISH_LOAD
, 0);
907 if (tracks
[next_idx
].id3_hid
== hid
)
908 send_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE
, NULL
);
912 /* This is most likely an audio handle, so we strip the useless
913 trailing tags that are left. */
916 if (hid
== tracks
[track_widx
-1].audio_hid
917 && filling
== STATE_END_OF_PLAYLIST
)
919 /* This was the last track in the playlist.
920 We now have all the data we need. */
921 logf("last track finished buffering");
922 filling
= STATE_FINISHED
;
928 /* --- Audio thread --- */
930 static bool audio_have_tracks(void)
932 return (audio_track_count() != 0);
935 static int audio_free_track_count(void)
937 /* Used tracks + free tracks adds up to MAX_TRACK - 1 */
938 return MAX_TRACK
- 1 - audio_track_count();
941 int audio_track_count(void)
943 /* Calculate difference from track_ridx to track_widx
944 * taking into account a possible wrap-around. */
945 return (MAX_TRACK
+ track_widx
- track_ridx
) & MAX_TRACK_MASK
;
948 long audio_filebufused(void)
950 return (long) buf_used();
953 /* Update track info after successful a codec track change */
954 static void audio_update_trackinfo(void)
956 /* Load the curent track's metadata into curtrack_id3 */
957 if (CUR_TI
->id3_hid
>= 0)
958 copy_mp3entry(thistrack_id3
, bufgetid3(CUR_TI
->id3_hid
));
960 /* Reset current position */
961 thistrack_id3
->elapsed
= 0;
962 thistrack_id3
->offset
= 0;
964 /* Update the codec API */
965 ci
.filesize
= CUR_TI
->filesize
;
966 ci
.id3
= thistrack_id3
;
968 ci
.taginfo_ready
= &CUR_TI
->taginfo_ready
;
971 /* Clear tracks between write and read, non inclusive */
972 static void audio_clear_track_entries(void)
974 int cur_idx
= track_widx
;
976 logf("Clearing tracks: r%d/w%d", track_ridx
, track_widx
);
978 /* Loop over all tracks from write-to-read */
981 cur_idx
= (cur_idx
+ 1) & MAX_TRACK_MASK
;
983 if (cur_idx
== track_ridx
)
986 clear_track_info(&tracks
[cur_idx
]);
990 /* Clear all tracks */
991 static bool audio_release_tracks(void)
995 logf("releasing all tracks");
997 for(i
= 0; i
< MAX_TRACK
; i
++)
999 cur_idx
= (track_ridx
+ i
) & MAX_TRACK_MASK
;
1000 if (!clear_track_info(&tracks
[cur_idx
]))
1007 static bool audio_loadcodec(bool start_play
)
1010 char codec_path
[MAX_PATH
]; /* Full path to codec */
1011 const struct mp3entry
*id3
, *prev_id3
;
1013 if (tracks
[track_widx
].id3_hid
< 0) {
1017 id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1021 const char *codec_fn
= get_codec_filename(id3
->codectype
);
1022 if (codec_fn
== NULL
)
1025 tracks
[track_widx
].codec_hid
= -1;
1029 /* Load the codec directly from disk and save some memory. */
1030 track_ridx
= track_widx
;
1031 ci
.filesize
= CUR_TI
->filesize
;
1032 ci
.id3
= thistrack_id3
;
1033 ci
.taginfo_ready
= &CUR_TI
->taginfo_ready
;
1035 LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK");
1036 queue_post(&codec_queue
, Q_CODEC_LOAD_DISK
, (intptr_t)codec_fn
);
1041 /* If we already have another track than this one buffered */
1042 if (track_widx
!= track_ridx
)
1044 prev_track
= (track_widx
- 1) & MAX_TRACK_MASK
;
1046 id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1047 prev_id3
= bufgetid3(tracks
[prev_track
].id3_hid
);
1049 /* If the previous codec is the same as this one, there is no need
1050 * to put another copy of it on the file buffer */
1051 if (id3
&& prev_id3
&&
1052 get_codec_base_type(id3
->codectype
) ==
1053 get_codec_base_type(prev_id3
->codectype
)
1054 && audio_codec_loaded
)
1056 logf("Reusing prev. codec");
1062 codec_get_full_path(codec_path
, codec_fn
);
1064 tracks
[track_widx
].codec_hid
= bufopen(codec_path
, 0, TYPE_CODEC
, NULL
);
1065 if (tracks
[track_widx
].codec_hid
< 0)
1068 logf("Loaded codec");
1073 /* Load metadata for the next track (with bufopen). The rest of the track
1074 loading will be handled by audio_finish_load_track once the metadata has been
1075 actually loaded by the buffering thread. */
1076 static bool audio_load_track(size_t offset
, bool start_play
)
1078 const char *trackname
;
1081 if (track_load_started
) {
1082 /* There is already a track load in progress, so track_widx hasn't been
1083 incremented yet. Loading another track would overwrite the one that
1084 hasn't finished loading. */
1085 logf("audio_load_track(): a track load is already in progress");
1089 start_play_g
= start_play
; /* will be read by audio_finish_load_track */
1091 /* Stop buffer filling if there is no free track entries.
1092 Don't fill up the last track entry (we wan't to store next track
1094 if (!audio_free_track_count())
1096 logf("No free tracks");
1101 tracks
[track_widx
].taginfo_ready
= false;
1103 logf("Buffering track: r%d/w%d", track_ridx
, track_widx
);
1104 /* Get track name from current playlist read position. */
1105 while ((trackname
= playlist_peek(last_peek_offset
)) != NULL
)
1107 /* Handle broken playlists. */
1108 fd
= open(trackname
, O_RDONLY
);
1111 logf("Open failed");
1112 /* Skip invalid entry from playlist. */
1113 playlist_skip_entry(NULL
, last_peek_offset
);
1121 logf("End-of-playlist");
1122 memset(&unbuffered_id3
, 0, sizeof(struct mp3entry
));
1123 filling
= STATE_END_OF_PLAYLIST
;
1125 if (thistrack_id3
->length
== 0 && thistrack_id3
->filesize
== 0)
1127 /* Stop playback if no valid track was found. */
1128 audio_stop_playback();
1134 tracks
[track_widx
].filesize
= filesize(fd
);
1136 if (offset
> tracks
[track_widx
].filesize
)
1139 /* Set default values */
1142 buf_set_watermark(filebuflen
/2);
1143 dsp_configure(ci
.dsp
, DSP_RESET
, 0);
1144 playlist_update_resume_info(audio_current_track());
1147 /* Get track metadata if we don't already have it. */
1148 if (tracks
[track_widx
].id3_hid
< 0)
1150 tracks
[track_widx
].id3_hid
= bufopen(trackname
, 0, TYPE_ID3
, NULL
);
1152 if (tracks
[track_widx
].id3_hid
< 0)
1154 /* Buffer is full. */
1155 get_metadata(&unbuffered_id3
, fd
, trackname
);
1158 logf("buffer is full for now (get metadata)");
1159 filling
= STATE_FULL
;
1163 if (track_widx
== track_ridx
)
1165 /* TODO: Superfluos buffering call? */
1166 buf_request_buffer_handle(tracks
[track_widx
].id3_hid
);
1167 struct mp3entry
*id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1170 copy_mp3entry(thistrack_id3
, id3
);
1171 thistrack_id3
->offset
= offset
;
1174 memset(thistrack_id3
, 0, sizeof(struct mp3entry
));
1179 playlist_update_resume_info(audio_current_track());
1184 track_load_started
= true; /* Remember that we've started loading a track */
1188 /* Second part of the track loading: We now have the metadata available, so we
1189 can load the codec, the album art and finally the audio data.
1190 This is called on the audio thread after the buffering thread calls the
1191 buffering_handle_finished_callback callback. */
1192 static void audio_finish_load_track(void)
1194 size_t file_offset
= 0;
1196 bool start_play
= start_play_g
;
1198 track_load_started
= false;
1200 if (tracks
[track_widx
].id3_hid
< 0) {
1201 logf("No metadata");
1205 struct mp3entry
*track_id3
;
1207 if (track_widx
== track_ridx
)
1208 track_id3
= thistrack_id3
;
1210 track_id3
= bufgetid3(tracks
[track_widx
].id3_hid
);
1212 if (track_id3
->length
== 0 && track_id3
->filesize
== 0)
1214 logf("audio_finish_load_track: invalid metadata");
1216 /* Invalid metadata */
1217 bufclose(tracks
[track_widx
].id3_hid
);
1218 tracks
[track_widx
].id3_hid
= -1;
1220 /* Skip invalid entry from playlist. */
1221 playlist_skip_entry(NULL
, last_peek_offset
--);
1223 /* load next track */
1224 LOGFQUEUE("audio > audio Q_AUDIO_FILL_BUFFER %d", (int)start_play
);
1225 queue_post(&audio_queue
, Q_AUDIO_FILL_BUFFER
, start_play
);
1229 /* Try to load a cuesheet for the track */
1232 char cuepath
[MAX_PATH
];
1233 if (look_for_cuesheet_file(track_id3
->path
, cuepath
))
1236 tracks
[track_widx
].cuesheet_hid
=
1237 bufalloc(NULL
, sizeof(struct cuesheet
), TYPE_CUESHEET
);
1238 if (tracks
[track_widx
].cuesheet_hid
>= 0)
1240 bufgetdata(tracks
[track_widx
].cuesheet_hid
,
1241 sizeof(struct cuesheet
), &temp
);
1242 struct cuesheet
*cuesheet
= (struct cuesheet
*)temp
;
1243 if (!parse_cuesheet(cuepath
, cuesheet
))
1245 bufclose(tracks
[track_widx
].cuesheet_hid
);
1246 track_id3
->cuesheet
= NULL
;
1251 #ifdef HAVE_ALBUMART
1254 char aa_path
[MAX_PATH
];
1257 /* albumart_slots may change during a yield of bufopen,
1258 * but that's no problem */
1259 if (tracks
[track_widx
].aa_hid
[i
] >= 0 || !albumart_slots
[i
].used
)
1261 /* find_albumart will error out if the wps doesn't have AA */
1262 if (find_albumart(track_id3
, aa_path
, sizeof(aa_path
),
1263 &(albumart_slots
[i
].dim
)))
1265 int aa_hid
= bufopen(aa_path
, 0, TYPE_BITMAP
,
1266 &(albumart_slots
[i
].dim
));
1268 if(aa_hid
== ERR_BUFFER_FULL
)
1270 filling
= STATE_FULL
;
1271 logf("buffer is full for now (get album art)");
1272 return; /* No space for track's album art, not an error */
1274 else if (aa_hid
< 0)
1276 /* another error, ignore AlbumArt */
1277 logf("Album art loading failed");
1279 tracks
[track_widx
].aa_hid
[i
] = aa_hid
;
1286 /* Load the codec. */
1287 if (!audio_loadcodec(start_play
))
1289 if (tracks
[track_widx
].codec_hid
== ERR_BUFFER_FULL
)
1291 /* No space for codec on buffer, not an error */
1292 filling
= STATE_FULL
;
1296 /* This is an error condition, either no codec was found, or reading
1297 * the codec file failed part way through, either way, skip the track */
1298 /* FIXME: We should not use splashf from audio thread! */
1299 splashf(HZ
*2, "No codec for: %s", track_id3
->path
);
1300 /* Skip invalid entry from playlist. */
1301 playlist_skip_entry(NULL
, last_peek_offset
);
1305 track_id3
->elapsed
= 0;
1306 offset
= track_id3
->offset
;
1308 enum data_type type
= TYPE_PACKET_AUDIO
;
1310 switch (track_id3
->codectype
) {
1315 file_offset
= offset
;
1316 track_id3
->offset
= offset
;
1322 file_offset
= offset
;
1323 track_id3
->offset
= offset
;
1324 track_id3
->elapsed
= track_id3
->length
/ 2;
1328 case AFMT_OGG_VORBIS
:
1338 track_id3
->offset
= offset
;
1344 logf("Loading atomic %d",track_id3
->codectype
);
1345 type
= TYPE_ATOMIC_AUDIO
;
1349 logf("load track: %s", track_id3
->path
);
1351 if (file_offset
> AUDIO_REBUFFER_GUESS_SIZE
)
1352 file_offset
-= AUDIO_REBUFFER_GUESS_SIZE
;
1353 else if (track_id3
->first_frame_offset
)
1354 file_offset
= track_id3
->first_frame_offset
;
1358 tracks
[track_widx
].audio_hid
= bufopen(track_id3
->path
, file_offset
, type
,
1361 /* No space left, not an error */
1362 if (tracks
[track_widx
].audio_hid
== ERR_BUFFER_FULL
)
1364 filling
= STATE_FULL
;
1365 logf("buffer is full for now (load audio)");
1368 else if (tracks
[track_widx
].audio_hid
< 0)
1370 /* another error, do not continue either */
1371 logf("Could not add audio data handle");
1375 /* All required data is now available for the codec. */
1376 tracks
[track_widx
].taginfo_ready
= true;
1380 ci
.curpos
=file_offset
;
1381 buf_request_buffer_handle(tracks
[track_widx
].audio_hid
);
1384 track_widx
= (track_widx
+ 1) & MAX_TRACK_MASK
;
1386 send_event(PLAYBACK_EVENT_TRACK_BUFFER
, track_id3
);
1388 /* load next track */
1389 LOGFQUEUE("audio > audio Q_AUDIO_FILL_BUFFER");
1390 queue_post(&audio_queue
, Q_AUDIO_FILL_BUFFER
, 0);
1395 static void audio_fill_file_buffer(bool start_play
, size_t offset
)
1397 trigger_cpu_boost();
1399 /* No need to rebuffer if there are track skips pending,
1400 * however don't cancel buffering on skipping while filling. */
1401 if (ci
.new_track
!= 0 && filling
!= STATE_FILLING
)
1403 filling
= STATE_FILLING
;
1405 /* Must reset the buffer before use if trashed or voice only - voice
1406 file size shouldn't have changed so we can go straight from
1407 AUDIOBUF_STATE_VOICED_ONLY to AUDIOBUF_STATE_INITIALIZED */
1408 if (buffer_state
!= AUDIOBUF_STATE_INITIALIZED
)
1409 audio_reset_buffer();
1411 logf("Starting buffer fill");
1414 audio_clear_track_entries();
1416 /* Save the current resume position once. */
1417 playlist_update_resume_info(audio_current_track());
1419 audio_load_track(offset
, start_play
);
1422 static void audio_rebuffer(void)
1424 logf("Forcing rebuffer");
1426 clear_track_info(CUR_TI
);
1428 /* Reset track pointers */
1429 track_widx
= track_ridx
;
1430 audio_clear_track_entries();
1432 /* Reset a possibly interrupted track load */
1433 track_load_started
= false;
1435 /* Fill the buffer */
1436 last_peek_offset
= -1;
1439 if (!CUR_TI
->taginfo_ready
)
1440 memset(thistrack_id3
, 0, sizeof(struct mp3entry
));
1442 audio_fill_file_buffer(false, 0);
1445 /* Called on request from the codec to get a new track. This is the codec part
1446 of the track transition. */
1447 static int audio_check_new_track(void)
1449 int track_count
= audio_track_count();
1450 int old_track_ridx
= track_ridx
;
1453 struct mp3entry
*temp
= thistrack_id3
;
1455 /* Now it's good time to send track finish events. */
1456 send_event(PLAYBACK_EVENT_TRACK_FINISH
, thistrack_id3
);
1457 /* swap the mp3entry pointers */
1458 thistrack_id3
= othertrack_id3
;
1459 othertrack_id3
= temp
;
1460 ci
.id3
= thistrack_id3
;
1461 memset(thistrack_id3
, 0, sizeof(struct mp3entry
));
1466 /* regardless of the return value we need to rebuffer.
1467 if it fails the old playlist will resume, else the
1468 next dir will start playing */
1469 playlist_next_dir(ci
.new_track
);
1478 /* If the playlist isn't that big */
1481 while (!playlist_check(ci
.new_track
))
1483 if (ci
.new_track
>= 0)
1485 LOGFQUEUE("audio >|= codec Q_CODEC_REQUEST_FAILED");
1486 return Q_CODEC_REQUEST_FAILED
;
1492 /* Update the playlist */
1493 last_peek_offset
-= ci
.new_track
;
1495 if (playlist_next(ci
.new_track
) < 0)
1497 LOGFQUEUE("audio >|= codec Q_CODEC_REQUEST_FAILED");
1498 return Q_CODEC_REQUEST_FAILED
;
1504 new_playlist
= false;
1507 /* Save a pointer to the old track to allow later clearing */
1510 for (i
= 0; i
< ci
.new_track
; i
++)
1512 idx
= (track_ridx
+ i
) & MAX_TRACK_MASK
;
1513 struct mp3entry
*id3
= bufgetid3(tracks
[idx
].id3_hid
);
1514 ssize_t offset
= buf_handle_offset(tracks
[idx
].audio_hid
);
1515 if (!id3
|| offset
< 0 || (unsigned)offset
> id3
->first_frame_offset
)
1517 /* We don't have all the audio data for that track, so clear it,
1518 but keep the metadata. */
1519 if (tracks
[idx
].audio_hid
>= 0 && bufclose(tracks
[idx
].audio_hid
))
1521 tracks
[idx
].audio_hid
= -1;
1522 tracks
[idx
].filesize
= 0;
1527 /* Move to the new track */
1528 track_ridx
= (track_ridx
+ ci
.new_track
) & MAX_TRACK_MASK
;
1530 buf_set_base_handle(CUR_TI
->audio_hid
);
1534 wps_offset
= -ci
.new_track
;
1537 /* If it is not safe to even skip this many track entries */
1538 if (ci
.new_track
>= track_count
|| ci
.new_track
<= track_count
- MAX_TRACK
)
1545 forward
= ci
.new_track
> 0;
1548 /* If the target track is clearly not in memory */
1549 if (CUR_TI
->filesize
== 0 || !CUR_TI
->taginfo_ready
)
1555 /* When skipping backwards, it is possible that we've found a track that's
1556 * buffered, but which is around the track-wrap and therefore not the track
1557 * we are looking for */
1560 int cur_idx
= track_ridx
;
1561 bool taginfo_ready
= true;
1562 /* We've wrapped the buffer backwards if new > old */
1563 bool wrap
= track_ridx
> old_track_ridx
;
1567 cur_idx
= (cur_idx
+ 1) & MAX_TRACK_MASK
;
1569 /* if we've advanced past the wrap when cur_idx is zeroed */
1573 /* if we aren't still on the wrap and we've caught the old track */
1574 if (!(wrap
|| cur_idx
< old_track_ridx
))
1577 /* If we hit a track in between without valid tag info, bail */
1578 if (!tracks
[cur_idx
].taginfo_ready
)
1580 taginfo_ready
= false;
1591 audio_update_trackinfo();
1592 LOGFQUEUE("audio >|= codec Q_CODEC_REQUEST_COMPLETE");
1593 return Q_CODEC_REQUEST_COMPLETE
;
1596 unsigned long audio_prev_elapsed(void)
1598 return prev_track_elapsed
;
1601 void audio_set_prev_elapsed(unsigned long setting
)
1603 prev_track_elapsed
= setting
;
1606 static void audio_stop_codec_flush(void)
1608 ci
.stop_codec
= true;
1611 while (audio_codec_loaded
)
1614 /* If the audio codec is not loaded any more, and the audio is still
1615 * playing, it is now and _only_ now safe to call this function from the
1617 if (pcm_is_playing())
1620 pcmbuf_queue_clear();
1622 pcmbuf_pause(paused
);
1625 static void audio_stop_playback(void)
1629 /* If we were playing, save resume information */
1630 struct mp3entry
*id3
= NULL
;
1634 /* Set this early, the outside code yields and may allow the codec
1635 to try to wait for a reply on a buffer wait */
1636 ci
.stop_codec
= true;
1637 id3
= audio_current_track();
1640 /* Save the current playing spot, or NULL if the playlist has ended */
1641 playlist_update_resume_info(id3
);
1643 /* TODO: Create auto bookmark too? */
1645 prev_track_elapsed
= othertrack_id3
->elapsed
;
1647 remove_event(BUFFER_EVENT_BUFFER_LOW
, buffering_low_buffer_callback
);
1650 audio_stop_codec_flush();
1653 track_load_started
= false;
1655 filling
= STATE_IDLE
;
1657 /* Mark all entries null. */
1658 audio_clear_track_entries();
1660 /* Close all tracks */
1661 audio_release_tracks();
1664 static void audio_play_start(size_t offset
)
1668 #if INPUT_SRC_CAPS != 0
1669 audio_set_input_source(AUDIO_SRC_PLAYBACK
, SRCF_PLAYBACK
);
1670 audio_set_output_source(AUDIO_SRC_PLAYBACK
);
1673 /* Wait for any previously playing audio to flush - TODO: Not necessary? */
1675 audio_stop_codec_flush();
1678 track_load_started
= false;
1684 sound_set_volume(global_settings
.volume
);
1685 track_widx
= track_ridx
= 0;
1687 /* Clear all track entries. */
1688 for (i
= 0; i
< MAX_TRACK
; i
++) {
1689 clear_track_info(&tracks
[i
]);
1692 last_peek_offset
= -1;
1694 /* Officially playing */
1695 queue_reply(&audio_queue
, 1);
1697 audio_fill_file_buffer(true, offset
);
1699 add_event(BUFFER_EVENT_BUFFER_LOW
, false, buffering_low_buffer_callback
);
1701 LOGFQUEUE("audio > audio Q_AUDIO_TRACK_CHANGED");
1702 queue_post(&audio_queue
, Q_AUDIO_TRACK_CHANGED
, 0);
1706 /* Invalidates all but currently playing track. */
1707 static void audio_invalidate_tracks(void)
1709 if (audio_have_tracks())
1711 last_peek_offset
= 0;
1712 track_widx
= track_ridx
;
1714 /* Mark all other entries null (also buffered wrong metadata). */
1715 audio_clear_track_entries();
1717 track_widx
= (track_widx
+ 1) & MAX_TRACK_MASK
;
1719 audio_fill_file_buffer(false, 0);
1723 static void audio_new_playlist(void)
1725 /* Prepare to start a new fill from the beginning of the playlist */
1726 last_peek_offset
= -1;
1727 if (audio_have_tracks())
1730 skipped_during_pause
= true;
1731 track_widx
= track_ridx
;
1732 audio_clear_track_entries();
1734 track_widx
= (track_widx
+ 1) & MAX_TRACK_MASK
;
1736 /* Mark the current track as invalid to prevent skipping back to it */
1737 CUR_TI
->taginfo_ready
= false;
1740 /* Signal the codec to initiate a track change forward */
1741 new_playlist
= true;
1744 /* Officially playing */
1745 queue_reply(&audio_queue
, 1);
1747 audio_fill_file_buffer(false, 0);
1750 /* Called on manual track skip */
1751 static void audio_initiate_track_change(long direction
)
1753 logf("audio_initiate_track_change(%ld)", direction
);
1755 ci
.new_track
+= direction
;
1756 wps_offset
-= direction
;
1758 skipped_during_pause
= true;
1761 /* Called on manual dir skip */
1762 static void audio_initiate_dir_change(long direction
)
1765 ci
.new_track
= direction
;
1767 skipped_during_pause
= true;
1770 /* Called when PCM track change is complete */
1771 static void audio_finalise_track_change(void)
1773 logf("audio_finalise_track_change");
1778 automatic_skip
= false;
1780 /* Invalidate prevtrack_id3 */
1781 memset(othertrack_id3
, 0, sizeof(struct mp3entry
));
1783 if (prev_ti
&& prev_ti
->audio_hid
< 0)
1785 /* No audio left so we clear all the track info. */
1786 clear_track_info(prev_ti
);
1789 send_event(PLAYBACK_EVENT_TRACK_CHANGE
, thistrack_id3
);
1790 playlist_update_resume_info(audio_current_track());
1794 * Layout audio buffer as follows - iram buffer depends on target:
1795 * [|SWAP:iram][|TALK]|FILE|GUARD|PCM|[SWAP:dram[|iram]|]
1797 static void audio_reset_buffer(void)
1799 /* see audio_get_recording_buffer if this is modified */
1800 logf("audio_reset_buffer");
1802 /* If the setup of anything allocated before the file buffer is
1803 changed, do check the adjustments after the buffer_alloc call
1804 as it will likely be affected and need sliding over */
1806 /* Initially set up file buffer as all space available */
1807 malloc_buf
= audiobuf
+ talk_get_bufsize();
1808 /* Align the malloc buf to line size. Especially important to cf
1809 targets that do line reads/writes. */
1810 malloc_buf
= (unsigned char *)(((uintptr_t)malloc_buf
+ 15) & ~15);
1811 filebuf
= malloc_buf
; /* filebuf line align implied */
1812 filebuflen
= audiobufend
- filebuf
;
1816 /* Subtract whatever the pcm buffer says it used plus the guard buffer */
1817 const size_t pcmbuf_size
= pcmbuf_init(filebuf
+ filebuflen
) +GUARD_BUFSIZE
;
1820 if(pcmbuf_size
> filebuflen
)
1821 panicf("Not enough memory for pcmbuf_init() : %d > %d",
1822 (int)pcmbuf_size
, (int)filebuflen
);
1825 filebuflen
-= pcmbuf_size
;
1827 /* Make sure filebuflen is a longword multiple after adjustment - filebuf
1828 will already be line aligned */
1831 buffering_reset(filebuf
, filebuflen
);
1833 /* Clear any references to the file buffer */
1834 buffer_state
= AUDIOBUF_STATE_INITIALIZED
;
1836 #if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
1837 /* Make sure everything adds up - yes, some info is a bit redundant but
1838 aids viewing and the sumation of certain variables should add up to
1839 the location of others. */
1842 const unsigned char *pcmbuf
= pcmbuf_get_meminfo(&pcmbufsize
);
1843 logf("mabuf: %08X", (unsigned)malloc_buf
);
1844 logf("fbuf: %08X", (unsigned)filebuf
);
1845 logf("fbufe: %08X", (unsigned)(filebuf
+ filebuflen
));
1846 logf("gbuf: %08X", (unsigned)(filebuf
+ filebuflen
));
1847 logf("gbufe: %08X", (unsigned)(filebuf
+ filebuflen
+ GUARD_BUFSIZE
));
1848 logf("pcmb: %08X", (unsigned)pcmbuf
);
1849 logf("pcmbe: %08X", (unsigned)(pcmbuf
+ pcmbufsize
));
1854 static void audio_thread(void)
1856 struct queue_event ev
;
1860 audio_thread_ready
= true;
1864 if (filling
!= STATE_FILLING
&& filling
!= STATE_IDLE
) {
1865 /* End of buffering, let's calculate the watermark and unboost */
1866 set_filebuf_watermark();
1870 if (!pcmbuf_queue_scan(&ev
))
1871 queue_wait_w_tmo(&audio_queue
, &ev
, HZ
/2);
1875 case Q_AUDIO_FILL_BUFFER
:
1876 LOGFQUEUE("audio < Q_AUDIO_FILL_BUFFER %d", (int)ev
.data
);
1877 audio_fill_file_buffer((bool)ev
.data
, 0);
1880 case Q_AUDIO_FINISH_LOAD
:
1881 LOGFQUEUE("audio < Q_AUDIO_FINISH_LOAD");
1882 audio_finish_load_track();
1886 LOGFQUEUE("audio < Q_AUDIO_PLAY");
1887 if (playing
&& ev
.data
<= 0)
1888 audio_new_playlist();
1891 audio_stop_playback();
1892 audio_play_start((size_t)ev
.data
);
1897 LOGFQUEUE("audio < Q_AUDIO_STOP");
1899 audio_stop_playback();
1901 queue_clear(&audio_queue
);
1905 LOGFQUEUE("audio < Q_AUDIO_PAUSE");
1906 if (!(bool) ev
.data
&& skipped_during_pause
&& !pcmbuf_is_crossfade_active())
1907 pcmbuf_play_stop(); /* Flush old track on resume after skip */
1908 skipped_during_pause
= false;
1911 pcmbuf_pause((bool)ev
.data
);
1912 paused
= (bool)ev
.data
;
1916 LOGFQUEUE("audio < Q_AUDIO_SKIP");
1917 audio_initiate_track_change((long)ev
.data
);
1920 case Q_AUDIO_PRE_FF_REWIND
:
1921 LOGFQUEUE("audio < Q_AUDIO_PRE_FF_REWIND");
1927 case Q_AUDIO_FF_REWIND
:
1928 LOGFQUEUE("audio < Q_AUDIO_FF_REWIND");
1933 /* An automatic track skip is in progress. Finalize it,
1934 then go back to the previous track */
1935 audio_finalise_track_change();
1938 ci
.seek_time
= (long)ev
.data
+1;
1941 case Q_AUDIO_CHECK_NEW_TRACK
:
1942 LOGFQUEUE("audio < Q_AUDIO_CHECK_NEW_TRACK");
1943 queue_reply(&audio_queue
, audio_check_new_track());
1946 case Q_AUDIO_DIR_SKIP
:
1947 LOGFQUEUE("audio < Q_AUDIO_DIR_SKIP");
1948 audio_initiate_dir_change(ev
.data
);
1952 LOGFQUEUE("audio < Q_AUDIO_FLUSH");
1953 audio_invalidate_tracks();
1956 case Q_AUDIO_TRACK_CHANGED
:
1957 /* PCM track change done */
1958 LOGFQUEUE("audio < Q_AUDIO_TRACK_CHANGED");
1959 audio_finalise_track_change();
1962 case SYS_USB_CONNECTED
:
1963 LOGFQUEUE("audio < SYS_USB_CONNECTED");
1965 audio_stop_playback();
1966 #ifdef PLAYBACK_VOICE
1969 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
1970 usb_wait_for_disconnect(&audio_queue
);
1972 /* Mark all entries null. */
1973 audio_clear_track_entries();
1975 /* release tracks to make sure all handles are closed */
1976 audio_release_tracks();
1981 LOGFQUEUE_SYS_TIMEOUT("audio < SYS_TIMEOUT");
1985 /* LOGFQUEUE("audio < default : %08lX", ev.id); */
1991 /* Initialize the audio system - called from init() in main.c.
1992 * Last function because of all the references to internal symbols
1994 void audio_init(void)
1996 unsigned int audio_thread_id
;
1998 /* Can never do this twice */
1999 if (audio_is_initialized
)
2001 logf("audio: already initialized");
2005 logf("audio: initializing");
2007 /* Initialize queues before giving control elsewhere in case it likes
2008 to send messages. Thread creation will be delayed however so nothing
2009 starts running until ready if something yields such as talk_init. */
2010 queue_init(&audio_queue
, true);
2011 queue_init(&codec_queue
, false);
2012 queue_init(&pcmbuf_queue
, false);
2016 codec_init_codec_api();
2018 thistrack_id3
= &mp3entry_buf
[0];
2019 othertrack_id3
= &mp3entry_buf
[1];
2021 /* cuesheet support */
2022 if (global_settings
.cuesheet
)
2023 curr_cue
= (struct cuesheet
*)buffer_alloc(sizeof(struct cuesheet
));
2025 /* initialize the buffer */
2028 /* audio_reset_buffer must to know the size of voice buffer so init
2032 make_codec_thread();
2034 audio_thread_id
= create_thread(audio_thread
, audio_stack
,
2035 sizeof(audio_stack
), CREATE_THREAD_FROZEN
,
2036 audio_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
)
2039 queue_enable_queue_send(&audio_queue
, &audio_queue_sender_list
,
2042 #ifdef PLAYBACK_VOICE
2043 voice_thread_init();
2046 /* Set crossfade setting for next buffer init which should be about... */
2047 pcmbuf_crossfade_enable(global_settings
.crossfade
);
2049 /* initialize the buffering system */
2052 /* ...now! Set up the buffers */
2053 audio_reset_buffer();
2056 for(i
= 0; i
< MAX_TRACK
; i
++)
2058 tracks
[i
].audio_hid
= -1;
2059 tracks
[i
].id3_hid
= -1;
2060 tracks
[i
].codec_hid
= -1;
2061 tracks
[i
].cuesheet_hid
= -1;
2063 #ifdef HAVE_ALBUMART
2067 for (j
= 0; j
< MAX_TRACK
; j
++)
2069 tracks
[j
].aa_hid
[i
] = -1;
2074 add_event(BUFFER_EVENT_REBUFFER
, false, buffering_handle_rebuffer_callback
);
2075 add_event(BUFFER_EVENT_FINISHED
, false, buffering_handle_finished_callback
);
2077 /* Probably safe to say */
2078 audio_is_initialized
= true;
2080 sound_settings_apply();
2081 #ifdef HAVE_DISK_STORAGE
2082 audio_set_buffer_margin(global_settings
.buffer_margin
);
2085 /* it's safe to let the threads run now */
2086 #ifdef PLAYBACK_VOICE
2087 voice_thread_resume();
2089 thread_thaw(codec_thread_id
);
2090 thread_thaw(audio_thread_id
);
2094 bool audio_is_thread_ready(void)
2096 return audio_thread_ready
;
2099 size_t audio_get_filebuflen(void)
2106 return CUR_TI
->audio_hid
;
2109 int *get_codec_hid()
2111 return &tracks
[track_ridx
].codec_hid
;
2114 bool audio_is_playing(void)
2119 bool audio_is_paused(void)