- 初始化rtmp
1 | //分配空间 |
- H264包封装。在发送每一帧关键帧之前得先发送SPS、PPS帧信息,发送的每一帧(I、P、SPS、PPS)数据得添加头部信息。
2.1 SPS PPS数据1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
void RtmpPush::pushSPSPPS(char *sps, int spsLen, char *pps, int ppsLen) {
if (!this->queue) return;
int bodySize = spsLen + ppsLen + 16;
RTMPPacket *rtmpPacket = static_cast<RTMPPacket *>(malloc(sizeof(RTMPPacket)));
RTMPPacket_Alloc(rtmpPacket, bodySize);
RTMPPacket_Reset(rtmpPacket);
char *body = rtmpPacket->m_body;
int i = 0;
//frame type(4bit)和CodecId(4bit)合成一个字节(byte)
//frame type 关键帧1 非关键帧2
//CodecId 7表示avc
body[i++] = 0x17;
//fixed 4byte
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;
//configurationVersion: 版本 1byte
body[i++] = 0x01;
//AVCProfileIndication:Profile 1byte sps[1]
body[i++] = sps[1];
//compatibility: 兼容性 1byte sps[2]
body[i++] = sps[2];
//AVCLevelIndication: ProfileLevel 1byte sps[3]
body[i++] = sps[3];
//lengthSizeMinusOne: 包长数据所使用的字节数 1byte
body[i++] = 0xff;
//sps个数 1byte
body[i++] = 0xe1;
//sps长度 2byte
body[i++] = (spsLen >> 8) & 0xff;
body[i++] = spsLen & 0xff;
//sps data 内容
memcpy(&body[i], sps, spsLen);
i += spsLen;
//pps个数 1byte
body[i++] = 0x01;
//pps长度 2byte
body[i++] = (ppsLen >> 8) & 0xff;
body[i++] = ppsLen & 0xff;
//pps data 内容
memcpy(&body[i], pps, ppsLen);
rtmpPacket->m_packetType = RTMP_PACKET_TYPE_VIDEO;
rtmpPacket->m_nBodySize = bodySize;
rtmpPacket->m_nTimeStamp = 0;
rtmpPacket->m_hasAbsTimestamp = 0;
rtmpPacket->m_nChannel = 0x04;//音频或者视频
rtmpPacket->m_headerType = RTMP_PACKET_SIZE_MEDIUM;
rtmpPacket->m_nInfoField2 = this->rtmp->m_stream_id;
queue->putRtmpPacket(rtmpPacket);
}
2.2 H264数据
1 | void RtmpPush::pushVideoData(char *data, int dataLen, bool keyFrame) { |
- AAC包封装 需要添加头部
1 |
|
- Android MediaCodec获取PPS和SPS
1 | int outputBufferIndex = videoEncodec.dequeueOutputBuffer(videoBufferinfo, 0); |