[Libav-user] problem while creating sdp with h264/opus encoding

Peter Pan hawkwithwind at gmail.com
Wed Sep 10 13:00:13 CEST 2014


Hi

I'm trying to use libavformat as RTSP client for pushing stream to server.
I'm using Darwin Streaming Server. I'm using h264/opus encoding.

I find a problem that the sdp file always set audio sampling rate to
48000hz,
no matter how I set the AVFormatContext before avformat_write_header() call.

the sdp file looks like below:

v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 121.***.***.***
t=0 0
a=tool:libavformat 55.48.100
m=video 0 RTP/AVP 96
b=AS:128
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1
a=control:streamid=0
m=audio 0 RTP/AVP 97
b=AS:16
a=rtpmap:97 opus/48000
a=control:streamid=1

I find that in libavformat/sdp.c file, the 48000 is hard coded.
Is this what meant to be?
(I've masked out the real ip address)

case AV_CODEC_ID_OPUS:
            av_strlcatf(buff, size, "a=rtpmap:%d opus/48000\r\n",
                                     payload_type);
            break;

comparing to speex case branch:

case AV_CODEC_ID_SPEEX:
            av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
                                     payload_type, c->sample_rate);

it is using the real sample rate, what confused me.

below is my code, if there's any mistake please tell me,
I just learn to use avlibs recently.
I referenced mostly from this link
https://www.ffmpeg.org/doxygen/0.6/output-example_8c-source.html

int live_rtsp_start(int fps){
  AVOutputFormat *fmt;
  int ret;

  const int buffsize = 1024;
  char logbuff[buffsize];
  char server[buffsize];

  av_register_all();
  avformat_network_init();

  //oc = avformat_alloc_context();
  snprintf(server, buffsize, "rtsp://%s/%s", "121.***.***.***",
"live12.sdp");
  avformat_alloc_output_context2(&oc, 0, "rtsp", server);
  if (!oc) {
    androidlog("memory error");
    return -1;
  }
  androidlog(oc->filename);

  //create video stream
  AVStream * v_stream = avformat_new_stream(oc, NULL);
  if(!v_stream){
    return -4;
  }
  v_stream->id = 0;

  AVCodecContext* codec = v_stream->codec;
  codec->codec_id = CODEC_ID_H264;
  codec->codec_type = AVMEDIA_TYPE_VIDEO;
  codec->width = out_width;
  codec->height = out_height;
  codec->time_base.den = 1;
  codec->time_base.num = fps;

  if(oc->oformat->flags & AVFMT_GLOBALHEADER){
    codec->flags != CODEC_FLAG_GLOBAL_HEADER;
  }

  //create audio stream
  AVStream * a_stream = avformat_new_stream(oc, NULL);
  if(!a_stream){
    return -5;
  }
  a_stream->id = 1;

  AVCodecContext* a_codec = a_stream->codec;
  a_codec->codec_type = AVMEDIA_TYPE_AUDIO;
  a_codec->codec_id = CODEC_ID_OPUS;
  a_codec->channels = 1;
  a_codec->sample_rate = 8000;
  a_codec->time_base = (AVRational){1, a_codec->sample_rate};
  a_codec->frame_size = 320;
  a_codec->bit_rate = 16000;
  //a_codec->codec_tag = av_codec_get_tag(oc, CODEC_ID_OPUS);

  if(oc->oformat->flags & AVFMT_GLOBALHEADER){
    a_codec->flags != CODEC_FLAG_GLOBAL_HEADER;
  }

  av_dump_format(oc, 0, server ,1);

  androidlog("write header\n");
  ret = avformat_write_header(oc, NULL);

  if(ret < 0){
    av_strerror(ret, logbuff, buffsize);
    androidlog("avformat_write_header err\n");
    androidlog(logbuff);
    return ret;
  }
}

Thanks,
Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20140910/ab0a4adb/attachment.html>


More information about the Libav-user mailing list