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