-
Notifications
You must be signed in to change notification settings - Fork 11
/
rtp_enc.c
executable file
·315 lines (282 loc) · 6.65 KB
/
rtp_enc.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "comm.h"
#include "rtp_enc.h"
struct rtphdr
{
#ifdef __BIG_ENDIAN__
uint16_t v : 2;
uint16_t p : 1;
uint16_t x : 1;
uint16_t cc : 4;
uint16_t m : 1;
uint16_t pt : 7;
#else
uint16_t cc : 4;
uint16_t x : 1;
uint16_t p : 1;
uint16_t v : 2;
uint16_t pt : 7;
uint16_t m : 1;
#endif
uint16_t seq;
uint32_t ts;
uint32_t ssrc;
};
#define RTPHDR_SIZE (12)
int rtp_enc_h264(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[])
{
int count = 0;
uint8_t nalhdr;
uint32_t rtp_ts;
if (!e || !frame || len <= 0 || !packets || !pktsizs)
return -1;
//drop 0001
if (frame[0] == 0 && frame[1] == 0 && frame[2] == 1)
{
frame += 3;
len -= 3;
}
if (frame[0] == 0 && frame[1] == 0 && frame[2] == 0 && frame[3] == 1)
{
frame += 4;
len -= 4;
}
nalhdr = frame[0];
rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000);
while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE)
{
struct rtphdr *hdr = (struct rtphdr *)packets[count];
int pktsiz = pktsizs[count];
hdr->v = 2;
hdr->p = 0;
hdr->x = 0;
hdr->cc = 0;
hdr->m = 0;
hdr->pt = e->pt;
hdr->seq = htons(e->seq++);
hdr->ts = htonl(rtp_ts);
hdr->ssrc = htonl(e->ssrc);
if (count == 0 && len <= pktsiz - RTPHDR_SIZE)
{
hdr->m = 1;
memcpy(packets[count] + RTPHDR_SIZE, frame, len);
pktsizs[count] = RTPHDR_SIZE + len;
frame += len;
len -= len;
}
else
{
int mark = 0;
if (count == 0)
{
frame++; //drop nalu header
len--;
}
else if (len <= pktsiz - RTPHDR_SIZE - 2)
{
mark = 1;
}
hdr->m = mark;
packets[count][RTPHDR_SIZE + 0] = (nalhdr & 0xe0) | 28; //FU-A
packets[count][RTPHDR_SIZE + 1] = (nalhdr & 0x1f); //FU-A
if (count == 0)
{
packets[count][RTPHDR_SIZE + 1] |= 0x80; //S
}
if (mark)
{
packets[count][RTPHDR_SIZE + 1] |= 0x40; //E
memcpy(packets[count] + RTPHDR_SIZE + 2, frame, len);
pktsizs[count] = RTPHDR_SIZE + 2 + len;
frame += len;
len -= len;
}
else
{
memcpy(packets[count] + RTPHDR_SIZE + 2, frame, pktsiz - RTPHDR_SIZE - 2);
pktsizs[count] = pktsiz;
frame += pktsiz - RTPHDR_SIZE - 2;
len -= pktsiz - RTPHDR_SIZE - 2;
}
}
count++;
}
return count;
}
int rtp_enc_h265(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[])
{
int count = 0;
uint8_t nalhdr[2];
uint32_t rtp_ts;
if (!e || !frame || len <= 0 || !packets || !pktsizs)
return -1;
//drop 0001
if (frame[0] == 0 && frame[1] == 0 && frame[2] == 1)
{
frame += 3;
len -= 3;
}
if (frame[0] == 0 && frame[1] == 0 && frame[2] == 0 && frame[3] == 1)
{
frame += 4;
len -= 4;
}
nalhdr[0] = frame[0];
nalhdr[1] = frame[1];
rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000);
while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE)
{
struct rtphdr *hdr = (struct rtphdr *)packets[count];
int pktsiz = pktsizs[count];
hdr->v = 2;
hdr->p = 0;
hdr->x = 0;
hdr->cc = 0;
hdr->m = 0;
hdr->pt = e->pt;
hdr->seq = htons(e->seq++);
hdr->ts = htonl(rtp_ts);
hdr->ssrc = htonl(e->ssrc);
if (count == 0 && len <= pktsiz - RTPHDR_SIZE)
{
hdr->m = 1;
memcpy(packets[count] + RTPHDR_SIZE, frame, len);
pktsizs[count] = RTPHDR_SIZE + len;
frame += len;
len -= len;
}
else
{
int mark = 0;
if (count == 0)
{
frame += 2; //drop nalu header
len -= 2;
}
else if (len <= pktsiz - RTPHDR_SIZE - 3)
{
mark = 1;
}
hdr->m = mark;
packets[count][RTPHDR_SIZE + 0] = (nalhdr[0] & 0x81) | (49 << 1); //FU-A
packets[count][RTPHDR_SIZE + 1] = (nalhdr[1]);
packets[count][RTPHDR_SIZE + 2] = ((nalhdr[0] >> 1) & 0x3f); //FU-A
if (count == 0)
{
packets[count][RTPHDR_SIZE + 2] |= 0x80; //S
}
if (mark)
{
packets[count][RTPHDR_SIZE + 2] |= 0x40; //E
memcpy(packets[count] + RTPHDR_SIZE + 3, frame, len);
pktsizs[count] = RTPHDR_SIZE + 3 + len;
frame += len;
len -= len;
}
else
{
memcpy(packets[count] + RTPHDR_SIZE + 3, frame, pktsiz - RTPHDR_SIZE - 3);
pktsizs[count] = pktsiz;
frame += pktsiz - RTPHDR_SIZE - 3;
len -= pktsiz - RTPHDR_SIZE - 3;
}
}
count++;
}
return count;
}
int rtp_enc_aac(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[])
{
int count = 0;
uint32_t rtp_ts;
uint32_t au_len;
if (!e || !frame || len <= 0 || !packets || !pktsizs)
return -1;
//drop fff
if (frame[0] == 0xff && (frame[1] & 0xf0) == 0xf0)
{
frame += 7;
len -= 7;
}
rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000);
au_len = len;
while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE + 4)
{
struct rtphdr *hdr = (struct rtphdr *)packets[count];
int pktsiz = pktsizs[count];
hdr->v = 2;
hdr->p = 0;
hdr->x = 0;
hdr->cc = 0;
hdr->m = 0;
hdr->pt = e->pt;
hdr->seq = htons(e->seq++);
hdr->ts = htonl(rtp_ts);
hdr->ssrc = htonl(e->ssrc);
packets[count][RTPHDR_SIZE + 0] = 0x00;
packets[count][RTPHDR_SIZE + 1] = 0x10;
packets[count][RTPHDR_SIZE + 2] = au_len >> 5;
packets[count][RTPHDR_SIZE + 3] = (au_len & 0x1f) << 3;
if (len <= pktsiz - RTPHDR_SIZE - 4)
{
hdr->m = 1;
memcpy(packets[count] + RTPHDR_SIZE + 4, frame, len);
pktsizs[count] = RTPHDR_SIZE + 4 + len;
frame += len;
len -= len;
}
else
{
memcpy(packets[count] + RTPHDR_SIZE + 4, frame, pktsiz - RTPHDR_SIZE - 4);
pktsizs[count] = pktsiz;
frame += pktsiz - RTPHDR_SIZE - 4;
len -= pktsiz - RTPHDR_SIZE - 4;
}
count++;
}
return count;
}
int rtp_enc_g711(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[])
{
int count = 0;
uint32_t rtp_ts;
if (!e || !frame || len <= 0 || !packets || !pktsizs)
return -1;
rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000);
while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE)
{
struct rtphdr *hdr = (struct rtphdr *)packets[count];
int pktsiz = pktsizs[count];
hdr->v = 2;
hdr->p = 0;
hdr->x = 0;
hdr->cc = 0;
hdr->m = (e->seq == 0);
hdr->pt = e->pt;
hdr->seq = htons(e->seq++);
hdr->ts = htonl(rtp_ts);
hdr->ssrc = htonl(e->ssrc);
if (len <= pktsiz - RTPHDR_SIZE)
{
memcpy(packets[count] + RTPHDR_SIZE, frame, len);
pktsizs[count] = RTPHDR_SIZE + len;
frame += len;
len -= len;
}
else
{
memcpy(packets[count] + RTPHDR_SIZE, frame, pktsiz - RTPHDR_SIZE);
pktsizs[count] = pktsiz;
frame += pktsiz - RTPHDR_SIZE;
len -= pktsiz - RTPHDR_SIZE;
}
count++;
}
return count;
}
int rtp_enc_g726(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[])
{
return rtp_enc_g711(e, frame, len, ts, packets, pktsizs);
}