音频结构调整

This commit is contained in:
JIe Jie 2024-02-26 17:42:56 +08:00
parent 34d74c5c2d
commit 800364ea78
3 changed files with 17 additions and 24 deletions

View File

@ -101,7 +101,6 @@ struct VideoParam
int videoStreamIndex;
bool eof = false;
bool pause = false;
bool quit = false;
};
@ -117,6 +116,7 @@ struct AudioParam
uint32_t bufferIndex = 0;
bool eof = false;
bool pause = false;
bool isVideo = false;
bool quit = false;
};

View File

@ -23,10 +23,13 @@ struct OpenglVideoParam
};
int InitAudio(SDL_Window* window, SDL_Renderer* renderer, const char* targetFilePath, MediaParam& param)
{
if (!param.audioParam.isVideo)
{
InitDecoder(targetFilePath, param);
window = SDL_CreateWindow("mp", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
}
std::jthread(RequestAudioPacket, std::ref(param)).detach();
SDL_AudioSpec des;
des.freq = param.audioParam.codecCtx->sample_rate;
@ -49,6 +52,8 @@ int InitAudio(SDL_Window* window, SDL_Renderer* renderer, const char* targetFile
int InitVideo(SDL_Window*& window, const char* targetFilepath, MediaParam& param, OpenglVideoParam& openglVideoParam, ShaderService*& shaderService)
{
InitDecoder(targetFilepath, param);
param.audioParam.isVideo = true;
InitAudio(nullptr, nullptr, nullptr, param);
//FIX: when app exited, the fmtCtx was freed, so need notify decode thread to stop decode and exit.
std::jthread(RequestVideoPacket, std::ref(param)).detach();
std::jthread(RequestVideoFrame, std::ref(param)).detach();

View File

@ -71,26 +71,14 @@ int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize, SDL_
const int nb = swr_convert(swrCtx, &audioBuffer, static_cast<int>(dstNbSamples), const_cast<const uint8_t**>(frame->data), frame->nb_samples);
dataSize = frame->ch_layout.nb_channels * nb * av_get_bytes_per_sample(dstFormat);
//render wave
int nbSamples = frame->nb_samples;
fftw_complex* in = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * nbSamples));
fftw_complex* out = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * nbSamples));
for(int i= 0; i < nbSamples; i++) {
in[i][0] = frame->data[0][i];
in[i][1] = 0;
}
fftw_plan p = fftw_plan_dft_1d(nbSamples, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(p);
fftw_destroy_plan(p);
//TODO: render wave
if (!param.isVideo) {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xff);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderDrawLine(renderer, 0, 0, 300, 300);
SDL_RenderPresent(renderer);
}
av_frame_free(&frame);
swr_free(&swrCtx);
return dataSize;