cannot get custom IO to work.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

cannot get custom IO to work.

PeterTaps
Folks,

I am trying to create a custom input stream. However, avformat_open_input fails will a random error value. I would appreciate it if someone can tell me what is it that I am doing wrong. Here is the simplified code.

Thank you in advance for your help.

Regards,
Peter

#define BUFSIZE (4 * 1024)

static int ReadPacket(void* opaque, uint8_t* buf, int bufsize) {
   FILE* file = (FILE*) opaque;

   int len = ::fread(buf, 1, bufsize, file);
   return len;
}


int main(int argc, char* argv[]) {

   av_register_all();
   // av_log_set_level(AV_LOG_DEBUG);

   const char* fileName = "/home/peter/sample_mpeg4.mp4";
   FILE* file = fopen(fileName, "rb");

   unsigned char* buffer = (unsigned char*) av_malloc(BUFSIZE);

   AVIOContext* avContext = avio_alloc_context(buffer, BUFSIZE, 1, file, ReadPacket, NULL, NULL);

   AVFormatContext* pFormatCtx = avformat_alloc_context();
   pFormatCtx->pb = avContext;

   int val = avformat_open_input(&pFormatCtx, fileName, NULL, NULL);
   if (val != 0) {
      printf("Open input failed. Err: %d\n", val);
      pFormatCtx = 0; // it is already freed
      av_free(avContext);
      av_free(buffer);
      fclose(file);
      return -1;
   }