VLC播放 RTP流的 音频 视频的 sdp描述文件


http://www.itdadao.com/articles/c15a234502p0.html

我们可以自己把音频(AAC)或视频(h264)数据 封装为RTP包 然后,通过UDP发送到一个端口 ,通过VLC播放,但VLC播放时不同于播放网络流方式,而是通过打开一个后缀名为sdp的文件来播放网络rtp流

个人测试了视频h264  音频aac  。PCM格式还没测试

打开一个文件,修改文件名为video.sdp   一下汉字为注解 ,不需要在video.sdp文件中。

 m=video 6688 RTP/AVP 96   //这里意思是 VLC要通过6688接收数据  同时发送端也要发送到这个端口。另外要注意端口别和其他程序重复了,例如注意 5678端口同时是迅雷的一个端口 ,不要用,害的我花了半天时间。96的意思是动态的
a=rtpmap:96 H264       //到这里告诉了VLC 编码方式为h264

c=IN IP4 109.112.145.38   //这一行 我由于发送数据程序也是在本机vlc也是在本机 所以测试的时候并没有,写到这里 只是为了假如你看了博客后 如果没这条语句,而不能播放的话,请你加上

同理音频(aac格式)如下:

m=audio 10020 RTP/AVP 96      同理10020为目标端口和vlc接收端口
a=rtpmap:96 mpeg4-generic/44100
 //  44100为采样率 假如你是22050 这里要改正。不要问我 mpeg4-generic啥意思 我是从live555里跟踪出来的。
a=fmtp:96 streamtype=5; profile-level-id=15; mode=AAC-hbr; config=1210; SizeLength=13; IndexLength=3; IndexDeltaLength=3;Profile=1//这一行很重要,虽然不知道里边的内容那个是必须的。profile中指定1表示低复杂度类型,config为AAC的详细格式信息
c=IN IP4 172.16.2.155//同理地址。

干净代码如下:

m=video 5678 RTP/AVP 96

96track->payload_type     视频:96 音频:97
a=rtpmap:96 H264

m=audio 10020 RTP/AVP 97
a=rtpmap:97
mpeg4-generic/44100
a=fmtp:97 streamtype=5; profile-level-id=15; mode=AAC-hbr; config=1210; SizeLength=13; IndexLength=3; IndexDeltaLength=3;Profile=1
c=IN IP4 172.16.2.155

streamtype=5; profile-level-id=15; mode=AAC-hbr:写死

SizeLength=13; IndexLength=3; IndexDeltaLength=3; Profile=1:写死


我会上传一个打包h264为rtp和打包aac为rtp的工程。

请交流QQ493061330

aac文件 每帧七个字节头 

  u_int8_t profile = (fixedHeader[2]&0xC0)>>6; // 2 bits 得到Profile
    if (profile == 3) {
      env.setResultMsg("Bad (reserved) 'profile': 3 in first frame of ADTS file");
      break;
    }


    // Get and check the 'sampling_frequency_index': 得到采样率
    u_int8_t sampling_frequency_index = (fixedHeader[2]&0x3C)>>2; // 4 bits
    if (samplingFrequencyTable[sampling_frequency_index] == 0) {
      env.setResultMsg("Bad 'sampling_frequency_index' in first frame of ADTS file");
      break;
    }


    // Get and check the 'channel_configuration': 得到通道数
    u_int8_t channel_configuration
      = ((fixedHeader[2]&0x01)<<2)|((fixedHeader[3]&0xC0)>>6); // 3 bits


sdp中的config 计算方法

 
 
音频:
audioObjectType:5
samplingFrequencyIndex:4
channelConfiguration:4 
reserve:3
 
代码:
UINT8 audioConfig[2] = {0}; 
UINT8 const audioObjectType = profile + 1; 
audioConfig[0] = (audioObjectType<<3) | (samplingFrequencyIndex>>1); 
audioConfig[1] = (samplingFrequencyIndex<<7) | (channelConfiguration<<3); 
printf("%02x%02x", audioConfig[0], audioConfig[1]);
 
对本例来说:
audioObjectType = profile(1) + 1 = 0x02;
samplingFrequencyIndex = 0x04(44100)
channelConfiguration = 0x02(立体声)
故:
0 1 2 3|4 5 6 7|8 9 a b|c d e f
0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0
 
config= 1210,与文件中描述相符。

 unsigned char audioSpecificConfig[2];
  u_int8_t const audioObjectType = profile + 1;
  audioSpecificConfig[0] = (audioObjectType<<3) | (samplingFrequencyIndex>>1);
  audioSpecificConfig[1] = (samplingFrequencyIndex<<7) | (channelConfiguration<<3);
  sprintf(fConfigStr, "%02X%02x", audioSpecificConfig[0], audioSpecificConfig[1]);


a=fmtp:96streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1210


streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; 此部分固定 摘自live555 config为上边的到的config



双声道 PCM 44100HZ 16位  左右声道交替存放 即2字节左2字节右交替,编aac时 4096个字节 编一帧 即左右各1024个采样混在一起编一帧


打包RTP时间戳 如下


例如 读七个字节 分析 出帧大小 再读取帧大小减去7个字节 则为纯数据 

12个字节RTP头 加上四个字节

sendbuf[12] = 0; sendbuf[13] = 16 /* bits */; // AU-headers-length
sendbuf[14] = (Framelen-7) >> 5; sendbuf[15] = ((Framelen-7)&0x1F)<<3;

RTP时间戳 如下 假如开始取起始时间 timeStart = GetTickCount();

则偏移时间为int timeDifferent= GetTickCount()-timeStart;

unsigned int  sampling_frequency_index = (adts_headerbuf[2]&0x3C)>>2;取采样率索引
int  sampling_frequency=mpeg4audio_sample_rates[sampling_frequency_index];
unsigned int timestampIncrement = (sampling_frequency*((int)timestamp/1000));//采样率乘以秒数差
        timestamp=(int)timestamp%1000;
timestampIncrement += (unsigned int)(sampling_frequency*((double)timestamp/1000.0)+ 0.5); // 再加上毫秒数乘积

rtp_hdr->timestamp = htonl(timestampIncrement);   



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值