创建线程解析packet并存入队列

This commit is contained in:
jie
2024-02-21 22:34:00 +08:00
parent 39697786c9
commit 659ca4772d
3 changed files with 141 additions and 44 deletions

26
main.cc
View File

@@ -17,11 +17,13 @@ struct OpenglVideoParam
unsigned int texs[3];
};
int InitVideo(SDL_Window*& window, const char* targetFilepath, DecoderParam& decoderParam, OpenglVideoParam& openglVideoParam, ShaderService*& shaderService)
int InitVideo(SDL_Window*& window, const char* targetFilepath, VideoParam& videoParam, OpenglVideoParam& openglVideoParam, ShaderService*& shaderService)
{
InitDecoder(targetFilepath, decoderParam);
const int client_width = decoderParam.width / 2;
const int client_height = decoderParam.height / 2;
InitDecoder(targetFilepath, videoParam);
//FIX: when app exited, the fmtCtx was freed, so need notify decode thread to stop decode and exit.
std::jthread(RequestPacket, std::ref(videoParam)).detach();
const int client_width = videoParam.width / 2;
const int client_height = videoParam.height / 2;
window = SDL_CreateWindow(
"MP",
SDL_WINDOWPOS_UNDEFINED,
@@ -121,9 +123,9 @@ void InitImg(SDL_Window*& window, const char* filepath, SDL_Renderer*& renderer,
texture = SDL_CreateTextureFromSurface(renderer, surface);
}
void OpenglRenderVideo(DecoderParam& decoderParam, const OpenglVideoParam& openglVideoParam, ShaderService* shaderService)
void OpenglRenderVideo(VideoParam& videoParam, const OpenglVideoParam& openglVideoParam, ShaderService* shaderService)
{
auto frame = RequestFrame(decoderParam);
auto frame = RequestFrame(videoParam);
if (frame == nullptr)
return;
// TODO: TIMER
@@ -169,7 +171,7 @@ int main(int argc, char** const argv)
int client_width, client_height;
SDL_Window* window = nullptr;
DecoderParam decoderParam{};
VideoParam videoParam{};
OpenglVideoParam openglVideoParam{};
ShaderService* shaderService = nullptr;
SDL_Surface* surface = nullptr;
@@ -185,8 +187,8 @@ int main(int argc, char** const argv)
{
case FileType::VIDEO:
{
InitVideo(window, targetFilepath, decoderParam, openglVideoParam, shaderService);
const auto stream_frame_rate = decoderParam.fmtCtx->streams[decoderParam.videoStreamIndex]->avg_frame_rate;
InitVideo(window, targetFilepath, videoParam, openglVideoParam, shaderService);
const auto stream_frame_rate = videoParam.fmtCtx->streams[videoParam.videoStreamIndex]->avg_frame_rate;
framerate = static_cast<double>(stream_frame_rate.den) / stream_frame_rate.num;
break;
}
@@ -234,7 +236,7 @@ int main(int argc, char** const argv)
switch (fileType)
{
case FileType::VIDEO:
OpenglRenderVideo(decoderParam, openglVideoParam, shaderService);
OpenglRenderVideo(videoParam, openglVideoParam, shaderService);
SDL_GL_SwapWindow(window);
std::this_thread::sleep_until(current_time + std::chrono::milliseconds(static_cast<int>(framerate * 1000)));
current_time = std::chrono::system_clock::now();
@@ -247,8 +249,8 @@ int main(int argc, char** const argv)
}
}
avcodec_close(decoderParam.codecCtx);
avformat_close_input(&(decoderParam.fmtCtx));
avcodec_close(videoParam.codecCtx);
avformat_close_input(&(videoParam.fmtCtx));
SDL_GL_DeleteContext(openglVideoParam.glContext);
SDL_DestroyWindow(window);
SDL_Quit();