//- commands internal void decoder_command(Decoder *decoder, DecoderCommandKind kind, u32 track_key, u64 frame) { u32 write = decoder->command_write; while(write - AtomicLoad(&decoder->command_read) >= DECODER_COMMAND_CAPACITY) { sleep_ns(1000000); // decoder is behind by 32 commands, practically never } DecoderCommand *command = &decoder->commands[write % DECODER_COMMAND_CAPACITY]; command->kind = (u32)kind; command->track_key = track_key; command->frame = frame; AtomicStore(&decoder->command_write, write + 1); signal_notify(&decoder->wake); } internal b32 decoder_has_command(Decoder *decoder) { return AtomicLoad(&decoder->command_write) != decoder->command_read || AtomicLoadRelaxed(&decoder->quit); } internal void decoder_set_status(Decoder *decoder, DecoderStatus status) { AtomicStoreRelaxed(&decoder->status_key, decoder->key); if(AtomicExchange(&decoder->status, (u32)status) != (u32)status) { wake_main(); } } //- reader // NOTE: mpg123 calls these on the decoder thread #define READER_POLL_NS 20000000ll // re-check for commands every 20ms while starved internal int reader_read(void *handle, void *buffer, size_t count, size_t *read_count) { TrackReader *reader = (TrackReader *)handle; TrackEntry *entry = reader->entry; TrackStore *store = reader->store; *read_count = 0; for(;;) { u32 snapshot = signal_snapshot(&reader->store->progress); u32 state = AtomicLoad(TrackStatePtr(store, entry)); u64 available = AtomicLoad(&entry->bytes_done); b32 complete = (state == TRACK_DONE); if(reader->map) { // NOTE: finished file, mapped: a plain copy out of the page cache u64 left = (reader->offset < (s64)reader->map_size) ? reader->map_size - (u64)reader->offset : 0; u64 take = Min((u64)count, left); MemCpy(buffer, reader->map + reader->offset, take); reader->offset += (s64)take; // NOTE: touched pages of a mapping count as our rss until unmapped, so a seek to the end // of a 60MB surah would show 60MB "used". every MB of movement, drop everything outside // a 2MB window around the read position (clean page cache, refaulted for free on reuse) if(reader->offset - reader->map_trimmed_at >= (s64)MiB(1) || reader->map_trimmed_at - reader->offset >= (s64)MiB(1)) { u64 page = (u64)getpagesize(); // 16K on apple silicon u64 start = (u64)Max(reader->offset - (s64)MiB(1), (s64)0) & ~(page - 1); u64 end = Min(((u64)reader->offset + MiB(1) + page - 1) & ~(page - 1), (reader->map_size + page - 1) & ~(page - 1)); if(start > 0) madvise(reader->map, start, MADV_DONTNEED); if(end < reader->map_size) madvise(reader->map + end, ((reader->map_size + page - 1) & ~(page - 1)) - end, MADV_DONTNEED); reader->map_trimmed_at = reader->offset; } *read_count = (size_t)take; return 0; } if((u64)reader->offset < available || complete) { u64 want = complete ? (u64)count : Min((u64)count, available - (u64)reader->offset); ssize_t got = pread(reader->fd, buffer, want, reader->offset); if(got < 0) { if(errno == EINTR) continue; return -1; } reader->offset += got; *read_count = (size_t)got; return 0; } if(state == TRACK_FAILED) { return -1; } if(state == TRACK_REMOTE || state == TRACK_PARTIAL) { store_request(reader->store, reader->key, true); } if(decoder_has_command(reader->decoder)) { reader->interrupted = true; return -1; } decoder_set_status(reader->decoder, DECODER_BUFFERING); signal_wait(&reader->store->progress, snapshot, READER_POLL_NS); } } internal int64_t reader_lseek(void *handle, int64_t offset, int whence) { TrackReader *reader = (TrackReader *)handle; s64 target = -1; switch(whence) { case SEEK_SET: target = offset; break; case SEEK_CUR: target = reader->offset + offset; break; case SEEK_END: { if(reader->map) { target = (s64)reader->map_size + offset; break; } // NOTE: needs the content length, which arrives with the first response headers for(;;) { u32 snapshot = signal_snapshot(&reader->store->progress); u64 total = AtomicLoad(&reader->entry->bytes_total); u32 state = AtomicLoad(TrackStatePtr(reader->store, reader->entry)); if(total == 0 && state == TRACK_DONE) { struct stat info; if(fstat(reader->fd, &info) == 0) total = (u64)info.st_size; } if(total > 0) { target = (s64)total + offset; break; } if(state == TRACK_FAILED || decoder_has_command(reader->decoder)) { reader->interrupted = decoder_has_command(reader->decoder); return -1; } signal_wait(&reader->store->progress, snapshot, READER_POLL_NS); } } break; } if(target < 0) { return -1; } // NOTE: we pread at the offset, the fd's own position is never used reader->offset = target; return target; } // NOTE: the file is either the finished .mp3 or the .part that is still growing. a download can // finish and rename between our check and the open, so loop until one of them opens. internal int reader_open_file(Decoder *decoder, u32 key) { TrackStore *store = decoder->store; TrackEntry *entry = store_entry(store, key); char path[1024]; for(;;) { u32 snapshot = signal_snapshot(&store->progress); u32 state = AtomicLoad(TrackStatePtr(store, entry)); store_track_path(store, key, state != TRACK_DONE, path, sizeof(path)); int fd = open(path, O_RDONLY | O_CLOEXEC); if(fd >= 0) { return fd; } if(state == TRACK_DONE) { // NOTE: someone deleted the file behind our back, fetch it again store_set_state(store, key, TRACK_REMOTE); store_request(store, key, true); } if(state == TRACK_FAILED || decoder_has_command(decoder)) { return -1; } decoder_set_status(decoder, DECODER_BUFFERING); signal_wait(&store->progress, snapshot, READER_POLL_NS); } } //- tracks internal u32 decoder_next_key(Decoder *decoder, u32 key); internal void decoder_close(Decoder *decoder) { if(decoder->open) { mpg123_close(decoder->handle); decoder->open = false; } if(decoder->reader.map) { munmap(decoder->reader.map, decoder->reader.map_size); decoder->reader.map = NULL; decoder->reader.map_size = 0; } if(decoder->reader.fd >= 0) { close(decoder->reader.fd); decoder->reader.fd = -1; } decoder->decoding = false; } internal void decoder_wait_for_drain(Decoder *decoder) { // NOTE: before changing the stream rate every queued sample at the old rate has to play out u64 deadline = time_now_ns() + 1500000000ull; while(audio_ring_fill(decoder->ring) > 0 && time_now_ns() < deadline && !decoder_has_command(decoder)) { sleep_ns(5000000); } } internal void decoder_push_marker(Decoder *decoder, AudioMarkerKind kind, u64 track_frame) { AudioMarker marker = {}; marker.kind = (u32)kind; marker.ring_frame = decoder->ring->write_frame; marker.track_key = decoder->key; marker.track_frame = track_frame; marker.track_total_frames = decoder->total_frames; marker.rate = decoder->rate; while(!audio_ring_push_marker(decoder->ring, marker)) { sleep_ns(2000000); } } internal b32 decoder_open(Decoder *decoder, u32 key, b32 gapless) { decoder_close(decoder); decoder->key = key; decoder_set_status(decoder, DECODER_BUFFERING); TrackStore *store = decoder->store; TrackEntry *entry = store_entry(store, key); if(!entry) { decoder_set_status(decoder, DECODER_ERROR); return false; } store_request(store, key, true); int fd = reader_open_file(decoder, key); if(fd < 0) { decoder_set_status(decoder, decoder_has_command(decoder) ? DECODER_IDLE : DECODER_ERROR); return false; } TrackReader *reader = &decoder->reader; reader->decoder = decoder; reader->store = store; reader->entry = entry; reader->fd = fd; reader->key = key; reader->offset = 0; reader->interrupted = false; reader->map = NULL; reader->map_size = 0; reader->map_trimmed_at = 0; // NOTE: a finished file is mapped once, read sequentially struct stat file_info; if(AtomicLoad(TrackStatePtr(store, entry)) == TRACK_DONE && fstat(fd, &file_info) == 0 && file_info.st_size > 0) { void *map = mmap(NULL, (size_t)file_info.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if(map != MAP_FAILED) { madvise(map, (size_t)file_info.st_size, MADV_SEQUENTIAL); reader->map = (u8 *)map; reader->map_size = (u64)file_info.st_size; } } // NOTE: by default mpg123 peeks at the last 128 bytes for an id3v1 tag. on a file that is // still downloading that read would park us until the whole file arrived, so don't. b32 complete = (AtomicLoad(TrackStatePtr(store, entry)) == TRACK_DONE); mpg123_param2(decoder->handle, complete ? MPG123_REMOVE_FLAGS : MPG123_ADD_FLAGS, MPG123_NO_PEEK_END, 0.0); if(mpg123_open_handle64(decoder->handle, reader) != MPG123_OK) { Log("mpg123 open failed: %s", mpg123_strerror(decoder->handle)); decoder_close(decoder); decoder_set_status(decoder, DECODER_ERROR); return false; } decoder->open = true; // NOTE: mpg123_scan reads the whole file for an exact length. measured on this machine: 136ms // before the first sample of a 114MB surah, 0.1ms without. seeking stays sample accurate // either way (mpg123 indexes frames as it goes), so only small files get scanned (<= ~15ms), // where the header's length estimate would be off by the largest share. big files start // instantly with a length that is off by a tenth of a second or so. u64 file_size = AtomicLoad(&entry->bytes_total); if(AtomicLoad(TrackStatePtr(store, entry)) == TRACK_DONE && file_size && file_size < MiB(16)) { mpg123_scan(decoder->handle); } else if(file_size > 0) { mpg123_set_filesize64(decoder->handle, (s64)AtomicLoad(&entry->bytes_total)); } long rate = 0; int channels = 0; int encoding = 0; if(mpg123_getformat(decoder->handle, &rate, &channels, &encoding) != MPG123_OK) { b32 interrupted = reader->interrupted; decoder_close(decoder); decoder_set_status(decoder, interrupted ? DECODER_IDLE : DECODER_ERROR); return false; } s64 length = mpg123_length64(decoder->handle); decoder->total_frames = (length > 0) ? (u64)length : 0; decoder->rate = (u32)rate; decoder->decoder_name = mpg123_current_decoder(decoder->handle); AtomicStore(&decoder->published_total, decoder->total_frames); if(decoder->rate != decoder->output->rate) { if(gapless) { decoder_wait_for_drain(decoder); } audio_output_set_rate(decoder->output, decoder->rate); } // NOTE: line up whatever plays next while this one plays, so a streamed next track is ready u32 next = decoder_next_key(decoder, key); if(next != TRACK_NONE && (decoder->queue_count || decoder->autoplay == AUTOPLAY_NEXT)) { store_request(store, next, false); } decoder->decoding = true; decoder_set_status(decoder, DECODER_DECODING); return true; } //- commands internal void decoder_play(Decoder *decoder, u32 key, u64 frame) { audio_ring_flush(decoder->ring); if(!decoder_open(decoder, key, false)) { decoder_push_marker(decoder, AUDIO_MARKER_END_OF_STREAM, 0); return; } u64 start = 0; if(frame > 0) { s64 actual = mpg123_seek64(decoder->handle, (s64)frame, SEEK_SET); start = (actual > 0) ? (u64)actual : 0; } // NOTE: the open may have taken a while (network), flush again so nothing old sneaks in audio_ring_flush(decoder->ring); decoder_push_marker(decoder, AUDIO_MARKER_TRACK_START, start); } internal void decoder_seek(Decoder *decoder, u64 frame) { if(!decoder->open) { return; } if(decoder->total_frames > 0) { frame = Min(frame, decoder->total_frames - 1); } s64 actual = mpg123_seek64(decoder->handle, (s64)frame, SEEK_SET); if(actual < 0) { Log("seek failed: %s", mpg123_strerror(decoder->handle)); return; } audio_ring_flush(decoder->ring); decoder->decoding = true; decoder_push_marker(decoder, AUDIO_MARKER_TRACK_START, (u64)actual); decoder_set_status(decoder, DECODER_DECODING); } internal void decoder_stop(Decoder *decoder) { decoder_close(decoder); audio_ring_flush(decoder->ring); decoder->key = TRACK_NONE; decoder_push_marker(decoder, AUDIO_MARKER_END_OF_STREAM, 0); decoder_set_status(decoder, DECODER_IDLE); } internal void decoder_process_commands(Decoder *decoder) { u32 write = AtomicLoad(&decoder->command_write); u32 read = decoder->command_read; if(read == write) { return; } // NOTE: coalesce. only the last play/stop matters, and only the last seek after it. scrubbing // the progress bar sends dozens of seeks, we do one. u32 last_play = write; u32 last_seek = write; for(u32 index = read; index != write; ++index) { DecoderCommand *command = &decoder->commands[index % DECODER_COMMAND_CAPACITY]; if(command->kind == DECODER_PLAY || command->kind == DECODER_STOP) { last_play = index; last_seek = write; } else if(command->kind == DECODER_SEEK) { last_seek = index; } else if(command->kind == DECODER_SET_AUTOPLAY) { decoder->autoplay = (u32)command->frame; } else if(command->kind == DECODER_QUEUE_CLEAR) { decoder->queue_count = 0; } else if(command->kind == DECODER_QUEUE_ADD && decoder->queue_count < DECODER_QUEUE_CAPACITY) { decoder->queue[decoder->queue_count++] = command->track_key; } } DecoderCommand play = {}; DecoderCommand seek = {}; if(last_play != write) play = decoder->commands[last_play % DECODER_COMMAND_CAPACITY]; if(last_seek != write) seek = decoder->commands[last_seek % DECODER_COMMAND_CAPACITY]; AtomicStore(&decoder->command_read, write); if(play.kind == DECODER_PLAY) { // NOTE: fold a following seek into the play so we don't decode the start first decoder_play(decoder, play.track_key, seek.kind ? seek.frame : play.frame); return; } if(play.kind == DECODER_STOP) { decoder_stop(decoder); } if(seek.kind == DECODER_SEEK) { decoder_seek(decoder, seek.frame); } } //- thread internal u32 decoder_next_key(Decoder *decoder, u32 key) { // NOTE: the queue wins while the playing track is part of it, and ends playback at its end for(u32 index = 0; index < decoder->queue_count; ++index) { if(decoder->queue[index] == key) { return (index + 1 < decoder->queue_count) ? decoder->queue[index + 1] : TRACK_NONE; } } if(decoder->autoplay == AUTOPLAY_NEXT) { return (TrackSurah(key) + 1 < SURAH_COUNT) ? key + 1 : TRACK_NONE; } if(decoder->autoplay == AUTOPLAY_NEXT_DOWNLOADED) { for(u32 surah = TrackSurah(key) + 1; surah < SURAH_COUNT; ++surah) { if(store_on_disk(decoder->store, TrackKey(TrackQari(key), surah))) { return TrackKey(TrackQari(key), surah); } } } return TRACK_NONE; } internal void decoder_end_of_track(Decoder *decoder) { u32 next = decoder_next_key(decoder, decoder->key); if(next != TRACK_NONE) { // NOTE: gapless: no flush, the next track's samples go right behind this one's if(decoder_open(decoder, next, true)) { decoder_push_marker(decoder, AUDIO_MARKER_TRACK_START, 0); return; } if(decoder_has_command(decoder)) { return; } } decoder_close(decoder); decoder_push_marker(decoder, AUDIO_MARKER_END_OF_STREAM, 0); decoder_set_status(decoder, DECODER_IDLE); } internal void decoder_main(void *param) { Decoder *decoder = (Decoder *)param; thread_set_name(str8("decoder")); profile_thread_register("decoder"); while(!AtomicLoad(&decoder->quit)) { u32 snapshot = signal_snapshot(&decoder->wake); decoder_process_commands(decoder); if(!decoder->decoding) { signal_wait(&decoder->wake, snapshot, -1); continue; } // NOTE: wait on the *total* free space, never on the contiguous span. the span ends at the // ring's physical end: a write that stops within 1024 frames of it would otherwise look // "full" forever while the ring drains empty (playback went silent until a restart). a short // span is fine, the next iteration wraps around to the start. f32 *span = NULL; u64 free_total = 0; u64 writable = audio_ring_writable(decoder->ring, &span, &free_total); if(free_total < 1024 || writable == 0) { // NOTE: ring is full (~1.5s ahead). sleep until a command or until it has drained a bit. signal_wait(&decoder->wake, snapshot, 15000000ll); continue; } u64 frames = Min(writable, (u64)DECODER_CHUNK_FRAMES); size_t done = 0; u64 started = time_now_ns(); int result = mpg123_read(decoder->handle, span, frames * sizeof(f32) * AUDIO_CHANNELS, &done); AtomicStoreRelaxed(&decoder->decode_ns, decoder->decode_ns + (time_now_ns() - started)); AtomicStoreRelaxed(&decoder->decoded_frames, decoder->decoded_frames + done / (sizeof(f32) * AUDIO_CHANNELS)); u64 decoded = done / (sizeof(f32) * AUDIO_CHANNELS); // NOTE: streams opened before the response headers arrived don't know their size yet if(decoder->total_frames == 0) { u64 bytes_total = AtomicLoad(&decoder->reader.entry->bytes_total); if(bytes_total > 0) { mpg123_set_filesize64(decoder->handle, (s64)bytes_total); s64 length = mpg123_length64(decoder->handle); decoder->total_frames = (length > 0) ? (u64)length : 0; AtomicStore(&decoder->published_total, decoder->total_frames); } } if(decoded > 0) { audio_ring_commit(decoder->ring, decoded); if(AtomicLoadRelaxed(&decoder->status) != DECODER_DECODING) { decoder_set_status(decoder, DECODER_DECODING); } } if(result == MPG123_OK || result == MPG123_NEW_FORMAT) { continue; } if(result == MPG123_DONE) { decoder_end_of_track(decoder); continue; } // NOTE: an error. either the reader bailed because a command arrived (handled next loop), // or the download failed / the file is broken. if(decoder->reader.interrupted) { decoder->reader.interrupted = false; continue; } Log("decode error: %s", mpg123_strerror(decoder->handle)); decoder_close(decoder); decoder_push_marker(decoder, AUDIO_MARKER_END_OF_STREAM, 0); decoder_set_status(decoder, DECODER_ERROR); } decoder_close(decoder); } internal b32 decoder_init(Arena *arena, Decoder *decoder, AudioRing *ring, AudioOutput *output, TrackStore *store) { decoder->ring = ring; decoder->output = output; decoder->store = store; decoder->key = TRACK_NONE; decoder->reader.fd = -1; decoder->autoplay = AUTOPLAY_NEXT; int error = MPG123_OK; decoder->handle = mpg123_new(NULL, &error); if(!decoder->handle) { Log("mpg123_new failed: %s", mpg123_plain_strerror(error)); return false; } // NOTE: always f32 stereo out. mono files get duplicated to both channels by mpg123. mpg123_param2(decoder->handle, MPG123_ADD_FLAGS, MPG123_FORCE_STEREO | MPG123_FORCE_FLOAT | MPG123_GAPLESS | MPG123_QUIET, 0.0); mpg123_param2(decoder->handle, MPG123_REMOVE_FLAGS, MPG123_FUZZY, 0.0); // sample accurate seeks mpg123_format_none(decoder->handle); const long *rates = NULL; size_t rate_count = 0; mpg123_rates(&rates, &rate_count); for(size_t index = 0; index < rate_count; ++index) { mpg123_format(decoder->handle, rates[index], MPG123_STEREO, MPG123_ENC_FLOAT_32); } if(mpg123_reader64(decoder->handle, reader_read, reader_lseek, NULL) != MPG123_OK) { Log("mpg123_reader64 failed: %s", mpg123_strerror(decoder->handle)); return false; } decoder->thread = thread_launch(arena, decoder_main, decoder); return true; } internal void decoder_shutdown(Decoder *decoder) { AtomicStore(&decoder->quit, 1u); signal_notify(&decoder->wake); signal_notify(&decoder->store->progress); thread_join(decoder->thread); mpg123_delete(decoder->handle); }