FFmpeg coverage


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