AAC support in the SDP generator
[ffmpeg.git] / libavformat / sdp.c
index 8cedcd1..8a723fc 100644 (file)
@@ -21,6 +21,7 @@
 #include "avstring.h"
 #include "avformat.h"
 
+#ifdef CONFIG_RTP_MUXER
 #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
 
 struct sdp_session_level {
@@ -109,6 +110,27 @@ static char *data_to_hex(char *buff, const uint8_t *src, int s)
     return buff;
 }
 
+static char *extradata2config(const uint8_t *extradata, int extradata_size)
+{
+    char *config;
+
+    if (extradata_size > MAX_EXTRADATA_SIZE) {
+        av_log(NULL, AV_LOG_ERROR, "Too many extra data!\n");
+
+        return NULL;
+    }
+    config = av_malloc(10 + extradata_size * 2);
+    if (config == NULL) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory for the config info\n");
+        return NULL;
+    }
+    memcpy(config, "; config=", 9);
+    data_to_hex(config + 9, extradata, extradata_size);
+    config[9 + extradata_size * 2] = 0;
+
+    return config;
+}
+
 static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
 {
     char *config = NULL;
@@ -116,25 +138,33 @@ static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int p
     switch (c->codec_id) {
         case CODEC_ID_MPEG4:
             if (c->flags & CODEC_FLAG_GLOBAL_HEADER) {
-                if (c->extradata_size > MAX_EXTRADATA_SIZE) {
-                    av_log(NULL, AV_LOG_ERROR, "Too many extra data!\n");
-
-                    return NULL;
-                }
-                config = av_malloc(10 + c->extradata_size * 2);
-                if (config == NULL) {
-                    av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory for the config info\n");
-                    return NULL;
-                }
-                memcpy(config, "; config=", 9);
-                data_to_hex(config + 9, c->extradata, c->extradata_size);
-                config[9 + c->extradata_size * 2] = 0;
+                config = extradata2config(c->extradata, c->extradata_size);
             }
             av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
                                     "a=fmtp:%d profile-level-id=1%s\r\n",
                                      payload_type,
                                      payload_type, config ? config : "");
             break;
+        case CODEC_ID_AAC:
+            if (c->flags & CODEC_FLAG_GLOBAL_HEADER) {
+                config = extradata2config(c->extradata, c->extradata_size);
+            } else {
+                /* FIXME: maybe we can forge config information based on the
+                 *        codec parameters...
+                 */
+                av_log(NULL, AV_LOG_ERROR, "AAC with no global headers is currently not supported\n");
+                return NULL;
+            }
+            if (config == NULL) {
+                return NULL;
+            }
+            av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
+                                    "a=fmtp:%d profile-level-id=1;"
+                                    "mode=AAC-hbr;sizelength=13;indexlength=3;"
+                                    "indexdeltalength=3%s\r\n",
+                                     payload_type, c->sample_rate, c->channels,
+                                     payload_type, config);
+            break;
         default:
             /* Nothing special to do, here... */
             break;
@@ -168,19 +198,13 @@ static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char
     sdp_media_attributes(buff, size, c, payload_type);
 }
 
-#define SDP_BUFFER_SIZE 2048
-char *avf_sdp_create(AVFormatContext *ac[], int n_files)
+int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
 {
-    char *buff;
     struct sdp_session_level s;
     int i, j, port, ttl;
     char dst[32];
 
-    buff = av_mallocz(SDP_BUFFER_SIZE);
-    if (buff == NULL) {
-        return NULL;
-    }
-
+    memset(buff, 0, size);
     memset(&s, 0, sizeof(struct sdp_session_level));
     s.user = "-";
     s.src_addr = "127.0.0.1";    /* FIXME: Properly set this */
@@ -195,7 +219,7 @@ char *avf_sdp_create(AVFormatContext *ac[], int n_files)
             s.ttl = ttl;
         }
     }
-    sdp_write_header(buff, SDP_BUFFER_SIZE, &s);
+    sdp_write_header(buff, size, &s);
 
     dst[0] = 0;
     for (i = 0; i < n_files; i++) {
@@ -203,15 +227,21 @@ char *avf_sdp_create(AVFormatContext *ac[], int n_files)
             port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
         }
         for (j = 0; j < ac[i]->nb_streams; j++) {
-            sdp_write_media(buff, SDP_BUFFER_SIZE,
+            sdp_write_media(buff, size,
                                   ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
                                   (port > 0) ? port + j * 2 : 0, ttl);
             if (port <= 0) {
-                av_strlcatf(buff, SDP_BUFFER_SIZE,
+                av_strlcatf(buff, size,
                                    "a=control:streamid=%d\r\n", i + j);
             }
         }
     }
 
-    return buff;
+    return 0;
+}
+#else
+int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
+{
+    return AVERROR(ENOSYS);
 }
+#endif