FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/sdp.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 42 521 8.1%
Functions: 4 13 30.8%
Branches: 12 299 4.0%

Line Branch Exec Source
1 /*
2 * copyright (c) 2007 Luca Abeni
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config_components.h"
22
23 #include <string.h>
24 #include "libavutil/avstring.h"
25 #include "libavutil/base64.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/opt.h"
30 #include "libavcodec/xiph.h"
31 #include "libavcodec/mpeg4audio.h"
32 #include "avformat.h"
33 #include "internal.h"
34 #include "avc.h"
35 #include "hevc.h"
36 #include "rtp.h"
37 #include "version.h"
38 #if CONFIG_NETWORK
39 #include "network.h"
40 #endif
41
42 #if CONFIG_RTP_MUXER
43 #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
44
45 struct sdp_session_level {
46 int sdp_version; /**< protocol version (currently 0) */
47 int id; /**< session ID */
48 int version; /**< session version */
49 int start_time; /**< session start time (NTP time, in seconds),
50 or 0 in case of permanent session */
51 int end_time; /**< session end time (NTP time, in seconds),
52 or 0 if the session is not bounded */
53 int ttl; /**< TTL, in case of multicast stream */
54 const char *user; /**< username of the session's creator */
55 const char *src_addr; /**< IP address of the machine from which the session was created */
56 const char *src_type; /**< address type of src_addr */
57 const char *dst_addr; /**< destination IP address (can be multicast) */
58 const char *dst_type; /**< destination IP address type */
59 const char *name; /**< session name (can be an empty string) */
60 };
61
62 2 static void sdp_write_address(char *buff, int size, const char *dest_addr,
63 const char *dest_type, int ttl)
64 {
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (dest_addr) {
66 if (!dest_type)
67 dest_type = "IP4";
68 if (ttl > 0 && !strcmp(dest_type, "IP4")) {
69 /* The TTL should only be specified for IPv4 multicast addresses,
70 * not for IPv6. */
71 av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
72 } else {
73 av_strlcatf(buff, size, "c=IN %s %s\r\n", dest_type, dest_addr);
74 }
75 }
76 2 }
77
78 static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
79 {
80 av_strlcatf(buff, size, "v=%d\r\n"
81 "o=- %d %d IN %s %s\r\n"
82 "s=%s\r\n",
83 s->sdp_version,
84 s->id, s->version, s->src_type, s->src_addr,
85 s->name);
86 sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);
87 av_strlcatf(buff, size, "t=%d %d\r\n"
88 "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
89 s->start_time, s->end_time);
90 }
91
92 #if CONFIG_NETWORK
93 static int resolve_destination(char *dest_addr, int size, char *type,
94 int type_size)
95 {
96 struct addrinfo hints = { 0 }, *ai;
97 int is_multicast;
98
99 av_strlcpy(type, "IP4", type_size);
100 if (!dest_addr[0])
101 return 0;
102
103 /* Resolve the destination, since it must be written
104 * as a numeric IP address in the SDP. */
105
106 if (getaddrinfo(dest_addr, NULL, &hints, &ai))
107 return 0;
108 getnameinfo(ai->ai_addr, ai->ai_addrlen, dest_addr, size,
109 NULL, 0, NI_NUMERICHOST);
110 #ifdef AF_INET6
111 if (ai->ai_family == AF_INET6)
112 av_strlcpy(type, "IP6", type_size);
113 #endif
114 is_multicast = ff_is_multicast_address(ai->ai_addr);
115 freeaddrinfo(ai);
116 return is_multicast;
117 }
118 #else
119 static int resolve_destination(char *dest_addr, int size, char *type,
120 int type_size)
121 {
122 return 0;
123 }
124 #endif
125
126 static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
127 {
128 int port;
129 const char *p;
130 char proto[32];
131
132 av_url_split(proto, sizeof(proto), NULL, 0, dest_addr, size, &port, NULL, 0, url);
133
134 *ttl = 0;
135
136 if (strcmp(proto, "rtp") && strcmp(proto, "srtp")) {
137 /* The url isn't for the actual rtp sessions,
138 * don't parse out anything else than the destination.
139 */
140 return 0;
141 }
142
143 p = strchr(url, '?');
144 if (p) {
145 char buff[64];
146
147 if (av_find_info_tag(buff, sizeof(buff), "ttl", p)) {
148 *ttl = strtol(buff, NULL, 10);
149 } else {
150 *ttl = 5;
151 }
152 }
153
154 return port;
155 }
156
157 #define MAX_PSET_SIZE 1024
158 static int extradata2psets(AVFormatContext *s, const AVCodecParameters *par,
159 char **out)
160 {
161 char *psets, *p;
162 const uint8_t *r;
163 static const char pset_string[] = "; sprop-parameter-sets=";
164 static const char profile_string[] = "; profile-level-id=";
165 uint8_t *extradata = par->extradata;
166 int extradata_size = par->extradata_size;
167 uint8_t *tmpbuf = NULL;
168 const uint8_t *sps = NULL, *sps_end;
169
170 *out = NULL;
171
172 if (par->extradata_size > MAX_EXTRADATA_SIZE) {
173 av_log(s, AV_LOG_ERROR, "Too much extradata!\n");
174 return AVERROR_INVALIDDATA;
175 }
176 if (par->extradata[0] == 1) {
177 int ret = ff_avc_write_annexb_extradata(par->extradata, &extradata,
178 &extradata_size);
179 if (ret < 0)
180 return ret;
181
182 tmpbuf = extradata;
183 }
184
185 psets = av_mallocz(MAX_PSET_SIZE);
186 if (!psets) {
187 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
188 av_free(tmpbuf);
189 return AVERROR(ENOMEM);
190 }
191 memcpy(psets, pset_string, strlen(pset_string));
192 p = psets + strlen(pset_string);
193 r = ff_avc_find_startcode(extradata, extradata + extradata_size);
194 while (r < extradata + extradata_size) {
195 const uint8_t *r1;
196 uint8_t nal_type;
197
198 while (!*(r++));
199 nal_type = *r & 0x1f;
200 r1 = ff_avc_find_startcode(r, extradata + extradata_size);
201 if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
202 r = r1;
203 continue;
204 }
205 if (p != (psets + strlen(pset_string))) {
206 *p = ',';
207 p++;
208 }
209 if (!sps) {
210 sps = r;
211 sps_end = r1;
212 }
213 if (!av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r)) {
214 av_log(s, AV_LOG_ERROR, "Cannot Base64-encode %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"!\n",
215 MAX_PSET_SIZE - (p - psets), r1 - r);
216 av_free(psets);
217 av_free(tmpbuf);
218
219 return AVERROR_INVALIDDATA;
220 }
221 p += strlen(p);
222 r = r1;
223 }
224 if (sps && sps_end - sps >= 4 && p - psets <= MAX_PSET_SIZE - strlen(profile_string) - 7) {
225 memcpy(p, profile_string, strlen(profile_string));
226 p += strlen(p);
227 ff_data_to_hex(p, sps + 1, 3, 0);
228 }
229 av_free(tmpbuf);
230
231 *out = psets;
232 return 0;
233 }
234
235 static int extradata2psets_hevc(const AVCodecParameters *par, char **out)
236 {
237 char *psets;
238 uint8_t *extradata = par->extradata;
239 int extradata_size = par->extradata_size;
240 uint8_t *tmpbuf = NULL;
241 int ps_pos[3] = { 0 };
242 static const char * const ps_names[3] = { "vps", "sps", "pps" };
243 int num_arrays, num_nalus;
244 int pos, i, j, ret = 0;
245
246 *out = NULL;
247
248 // Convert to hvcc format. Since we need to group multiple NALUs of
249 // the same type, and we might need to convert from one format to the
250 // other anyway, we get away with a little less work by using the hvcc
251 // format.
252 if (par->extradata[0] != 1) {
253 AVIOContext *pb;
254
255 ret = avio_open_dyn_buf(&pb);
256 if (ret < 0)
257 return ret;
258
259 ret = ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0);
260 if (ret < 0) {
261 avio_close_dyn_buf(pb, &tmpbuf);
262 goto err;
263 }
264 extradata_size = avio_close_dyn_buf(pb, &extradata);
265 tmpbuf = extradata;
266 }
267
268 if (extradata_size < 23)
269 goto err;
270
271 num_arrays = extradata[22];
272 pos = 23;
273 for (i = 0; i < num_arrays; i++) {
274 int num_nalus, nalu_type;
275 if (pos + 3 > extradata_size)
276 goto err;
277 nalu_type = extradata[pos] & 0x3f;
278 // Not including libavcodec/hevc.h to avoid confusion between
279 // NAL_* with the same name for both H.264 and HEVC.
280 if (nalu_type == 32) // VPS
281 ps_pos[0] = pos;
282 else if (nalu_type == 33) // SPS
283 ps_pos[1] = pos;
284 else if (nalu_type == 34) // PPS
285 ps_pos[2] = pos;
286 num_nalus = AV_RB16(&extradata[pos + 1]);
287 pos += 3;
288 for (j = 0; j < num_nalus; j++) {
289 int len;
290 if (pos + 2 > extradata_size)
291 goto err;
292 len = AV_RB16(&extradata[pos]);
293 pos += 2;
294 if (pos + len > extradata_size)
295 goto err;
296 pos += len;
297 }
298 }
299 if (!ps_pos[0] || !ps_pos[1] || !ps_pos[2])
300 goto err;
301
302 psets = av_mallocz(MAX_PSET_SIZE);
303 if (!psets) {
304 ret = AVERROR(ENOMEM);
305 goto err;
306 }
307
308 psets[0] = '\0';
309
310 for (i = 0; i < 3; i++) {
311 pos = ps_pos[i];
312
313 if (i > 0)
314 av_strlcat(psets, "; ", MAX_PSET_SIZE);
315 av_strlcatf(psets, MAX_PSET_SIZE, "sprop-%s=", ps_names[i]);
316
317 // Skipping boundary checks in the input here; we've already traversed
318 // the whole hvcc structure above without issues
319 num_nalus = AV_RB16(&extradata[pos + 1]);
320 pos += 3;
321 for (j = 0; j < num_nalus; j++) {
322 int len = AV_RB16(&extradata[pos]);
323 int strpos;
324 pos += 2;
325 if (j > 0)
326 av_strlcat(psets, ",", MAX_PSET_SIZE);
327 strpos = strlen(psets);
328 if (!av_base64_encode(psets + strpos, MAX_PSET_SIZE - strpos,
329 &extradata[pos], len)) {
330 av_free(psets);
331 goto err;
332 }
333 pos += len;
334 }
335 }
336 av_free(tmpbuf);
337
338 *out = psets;
339 return 0;
340 err:
341 if (ret >= 0)
342 ret = AVERROR_INVALIDDATA;
343 av_free(tmpbuf);
344 return ret;
345 }
346
347 1 static int extradata2config(AVFormatContext *s, const AVCodecParameters *par,
348 char **out)
349 {
350 char *config;
351
352 1 *out = NULL;
353
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->extradata_size > MAX_EXTRADATA_SIZE) {
355 av_log(s, AV_LOG_ERROR, "Too much extradata!\n");
356 return AVERROR_INVALIDDATA;
357 }
358 1 config = av_malloc(10 + par->extradata_size * 2);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!config) {
360 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
361 return AVERROR(ENOMEM);
362 }
363 1 memcpy(config, "; config=", 9);
364 1 ff_data_to_hex(config + 9, par->extradata, par->extradata_size, 0);
365
366 1 *out = config;
367 1 return 0;
368 }
369
370 static int xiph_extradata2config(AVFormatContext *s, const AVCodecParameters *par,
371 char **out)
372 {
373 uint8_t *config;
374 char *encoded_config;
375 const uint8_t *header_start[3];
376 int headers_len, header_len[3], config_len;
377 int first_header_size, ret;
378
379 *out = NULL;
380
381 switch (par->codec_id) {
382 case AV_CODEC_ID_THEORA:
383 first_header_size = 42;
384 break;
385 case AV_CODEC_ID_VORBIS:
386 first_header_size = 30;
387 break;
388 default:
389 av_log(s, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
390 return AVERROR(ENOSYS);
391 }
392
393 ret = avpriv_split_xiph_headers(par->extradata, par->extradata_size,
394 first_header_size, header_start,
395 header_len);
396 if (ret < 0) {
397 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
398 return ret;
399 }
400
401 headers_len = header_len[0] + header_len[2];
402 config_len = 4 + // count
403 3 + // ident
404 2 + // packet size
405 1 + // header count
406 2 + // header size
407 headers_len; // and the rest
408
409 config = av_malloc(config_len);
410 if (!config)
411 goto xiph_fail;
412
413 encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
414 if (!encoded_config) {
415 av_free(config);
416 goto xiph_fail;
417 }
418
419 config[0] = config[1] = config[2] = 0;
420 config[3] = 1;
421 config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
422 config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
423 config[6] = (RTP_XIPH_IDENT ) & 0xff;
424 config[7] = (headers_len >> 8) & 0xff;
425 config[8] = headers_len & 0xff;
426 config[9] = 2;
427 config[10] = header_len[0];
428 config[11] = 0; // size of comment header; nonexistent
429 memcpy(config + 12, header_start[0], header_len[0]);
430 memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
431
432 av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
433 config, config_len);
434 av_free(config);
435
436 *out = encoded_config;
437 return 0;
438
439 xiph_fail:
440 av_log(s, AV_LOG_ERROR,
441 "Not enough memory for configuration string\n");
442 return AVERROR(ENOMEM);
443 }
444
445 static int latm_context2profilelevel(const AVCodecParameters *par)
446 {
447 /* MP4A-LATM
448 * The RTP payload format specification is described in RFC 3016
449 * The encoding specifications are provided in ISO/IEC 14496-3 */
450
451 int profile_level = 0x2B;
452
453 /* TODO: AAC Profile only supports AAC LC Object Type.
454 * Different Object Types should implement different Profile Levels */
455
456 if (par->sample_rate <= 24000) {
457 if (par->ch_layout.nb_channels <= 2)
458 profile_level = 0x28; // AAC Profile, Level 1
459 } else if (par->sample_rate <= 48000) {
460 if (par->ch_layout.nb_channels <= 2) {
461 profile_level = 0x29; // AAC Profile, Level 2
462 } else if (par->ch_layout.nb_channels <= 5) {
463 profile_level = 0x2A; // AAC Profile, Level 4
464 }
465 } else if (par->sample_rate <= 96000) {
466 if (par->ch_layout.nb_channels <= 5) {
467 profile_level = 0x2B; // AAC Profile, Level 5
468 }
469 }
470
471 return profile_level;
472 }
473
474 static int latm_context2config(AVFormatContext *s, const AVCodecParameters *par,
475 char **out)
476 {
477 /* MP4A-LATM
478 * The RTP payload format specification is described in RFC 3016
479 * The encoding specifications are provided in ISO/IEC 14496-3 */
480
481 uint8_t config_byte[6];
482 int rate_index;
483 char *config;
484
485 *out = NULL;
486
487 for (rate_index = 0; rate_index < 16; rate_index++)
488 if (ff_mpeg4audio_sample_rates[rate_index] == par->sample_rate)
489 break;
490 if (rate_index == 16) {
491 av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
492 return AVERROR(ENOSYS);
493 }
494
495 config_byte[0] = 0x40;
496 config_byte[1] = 0;
497 config_byte[2] = 0x20 | rate_index;
498 config_byte[3] = par->ch_layout.nb_channels << 4;
499 config_byte[4] = 0x3f;
500 config_byte[5] = 0xc0;
501
502 config = av_malloc(6*2+1);
503 if (!config) {
504 av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
505 return AVERROR(ENOMEM);
506 }
507 ff_data_to_hex(config, config_byte, 6, 1);
508
509 *out = config;
510 return 0;
511 }
512
513 2 static int sdp_write_media_attributes(char *buff, int size, const AVStream *st,
514 int payload_type, AVFormatContext *fmt)
515 {
516 2 char *config = NULL;
517 2 const AVCodecParameters *p = st->codecpar;
518 2 int ret = 0;
519
520
2/26
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
2 switch (p->codec_id) {
521 case AV_CODEC_ID_DIRAC:
522 av_strlcatf(buff, size, "a=rtpmap:%d VC2/90000\r\n", payload_type);
523 break;
524 case AV_CODEC_ID_H264: {
525 int mode = 1;
526 if (fmt && fmt->oformat && fmt->oformat->priv_class &&
527 av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
528 mode = 0;
529 if (p->extradata_size) {
530 ret = extradata2psets(fmt, p, &config);
531 if (ret < 0)
532 return ret;
533 }
534 av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
535 "a=fmtp:%d packetization-mode=%d%s\r\n",
536 payload_type,
537 payload_type, mode, config ? config : "");
538 break;
539 }
540 case AV_CODEC_ID_H261:
541 {
542 const char *pic_fmt = NULL;
543 /* only QCIF and CIF are specified as supported in RFC 4587 */
544 if (p->width == 176 && p->height == 144)
545 pic_fmt = "QCIF=1";
546 else if (p->width == 352 && p->height == 288)
547 pic_fmt = "CIF=1";
548 if (payload_type >= RTP_PT_PRIVATE)
549 av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type);
550 if (pic_fmt)
551 av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt);
552 break;
553 }
554 case AV_CODEC_ID_H263:
555 case AV_CODEC_ID_H263P:
556 /* a=framesize is required by 3GPP TS 26.234 (PSS). It
557 * actually specifies the maximum video size, but we only know
558 * the current size. This is required for playback on Android
559 * stagefright and on Samsung bada. */
560 if (!fmt || !fmt->oformat->priv_class ||
561 !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
562 p->codec_id == AV_CODEC_ID_H263P)
563 av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
564 "a=framesize:%d %d-%d\r\n",
565 payload_type,
566 payload_type, p->width, p->height);
567 break;
568 case AV_CODEC_ID_HEVC:
569 if (p->extradata_size) {
570 ret = extradata2psets_hevc(p, &config);
571 if (ret < 0)
572 return ret;
573 }
574 av_strlcatf(buff, size, "a=rtpmap:%d H265/90000\r\n", payload_type);
575 if (config)
576 av_strlcatf(buff, size, "a=fmtp:%d %s\r\n",
577 payload_type, config);
578 break;
579 1 case AV_CODEC_ID_MPEG4:
580
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (p->extradata_size) {
581 1 ret = extradata2config(fmt, p, &config);
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
583 return ret;
584 }
585 1 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
586 "a=fmtp:%d profile-level-id=1%s\r\n",
587 payload_type,
588
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 payload_type, config ? config : "");
589 1 break;
590 case AV_CODEC_ID_AAC:
591 if (fmt && fmt->oformat && fmt->oformat->priv_class &&
592 av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
593 ret = latm_context2config(fmt, p, &config);
594 if (ret < 0)
595 return ret;
596 av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
597 "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
598 payload_type, p->sample_rate, p->ch_layout.nb_channels,
599 payload_type, latm_context2profilelevel(p), config);
600 } else {
601 if (p->extradata_size) {
602 ret = extradata2config(fmt, p, &config);
603 if (ret < 0)
604 return ret;
605 } else {
606 /* FIXME: maybe we can forge config information based on the
607 * codec parameters...
608 */
609 av_log(fmt, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
610 return AVERROR(ENOSYS);
611 }
612 av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
613 "a=fmtp:%d profile-level-id=1;"
614 "mode=AAC-hbr;sizelength=13;indexlength=3;"
615 "indexdeltalength=3%s\r\n",
616 payload_type, p->sample_rate, p->ch_layout.nb_channels,
617 payload_type, config);
618 }
619 break;
620 case AV_CODEC_ID_PCM_S16BE:
621 if (payload_type >= RTP_PT_PRIVATE)
622 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
623 payload_type,
624 p->sample_rate, p->ch_layout.nb_channels);
625 break;
626 case AV_CODEC_ID_PCM_S24BE:
627 if (payload_type >= RTP_PT_PRIVATE)
628 av_strlcatf(buff, size, "a=rtpmap:%d L24/%d/%d\r\n",
629 payload_type,
630 p->sample_rate, p->ch_layout.nb_channels);
631 break;
632 case AV_CODEC_ID_PCM_MULAW:
633 if (payload_type >= RTP_PT_PRIVATE)
634 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
635 payload_type,
636 p->sample_rate, p->ch_layout.nb_channels);
637 break;
638 1 case AV_CODEC_ID_PCM_ALAW:
639
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (payload_type >= RTP_PT_PRIVATE)
640 1 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
641 payload_type,
642 1 p->sample_rate, p->ch_layout.nb_channels);
643 1 break;
644 case AV_CODEC_ID_AMR_NB:
645 av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
646 "a=fmtp:%d octet-align=1\r\n",
647 payload_type, p->sample_rate, p->ch_layout.nb_channels,
648 payload_type);
649 break;
650 case AV_CODEC_ID_AMR_WB:
651 av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
652 "a=fmtp:%d octet-align=1\r\n",
653 payload_type, p->sample_rate, p->ch_layout.nb_channels,
654 payload_type);
655 break;
656 case AV_CODEC_ID_VORBIS:
657 if (p->extradata_size)
658 ret = xiph_extradata2config(fmt, p, &config);
659 else {
660 av_log(fmt, AV_LOG_ERROR, "Vorbis configuration info missing\n");
661 ret = AVERROR_INVALIDDATA;
662 }
663 if (ret < 0)
664 return ret;
665
666 av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
667 "a=fmtp:%d configuration=%s\r\n",
668 payload_type, p->sample_rate, p->ch_layout.nb_channels,
669 payload_type, config);
670 break;
671 case AV_CODEC_ID_THEORA: {
672 const char *pix_fmt;
673 switch (p->format) {
674 case AV_PIX_FMT_YUV420P:
675 pix_fmt = "YCbCr-4:2:0";
676 break;
677 case AV_PIX_FMT_YUV422P:
678 pix_fmt = "YCbCr-4:2:2";
679 break;
680 case AV_PIX_FMT_YUV444P:
681 pix_fmt = "YCbCr-4:4:4";
682 break;
683 default:
684 av_log(fmt, AV_LOG_ERROR, "Unsupported pixel format.\n");
685 return AVERROR(ENOSYS);
686 }
687
688 if (p->extradata_size)
689 ret = xiph_extradata2config(fmt, p, &config);
690 else {
691 av_log(fmt, AV_LOG_ERROR, "Theora configuration info missing\n");
692 ret = AVERROR_INVALIDDATA;
693 }
694 if (ret < 0)
695 return ret;
696
697 av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
698 "a=fmtp:%d delivery-method=inline; "
699 "width=%d; height=%d; sampling=%s; "
700 "configuration=%s\r\n",
701 payload_type, payload_type,
702 p->width, p->height, pix_fmt, config);
703 break;
704 }
705 case AV_CODEC_ID_BITPACKED:
706 case AV_CODEC_ID_RAWVIDEO: {
707 const char *pix_fmt;
708 int bit_depth = 8;
709
710 switch (p->format) {
711 case AV_PIX_FMT_UYVY422:
712 pix_fmt = "YCbCr-4:2:2";
713 break;
714 case AV_PIX_FMT_YUV422P10:
715 pix_fmt = "YCbCr-4:2:2";
716 bit_depth = 10;
717 break;
718 case AV_PIX_FMT_YUV420P:
719 pix_fmt = "YCbCr-4:2:0";
720 break;
721 case AV_PIX_FMT_RGB24:
722 pix_fmt = "RGB";
723 break;
724 case AV_PIX_FMT_BGR24:
725 pix_fmt = "BGR";
726 break;
727 default:
728 av_log(fmt, AV_LOG_ERROR, "Unsupported pixel format.\n");
729 return AVERROR(ENOSYS);
730 }
731
732 av_strlcatf(buff, size, "a=rtpmap:%d raw/90000\r\n"
733 "a=fmtp:%d sampling=%s; "
734 "width=%d; height=%d; "
735 "depth=%d",
736 payload_type, payload_type,
737 pix_fmt, p->width, p->height, bit_depth);
738 if (p->field_order != AV_FIELD_PROGRESSIVE)
739 av_strlcatf(buff, size, "; interlace");
740 av_strlcatf(buff, size, "\r\n");
741 break;
742 }
743
744 case AV_CODEC_ID_VP8:
745 av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
746 payload_type);
747 break;
748 case AV_CODEC_ID_VP9:
749 av_strlcatf(buff, size, "a=rtpmap:%d VP9/90000\r\n",
750 payload_type);
751 break;
752 case AV_CODEC_ID_MJPEG:
753 if (payload_type >= RTP_PT_PRIVATE)
754 av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
755 payload_type);
756 break;
757 case AV_CODEC_ID_ADPCM_G722:
758 if (payload_type >= RTP_PT_PRIVATE)
759 av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
760 payload_type,
761 8000, p->ch_layout.nb_channels);
762 break;
763 case AV_CODEC_ID_ADPCM_G726: {
764 if (payload_type >= RTP_PT_PRIVATE)
765 av_strlcatf(buff, size, "a=rtpmap:%d AAL2-G726-%d/%d\r\n",
766 payload_type,
767 p->bits_per_coded_sample*8,
768 p->sample_rate);
769 break;
770 }
771 case AV_CODEC_ID_ADPCM_G726LE: {
772 if (payload_type >= RTP_PT_PRIVATE)
773 av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
774 payload_type,
775 p->bits_per_coded_sample*8,
776 p->sample_rate);
777 break;
778 }
779 case AV_CODEC_ID_ILBC:
780 av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
781 "a=fmtp:%d mode=%d\r\n",
782 payload_type, p->sample_rate,
783 payload_type, p->block_align == 38 ? 20 : 30);
784 break;
785 case AV_CODEC_ID_SPEEX:
786 av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
787 payload_type, p->sample_rate);
788 break;
789 case AV_CODEC_ID_OPUS:
790 /* The opus RTP draft says that all opus streams MUST be declared
791 as stereo, to avoid negotiation failures. The actual number of
792 channels can change on a packet-by-packet basis. The number of
793 channels a receiver prefers to receive or a sender plans to send
794 can be declared via fmtp parameters (both default to mono), but
795 receivers MUST be able to receive and process stereo packets. */
796 av_strlcatf(buff, size, "a=rtpmap:%d opus/48000/2\r\n",
797 payload_type);
798 if (p->ch_layout.nb_channels == 2) {
799 av_strlcatf(buff, size, "a=fmtp:%d sprop-stereo=1\r\n",
800 payload_type);
801 }
802 break;
803 default:
804 /* Nothing special to do here... */
805 break;
806 }
807
808 2 av_free(config);
809
810 2 return 0;
811 }
812
813 2 int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx,
814 const char *dest_addr, const char *dest_type,
815 int port, int ttl, AVFormatContext *fmt)
816 {
817 2 const AVCodecParameters *p = st->codecpar;
818 const char *type;
819 int payload_type;
820
821 2 payload_type = ff_rtp_get_payload_type(fmt, st->codecpar, idx);
822
823
2/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 switch (p->codec_type) {
824 1 case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
825 1 case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
826 case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
827 default : type = "application"; break;
828 }
829
830 2 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
831 2 sdp_write_address(buff, size, dest_addr, dest_type, ttl);
832
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (p->bit_rate) {
833 2 av_strlcatf(buff, size, "b=AS:%"PRId64"\r\n", p->bit_rate / 1000);
834 }
835
836 2 return sdp_write_media_attributes(buff, size, st, payload_type, fmt);
837 }
838
839 int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
840 {
841 AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
842 struct sdp_session_level s = { 0 };
843 int i, j, port, ttl, is_multicast, index = 0;
844 char dst[32], dst_type[5];
845
846 memset(buf, 0, size);
847 s.user = "-";
848 s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
849 s.src_type = "IP4";
850 s.name = title ? title->value : "No Name";
851
852 port = 0;
853 ttl = 0;
854 if (n_files == 1) {
855 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->url ? ac[0]->url : "");
856 is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
857 sizeof(dst_type));
858 if (!is_multicast)
859 ttl = 0;
860 if (dst[0]) {
861 s.dst_addr = dst;
862 s.dst_type = dst_type;
863 s.ttl = ttl;
864 if (!strcmp(dst_type, "IP6")) {
865 s.src_addr = "::1";
866 s.src_type = "IP6";
867 }
868 }
869 }
870 sdp_write_header(buf, size, &s);
871
872 dst[0] = 0;
873 for (i = 0; i < n_files; i++) {
874 if (n_files != 1) {
875 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->url ? ac[i]->url : "");
876 is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
877 sizeof(dst_type));
878 if (!is_multicast)
879 ttl = 0;
880 }
881 for (j = 0; j < ac[i]->nb_streams; j++) {
882 int ret = ff_sdp_write_media(buf, size, ac[i]->streams[j], index++,
883 dst[0] ? dst : NULL, dst_type,
884 (port > 0) ? port + j * 2 : 0,
885 ttl, ac[i]);
886 if (ret < 0)
887 return ret;
888
889 if (port <= 0) {
890 av_strlcatf(buf, size,
891 "a=control:streamid=%d\r\n", i + j);
892 }
893 if (ac[i]->pb && ac[i]->pb->av_class) {
894 uint8_t *crypto_suite = NULL, *crypto_params = NULL;
895 av_opt_get(ac[i]->pb, "srtp_out_suite", AV_OPT_SEARCH_CHILDREN,
896 &crypto_suite);
897 av_opt_get(ac[i]->pb, "srtp_out_params", AV_OPT_SEARCH_CHILDREN,
898 &crypto_params);
899 if (crypto_suite && crypto_suite[0])
900 av_strlcatf(buf, size,
901 "a=crypto:1 %s inline:%s\r\n",
902 crypto_suite, crypto_params);
903 av_free(crypto_suite);
904 av_free(crypto_params);
905 }
906 }
907 }
908
909 return 0;
910 }
911 #else
912 int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
913 {
914 return AVERROR(ENOSYS);
915 }
916
917 int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx,
918 const char *dest_addr, const char *dest_type,
919 int port, int ttl, AVFormatContext *fmt)
920 {
921 return AVERROR(ENOSYS);
922 }
923 #endif
924