libavformat/librtmp.c
0f943ce6
 /*
  * RTMP network protocol
  * Copyright (c) 2010 Howard Chu
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg 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.
  *
  * FFmpeg 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
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
fc122efd
  * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
0f943ce6
  */
 
007aedee
 #include "libavutil/avstring.h"
d3f751e6
 #include "libavutil/mathematics.h"
007aedee
 #include "libavutil/opt.h"
0f943ce6
 #include "avformat.h"
c486dade
 #include "url.h"
0f943ce6
 
 #include <librtmp/rtmp.h>
 #include <librtmp/log.h>
 
007aedee
 typedef struct LibRTMPContext {
     const AVClass *class;
     RTMP rtmp;
     char *app;
     char *playpath;
f6b3dce9
     char *temp_filename;
007aedee
 } LibRTMPContext;
 
19c9eedc
 static void rtmp_log(int level, const char *fmt, va_list args)
 {
     switch (level) {
     default:
     case RTMP_LOGCRIT:    level = AV_LOG_FATAL;   break;
     case RTMP_LOGERROR:   level = AV_LOG_ERROR;   break;
     case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
     case RTMP_LOGINFO:    level = AV_LOG_INFO;    break;
     case RTMP_LOGDEBUG:   level = AV_LOG_VERBOSE; break;
     case RTMP_LOGDEBUG2:  level = AV_LOG_DEBUG;   break;
     }
 
     av_vlog(NULL, level, fmt, args);
     av_log(NULL, level, "\n");
 }
 
0f943ce6
 static int rtmp_close(URLContext *s)
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
0f943ce6
 
     RTMP_Close(r);
f6b3dce9
     av_freep(&ctx->temp_filename);
0f943ce6
     return 0;
 }
 
 /**
49bd8e4b
  * Open RTMP connection and verify that the stream can be played.
0f943ce6
  *
  * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  *             where 'app' is first one or two directories in the path
  *             (e.g. /ondemand/, /flash/live/, etc.)
  *             and 'playpath' is a file name (the rest of the path,
  *             may be prefixed with "mp4:")
  *
  *             Additional RTMP library options may be appended as
  *             space-separated key-value pairs.
  */
 static int rtmp_open(URLContext *s, const char *uri, int flags)
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
a91943bc
     int rc = 0, level;
007aedee
     char *filename = s->filename;
0f943ce6
 
fc122efd
     switch (av_log_get_level()) {
0f943ce6
     default:
a91943bc
     case AV_LOG_FATAL:   level = RTMP_LOGCRIT;    break;
     case AV_LOG_ERROR:   level = RTMP_LOGERROR;   break;
     case AV_LOG_WARNING: level = RTMP_LOGWARNING; break;
     case AV_LOG_INFO:    level = RTMP_LOGINFO;    break;
     case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG;   break;
     case AV_LOG_DEBUG:   level = RTMP_LOGDEBUG2;  break;
0f943ce6
     }
a91943bc
     RTMP_LogSetLevel(level);
19c9eedc
     RTMP_LogSetCallback(rtmp_log);
0f943ce6
 
007aedee
     if (ctx->app || ctx->playpath) {
         int len = strlen(s->filename) + 1;
         if (ctx->app)      len += strlen(ctx->app)      + sizeof(" app=");
         if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath=");
 
f6b3dce9
         if (!(ctx->temp_filename = filename = av_malloc(len)))
007aedee
             return AVERROR(ENOMEM);
 
         av_strlcpy(filename, s->filename, len);
         if (ctx->app) {
             av_strlcat(filename, " app=", len);
             av_strlcat(filename, ctx->app, len);
         }
         if (ctx->playpath) {
             av_strlcat(filename, " playpath=", len);
             av_strlcat(filename, ctx->playpath, len);
         }
     }
 
0f943ce6
     RTMP_Init(r);
007aedee
     if (!RTMP_SetupURL(r, filename)) {
19dfbf19
         rc = AVERROR_UNKNOWN;
0f943ce6
         goto fail;
     }
 
59d96941
     if (flags & AVIO_FLAG_WRITE)
4bbb3e3a
         RTMP_EnableWrite(r);
0f943ce6
 
     if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
19dfbf19
         rc = AVERROR_UNKNOWN;
0f943ce6
         goto fail;
     }
 
     s->is_streamed = 1;
f6b3dce9
     return 0;
0f943ce6
 fail:
f6b3dce9
     av_freep(&ctx->temp_filename);
b79bccba
     if (rc)
         RTMP_Close(r);
 
0f943ce6
     return rc;
 }
 
27241cbf
 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
0f943ce6
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
0f943ce6
 
     return RTMP_Write(r, buf, size);
 }
 
 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
0f943ce6
 
     return RTMP_Read(r, buf, size);
 }
 
 static int rtmp_read_pause(URLContext *s, int pause)
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
0f943ce6
 
4bbb3e3a
     if (!RTMP_Pause(r, pause))
19dfbf19
         return AVERROR_UNKNOWN;
0f943ce6
     return 0;
 }
 
 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
                               int64_t timestamp, int flags)
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
0f943ce6
 
     if (flags & AVSEEK_FLAG_BYTE)
d79fc840
         return AVERROR(ENOSYS);
0f943ce6
 
     /* seeks are in milliseconds */
fc8fa007
     if (stream_index < 0)
         timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
             flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
 
0f943ce6
     if (!RTMP_SendSeek(r, timestamp))
19dfbf19
         return AVERROR_UNKNOWN;
0f943ce6
     return timestamp;
 }
 
 static int rtmp_get_file_handle(URLContext *s)
 {
007aedee
     LibRTMPContext *ctx = s->priv_data;
     RTMP *r = &ctx->rtmp;
0f943ce6
 
4bbb3e3a
     return RTMP_Socket(r);
0f943ce6
 }
 
007aedee
 #define OFFSET(x) offsetof(LibRTMPContext, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 #define ENC AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     {"rtmp_app",      "Name of application to connect to on the RTMP server", OFFSET(app),      AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
     {"rtmp_playpath", "Stream identifier to play or to publish",              OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
     { NULL },
 };
 
 #define RTMP_CLASS(flavor)\
 static const AVClass lib ## flavor ## _class = {\
     .class_name = "lib" #flavor " protocol",\
     .item_name  = av_default_item_name,\
     .option     = options,\
     .version    = LIBAVUTIL_VERSION_INT,\
 };
 
 RTMP_CLASS(rtmp)
c3b57d6e
 URLProtocol ff_librtmp_protocol = {
f35ff97f
     .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,
7e580505
     .url_get_file_handle = rtmp_get_file_handle,
007aedee
     .priv_data_size      = sizeof(LibRTMPContext),
     .priv_data_class     = &librtmp_class,
32b83aee
     .flags               = URL_PROTOCOL_FLAG_NETWORK,
0f943ce6
 };
 
007aedee
 RTMP_CLASS(rtmpt)
c3b57d6e
 URLProtocol ff_librtmpt_protocol = {
f35ff97f
     .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,
7e580505
     .url_get_file_handle = rtmp_get_file_handle,
007aedee
     .priv_data_size      = sizeof(LibRTMPContext),
     .priv_data_class     = &librtmpt_class,
32b83aee
     .flags               = URL_PROTOCOL_FLAG_NETWORK,
0f943ce6
 };
 
007aedee
 RTMP_CLASS(rtmpe)
c3b57d6e
 URLProtocol ff_librtmpe_protocol = {
f35ff97f
     .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,
7e580505
     .url_get_file_handle = rtmp_get_file_handle,
007aedee
     .priv_data_size      = sizeof(LibRTMPContext),
     .priv_data_class     = &librtmpe_class,
32b83aee
     .flags               = URL_PROTOCOL_FLAG_NETWORK,
0f943ce6
 };
 
007aedee
 RTMP_CLASS(rtmpte)
c3b57d6e
 URLProtocol ff_librtmpte_protocol = {
f35ff97f
     .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,
7e580505
     .url_get_file_handle = rtmp_get_file_handle,
007aedee
     .priv_data_size      = sizeof(LibRTMPContext),
     .priv_data_class     = &librtmpte_class,
32b83aee
     .flags               = URL_PROTOCOL_FLAG_NETWORK,
0f943ce6
 };
 
007aedee
 RTMP_CLASS(rtmps)
c3b57d6e
 URLProtocol ff_librtmps_protocol = {
f35ff97f
     .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,
7e580505
     .url_get_file_handle = rtmp_get_file_handle,
007aedee
     .priv_data_size      = sizeof(LibRTMPContext),
     .priv_data_class     = &librtmps_class,
32b83aee
     .flags               = URL_PROTOCOL_FLAG_NETWORK,
0f943ce6
 };