void closeFile(){ if(pCodecCtx) avcodec_close(pCodecCtx); // force audio handler to close its codec audioHandler = 0; if(pFormatCtx){ pFormatCtx->pb = 0; avformat_close_input(&pFormatCtx); } stream->Close(); messageCallback(MUnloadedFile, stream->GetPath()); }
void openFile(StreamPtr stream, IAudioDevicePtr audioDevice) { FlogI("Trying to load file: " << stream->GetPath()); int ret; this->stream = stream; this->audioDevice = audioDevice; timeHandler = TimeHandler::Create(audioDevice); audioDevice->SetPaused(true); pFormatCtx = avformat_alloc_context(); pFormatCtx->pb = stream->GetAVIOContext(); if((ret = avformat_open_input(&pFormatCtx, stream->GetPath().c_str(), NULL, NULL)) != 0){ char ebuf[512]; av_strerror(ret, ebuf, sizeof(ebuf)); FlogE("couldn't open file"); FlogE(ebuf); throw VideoException(VideoException::EFile); } /* Get stream information */ if(avformat_find_stream_info(pFormatCtx, NULL) < 0){ FlogE("couldn't get stream info"); throw VideoException(VideoException::EStreamInfo); } /* Print video format information */ av_dump_format(pFormatCtx, 0, stream->GetPath().c_str(), 0); // If the loader logged something about wmv being DRM protected, give up if(drm){ throw VideoException(VideoException::EStream); } // find the best audio and video streams audioStream = videoStream = AVERROR_STREAM_NOT_FOUND; pCodec = 0; videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0); if(videoStream == AVERROR_STREAM_NOT_FOUND){ FlogE("couldn't find stream"); throw VideoException(VideoException::EStream); } if(videoStream == AVERROR_DECODER_NOT_FOUND || !pCodec){ FlogE("unsupported codec"); throw VideoException(VideoException::EVideoCodec); } audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); if(hasAudioStream()){ audioHandler = AudioHandler::Create(pFormatCtx->streams[audioStream]->codec, audioDevice, timeHandler); }else{ audioHandler = AudioHandlerNoSound::Create(audioDevice, timeHandler); FlogD("no audio stream or unsupported audio codec"); } /* Get a pointer to the codec context for the video stream */ pCodecCtx = pFormatCtx->streams[videoStream]->codec; // Open codec if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0){ FlogE("unsupported codec"); throw VideoException(VideoException::EVideoCodec); } w = pCodecCtx->width; h = pCodecCtx->height; // limit framequeue memory size int frameMemSize = avpicture_get_size((AVPixelFormat)pCodecCtx->pix_fmt, w, h); int maxVideoQueueMem = 512 * 1024 * 1024; // 512 MB maxFrameQueueSize = maxVideoQueueMem / frameMemSize; // cap to 256 maxFrameQueueSize = std::min(maxFrameQueueSize, 256); // tick the video so that firstPts and firstDts are set tick(true); }