librtmp.c 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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
 */

/**
23
 * @file
Diego Biurrun's avatar
Diego Biurrun committed
24
 * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
25 26
 */

27
#include "libavutil/avstring.h"
28
#include "libavutil/mathematics.h"
29
#include "libavutil/opt.h"
30
#include "avformat.h"
31
#include "url.h"
32 33 34 35

#include <librtmp/rtmp.h>
#include <librtmp/log.h>

36 37 38 39 40 41 42
typedef struct LibRTMPContext {
    const AVClass *class;
    RTMP rtmp;
    char *app;
    char *playpath;
} LibRTMPContext;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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");
}

59 60
static int rtmp_close(URLContext *s)
{
61 62
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
63 64 65 66 67 68

    RTMP_Close(r);
    return 0;
}

/**
69
 * Open RTMP connection and verify that the stream can be played.
70 71 72 73 74 75 76 77 78 79 80 81
 *
 * 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)
{
82 83
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
84
    int rc = 0, level;
85
    char *filename = s->filename;
86

Diego Biurrun's avatar
Diego Biurrun committed
87
    switch (av_log_get_level()) {
88
    default:
89 90 91 92 93 94
    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;
95
    }
96
    RTMP_LogSetLevel(level);
97
    RTMP_LogSetCallback(rtmp_log);
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    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=");

        if (!(filename = av_malloc(len)))
            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);
        }
    }

118
    RTMP_Init(r);
119
    if (!RTMP_SetupURL(r, filename)) {
120
        rc = AVERROR_UNKNOWN;
121 122 123
        goto fail;
    }

124
    if (flags & AVIO_FLAG_WRITE)
125
        RTMP_EnableWrite(r);
126 127

    if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
128
        rc = AVERROR_UNKNOWN;
129 130 131 132
        goto fail;
    }

    s->is_streamed = 1;
133
    rc = 0;
134
fail:
135 136
    if (filename != s->filename)
        av_freep(&filename);
137 138 139
    if (rc)
        RTMP_Close(r);

140 141 142
    return rc;
}

143
static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
144
{
145 146
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
147 148 149 150 151 152

    return RTMP_Write(r, buf, size);
}

static int rtmp_read(URLContext *s, uint8_t *buf, int size)
{
153 154
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
155 156 157 158 159 160

    return RTMP_Read(r, buf, size);
}

static int rtmp_read_pause(URLContext *s, int pause)
{
161 162
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
163

164
    if (!RTMP_Pause(r, pause))
165
        return AVERROR_UNKNOWN;
166 167 168 169 170 171
    return 0;
}

static int64_t rtmp_read_seek(URLContext *s, int stream_index,
                              int64_t timestamp, int flags)
{
172 173
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
174 175

    if (flags & AVSEEK_FLAG_BYTE)
176
        return AVERROR(ENOSYS);
177 178

    /* seeks are in milliseconds */
179 180 181 182
    if (stream_index < 0)
        timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
            flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);

183
    if (!RTMP_SendSeek(r, timestamp))
184
        return AVERROR_UNKNOWN;
185 186 187 188 189
    return timestamp;
}

static int rtmp_get_file_handle(URLContext *s)
{
190 191
    LibRTMPContext *ctx = s->priv_data;
    RTMP *r = &ctx->rtmp;
192

193
    return RTMP_Socket(r);
194 195
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#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)
214
URLProtocol ff_librtmp_protocol = {
215 216 217 218 219 220 221
    .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,
222
    .url_get_file_handle = rtmp_get_file_handle,
223 224
    .priv_data_size      = sizeof(LibRTMPContext),
    .priv_data_class     = &librtmp_class,
225
    .flags               = URL_PROTOCOL_FLAG_NETWORK,
226 227
};

228
RTMP_CLASS(rtmpt)
229
URLProtocol ff_librtmpt_protocol = {
230 231 232 233 234 235 236
    .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,
237
    .url_get_file_handle = rtmp_get_file_handle,
238 239
    .priv_data_size      = sizeof(LibRTMPContext),
    .priv_data_class     = &librtmpt_class,
240
    .flags               = URL_PROTOCOL_FLAG_NETWORK,
241 242
};

243
RTMP_CLASS(rtmpe)
244
URLProtocol ff_librtmpe_protocol = {
245 246 247 248 249 250 251
    .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,
252
    .url_get_file_handle = rtmp_get_file_handle,
253 254
    .priv_data_size      = sizeof(LibRTMPContext),
    .priv_data_class     = &librtmpe_class,
255
    .flags               = URL_PROTOCOL_FLAG_NETWORK,
256 257
};

258
RTMP_CLASS(rtmpte)
259
URLProtocol ff_librtmpte_protocol = {
260 261 262 263 264 265 266
    .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,
267
    .url_get_file_handle = rtmp_get_file_handle,
268 269
    .priv_data_size      = sizeof(LibRTMPContext),
    .priv_data_class     = &librtmpte_class,
270
    .flags               = URL_PROTOCOL_FLAG_NETWORK,
271 272
};

273
RTMP_CLASS(rtmps)
274
URLProtocol ff_librtmps_protocol = {
275 276 277 278 279 280 281
    .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,
282
    .url_get_file_handle = rtmp_get_file_handle,
283 284
    .priv_data_size      = sizeof(LibRTMPContext),
    .priv_data_class     = &librtmps_class,
285
    .flags               = URL_PROTOCOL_FLAG_NETWORK,
286
};