diff --git a/include/decodeParam.h b/include/decodeParam.h index 6856ce1..c03e679 100644 --- a/include/decodeParam.h +++ b/include/decodeParam.h @@ -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; }; diff --git a/main.cc b/main.cc index 210d5b4..74bebb5 100644 --- a/main.cc +++ b/main.cc @@ -24,9 +24,12 @@ struct OpenglVideoParam int InitAudio(SDL_Window* window, SDL_Renderer* renderer, const char* targetFilePath, MediaParam& param) { - 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); + 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; @@ -34,7 +37,7 @@ int InitAudio(SDL_Window* window, SDL_Renderer* renderer, const char* targetFile des.format = AUDIO_S16SYS; des.samples = 1024; des.silence = 0; - std::tuple* callbackParam = new std::tuple{window, renderer, &(param.audioParam)}; + std::tuple* callbackParam = new std::tuple{ window, renderer, &(param.audioParam) }; des.userdata = callbackParam; des.callback = audioCallback; if (SDL_OpenAudio(&des, nullptr) < 0) @@ -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(); diff --git a/src/audioDecoder.cc b/src/audioDecoder.cc index 273950b..410eae0 100644 --- a/src/audioDecoder.cc +++ b/src/audioDecoder.cc @@ -71,26 +71,14 @@ int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize, SDL_ const int nb = swr_convert(swrCtx, &audioBuffer, static_cast(dstNbSamples), const_cast(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_malloc(sizeof(fftw_complex) * nbSamples)); - fftw_complex* out = static_cast(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 - - 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); + 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;