您的位置:首页 > 编程语言 > PHP开发

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

2018-01-23 15:21 513 查看
我们可以自己把音频(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//这一行很重要,虽然不知道里边的内容那个是必须的。

c=IN IP4 172.16.2.155//同理地址。

干净代码如下:

m=video 5678 RTP/AVP 96

a=rtpmap:96 H264

m=audio 10020 RTP/AVP 96

a=rtpmap:96 mpeg4-generic/44100

a=fmtp:96 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

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 计算方法

 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);   

转载:http://blog.csdn.net/tianweibooo/article/details/50915104
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: