FFmpeg coverage


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