2010-03-28 15:39:36 +02:00
|
|
|
/*
|
|
|
|
* RTMP network protocol
|
|
|
|
* Copyright (c) 2010 Howard Chu
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2010-03-28 15:39:36 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-03-28 15:39:36 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-03-28 15:39:36 +02:00
|
|
|
* 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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-03-28 15:39:36 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2010-04-14 00:08:36 +02:00
|
|
|
* RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
|
2010-03-28 15:39:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
2011-04-07 20:25:52 +02:00
|
|
|
#include "url.h"
|
2010-03-28 15:39:36 +02:00
|
|
|
|
|
|
|
#include <librtmp/rtmp.h>
|
|
|
|
#include <librtmp/log.h>
|
|
|
|
|
2010-04-14 00:11:21 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2010-03-28 15:39:36 +02:00
|
|
|
static int rtmp_close(URLContext *s)
|
|
|
|
{
|
|
|
|
RTMP *r = s->priv_data;
|
|
|
|
|
|
|
|
RTMP_Close(r);
|
|
|
|
av_free(r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 17:38:06 +02:00
|
|
|
* Open RTMP connection and verify that the stream can be played.
|
2010-03-28 15:39:36 +02:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
RTMP *r;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
r = av_mallocz(sizeof(RTMP));
|
|
|
|
if (!r)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2010-04-14 00:08:36 +02:00
|
|
|
switch (av_log_get_level()) {
|
2010-03-28 15:39:36 +02:00
|
|
|
default:
|
|
|
|
case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break;
|
|
|
|
case AV_LOG_ERROR: rc = RTMP_LOGERROR; break;
|
|
|
|
case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
|
|
|
|
case AV_LOG_INFO: rc = RTMP_LOGINFO; break;
|
|
|
|
case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG; break;
|
|
|
|
case AV_LOG_DEBUG: rc = RTMP_LOGDEBUG2; break;
|
|
|
|
}
|
|
|
|
RTMP_LogSetLevel(rc);
|
2010-04-14 00:11:21 +02:00
|
|
|
RTMP_LogSetCallback(rtmp_log);
|
2010-03-28 15:39:36 +02:00
|
|
|
|
|
|
|
RTMP_Init(r);
|
|
|
|
if (!RTMP_SetupURL(r, s->filename)) {
|
|
|
|
rc = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-04-04 20:11:19 +02:00
|
|
|
if (flags & AVIO_WRONLY)
|
2010-07-01 12:59:44 +02:00
|
|
|
RTMP_EnableWrite(r);
|
2010-03-28 15:39:36 +02:00
|
|
|
|
|
|
|
if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
|
|
|
|
rc = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-04-14 00:08:36 +02:00
|
|
|
s->priv_data = r;
|
2010-03-28 15:39:36 +02:00
|
|
|
s->is_streamed = 1;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_free(r);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-06-01 09:46:23 +02:00
|
|
|
static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
|
2010-03-28 15:39:36 +02:00
|
|
|
{
|
|
|
|
RTMP *r = s->priv_data;
|
|
|
|
|
|
|
|
return RTMP_Write(r, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtmp_read(URLContext *s, uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
RTMP *r = s->priv_data;
|
|
|
|
|
|
|
|
return RTMP_Read(r, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtmp_read_pause(URLContext *s, int pause)
|
|
|
|
{
|
|
|
|
RTMP *r = s->priv_data;
|
|
|
|
|
2010-07-01 12:59:44 +02:00
|
|
|
if (!RTMP_Pause(r, pause))
|
2010-03-28 15:39:36 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t rtmp_read_seek(URLContext *s, int stream_index,
|
|
|
|
int64_t timestamp, int flags)
|
|
|
|
{
|
|
|
|
RTMP *r = s->priv_data;
|
|
|
|
|
|
|
|
if (flags & AVSEEK_FLAG_BYTE)
|
2010-04-18 21:09:22 +02:00
|
|
|
return AVERROR(ENOSYS);
|
2010-03-28 15:39:36 +02:00
|
|
|
|
|
|
|
/* seeks are in milliseconds */
|
2010-04-18 21:09:25 +02:00
|
|
|
if (stream_index < 0)
|
|
|
|
timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
|
|
|
|
flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
|
|
|
|
|
2010-03-28 15:39:36 +02:00
|
|
|
if (!RTMP_SendSeek(r, timestamp))
|
|
|
|
return -1;
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtmp_get_file_handle(URLContext *s)
|
|
|
|
{
|
|
|
|
RTMP *r = s->priv_data;
|
|
|
|
|
2010-07-01 12:59:44 +02:00
|
|
|
return RTMP_Socket(r);
|
2010-03-28 15:39:36 +02:00
|
|
|
}
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
URLProtocol ff_rtmp_protocol = {
|
2011-04-08 07:41:47 +02:00
|
|
|
.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
|
2010-03-28 15:39:36 +02:00
|
|
|
};
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
URLProtocol ff_rtmpt_protocol = {
|
2011-04-08 07:41:47 +02:00
|
|
|
.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
|
2010-03-28 15:39:36 +02:00
|
|
|
};
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
URLProtocol ff_rtmpe_protocol = {
|
2011-04-08 07:41:47 +02:00
|
|
|
.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
|
2010-03-28 15:39:36 +02:00
|
|
|
};
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
URLProtocol ff_rtmpte_protocol = {
|
2011-04-08 07:41:47 +02:00
|
|
|
.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
|
2010-03-28 15:39:36 +02:00
|
|
|
};
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
URLProtocol ff_rtmps_protocol = {
|
2011-04-08 07:41:47 +02:00
|
|
|
.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
|
2010-03-28 15:39:36 +02:00
|
|
|
};
|