Autodetect JPEG2000 imagess.
[ffmpeg.git] / libavformat / sapenc.c
index 862f114..82a6ef6 100644 (file)
  */
 
 #include "avformat.h"
+#include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/avstring.h"
+#include "libavutil/dict.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/time.h"
 #include "internal.h"
 #include "network.h"
+#include "os_support.h"
 #include "rtpenc_chain.h"
+#include "url.h"
 
 struct SAPState {
     uint8_t    *ann;
@@ -44,22 +49,19 @@ static int sap_write_close(AVFormatContext *s)
         if (!rtpctx)
             continue;
         av_write_trailer(rtpctx);
-        url_fclose(rtpctx->pb);
-        av_metadata_free(&rtpctx->streams[0]->metadata);
-        av_metadata_free(&rtpctx->metadata);
-        av_free(rtpctx->streams[0]);
-        av_free(rtpctx);
+        avio_close(rtpctx->pb);
+        avformat_free_context(rtpctx);
         s->streams[i]->priv_data = NULL;
     }
 
     if (sap->last_time && sap->ann && sap->ann_fd) {
         sap->ann[0] |= 4; /* Session deletion*/
-        url_write(sap->ann_fd, sap->ann, sap->ann_size);
+        ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
     }
 
     av_freep(&sap->ann);
     if (sap->ann_fd)
-        url_close(sap->ann_fd);
+        ffurl_close(sap->ann_fd);
     ff_network_close();
     return 0;
 }
@@ -69,44 +71,44 @@ static int sap_write_header(AVFormatContext *s)
     struct SAPState *sap = s->priv_data;
     char host[1024], path[1024], url[1024], announce_addr[50] = "";
     char *option_list;
-    int port, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
+    int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
     AVFormatContext **contexts = NULL;
     int ret = 0;
     struct sockaddr_storage localaddr;
     socklen_t addrlen = sizeof(localaddr);
     int udp_fd;
+    AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
 
     if (!ff_network_init())
         return AVERROR(EIO);
 
     /* extract hostname and port */
-    av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
+    av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
                  path, sizeof(path), s->filename);
-    if (port < 0)
-        port = 9875;
+    if (base_port < 0)
+        base_port = 5004;
 
     /* search for options */
     option_list = strrchr(path, '?');
     if (option_list) {
         char buf[50];
-        if (find_info_tag(buf, sizeof(buf), "base_port", option_list)) {
-            base_port = strtol(buf, NULL, 10);
+        if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
+            port = strtol(buf, NULL, 10);
         }
-        if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
+        if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
             same_port = strtol(buf, NULL, 10);
         }
-        if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
+        if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
             ttl = strtol(buf, NULL, 10);
         }
-        if (find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
+        if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
             av_strlcpy(announce_addr, buf, sizeof(announce_addr));
         }
     }
 
     if (!announce_addr[0]) {
-        struct addrinfo hints, *ai = NULL;
-        memset(&hints, 0, sizeof(hints));
-        hints.ai_family = PF_UNSPEC;
+        struct addrinfo hints = { 0 }, *ai = NULL;
+        hints.ai_family = AF_UNSPEC;
         if (getaddrinfo(host, NULL, &hints, &ai)) {
             av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
             ret = AVERROR(EIO);
@@ -115,11 +117,13 @@ static int sap_write_header(AVFormatContext *s)
         if (ai->ai_family == AF_INET) {
             /* Also known as sap.mcast.net */
             av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
+#if HAVE_STRUCT_SOCKADDR_IN6
         } else if (ai->ai_family == AF_INET6) {
             /* With IPv6, you can use the same destination in many different
              * multicast subnets, to choose how far you want it routed.
              * This one is intended to be routed globally. */
             av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
+#endif
         } else {
             freeaddrinfo(ai);
             av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
@@ -136,7 +140,8 @@ static int sap_write_header(AVFormatContext *s)
         goto fail;
     }
 
-    s->start_time_realtime = av_gettime();
+    if (s->start_time_realtime == 0  ||  s->start_time_realtime == AV_NOPTS_VALUE)
+        s->start_time_realtime = av_gettime();
     for (i = 0; i < s->nb_streams; i++) {
         URLContext *fd;
 
@@ -144,30 +149,41 @@ static int sap_write_header(AVFormatContext *s)
                     "?ttl=%d", ttl);
         if (!same_port)
             base_port += 2;
-        ret = url_open(&fd, url, URL_WRONLY);
+        ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
         if (ret) {
             ret = AVERROR(EIO);
             goto fail;
         }
-        s->streams[i]->priv_data = contexts[i] =
-            ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
+        ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
+        if (ret < 0)
+            goto fail;
+        s->streams[i]->priv_data = contexts[i];
+        s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
         av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
     }
 
+    if (s->nb_streams > 0 && title)
+        av_dict_set(&contexts[0]->metadata, "title", title->value, 0);
+
     ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
                 "?ttl=%d&connect=1", ttl);
-    ret = url_open(&sap->ann_fd, url, URL_WRONLY);
+    ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
+                     &s->interrupt_callback, NULL);
     if (ret) {
         ret = AVERROR(EIO);
         goto fail;
     }
 
-    udp_fd = url_get_file_handle(sap->ann_fd);
+    udp_fd = ffurl_get_file_handle(sap->ann_fd);
     if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
         ret = AVERROR(EIO);
         goto fail;
     }
-    if (localaddr.ss_family != AF_INET && localaddr.ss_family != AF_INET6) {
+    if (localaddr.ss_family != AF_INET
+#if HAVE_STRUCT_SOCKADDR_IN6
+        && localaddr.ss_family != AF_INET6
+#endif
+        ) {
         av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
         ret = AVERROR(EIO);
         goto fail;
@@ -178,7 +194,12 @@ static int sap_write_header(AVFormatContext *s)
         ret = AVERROR(EIO);
         goto fail;
     }
-    sap->ann[pos++] = (1 << 5) | ((localaddr.ss_family == AF_INET6) << 4);
+    sap->ann[pos] = (1 << 5);
+#if HAVE_STRUCT_SOCKADDR_IN6
+    if (localaddr.ss_family == AF_INET6)
+        sap->ann[pos] |= 0x10;
+#endif
+    pos++;
     sap->ann[pos++] = 0; /* Authentication length */
     AV_WB16(&sap->ann[pos], av_get_random_seed());
     pos += 2;
@@ -186,17 +207,19 @@ static int sap_write_header(AVFormatContext *s)
         memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
                sizeof(struct in_addr));
         pos += sizeof(struct in_addr);
+#if HAVE_STRUCT_SOCKADDR_IN6
     } else {
         memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
                sizeof(struct in6_addr));
         pos += sizeof(struct in6_addr);
+#endif
     }
 
     av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
     pos += strlen(&sap->ann[pos]) + 1;
 
-    if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
-                       sap->ann_size - pos)) {
+    if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
+                      sap->ann_size - pos)) {
         ret = AVERROR_INVALIDDATA;
         goto fail;
     }
@@ -205,7 +228,7 @@ static int sap_write_header(AVFormatContext *s)
     pos += strlen(&sap->ann[pos]);
     sap->ann_size = pos;
 
-    if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
+    if (sap->ann_size > sap->ann_fd->max_packet_size) {
         av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
                                 "packet\n");
         goto fail;
@@ -223,12 +246,12 @@ static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     AVFormatContext *rtpctx;
     struct SAPState *sap = s->priv_data;
-    int64_t now = av_gettime();
+    int64_t now = av_gettime_relative();
 
     if (!sap->last_time || now - sap->last_time > 5000000) {
-        int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
+        int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
         /* Don't abort even if we get "Destination unreachable" */
-        if (ret < 0 && ret != FF_NETERROR(ECONNREFUSED))
+        if (ret < 0 && ret != AVERROR(ECONNREFUSED))
             return ret;
         sap->last_time = now;
     }
@@ -236,17 +259,14 @@ static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
     return ff_write_chained(rtpctx, 0, pkt, s);
 }
 
-AVOutputFormat sap_muxer = {
-    "sap",
-    NULL_IF_CONFIG_SMALL("SAP output format"),
-    NULL,
-    NULL,
-    sizeof(struct SAPState),
-    CODEC_ID_AAC,
-    CODEC_ID_MPEG4,
-    sap_write_header,
-    sap_write_packet,
-    sap_write_close,
-    .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
+AVOutputFormat ff_sap_muxer = {
+    .name              = "sap",
+    .long_name         = NULL_IF_CONFIG_SMALL("SAP output"),
+    .priv_data_size    = sizeof(struct SAPState),
+    .audio_codec       = AV_CODEC_ID_AAC,
+    .video_codec       = AV_CODEC_ID_MPEG4,
+    .write_header      = sap_write_header,
+    .write_packet      = sap_write_packet,
+    .write_trailer     = sap_write_close,
+    .flags             = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
 };
-