avio: Add an URLProtocol flag for indicating that a protocol uses network
[ffmpeg.git] / libavformat / librtmp.c
index 69a69f1..2d028b0 100644 (file)
@@ -2,29 +2,31 @@
  * RTMP network protocol
  * Copyright (c) 2010 Howard Chu
  *
  * RTMP network protocol
  * Copyright (c) 2010 Howard Chu
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
- * @file libavformat/librtmp.c
+ * @file
  * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
  */
 
  * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
  */
 
+#include "libavutil/mathematics.h"
 #include "avformat.h"
 #include "avformat.h"
+#include "url.h"
 
 #include <librtmp/rtmp.h>
 #include <librtmp/log.h>
 
 #include <librtmp/rtmp.h>
 #include <librtmp/log.h>
@@ -50,12 +52,11 @@ static int rtmp_close(URLContext *s)
     RTMP *r = s->priv_data;
 
     RTMP_Close(r);
     RTMP *r = s->priv_data;
 
     RTMP_Close(r);
-    av_free(r);
     return 0;
 }
 
 /**
     return 0;
 }
 
 /**
- * Opens RTMP connection and verifies that the stream can be played.
+ * Open RTMP connection and verify that the stream can be played.
  *
  * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  *             where 'app' is first one or two directories in the path
  *
  * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  *             where 'app' is first one or two directories in the path
@@ -68,13 +69,9 @@ static int rtmp_close(URLContext *s)
  */
 static int rtmp_open(URLContext *s, const char *uri, int flags)
 {
  */
 static int rtmp_open(URLContext *s, const char *uri, int flags)
 {
-    RTMP *r;
+    RTMP *r = s->priv_data;
     int rc;
 
     int rc;
 
-    r = av_mallocz(sizeof(RTMP));
-    if (!r)
-        return AVERROR(ENOMEM);
-
     switch (av_log_get_level()) {
     default:
     case AV_LOG_FATAL:   rc = RTMP_LOGCRIT;    break;
     switch (av_log_get_level()) {
     default:
     case AV_LOG_FATAL:   rc = RTMP_LOGCRIT;    break;
@@ -93,23 +90,21 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
         goto fail;
     }
 
         goto fail;
     }
 
-    if (flags & URL_WRONLY)
-        r->Link.protocol |= RTMP_FEATURE_WRITE;
+    if (flags & AVIO_FLAG_WRITE)
+        RTMP_EnableWrite(r);
 
     if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
         rc = -1;
         goto fail;
     }
 
 
     if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
         rc = -1;
         goto fail;
     }
 
-    s->priv_data   = r;
     s->is_streamed = 1;
     return 0;
 fail:
     s->is_streamed = 1;
     return 0;
 fail:
-    av_free(r);
     return rc;
 }
 
     return rc;
 }
 
-static int rtmp_write(URLContext *s, uint8_t *buf, int size)
+static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
 {
     RTMP *r = s->priv_data;
 
 {
     RTMP *r = s->priv_data;
 
@@ -127,10 +122,7 @@ static int rtmp_read_pause(URLContext *s, int pause)
 {
     RTMP *r = s->priv_data;
 
 {
     RTMP *r = s->priv_data;
 
-    if (pause)
-        r->m_pauseStamp =
-            r->m_channelTimestamp[r->m_mediaChannel];
-    if (!RTMP_SendPause(r, pause, r->m_pauseStamp))
+    if (!RTMP_Pause(r, pause))
         return -1;
     return 0;
 }
         return -1;
     return 0;
 }
@@ -144,7 +136,10 @@ static int64_t rtmp_read_seek(URLContext *s, int stream_index,
         return AVERROR(ENOSYS);
 
     /* seeks are in milliseconds */
         return AVERROR(ENOSYS);
 
     /* seeks are in milliseconds */
-    timestamp = av_rescale(timestamp, AV_TIME_BASE, 1000);
+    if (stream_index < 0)
+        timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
+            flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
+
     if (!RTMP_SendSeek(r, timestamp))
         return -1;
     return timestamp;
     if (!RTMP_SendSeek(r, timestamp))
         return -1;
     return timestamp;
@@ -154,70 +149,70 @@ static int rtmp_get_file_handle(URLContext *s)
 {
     RTMP *r = s->priv_data;
 
 {
     RTMP *r = s->priv_data;
 
-    return r->m_sb.sb_socket;
+    return RTMP_Socket(r);
 }
 
 }
 
-URLProtocol rtmp_protocol = {
-    "rtmp",
-    rtmp_open,
-    rtmp_read,
-    rtmp_write,
-    NULL,                   /* seek */
-    rtmp_close,
-    NULL,                   /* next */
-    rtmp_read_pause,
-    rtmp_read_seek,
-    rtmp_get_file_handle
+URLProtocol ff_rtmp_protocol = {
+    .name                = "rtmp",
+    .url_open            = rtmp_open,
+    .url_read            = rtmp_read,
+    .url_write           = rtmp_write,
+    .url_close           = rtmp_close,
+    .url_read_pause      = rtmp_read_pause,
+    .url_read_seek       = rtmp_read_seek,
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
+    .flags               = URL_PROTOCOL_FLAG_NETWORK,
 };
 
 };
 
-URLProtocol rtmpt_protocol = {
-    "rtmpt",
-    rtmp_open,
-    rtmp_read,
-    rtmp_write,
-    NULL,                   /* seek */
-    rtmp_close,
-    NULL,                   /* next */
-    rtmp_read_pause,
-    rtmp_read_seek,
-    rtmp_get_file_handle
+URLProtocol ff_rtmpt_protocol = {
+    .name                = "rtmpt",
+    .url_open            = rtmp_open,
+    .url_read            = rtmp_read,
+    .url_write           = rtmp_write,
+    .url_close           = rtmp_close,
+    .url_read_pause      = rtmp_read_pause,
+    .url_read_seek       = rtmp_read_seek,
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
+    .flags               = URL_PROTOCOL_FLAG_NETWORK,
 };
 
 };
 
-URLProtocol rtmpe_protocol = {
-    "rtmpe",
-    rtmp_open,
-    rtmp_read,
-    rtmp_write,
-    NULL,                   /* seek */
-    rtmp_close,
-    NULL,                   /* next */
-    rtmp_read_pause,
-    rtmp_read_seek,
-    rtmp_get_file_handle
+URLProtocol ff_rtmpe_protocol = {
+    .name                = "rtmpe",
+    .url_open            = rtmp_open,
+    .url_read            = rtmp_read,
+    .url_write           = rtmp_write,
+    .url_close           = rtmp_close,
+    .url_read_pause      = rtmp_read_pause,
+    .url_read_seek       = rtmp_read_seek,
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
+    .flags               = URL_PROTOCOL_FLAG_NETWORK,
 };
 
 };
 
-URLProtocol rtmpte_protocol = {
-    "rtmpte",
-    rtmp_open,
-    rtmp_read,
-    rtmp_write,
-    NULL,                   /* seek */
-    rtmp_close,
-    NULL,                   /* next */
-    rtmp_read_pause,
-    rtmp_read_seek,
-    rtmp_get_file_handle
+URLProtocol ff_rtmpte_protocol = {
+    .name                = "rtmpte",
+    .url_open            = rtmp_open,
+    .url_read            = rtmp_read,
+    .url_write           = rtmp_write,
+    .url_close           = rtmp_close,
+    .url_read_pause      = rtmp_read_pause,
+    .url_read_seek       = rtmp_read_seek,
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
+    .flags               = URL_PROTOCOL_FLAG_NETWORK,
 };
 
 };
 
-URLProtocol rtmps_protocol = {
-    "rtmps",
-    rtmp_open,
-    rtmp_read,
-    rtmp_write,
-    NULL,                   /* seek */
-    rtmp_close,
-    NULL,                   /* next */
-    rtmp_read_pause,
-    rtmp_read_seek,
-    rtmp_get_file_handle
+URLProtocol ff_rtmps_protocol = {
+    .name                = "rtmps",
+    .url_open            = rtmp_open,
+    .url_read            = rtmp_read,
+    .url_write           = rtmp_write,
+    .url_close           = rtmp_close,
+    .url_read_pause      = rtmp_read_pause,
+    .url_read_seek       = rtmp_read_seek,
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
+    .flags               = URL_PROTOCOL_FLAG_NETWORK,
 };
 };