1. 程式人生 > >RTSP to RTMP 基於 Live555

RTSP to RTMP 基於 Live555

1.前期準備:

Live555

rtmpdunp

2.設計思路:

使用Live555接收碼流

將碼流封裝成RTMP格式

使用librtmp將碼流推送到直播平臺。

3.相關程式碼:

3.2.rtsp2rtmp

3.2.1 AAC info header

unsigned char body[4];
body[0] = 0xaf;
body[1] = 0x00;

switch (profile){
 case 0:
	body[2]|=(1<<3);//main
	break;
 case 1:
	body[2]|=(1<<4);//LC
	break;
 case 2:
	body[2]|=(1<<3);//SSR
	body[2]|=(1<<4);
	break;
 default:
	;
}
switch(channel){
 case 1:
	body[3]|=(1<<3);//channel1
	break;
 case 2:
	body[3]|=(1<<4);//channel2
	break;
 default:
	;
}
switch(rate){
 case 48000:
	body[2]|=(1);
	body[3]|=(1<<7);
	break;
 case 44100:
	body[2]|=(1<<1);
	break;
 case 32000:
	body[2]|=(1<<1);
	body[3]|=(1<<7);
	break;
 case 24000:
	body[2]|=(1<<1);
	body[2]|=(1);
	break;
 case 22050:
	body[2]|=(1<<1);
	body[2]|=(1);
	body[3]|=(1<<7);
	break;
 case 16000:
	body[2]|=(1<<2);
	break;
 case 8000:
	body[2]|=(1<<2);
	body[2]|=(1);
	body[3]|=(1<<7);
 default:
	;
}

3.2.2.AAC data frame

只需要在資料幀前面加上2個byte的header:

spec_info[0] = 0xAF;
spec_info[1] = 0x01;

3.2.3 H.264 info header

unsigned char *body;

body = (unsigned char *)malloc(16 + sps_len + pps_len);
memset(body, 0, 16 + sps_len + pps_len);

body[i++] = 0x17;	// 1: IFrame, 7: AVC
					// AVC Sequence Header
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;

// AVCDecoderConfigurationRecord
body[i++] = 0x01;
body[i++] = sps[1];
body[i++] = sps[2];
body[i++] = sps[3];
body[i++] = 0xff;
body[i++] = 0xe1;
body[i++] = (sps_len >> 8) & 0xff;
body[i++] = sps_len & 0xff;
for (size_t j = 0; j < sps_len; j++)
{
	body[i++] = sps[j];
}
body[i++] = 0x01;
body[i++] = (pps_len >> 8) & 0xff;
body[i++] = pps_len & 0xff;
for (size_t j = 0; j < pps_len; j++)
{
	body[i++] = pps[j];
}
3.2.4 H.264 data frame
body = data - 9;
memset(body, 0, 9);
i = 0;
if (bIsKeyFrame== 0) {
	body[i++] = 0x17;	// 1: IFrame, 7: AVC
}
else {
	body[i++] = 0x27;	// 2: PFrame, 7: AVC
}
// AVCVIDEOPACKET
body[i++] = 0x01;
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;

// NALUs
body[i++] = size >> 24 & 0xff;
body[i++] = size >> 16 & 0xff;
body[i++] = size >> 8 & 0xff;
body[i++] = size & 0xff;
memcpy(&body[i], data, size);


<最近在做一些直播相關的專案,希望將一些思路記錄下來。供自己及大家參考!>