ok so this is what i have so far but after i call glTexImage2D. nothing is updated on the screen at all. using opengl is a entirely new area for me so I have been looking over trying to figure out what I am doing wrong to not have anything updated on the screen
void MainScene::onDraw(const Mat4 &transform, uint32_t flags)
{
const char * input_filename = "/home/testuser/Desktop/projecttest/Classes/testvid.mp4";
AVFormatContext *pFormatCtx = nullptr;
avformat_open_input(&pFormatCtx,input_filename,nullptr,nullptr);
avformat_find_stream_info(pFormatCtx, nullptr);
av_dump_format(pFormatCtx, 0, input_filename, 0);
AVCodec *pCodec = nullptr;
AVCodecParameters *pCodecParameters = nullptr;
int video_stream_index = -1;
for(unsigned int i = 0; i < pFormatCtx->nb_streams; i++){
AVCodecParameters * LocalCodecParameters = nullptr;
LocalCodecParameters = pFormatCtx->streams[i]->codecpar;
std::cout << "AVStream->time_base before open coded " << pFormatCtx->streams[i]->time_base.num << pFormatCtx->streams[i]->time_base.den << std::endl;
std::cout << "AVStream->r_frame_rate before open coded " << pFormatCtx->streams[i]->r_frame_rate.num << pFormatCtx->streams[i]->r_frame_rate.den << std::endl;
std::cout << "AVStream->start_time " << pFormatCtx->streams[i]->start_time << std::endl;
std::cout << "AVStream->duration " << pFormatCtx->streams[i]->duration << std::endl;
std::cout << "finding the proper decoder (CODEC)" << std::endl;
AVCodec *pLocalCodec = nullptr;
pLocalCodec = avcodec_find_decoder(LocalCodecParameters->codec_id);
if(pLocalCodec == nullptr) {
logging("ERROR unsupported codec!");
}
if (LocalCodecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {
if (video_stream_index == -1) {
video_stream_index = i;
pCodec = pLocalCodec;
pCodecParameters = LocalCodecParameters;
}
std::cout << "Video Codec: resolution" << LocalCodecParameters->width << " x " << LocalCodecParameters->height << std::endl;
} else if (LocalCodecParameters->codec_type == AVMEDIA_TYPE_AUDIO) {
std::cout << "Audio Codec: " << LocalCodecParameters->channels << " channels " << " sample rate " << LocalCodecParameters->sample_rate << std::endl;
}
std::cout << "Codec " << pLocalCodec->name << " ID " << pLocalCodec->id << " bit_rate " << pCodecParameters->bit_rate << std::endl;
}
AVCodecContext * pCodecContext = avcodec_alloc_context3(pCodec);
if (!pCodecContext)
{
logging("failed to allocated memory for AVCodecContext");
}
if (avcodec_parameters_to_context(pCodecContext, pCodecParameters) < 0)
{
logging("failed to copy codec params to codec context");
}
if (avcodec_open2(pCodecContext, pCodec, nullptr) < 0)
{
logging("failed to open codec through avcodec_open2");
}
AVFrame *pFrame = av_frame_alloc();
if (!pFrame)
{
logging("failed to allocated memory for AVFrame");
}
AVPacket *pPacket = av_packet_alloc();
if (!pPacket)
{
logging("failed to allocated memory for AVPacket");
}
int response = 0;
int how_many_packets_to_process = 8;
while (av_read_frame(pFormatCtx, pPacket) >= 0)
{
if (pPacket->stream_index == video_stream_index) {
logging("AVPacket->pts %" PRId64, pPacket->pts);
// Supply raw packet data as input to a decoder
response = avcodec_send_packet(pCodecContext, pPacket);
if (response < 0) {
//logging("Error while sending a packet to the decoder: %s", av_err2str(response));
}
while (response >= 0)
{
// Return decoded output data (into a frame) from a decoder
// https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
response = avcodec_receive_frame(pCodecContext, pFrame);
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
break;
} else if (response < 0) {
//logging("Error while receiving a frame from the decoder: %s", av_err2str(response));
}
if (response >= 0) {
logging(
"Frame %d (type=%c, size=%d bytes) pts %d key_frame %d [DTS %d]",
pCodecContext->frame_number,
av_get_picture_type_char(pFrame->pict_type),
pFrame->pkt_size,
pFrame->pts,
pFrame->key_frame,
pFrame->coded_picture_number
);
glTexImage2D(GL_TEXTURE_2D,
0,
3,
pFrame->width,
pFrame->height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
pFrame->data[0]);
}
}
if (response < 0)
break;
if (--how_many_packets_to_process <= 0) break;
}
av_packet_unref(pPacket);
}
logging("releasing all the resources");
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
av_packet_free(&pPacket);
av_frame_free(&pFrame);
avcodec_free_context(&pCodecContext);
}
void MainScene::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
//renderer = Director::getInstance()->getRenderer();
RenderTexture * VideoTest = RenderTexture::create(1280, 720);
VideoTest->setPosition(960,540);
_customCommand.init(VideoTest->getGlobalZOrder(), transform, flags);
_customCommand.func = CC_CALLBACK_0(MainScene::onDraw, this, transform, flags);
renderer->addCommand(&_customCommand);
}