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" |
d36e66bb |
#if CONFIG_NETWORK |
f845ae0c |
#include "network.h" |
d36e66bb |
#endif |
c486dade |
#include "url.h" |
0f943ce6 |
#include <librtmp/rtmp.h>
#include <librtmp/log.h>
|
007aedee |
typedef struct LibRTMPContext {
const AVClass *class;
RTMP rtmp;
char *app; |
7d027b9d |
char *conn;
char *subscribe; |
007aedee |
char *playpath; |
7d027b9d |
char *tcurl;
char *flashver;
char *swfurl;
char *swfverify;
char *pageurl;
char *client_buffer_time;
int live; |
86546109 |
char *temp_filename; |
f845ae0c |
int buffer_size; |
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); |
86546109 |
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; |
7d027b9d |
int len = strlen(s->filename) + 1; |
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 |
|
7d027b9d |
if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl=");
if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl=");
if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver=");
if (ctx->conn) {
char *sep, *p = ctx->conn;
int options = 0;
while (p) {
options++;
p += strspn(p, " ");
if (!*p)
break;
sep = strchr(p, ' ');
if (sep)
p = sep + 1;
else
break;
}
len += options * sizeof(" conn=");
len += strlen(ctx->conn);
}
if (ctx->playpath)
len += strlen(ctx->playpath) + sizeof(" playpath=");
if (ctx->live)
len += sizeof(" live=1");
if (ctx->subscribe)
len += strlen(ctx->subscribe) + sizeof(" subscribe=");
if (ctx->client_buffer_time)
len += strlen(ctx->client_buffer_time) + sizeof(" buffer="); |
007aedee |
|
7d027b9d |
if (ctx->swfurl || ctx->swfverify) {
len += sizeof(" swfUrl="); |
007aedee |
|
7d027b9d |
if (ctx->swfverify)
len += strlen(ctx->swfverify) + sizeof(" swfVfy=1");
else
len += strlen(ctx->swfurl);
}
|
86546109 |
if (!(ctx->temp_filename = filename = av_malloc(len))) |
7d027b9d |
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->tcurl) {
av_strlcat(filename, " tcUrl=", len);
av_strlcat(filename, ctx->tcurl, len);
}
if (ctx->pageurl) {
av_strlcat(filename, " pageUrl=", len);
av_strlcat(filename, ctx->pageurl, len);
}
if (ctx->swfurl) {
av_strlcat(filename, " swfUrl=", len); |
d1970929 |
av_strlcat(filename, ctx->swfurl, len); |
7d027b9d |
}
if (ctx->flashver) {
av_strlcat(filename, " flashVer=", len);
av_strlcat(filename, ctx->flashver, len);
}
if (ctx->conn) {
char *sep, *p = ctx->conn;
while (p) {
av_strlcat(filename, " conn=", len);
p += strspn(p, " ");
if (!*p)
break;
sep = strchr(p, ' ');
if (sep)
*sep = '\0';
av_strlcat(filename, p, len);
if (sep)
p = sep + 1; |
e85d38c2 |
else
break; |
007aedee |
} |
7d027b9d |
}
if (ctx->playpath) {
av_strlcat(filename, " playpath=", len);
av_strlcat(filename, ctx->playpath, len);
}
if (ctx->live)
av_strlcat(filename, " live=1", len);
if (ctx->subscribe) {
av_strlcat(filename, " subscribe=", len);
av_strlcat(filename, ctx->subscribe, len);
}
if (ctx->client_buffer_time) {
av_strlcat(filename, " buffer=", len);
av_strlcat(filename, ctx->client_buffer_time, len);
}
if (ctx->swfurl || ctx->swfverify) {
av_strlcat(filename, " swfUrl=", len);
if (ctx->swfverify) {
av_strlcat(filename, ctx->swfverify, len);
av_strlcat(filename, " swfVfy=1", len);
} else {
av_strlcat(filename, ctx->swfurl, len); |
007aedee |
}
}
|
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;
}
|
d36e66bb |
#if CONFIG_NETWORK |
f845ae0c |
if (ctx->buffer_size >= 0 && (flags & AVIO_FLAG_WRITE)) {
int tmp = ctx->buffer_size; |
4b2a2969 |
if (setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp))) {
rc = AVERROR_EXTERNAL;
goto fail;
} |
f845ae0c |
} |
d36e66bb |
#endif |
f845ae0c |
|
0f943ce6 |
s->is_streamed = 1; |
86546109 |
return 0; |
0f943ce6 |
fail: |
86546109 |
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 |
|
ed647ab7 |
int ret = RTMP_Write(r, buf, size);
if (!ret)
return AVERROR_EOF;
return ret; |
0f943ce6 |
}
static int rtmp_read(URLContext *s, uint8_t *buf, int size)
{ |
007aedee |
LibRTMPContext *ctx = s->priv_data;
RTMP *r = &ctx->rtmp; |
0f943ce6 |
|
ed647ab7 |
int ret = RTMP_Read(r, buf, size);
if (!ret)
return AVERROR_EOF;
return ret; |
0f943ce6 |
}
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[] = { |
7d027b9d |
{"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_buffer", "Set buffer time in milliseconds. The default is 3000.", OFFSET(client_buffer_time), AV_OPT_TYPE_STRING, {.str = "3000"}, 0, 0, DEC|ENC},
{"rtmp_conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
{"rtmp_flashver", "Version of the Flash plugin used to run the SWF player.", OFFSET(flashver), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
{"rtmp_live", "Specify that the media is a live stream.", OFFSET(live), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtmp_live"},
{"any", "both", 0, AV_OPT_TYPE_CONST, {.i64 = -2}, 0, 0, DEC, "rtmp_live"},
{"live", "live stream", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, DEC, "rtmp_live"},
{"recorded", "recorded stream", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, DEC, "rtmp_live"},
{"rtmp_pageurl", "URL of the web page in which the media was embedded. By default no value will be sent.", OFFSET(pageurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
{"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
{"rtmp_subscribe", "Name of live stream to subscribe to. Defaults to rtmp_playpath.", OFFSET(subscribe), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
{"rtmp_swfurl", "URL of the SWF player. By default no value will be sent", OFFSET(swfurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
{"rtmp_swfverify", "URL to player swf file, compute hash/size automatically. (unimplemented)", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
{"rtmp_tcurl", "URL of the target stream. Defaults to proto://host[:port]/app.", OFFSET(tcurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC}, |
d36e66bb |
#if CONFIG_NETWORK |
f845ae0c |
{"rtmp_buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC|ENC }, |
d36e66bb |
#endif |
007aedee |
{ 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) |
2758cded |
const 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) |
2758cded |
const 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) |
2758cded |
const 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) |
2758cded |
const 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) |
2758cded |
const 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 |
}; |