Audio播放逻辑

This commit is contained in:
2024-02-23 16:24:42 +08:00
parent 5e794cf825
commit f005b19ee4
8 changed files with 206 additions and 36 deletions

36
main.cc
View File

@@ -9,6 +9,7 @@
#include "mediaDecoder.h"
#include "shaderService.h"
#include "shader.h"
#include "audioDecoder.h"
using std::cout;
struct OpenglVideoParam
@@ -18,11 +19,32 @@ struct OpenglVideoParam
unsigned int texs[3];
};
int InitAudio(const char* targetFilePath, MediaParam& param)
{
InitDecoder(targetFilePath, param);
std::jthread(RequestAudioPacket, std::ref(param)).detach();
SDL_AudioSpec des;
des.freq = param.audioParam.codecCtx->sample_rate;
des.channels = param.audioParam.codecCtx->channels;
des.format = AUDIO_S16SYS;
des.samples = 1024;
des.silence = 0;
des.userdata = &(param.audioParam);
des.callback = audioCallback;
if (SDL_OpenAudio(&des, nullptr) < 0)
{
cout << SDL_GetError() << "\n";
return -1;
}
SDL_PauseAudio(0);
return 0;
}
int InitVideo(SDL_Window*& window, const char* targetFilepath, MediaParam& param, OpenglVideoParam& openglVideoParam, ShaderService*& shaderService)
{
InitDecoder(targetFilepath, param.videoParam);
InitDecoder(targetFilepath, param);
//FIX: when app exited, the fmtCtx was freed, so need notify decode thread to stop decode and exit.
std::jthread(RequestPacket, std::ref(param)).detach();
std::jthread(RequestVideoPacket, std::ref(param)).detach();
std::jthread(RequestVideoFrame, std::ref(param)).detach();
const int client_width = param.videoParam.width / 2;
const int client_height = param.videoParam.height / 2;
@@ -152,7 +174,6 @@ void OpenglRenderVideo(MediaParam& param, const OpenglVideoParam& openglVideoPar
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}
int main(int argc, char** argv)
{
// Check File
@@ -189,7 +210,7 @@ int main(int argc, char** argv)
case FileType::VIDEO:
{
InitVideo(window, targetFilepath, mediaParam, openglVideoParam, shaderService);
const auto stream_frame_rate = mediaParam.videoParam.fmtCtx->streams[mediaParam.videoParam.videoStreamIndex]->avg_frame_rate;
const auto stream_frame_rate = mediaParam.fmtCtx->streams[mediaParam.videoParam.videoStreamIndex]->avg_frame_rate;
framerate = static_cast<double>(stream_frame_rate.den) / stream_frame_rate.num;
break;
}
@@ -198,8 +219,9 @@ int main(int argc, char** argv)
InitImg(window, targetFilepath, renderer, surface, texture);
break;
}
case FileType::MUSIC:
case FileType::AUDIO:
{
InitAudio(targetFilepath, mediaParam);
break;
}
case FileType::ERRORTYPE:
@@ -245,13 +267,15 @@ int main(int argc, char** argv)
case FileType::IMG:
RenderPicture(window, renderer, texture);
break;
case FileType::AUDIO:
break;
default:
break;
}
}
avcodec_close(mediaParam.videoParam.codecCtx);
avformat_close_input(&(mediaParam.videoParam.fmtCtx));
avformat_close_input(&(mediaParam.fmtCtx));
SDL_GL_DeleteContext(openglVideoParam.glContext);
SDL_DestroyWindow(window);
SDL_Quit();