diff options
author | Tuomas Virtanen <katajakasa@gmail.com> | 2018-01-15 20:04:09 +0200 |
---|---|---|
committer | Tuomas Virtanen <katajakasa@gmail.com> | 2018-01-15 20:04:09 +0200 |
commit | eace70f1c1c7ba7339713666a76213acf1125202 (patch) | |
tree | a13d1468406644b91e3bc50bc139ebc32cda2e90 /src/kitplayer.c | |
parent | 2267d51d2b402c377a21bf03be6fc51b251f063b (diff) |
Dump reorganized code
Diffstat (limited to 'src/kitplayer.c')
-rw-r--r-- | src/kitplayer.c | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/src/kitplayer.c b/src/kitplayer.c index 809a5e2..fceb233 100644 --- a/src/kitplayer.c +++ b/src/kitplayer.c @@ -1,39 +1,37 @@ +#include <assert.h> + +#include <SDL2/SDL.h> + #include "kitchensink/kitplayer.h" #include "kitchensink/kiterror.h" -#include "kitchensink/internal/kitlist.h" #include "kitchensink/internal/kitlibstate.h" -#include "kitchensink/internal/kitvideo.h" -#include "kitchensink/internal/kitaudio.h" -#include "kitchensink/internal/kitsubtitle.h" -#include "kitchensink/internal/kithelpers.h" -#include "kitchensink/internal/kitlog.h" - -#include <SDL2/SDL.h> -#include <assert.h> +#include "kitchensink/internal/video/kitvideo.h" +#include "kitchensink/internal/audio/kitaudio.h" +#include "kitchensink/internal/subtitle/kitsubtitle.h" +#include "kitchensink/internal/utils/kithelpers.h" +#include "kitchensink/internal/utils/kitlog.h" // Return 0 if stream is good but nothing else to do for now -// Return -1 if there is still work to be done +// Return -1 if there may still work to be done // Return 1 if there was an error or stream end static int _DemuxStream(const Kit_Player *player) { assert(player != NULL); - Kit_Decoder *video_dec = player->video_dec; - Kit_Decoder *audio_dec = player->audio_dec; - //Kit_Decoder *subtitle_dec = player->subtitle_dec; AVFormatContext *format_ctx = player->src->format_ctx; + Kit_Decoder *decoders[] = { + player->video_dec, + player->audio_dec, + player->subtitle_dec + }; - // If either buffer is full, just stop here for now. + // If any buffer is full, just stop here for now. // Since we don't know what kind of data is going to come out of av_read_frame, we really // want to make sure we are prepared for everything :) - if(video_dec != NULL) { - if(!Kit_CanWriteDecoderInput(video_dec)) { - return 0; - } - } - if(audio_dec != NULL) { - if(!Kit_CanWriteDecoderInput(audio_dec)) { + for(int i = 0; i < 3; i++) { + if(decoders[i] == NULL) + continue; + if(!Kit_CanWriteDecoderInput(decoders[i])) return 0; - } } // Attempt to read frame. Just return here if it fails. @@ -44,16 +42,18 @@ static int _DemuxStream(const Kit_Player *player) { } // Check if this is a packet we need to handle and pass it on - if(video_dec != NULL && packet->stream_index == video_dec->stream_index) { - Kit_WriteDecoderInput(video_dec, packet); - } - else if(audio_dec != NULL && packet->stream_index == audio_dec->stream_index) { - Kit_WriteDecoderInput(audio_dec, packet); - } - else { - av_packet_free(&packet); + for(int i = 0; i < 3; i++) { + if(decoders[i] == NULL) + continue; + if(decoders[i]->stream_index == packet->stream_index) { + Kit_WriteDecoderInput(decoders[i], packet); + return -1; + } } + // We only get here if packet was not written to a decoder. IF that is the case, + // disregard and free the packet. + av_packet_free(&packet); return -1; } @@ -92,7 +92,8 @@ static int _DecoderThread(void *ptr) { // Run decoders for a bit while(Kit_RunDecoder(player->video_dec) == 1); while(Kit_RunDecoder(player->audio_dec) == 1); - + while(Kit_RunDecoder(player->subtitle_dec) == 1); + // Free decoder thread lock. SDL_UnlockMutex(player->dec_lock); } @@ -139,14 +140,14 @@ Kit_Player* Kit_CreatePlayer(const Kit_Source *src) { // Decoder thread lock player->dec_lock = SDL_CreateMutex(); if(player->dec_lock == NULL) { - Kit_SetError("Unable to create a decoder thread: %s", SDL_GetError()); + Kit_SetError("Unable to create a decoder thread lock mutex: %s", SDL_GetError()); goto error; } // Decoder thread player->dec_thread = SDL_CreateThread(_DecoderThread, "Kit Decoder Thread", player); if(player->dec_thread == NULL) { - Kit_SetError("Unable to create a decoder thread lock mutex: %s", SDL_GetError()); + Kit_SetError("Unable to create a decoder thread: %s", SDL_GetError()); goto error; } @@ -161,7 +162,7 @@ error: void Kit_ClosePlayer(Kit_Player *player) { if(player == NULL) return; - // Kill the decoder thread and lock mutex + // Kill the decoder thread and mutex player->state = KIT_CLOSED; SDL_WaitThread(player->dec_thread, NULL); SDL_DestroyMutex(player->dec_lock); @@ -192,7 +193,7 @@ int Kit_GetVideoData(Kit_Player *player, SDL_Texture *texture) { return Kit_GetVideoDecoderData(player->video_dec, texture); } -int Kit_GetAudioData(Kit_Player *player, unsigned char *buffer, int length, int cur_buf_len) { +int Kit_GetAudioData(Kit_Player *player, unsigned char *buffer, int length) { assert(player != NULL); assert(buffer != NULL); if(player->audio_dec == NULL) { @@ -215,7 +216,7 @@ int Kit_GetAudioData(Kit_Player *player, unsigned char *buffer, int length, int return Kit_GetAudioDecoderData(player->audio_dec, buffer, length); } -int Kit_GetSubtitleData(Kit_Player *player, SDL_Renderer *renderer) { +int Kit_GetSubtitleData(Kit_Player *player, SDL_Texture *texture) { assert(player != NULL); if(player->subtitle_dec == NULL) { return 0; @@ -229,9 +230,7 @@ int Kit_GetSubtitleData(Kit_Player *player, SDL_Renderer *renderer) { return 0; } - - - return 0; + return Kit_GetSubtitleDecoderData(player->subtitle_dec, texture); } void Kit_GetPlayerInfo(const Kit_Player *player, Kit_PlayerInfo *info) { @@ -345,15 +344,15 @@ int Kit_PlayerSeek(Kit_Player *player, double seek_set) { // Set source to timestamp AVFormatContext *format_ctx = player->src->format_ctx; seek_target = seek_set * AV_TIME_BASE; - if(avformat_seek_file(format_ctx, -1, INT64_MIN, seek_target, seek_target, 0) < 0) { + if(avformat_seek_file(format_ctx, -1, seek_target, seek_target, seek_target, AVSEEK_FLAG_ANY) < 0) { Kit_SetError("Unable to seek source"); + SDL_UnlockMutex(player->dec_lock); return 1; } else { _ChangeClockSync(player, position - seek_set); Kit_ClearDecoderBuffers(player->video_dec); Kit_ClearDecoderBuffers(player->audio_dec); Kit_ClearDecoderBuffers(player->subtitle_dec); - while(_DemuxStream(player) == -1); } // That's it. Unlock and continue. |