From 94d1a87d0432d885756f9d23cfba1f8229cfe453 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 8 Mar 2009 13:45:24 +0100 Subject: [PATCH] music_chunk: added assertions on the audio format In !NDEBUG, remember which audio_format is stored in every chunk and every pipe. Check the audio_format of every new data block appended to the music_chunk, and the format of every new chunk appended to the music_pipe. --- src/chunk.c | 20 ++++++++++++++++++++ src/chunk.h | 18 ++++++++++++++++++ src/pipe.c | 33 +++++++++++++++++++++++++++++++++ src/pipe.h | 16 ++++++++++++++++ src/player_thread.c | 2 ++ 5 files changed, 89 insertions(+) diff --git a/src/chunk.c b/src/chunk.c index 65894f733..54fc11d3c 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -36,6 +36,19 @@ music_chunk_free(struct music_chunk *chunk) tag_free(chunk->tag); } +#ifndef NDEBUG +bool +music_chunk_check_format(const struct music_chunk *chunk, + const struct audio_format *audio_format) +{ + assert(chunk != NULL); + assert(audio_format != NULL); + + return chunk->length == 0 || + audio_format_equals(&chunk->audio_format, audio_format); +} +#endif + void * music_chunk_write(struct music_chunk *chunk, const struct audio_format *audio_format, @@ -45,6 +58,8 @@ music_chunk_write(struct music_chunk *chunk, const size_t frame_size = audio_format_frame_size(audio_format); size_t num_frames; + assert(music_chunk_check_format(chunk, audio_format)); + if (chunk->length == 0) { /* if the chunk is empty, nobody has set bitRate and times yet */ @@ -57,6 +72,10 @@ music_chunk_write(struct music_chunk *chunk, if (num_frames == 0) return NULL; +#ifndef NDEBUG + chunk->audio_format = *audio_format; +#endif + *max_length_r = num_frames * frame_size; return chunk->data + chunk->length; } @@ -69,6 +88,7 @@ music_chunk_expand(struct music_chunk *chunk, assert(chunk != NULL); assert(chunk->length + length <= sizeof(chunk->data)); + assert(audio_format_equals(&chunk->audio_format, audio_format)); chunk->length += length; diff --git a/src/chunk.h b/src/chunk.h index 2136c9349..7d39ebdf7 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -19,6 +19,10 @@ #ifndef MPD_CHUNK_H #define MPD_CHUNK_H +#ifndef NDEBUG +#include "audio_format.h" +#endif + #include #include #include @@ -57,6 +61,10 @@ struct music_chunk { /** the data (probably PCM) */ char data[CHUNK_SIZE]; + +#ifndef NDEBUG + struct audio_format audio_format; +#endif }; void @@ -71,6 +79,16 @@ music_chunk_is_empty(const struct music_chunk *chunk) return chunk->length == 0 && chunk->tag == NULL; } +#ifndef NDEBUG +/** + * Checks if the audio format if the chunk is equal to the specified + * audio_format. + */ +bool +music_chunk_check_format(const struct music_chunk *chunk, + const struct audio_format *audio_format); +#endif + /** * Prepares appending to the music chunk. Returns a buffer where you * may write into. After you are finished, call music_chunk_expand(). diff --git a/src/pipe.c b/src/pipe.c index c433aa254..fe011f3c2 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -36,6 +36,10 @@ struct music_pipe { /** a mutex which protects #head and #tail_r */ GMutex *mutex; + +#ifndef NDEBUG + struct audio_format audio_format; +#endif }; struct music_pipe * @@ -48,6 +52,10 @@ music_pipe_new(void) mp->size = 0; mp->mutex = g_mutex_new(); +#ifndef NDEBUG + audio_format_clear(&mp->audio_format); +#endif + return mp; } @@ -61,6 +69,19 @@ music_pipe_free(struct music_pipe *mp) g_free(mp); } +#ifndef NDEBUG +bool +music_pipe_check_format(const struct music_pipe *pipe, + const struct audio_format *audio_format) +{ + assert(pipe != NULL); + assert(audio_format != NULL); + + return !audio_format_defined(&pipe->audio_format) || + audio_format_equals(&pipe->audio_format, audio_format); +} +#endif + const struct music_chunk * music_pipe_peek(const struct music_pipe *mp) { @@ -94,6 +115,9 @@ music_pipe_shift(struct music_pipe *mp) #ifndef NDEBUG /* poison the "next" reference */ chunk->next = (void*)0x01010101; + + if (mp->size == 0) + audio_format_clear(&mp->audio_format); #endif } @@ -118,6 +142,15 @@ music_pipe_push(struct music_pipe *mp, struct music_chunk *chunk) g_mutex_lock(mp->mutex); + assert(mp->size > 0 || !audio_format_defined(&mp->audio_format)); + assert(!audio_format_defined(&mp->audio_format) || + music_chunk_check_format(chunk, &mp->audio_format)); + +#ifndef NDEBUG + if (!audio_format_defined(&mp->audio_format) && chunk->length > 0) + mp->audio_format = chunk->audio_format; +#endif + chunk->next = NULL; *mp->tail_r = chunk; mp->tail_r = &chunk->next; diff --git a/src/pipe.h b/src/pipe.h index 7ed6c917d..dd97ce820 100644 --- a/src/pipe.h +++ b/src/pipe.h @@ -19,6 +19,12 @@ #ifndef MPD_PIPE_H #define MPD_PIPE_H +#ifndef NDEBUG +#include + +struct audio_format; +#endif + struct music_chunk; struct music_buffer; @@ -40,6 +46,16 @@ music_pipe_new(void); void music_pipe_free(struct music_pipe *mp); +#ifndef NDEBUG +/** + * Checks if the audio format if the chunk is equal to the specified + * audio_format. + */ +bool +music_pipe_check_format(const struct music_pipe *pipe, + const struct audio_format *audio_format); +#endif + /** * Returns the first #music_chunk from the pipe. Returns NULL if the * pipe is empty. diff --git a/src/player_thread.c b/src/player_thread.c index bc325ca00..3302e51a9 100644 --- a/src/player_thread.c +++ b/src/player_thread.c @@ -313,6 +313,8 @@ play_chunk(struct song *song, struct music_chunk *chunk, { bool success; + assert(music_chunk_check_format(chunk, format)); + pc.elapsed_time = chunk->times; pc.bit_rate = chunk->bit_rate;