FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/sdp.c
Date: 2026-10-11 02:49:39
Exec Total Coverage
Lines: 44 545 8.1%
Functions: 4 14 28.6%
Branches: 15 314 4.8%

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