summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuomas Virtanen <katajakasa@gmail.com>2020-05-02 02:35:17 +0300
committerGitHub <noreply@github.com>2020-05-02 02:35:17 +0300
commit51bf330f587649f6be179dc7418525c75777e811 (patch)
tree974be5b597c6f42c13cd547ad0a0c926cdcfe1a1
parent243140385848145c53b5b2c209f715be864bcecb (diff)
parent5ac1bdda13ce9557e18f924addd3bb54a8da5a15 (diff)
Merge pull request #62 from katajakasa/code-cleanups
Add consts and clean unnecessary state
-rw-r--r--include/kitchensink/internal/kitdecoder.h32
-rw-r--r--include/kitchensink/internal/utils/kithelpers.h2
-rw-r--r--src/internal/audio/kitaudio.c4
-rw-r--r--src/internal/kitdecoder.c42
-rw-r--r--src/internal/subtitle/kitsubtitle.c8
-rw-r--r--src/internal/subtitle/renderers/kitsubass.c2
-rw-r--r--src/internal/utils/kithelpers.c4
-rw-r--r--src/internal/video/kitvideo.c2
-rw-r--r--src/kitlib.c2
-rw-r--r--src/kitplayer.c77
-rw-r--r--src/kitsource.c4
11 files changed, 88 insertions, 91 deletions
diff --git a/include/kitchensink/internal/kitdecoder.h b/include/kitchensink/internal/kitdecoder.h
index 17e1e4b..a3f6b61 100644
--- a/include/kitchensink/internal/kitdecoder.h
+++ b/include/kitchensink/internal/kitdecoder.h
@@ -57,23 +57,23 @@ KIT_LOCAL void Kit_ChangeDecoderClockSync(Kit_Decoder *dec, double sync);
KIT_LOCAL int Kit_RunDecoder(Kit_Decoder *dec);
KIT_LOCAL void Kit_ClearDecoderBuffers(Kit_Decoder *dec);
-KIT_LOCAL bool Kit_CanWriteDecoderInput(Kit_Decoder *dec);
-KIT_LOCAL int Kit_WriteDecoderInput(Kit_Decoder *dec, AVPacket *packet);
-KIT_LOCAL AVPacket* Kit_ReadDecoderInput(Kit_Decoder *dec);
-KIT_LOCAL void Kit_ClearDecoderInput(Kit_Decoder *dec);
-KIT_LOCAL AVPacket* Kit_PeekDecoderInput(Kit_Decoder *dec);
-KIT_LOCAL void Kit_AdvanceDecoderInput(Kit_Decoder *dec);
-
-KIT_LOCAL int Kit_WriteDecoderOutput(Kit_Decoder *dec, void *packet);
-KIT_LOCAL bool Kit_CanWriteDecoderOutput(Kit_Decoder *dec);
-KIT_LOCAL void* Kit_PeekDecoderOutput(Kit_Decoder *dec);
-KIT_LOCAL void* Kit_ReadDecoderOutput(Kit_Decoder *dec);
-KIT_LOCAL void Kit_AdvanceDecoderOutput(Kit_Decoder *dec);
-KIT_LOCAL void Kit_ForEachDecoderOutput(Kit_Decoder *dec, Kit_ForEachItemCallback foreach_cb, void *userdata);
+KIT_LOCAL bool Kit_CanWriteDecoderInput(const Kit_Decoder *dec);
+KIT_LOCAL int Kit_WriteDecoderInput(const Kit_Decoder *dec, AVPacket *packet);
+KIT_LOCAL AVPacket* Kit_ReadDecoderInput(const Kit_Decoder *dec);
+KIT_LOCAL void Kit_ClearDecoderInput(const Kit_Decoder *dec);
+KIT_LOCAL AVPacket* Kit_PeekDecoderInput(const Kit_Decoder *dec);
+KIT_LOCAL void Kit_AdvanceDecoderInput(const Kit_Decoder *dec);
+
+KIT_LOCAL int Kit_WriteDecoderOutput(const Kit_Decoder *dec, void *packet);
+KIT_LOCAL bool Kit_CanWriteDecoderOutput(const Kit_Decoder *dec);
+KIT_LOCAL void* Kit_PeekDecoderOutput(const Kit_Decoder *dec);
+KIT_LOCAL void* Kit_ReadDecoderOutput(const Kit_Decoder *dec);
+KIT_LOCAL void Kit_ClearDecoderOutput(const Kit_Decoder *dec);
+KIT_LOCAL void Kit_AdvanceDecoderOutput(const Kit_Decoder *dec);
+KIT_LOCAL void Kit_ForEachDecoderOutput(const Kit_Decoder *dec, Kit_ForEachItemCallback foreach_cb, void *userdata);
+KIT_LOCAL unsigned int Kit_GetDecoderOutputLength(const Kit_Decoder *dec);
+
KIT_LOCAL int Kit_LockDecoderOutput(Kit_Decoder *dec);
KIT_LOCAL void Kit_UnlockDecoderOutput(Kit_Decoder *dec);
-KIT_LOCAL void Kit_ClearDecoderOutput(Kit_Decoder *dec);
-KIT_LOCAL unsigned int Kit_GetDecoderOutputLength(Kit_Decoder *dec);
-
#endif // KITDECODER_H
diff --git a/include/kitchensink/internal/utils/kithelpers.h b/include/kitchensink/internal/utils/kithelpers.h
index 5b94a7a..e4c163c 100644
--- a/include/kitchensink/internal/utils/kithelpers.h
+++ b/include/kitchensink/internal/utils/kithelpers.h
@@ -6,6 +6,6 @@
#include "kitchensink/kitconfig.h"
KIT_LOCAL double _GetSystemTime();
-KIT_LOCAL bool attachment_is_font(AVStream *stream);
+KIT_LOCAL bool attachment_is_font(const AVStream *stream);
#endif // KITHELPERS_H
diff --git a/src/internal/audio/kitaudio.c b/src/internal/audio/kitaudio.c
index b2f41fc..04dd4f1 100644
--- a/src/internal/audio/kitaudio.c
+++ b/src/internal/audio/kitaudio.c
@@ -278,7 +278,7 @@ Kit_Decoder* Kit_CreateAudioDecoder(const Kit_Source *src, int stream_index) {
return NULL;
}
- Kit_LibraryState *state = Kit_GetLibraryState();
+ const Kit_LibraryState *state = Kit_GetLibraryState();
// First the generic decoder component ...
Kit_Decoder *dec = Kit_CreateDecoder(
@@ -347,7 +347,7 @@ exit_0:
}
double Kit_GetAudioDecoderPTS(Kit_Decoder *dec) {
- Kit_AudioPacket *packet = Kit_PeekDecoderOutput(dec);
+ const Kit_AudioPacket *packet = Kit_PeekDecoderOutput(dec);
if(packet == NULL) {
return -1.0;
}
diff --git a/src/internal/kitdecoder.c b/src/internal/kitdecoder.c
index 9b925c7..530473e 100644
--- a/src/internal/kitdecoder.c
+++ b/src/internal/kitdecoder.c
@@ -170,6 +170,13 @@ int Kit_RunDecoder(Kit_Decoder *dec) {
return 0;
}
+void Kit_ClearDecoderBuffers(Kit_Decoder *dec) {
+ if(dec == NULL) return;
+ Kit_ClearDecoderInput(dec);
+ Kit_ClearDecoderOutput(dec);
+ avcodec_flush_buffers(dec->codec_ctx);
+}
+
// ---- Information API ----
int Kit_GetDecoderCodecInfo(const Kit_Decoder *dec, Kit_Codec *codec) {
@@ -214,38 +221,38 @@ void Kit_ChangeDecoderClockSync(Kit_Decoder *dec, double sync) {
// ---- Input buffer handling ----
-int Kit_WriteDecoderInput(Kit_Decoder *dec, AVPacket *packet) {
+int Kit_WriteDecoderInput(const Kit_Decoder *dec, AVPacket *packet) {
assert(dec != NULL);
return Kit_WriteBuffer(dec->buffer[KIT_DEC_BUF_IN], packet);
}
-bool Kit_CanWriteDecoderInput(Kit_Decoder *dec) {
+bool Kit_CanWriteDecoderInput(const Kit_Decoder *dec) {
assert(dec != NULL);
return !Kit_IsBufferFull(dec->buffer[KIT_DEC_BUF_IN]);
}
-AVPacket* Kit_ReadDecoderInput(Kit_Decoder *dec) {
+AVPacket* Kit_ReadDecoderInput(const Kit_Decoder *dec) {
assert(dec != NULL);
return Kit_ReadBuffer(dec->buffer[KIT_DEC_BUF_IN]);
}
-AVPacket* Kit_PeekDecoderInput(Kit_Decoder *dec) {
+AVPacket* Kit_PeekDecoderInput(const Kit_Decoder *dec) {
assert(dec != NULL);
return Kit_PeekBuffer(dec->buffer[KIT_DEC_BUF_IN]);
}
-void Kit_AdvanceDecoderInput(Kit_Decoder *dec) {
+void Kit_AdvanceDecoderInput(const Kit_Decoder *dec) {
assert(dec != NULL);
Kit_AdvanceBuffer(dec->buffer[KIT_DEC_BUF_IN]);
}
-void Kit_ClearDecoderInput(Kit_Decoder *dec) {
+void Kit_ClearDecoderInput(const Kit_Decoder *dec) {
Kit_ClearBuffer(dec->buffer[KIT_DEC_BUF_IN]);
}
// ---- Output buffer handling ----
-int Kit_WriteDecoderOutput(Kit_Decoder *dec, void *packet) {
+int Kit_WriteDecoderOutput(const Kit_Decoder *dec, void *packet) {
assert(dec != NULL);
int ret = 1;
if(SDL_LockMutex(dec->output_lock) == 0) {
@@ -255,14 +262,14 @@ int Kit_WriteDecoderOutput(Kit_Decoder *dec, void *packet) {
return ret;
}
-void Kit_ClearDecoderOutput(Kit_Decoder *dec) {
+void Kit_ClearDecoderOutput(const Kit_Decoder *dec) {
if(SDL_LockMutex(dec->output_lock) == 0) {
Kit_ClearBuffer(dec->buffer[KIT_DEC_BUF_OUT]);
SDL_UnlockMutex(dec->output_lock);
}
}
-void* Kit_PeekDecoderOutput(Kit_Decoder *dec) {
+void* Kit_PeekDecoderOutput(const Kit_Decoder *dec) {
assert(dec != NULL);
void *ret = NULL;
if(SDL_LockMutex(dec->output_lock) == 0) {
@@ -272,7 +279,7 @@ void* Kit_PeekDecoderOutput(Kit_Decoder *dec) {
return ret;
}
-void* Kit_ReadDecoderOutput(Kit_Decoder *dec) {
+void* Kit_ReadDecoderOutput(const Kit_Decoder *dec) {
assert(dec != NULL);
void *ret = NULL;
if(SDL_LockMutex(dec->output_lock) == 0) {
@@ -282,7 +289,7 @@ void* Kit_ReadDecoderOutput(Kit_Decoder *dec) {
return ret;
}
-bool Kit_CanWriteDecoderOutput(Kit_Decoder *dec) {
+bool Kit_CanWriteDecoderOutput(const Kit_Decoder *dec) {
assert(dec != NULL);
bool ret = false;
if(SDL_LockMutex(dec->output_lock) == 0) {
@@ -292,7 +299,7 @@ bool Kit_CanWriteDecoderOutput(Kit_Decoder *dec) {
return ret;
}
-void Kit_ForEachDecoderOutput(Kit_Decoder *dec, Kit_ForEachItemCallback cb, void *userdata) {
+void Kit_ForEachDecoderOutput(const Kit_Decoder *dec, Kit_ForEachItemCallback cb, void *userdata) {
assert(dec != NULL);
if(SDL_LockMutex(dec->output_lock) == 0) {
Kit_ForEachItemInBuffer(dec->buffer[KIT_DEC_BUF_OUT], cb, userdata);
@@ -300,7 +307,7 @@ void Kit_ForEachDecoderOutput(Kit_Decoder *dec, Kit_ForEachItemCallback cb, void
}
}
-void Kit_AdvanceDecoderOutput(Kit_Decoder *dec) {
+void Kit_AdvanceDecoderOutput(const Kit_Decoder *dec) {
assert(dec != NULL);
if(SDL_LockMutex(dec->output_lock) == 0) {
Kit_AdvanceBuffer(dec->buffer[KIT_DEC_BUF_OUT]);
@@ -308,7 +315,7 @@ void Kit_AdvanceDecoderOutput(Kit_Decoder *dec) {
}
}
-unsigned int Kit_GetDecoderOutputLength(Kit_Decoder *dec) {
+unsigned int Kit_GetDecoderOutputLength(const Kit_Decoder *dec) {
assert(dec != NULL);
unsigned int len = 0;
if(SDL_LockMutex(dec->output_lock) == 0) {
@@ -318,13 +325,6 @@ unsigned int Kit_GetDecoderOutputLength(Kit_Decoder *dec) {
return len;
}
-void Kit_ClearDecoderBuffers(Kit_Decoder *dec) {
- if(dec == NULL) return;
- Kit_ClearDecoderInput(dec);
- Kit_ClearDecoderOutput(dec);
- avcodec_flush_buffers(dec->codec_ctx);
-}
-
int Kit_LockDecoderOutput(Kit_Decoder *dec) {
return SDL_LockMutex(dec->output_lock);
}
diff --git a/src/internal/subtitle/kitsubtitle.c b/src/internal/subtitle/kitsubtitle.c
index 024179e..e222794 100644
--- a/src/internal/subtitle/kitsubtitle.c
+++ b/src/internal/subtitle/kitsubtitle.c
@@ -95,7 +95,7 @@ Kit_Decoder* Kit_CreateSubtitleDecoder(const Kit_Source *src, int stream_index,
return NULL;
}
- Kit_LibraryState *state = Kit_GetLibraryState();
+ const Kit_LibraryState *state = Kit_GetLibraryState();
// First the generic decoder component
Kit_Decoder *dec = Kit_CreateDecoder(
@@ -173,7 +173,7 @@ exit_0:
void Kit_SetSubtitleDecoderSize(Kit_Decoder *dec, int screen_w, int screen_h) {
assert(dec != NULL);
- Kit_SubtitleDecoder *subtitle_dec = dec->userdata;
+ const Kit_SubtitleDecoder *subtitle_dec = dec->userdata;
Kit_SetSubtitleRendererSize(subtitle_dec->renderer, screen_w, screen_h);
}
@@ -181,11 +181,11 @@ void Kit_GetSubtitleDecoderTexture(Kit_Decoder *dec, SDL_Texture *texture, doubl
assert(dec != NULL);
assert(texture != NULL);
- Kit_SubtitleDecoder *subtitle_dec = dec->userdata;
+ const Kit_SubtitleDecoder *subtitle_dec = dec->userdata;
Kit_GetSubtitleRendererData(subtitle_dec->renderer, subtitle_dec->atlas, texture, sync_ts);
}
int Kit_GetSubtitleDecoderInfo(Kit_Decoder *dec, SDL_Texture *texture, SDL_Rect *sources, SDL_Rect *targets, int limit) {
- Kit_SubtitleDecoder *subtitle_dec = dec->userdata;
+ const Kit_SubtitleDecoder *subtitle_dec = dec->userdata;
return Kit_GetAtlasItems(subtitle_dec->atlas, sources, targets, limit);
}
diff --git a/src/internal/subtitle/renderers/kitsubass.c b/src/internal/subtitle/renderers/kitsubass.c
index 7dfb31b..b2c37c8 100644
--- a/src/internal/subtitle/renderers/kitsubass.c
+++ b/src/internal/subtitle/renderers/kitsubass.c
@@ -137,7 +137,7 @@ Kit_SubtitleRenderer* Kit_CreateASSSubtitleRenderer(Kit_Decoder *dec, int video_
assert(screen_h >= 0);
// Make sure that libass library has been initialized + get handle
- Kit_LibraryState *state = Kit_GetLibraryState();
+ const Kit_LibraryState *state = Kit_GetLibraryState();
if(state->libass_handle == NULL) {
Kit_SetError("Libass library has not been initialized");
return NULL;
diff --git a/src/internal/utils/kithelpers.c b/src/internal/utils/kithelpers.c
index c68f1c7..0108f1b 100644
--- a/src/internal/utils/kithelpers.c
+++ b/src/internal/utils/kithelpers.c
@@ -17,8 +17,8 @@ double _GetSystemTime() {
return (double)av_gettime() / 1000000.0;
}
-bool attachment_is_font(AVStream *stream) {
- AVDictionaryEntry *tag = av_dict_get(stream->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE);
+bool attachment_is_font(const AVStream *stream) {
+ const AVDictionaryEntry *tag = av_dict_get(stream->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE);
if(tag) {
for(int n = 0; font_mime[n]; n++) {
if(av_strcasecmp(font_mime[n], tag->value) == 0) {
diff --git a/src/internal/video/kitvideo.c b/src/internal/video/kitvideo.c
index 1f2bed9..a29d58d 100644
--- a/src/internal/video/kitvideo.c
+++ b/src/internal/video/kitvideo.c
@@ -232,7 +232,7 @@ Kit_Decoder* Kit_CreateVideoDecoder(const Kit_Source *src, int stream_index) {
return NULL;
}
- Kit_LibraryState *state = Kit_GetLibraryState();
+ const Kit_LibraryState *state = Kit_GetLibraryState();
// First the generic decoder component ...
Kit_Decoder *dec = Kit_CreateDecoder(
diff --git a/src/kitlib.c b/src/kitlib.c
index e823c52..21de714 100644
--- a/src/kitlib.c
+++ b/src/kitlib.c
@@ -106,7 +106,7 @@ void Kit_SetHint(Kit_HintType type, int value) {
}
int Kit_GetHint(Kit_HintType type) {
- Kit_LibraryState *state = Kit_GetLibraryState();
+ const Kit_LibraryState *state = Kit_GetLibraryState();
switch(type) {
case KIT_HINT_THREAD_COUNT:
return state->thread_count;
diff --git a/src/kitplayer.c b/src/kitplayer.c
index 2135a65..39f1cb1 100644
--- a/src/kitplayer.c
+++ b/src/kitplayer.c
@@ -71,9 +71,10 @@ static bool _IsOutputEmpty(const Kit_Player *player) {
return true;
}
-static int _RunDecoder(Kit_Player *player) {
+static int _RunDecoder(const Kit_Player *player) {
int got;
bool has_room = true;
+ const Kit_Decoder *dec = NULL;
do {
while((got = _DemuxStream(player)) == -1);
@@ -88,7 +89,7 @@ static int _RunDecoder(Kit_Player *player) {
// If there is no room in any decoder input, just stop here since it likely means that
// at least some decoder output is full.
for(int i = 0; i < KIT_DEC_COUNT; i++) {
- Kit_Decoder *dec = player->decoders[i];
+ dec = player->decoders[i];
if(dec == NULL)
continue;
if(!Kit_CanWriteDecoderInput(dec) || got == 1) {
@@ -101,42 +102,38 @@ static int _RunDecoder(Kit_Player *player) {
return 0;
}
+static void _TryWork(Kit_Player *player) {
+ /**
+ * \brief Run the decoders and demuxer as long as there is work. Returns when playback stops.
+ */
+ while(player->state == KIT_PLAYING || player->state == KIT_PAUSED) {
+ // Grab the decoder lock, and run demuxer & decoders for a bit.
+ if(SDL_LockMutex(player->dec_lock) == 0) {
+ if(_RunDecoder(player) == 1) {
+ player->state = KIT_STOPPED;
+ }
+ SDL_UnlockMutex(player->dec_lock);
+ }
+
+ // Delay to make sure this thread does not hog all cpu
+ SDL_Delay(2);
+ }
+}
+
static int _DecoderThread(void *ptr) {
+ /**
+ * \brief Decoder thread main, which runs as long as the player exists.
+ */
Kit_Player *player = ptr;
- bool is_running = true;
- bool is_playing = true;
- while(is_running) {
+ while(true) {
if(player->state == KIT_CLOSED) {
- is_running = false;
- continue;
- }
- if(player->state == KIT_PLAYING) {
- is_playing = true;
- }
- while(is_running && is_playing) {
- // Grab the decoder lock, and run demuxer & decoders for a bit.
- if(SDL_LockMutex(player->dec_lock) == 0) {
- if(player->state == KIT_CLOSED) {
- is_running = false;
- goto end_block;
- }
- if(player->state == KIT_STOPPED) {
- is_playing = false;
- goto end_block;
- }
- if(_RunDecoder(player) == 1) {
- player->state = KIT_STOPPED;
- goto end_block;
- }
-
-end_block:
- SDL_UnlockMutex(player->dec_lock);
- }
-
- // Delay to make sure this thread does not hog all cpu
- SDL_Delay(2);
+ break;
}
+
+ // This will block as long as there is something to demux/decode
+ // Returns when playback stops.
+ _TryWork(player);
// Just idle while waiting for work.
SDL_Delay(25);
@@ -334,24 +331,24 @@ int Kit_GetPlayerSubtitleData(Kit_Player *player, SDL_Texture *texture, SDL_Rect
void Kit_GetPlayerInfo(const Kit_Player *player, Kit_PlayerInfo *info) {
assert(player != NULL);
assert(info != NULL);
+ const Kit_Decoder *dec = NULL;
+ Kit_PlayerStreamInfo *streams[] = {&info->video, &info->audio, &info->subtitle};
- void *streams[] = {&info->video, &info->audio, &info->subtitle};
for(int i = 0; i < KIT_DEC_COUNT; i++) {
- Kit_Decoder *dec = player->decoders[i];
- Kit_PlayerStreamInfo *stream = streams[i];
- Kit_GetDecoderCodecInfo(dec, &stream->codec);
- Kit_GetDecoderOutputFormat(dec, &stream->output);
+ dec = player->decoders[i];
+ Kit_GetDecoderCodecInfo(dec, &streams[i]->codec);
+ Kit_GetDecoderOutputFormat(dec, &streams[i]->output);
}
}
-static void _SetClockSync(Kit_Player *player) {
+static void _SetClockSync(const Kit_Player *player) {
double sync = _GetSystemTime();
for(int i = 0; i < KIT_DEC_COUNT; i++) {
Kit_SetDecoderClockSync(player->decoders[i], sync);
}
}
-static void _ChangeClockSync(Kit_Player *player, double delta) {
+static void _ChangeClockSync(const Kit_Player *player, double delta) {
for(int i = 0; i < KIT_DEC_COUNT; i++) {
Kit_ChangeDecoderClockSync(player->decoders[i], delta);
}
diff --git a/src/kitsource.c b/src/kitsource.c
index 1a3bb00..f9ec228 100644
--- a/src/kitsource.c
+++ b/src/kitsource.c
@@ -170,13 +170,13 @@ int Kit_GetSourceStreamInfo(const Kit_Source *src, Kit_SourceStreamInfo *info, i
assert(src != NULL);
assert(info != NULL);
- AVFormatContext *format_ctx = (AVFormatContext *)src->format_ctx;
+ const AVFormatContext *format_ctx = (AVFormatContext *)src->format_ctx;
if(index < 0 || index >= format_ctx->nb_streams) {
Kit_SetError("Invalid stream index");
return 1;
}
- AVStream *stream = format_ctx->streams[index];
+ const AVStream *stream = format_ctx->streams[index];
enum AVMediaType codec_type;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
codec_type = stream->codec->codec_type;