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>
39 #if CONFIG_NETWORK
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 static void sdp_write_address(
char *buff,
int size,
const char *dest_addr,
64 const char *dest_type, int ttl)
65 {
66 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 {
75 }
76 }
77 }
78
79 static void sdp_write_header(
char *buff,
int size,
struct sdp_session_level *
s)
80 {
82 "o=- %d %d IN %s %s\r\n"
83 "s=%s\r\n",
85 s->id,
s->version,
s->src_type,
s->src_addr,
87 sdp_write_address(buff,
size,
s->dst_addr,
s->dst_type,
s->ttl);
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 {
98 int is_multicast;
99
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
108 return 0;
111 #ifdef AF_INET6
112 if (ai->ai_family == AF_INET6)
114 #endif
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
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
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
161 {
162 char *psets, *p;
164 static const char pset_string[] = "; sprop-parameter-sets=";
165 static const char profile_string[] = "; profile-level-id=";
168 uint8_t *tmpbuf =
NULL;
169 const uint8_t *
sps =
NULL, *sps_end;
170
172
176 }
179 &extradata_size);
182
183 tmpbuf = extradata;
184 }
185
187 if (!psets) {
191 }
192 memcpy(psets, pset_string, strlen(pset_string));
193 p = psets + strlen(pset_string);
195 while (
r < extradata + extradata_size) {
196 const uint8_t *r1;
197 uint8_t nal_type;
198
200 nal_type = *
r & 0x1f;
202 if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
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 }
214 sps_end = r1;
215 }
218 MAX_PSET_SIZE - (p - psets), r1 -
r);
219 fail_in_loop:
222
224 }
225 p += strlen(p);
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);
232 }
234
236 return 0;
237 }
238
240 {
241 char *psets;
244 uint8_t *tmpbuf =
NULL;
245 int ps_pos[3] = { 0 };
246 static const char * const ps_names[3] = { "vps", "sps", "pps" };
247 int num_arrays, num_nalus;
249
251
252 // Convert to hvcc format. Since we need to group multiple NALUs of
253 // the same type, and we might need to convert from one format to the
254 // other anyway, we get away with a little less work by using the hvcc
255 // format.
258
262
266 goto err;
267 }
269 tmpbuf = extradata;
270 }
271
272 if (extradata_size < 23)
273 goto err;
274
275 num_arrays = extradata[22];
277 for (
i = 0;
i < num_arrays;
i++) {
278 int num_nalus, nalu_type;
279 if (
pos + 3 > extradata_size)
280 goto err;
281 nalu_type = extradata[
pos] & 0x3f;
282 // Not including libavcodec/hevc.h to avoid confusion between
283 // NAL_* with the same name for both H.264 and HEVC.
284 if (nalu_type == 32) // VPS
286 else if (nalu_type == 33) // SPS
288 else if (nalu_type == 34) // PPS
292 for (j = 0; j < num_nalus; j++) {
294 if (
pos + 2 > extradata_size)
295 goto err;
298 if (
pos +
len > extradata_size)
299 goto err;
301 }
302 }
303 if (!ps_pos[0] || !ps_pos[1] || !ps_pos[2])
304 goto err;
305
307 if (!psets) {
309 goto err;
310 }
311
312 psets[0] = '0円';
313
314 for (
i = 0;
i < 3;
i++) {
316
319 av_strlcatf(psets, MAX_PSET_SIZE,
"sprop-%s=", ps_names[
i]);
320
321 // Skipping boundary checks in the input here; we've already traversed
322 // the whole hvcc structure above without issues
325 for (j = 0; j < num_nalus; j++) {
327 int strpos;
329 if (j > 0)
331 strpos = strlen(psets);
335 goto err;
336 }
338 }
339 }
341
343 return 0;
344 err:
349 }
350
353 {
355
357
361 }
366 }
367 memcpy(
config,
"; config=", 9);
369
371 return 0;
372 }
373
376 {
378 char *encoded_config;
379 const uint8_t *header_start[3];
380 int headers_len, header_len[3], config_len;
381 int first_header_size,
ret;
382
384
387 first_header_size = 42;
388 break;
390 first_header_size = 30;
391 break;
392 default:
395 }
396
398 first_header_size, header_start,
399 header_len);
403 }
404
405 headers_len = header_len[0] + header_len[2];
406 config_len = 4 + // count
407 3 + // ident
408 2 + // packet size
409 1 + // header count
410 2 + // header size
411 headers_len; // and the rest
412
415 goto xiph_fail;
416
418 if (!encoded_config) {
420 goto xiph_fail;
421 }
422
428 config[7] = (headers_len >> 8) & 0xff;
429 config[8] = headers_len & 0xff;
431 config[10] = header_len[0];
432 config[11] = 0;
// size of comment header; nonexistent
433 memcpy(
config + 12, header_start[0], header_len[0]);
434 memcpy(
config + 12 + header_len[0], header_start[2], header_len[2]);
435
439
440 *
out = encoded_config;
441 return 0;
442
443 xiph_fail:
445 "Not enough memory for configuration string\n");
447 }
448
450 {
451 /* MP4A-LATM
452 * The RTP payload format specification is described in RFC 3016
453 * The encoding specifications are provided in ISO/IEC 14496-3 */
454
455 int profile_level = 0x2B;
456
457 /* TODO: AAC Profile only supports AAC LC Object Type.
458 * Different Object Types should implement different Profile Levels */
459
462 profile_level = 0x28; // AAC Profile, Level 1
465 profile_level = 0x29; // AAC Profile, Level 2
467 profile_level = 0x2A; // AAC Profile, Level 4
468 }
471 profile_level = 0x2B; // AAC Profile, Level 5
472 }
473 }
474
475 return profile_level;
476 }
477
480 {
481 /* MP4A-LATM
482 * The RTP payload format specification is described in RFC 3016
483 * The encoding specifications are provided in ISO/IEC 14496-3 */
484
485 uint8_t config_byte[6];
486 int rate_index;
488
490
491 for (rate_index = 0; rate_index < 16; rate_index++)
493 break;
494 if (rate_index == 16) {
497 }
498
499 config_byte[0] = 0x40;
500 config_byte[1] = 0;
501 config_byte[2] = 0x20 | rate_index;
503 config_byte[4] = 0x3f;
504 config_byte[5] = 0xc0;
505
510 }
512
514 return 0;
515 }
516
517 static int sdp_write_media_attributes(
char *buff,
int size,
const AVStream *st,
519 {
523
527 break;
537 }
539 "a=fmtp:%d packetization-mode=%d%s\r\n",
540 payload_type,
542 break;
543 }
545 {
546 const char *pic_fmt =
NULL;
547 /* only QCIF and CIF are specified as supported in RFC 4587 */
549 pic_fmt = "QCIF=1";
551 pic_fmt = "CIF=1";
554 if (pic_fmt)
556 break;
557 }
560 /* a=framesize is required by 3GPP TS 26.234 (PSS). It
561 * actually specifies the maximum video size, but we only know
562 * the current size. This is required for playback on Android
563 * stagefright and on Samsung bada. */
568 "a=framesize:%d %d-%d\r\n",
569 payload_type,
571 break;
577 }
582 break;
588 }
590 "a=fmtp:%d profile-level-id=1%s\r\n",
591 payload_type,
593 break;
597 ret = latm_context2config(fmt, p, &
config);
601 "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
603 payload_type, latm_context2profilelevel(p),
config);
604 } else {
609 } else {
610 /* FIXME: maybe we can forge config information based on the
611 * codec parameters...
612 */
613 av_log(fmt,
AV_LOG_ERROR,
"AAC with no global headers is currently not supported.\n");
615 }
617 "a=fmtp:%d profile-level-id=1;"
618 "mode=AAC-hbr;sizelength=13;indexlength=3;"
619 "indexdeltalength=3%s\r\n",
622 }
623 break;
627 payload_type,
629 break;
633 payload_type,
635 break;
639 payload_type,
641 break;
645 payload_type,
647 break;
650 "a=fmtp:%d octet-align=1\r\n",
652 payload_type);
653 break;
656 "a=fmtp:%d octet-align=1\r\n",
658 payload_type);
659 break;
662 ret = xiph_extradata2config(fmt, p, &
config);
663 else {
666 }
669
671 "a=fmtp:%d configuration=%s\r\n",
674 break;
680 break;
683 break;
686 break;
687 default:
690 }
691
693 ret = xiph_extradata2config(fmt, p, &
config);
694 else {
697 }
700
702 "a=fmtp:%d delivery-method=inline; "
703 "width=%d; height=%d; sampling=%s; "
704 "configuration=%s\r\n",
705 payload_type, payload_type,
707 break;
708 }
713
717 break;
721 break;
724 break;
727 break;
730 break;
731 default:
734 }
735
737 "a=fmtp:%d sampling=%s; "
738 "width=%d; height=%d; "
739 "depth=%d",
740 payload_type, payload_type,
745 break;
746 }
747
750 payload_type);
751 break;
754 payload_type);
755 break;
759 payload_type);
760 break;
764 payload_type,
766 break;
770 payload_type,
773 break;
774 }
778 payload_type,
781 break;
782 }
785 "a=fmtp:%d mode=%d\r\n",
788 break;
792 break;
794 /* The opus RTP draft says that all opus streams MUST be declared
795 as stereo, to avoid negotiation failures. The actual number of
796 channels can change on a packet-by-packet basis. The number of
797 channels a receiver prefers to receive or a sender plans to send
798 can be declared via fmtp parameters (both default to mono), but
799 receivers MUST be able to receive and process stereo packets. */
801 payload_type);
804 payload_type);
805 }
806 break;
807 default:
808 /* Nothing special to do here... */
809 break;
810 }
811
813
814 return 0;
815 }
816
818 const char *dest_addr, const char *dest_type,
820 {
823 int payload_type;
824
826
831 default :
type =
"application";
break;
832 }
833
835 sdp_write_address(buff,
size, dest_addr, dest_type, ttl);
838 }
839
840 return sdp_write_media_attributes(buff,
size, st, payload_type, fmt);
841 }
842
844 {
846 struct sdp_session_level
s = { 0 };
847 int i, j, port, ttl, is_multicast,
index = 0;
848 char dst[32], dst_type[5];
849
850 memset(buf, 0,
size);
852 s.src_addr =
"127.0.0.1";
/* FIXME: Properly set this */
854 s.name = title ? title->
value :
"No Name";
855
856 port = 0;
857 ttl = 0;
858 if (n_files == 1) {
859 port = sdp_get_address(
dst,
sizeof(
dst), &ttl, ac[0]->url ? ac[0]->url :
"");
860 is_multicast = resolve_destination(
dst,
sizeof(
dst), dst_type,
861 sizeof(dst_type));
862 if (!is_multicast)
863 ttl = 0;
866 s.dst_type = dst_type;
868 if (!strcmp(dst_type, "IP6")) {
871 }
872 }
873 }
874 sdp_write_header(buf,
size, &
s);
875
877 for (
i = 0;
i < n_files;
i++) {
878 if (n_files != 1) {
879 port = sdp_get_address(
dst,
sizeof(
dst), &ttl, ac[
i]->url ? ac[
i]->url :
"");
880 is_multicast = resolve_destination(
dst,
sizeof(
dst), dst_type,
881 sizeof(dst_type));
882 if (!is_multicast)
883 ttl = 0;
884 }
888 (port > 0) ? port + j * 2 : 0,
892
893 if (port <= 0) {
895 "a=control:streamid=%d\r\n",
i + j);
896 }
898 uint8_t *crypto_suite =
NULL, *crypto_params =
NULL;
900 &crypto_suite);
902 &crypto_params);
903 if (crypto_suite && crypto_suite[0])
905 "a=crypto:1 %s inline:%s\r\n",
906 crypto_suite, crypto_params);
909 }
910 }
911 }
912
913 return 0;
914 }
915 #else
917 {
919 }
920
922 const char *dest_addr, const char *dest_type,
924 {
926 }
927 #endif