[Ffmpeg-devel] how to decode RTP packet containing mp3 data

sanjay kumar gupta sanjay417
Wed Feb 21 18:26:08 CET 2007


Hi All,
  Helix server in LAN is streaming mp3 file and I am receiving the RTP packets.
  I would like to decode this RTP packets data and get the decoded buffer and its length so that I can play this decoded data using SDL library.
I have a program which plays mp3 locally. To play mp3 file locally, I needed format context and codec context pointers. Piece of code used to decode the local audio file is given below.
But in case of RTP packet containing mp3 data.
how to get the pFormatCtx. can we get the stream info as with local file as it is needed to get the pCodecCtx which in turn used to get the codec id.
Any example code or document would be helpful.
Thanks in advance for help.
//---------------
// Register all formats and codecs
  av_register_all();

  // Open video file
  if(av_open_input_file(&pFormatCtx, argv[1]/*mp3 file name*/, NULL, 0, NULL)!=0)
     return -1; // Couldn't open file

  // Retrieve stream information
  if(av_find_stream_info(pFormatCtx)<0)
          return -1; // Couldn't find stream information

     // Find the first video stream
     audioStream=-1;
     for(i=0; i<pFormatCtx->nb_streams; i++)
     {
     if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
          {
               audioStream=i;
               break;
          }
     }
     if(audioStream == -1)
          return -1; // Didn't find a video/audio stream

     // Get a pointer to the codec context for the video stream
     pCodecCtx=pFormatCtx->streams[audioStream]->codec;

     // Find the decoder for the video stream
     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
     if(pCodec==NULL)
          return -1; // Codec not found

     // Open codec
     if(avcodec_open(pCodecCtx, pCodec)<0)
          return -1; // Could not open codec

     while(av_read_frame(pFormatCtx, &packet)>= 0 )
     {
          // Is this a packet from the audio stream?
          if(packet.stream_index==audioStream)
          {
               // Decode audio frame
               len=avcodec_decode_audio(pCodecCtx, outBuf, &outBufSize, packet.data, packet.size);
               if (len < 0)
               {
                    printf("Error while decoding\n");
               }
               else
               {
                          //send this decoded audio to SDL.
               }
          }
          // Free the packet that was allocated by av_read_frame
          av_free_packet(&packet);
     }
//---------------------

Thanks & Regards,
Sanjay




More information about the ffmpeg-devel mailing list