00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/mathematics.h"
00029 #include "libavutil/opt.h"
00030 #include "avformat.h"
00031 #include "url.h"
00032
00033 #include <librtmp/rtmp.h>
00034 #include <librtmp/log.h>
00035
00036 typedef struct LibRTMPContext {
00037 const AVClass *class;
00038 RTMP rtmp;
00039 char *app;
00040 char *playpath;
00041 } LibRTMPContext;
00042
00043 static void rtmp_log(int level, const char *fmt, va_list args)
00044 {
00045 switch (level) {
00046 default:
00047 case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
00048 case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
00049 case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
00050 case RTMP_LOGINFO: level = AV_LOG_INFO; break;
00051 case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
00052 case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
00053 }
00054
00055 av_vlog(NULL, level, fmt, args);
00056 av_log(NULL, level, "\n");
00057 }
00058
00059 static int rtmp_close(URLContext *s)
00060 {
00061 LibRTMPContext *ctx = s->priv_data;
00062 RTMP *r = &ctx->rtmp;
00063
00064 RTMP_Close(r);
00065 return 0;
00066 }
00067
00080 static int rtmp_open(URLContext *s, const char *uri, int flags)
00081 {
00082 LibRTMPContext *ctx = s->priv_data;
00083 RTMP *r = &ctx->rtmp;
00084 int rc = 0, level;
00085 char *filename = s->filename;
00086
00087 switch (av_log_get_level()) {
00088 default:
00089 case AV_LOG_FATAL: level = RTMP_LOGCRIT; break;
00090 case AV_LOG_ERROR: level = RTMP_LOGERROR; break;
00091 case AV_LOG_WARNING: level = RTMP_LOGWARNING; break;
00092 case AV_LOG_INFO: level = RTMP_LOGINFO; break;
00093 case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break;
00094 case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break;
00095 }
00096 RTMP_LogSetLevel(level);
00097 RTMP_LogSetCallback(rtmp_log);
00098
00099 if (ctx->app || ctx->playpath) {
00100 int len = strlen(s->filename) + 1;
00101 if (ctx->app) len += strlen(ctx->app) + sizeof(" app=");
00102 if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath=");
00103
00104 if (!(filename = av_malloc(len)))
00105 return AVERROR(ENOMEM);
00106
00107 av_strlcpy(filename, s->filename, len);
00108 if (ctx->app) {
00109 av_strlcat(filename, " app=", len);
00110 av_strlcat(filename, ctx->app, len);
00111 }
00112 if (ctx->playpath) {
00113 av_strlcat(filename, " playpath=", len);
00114 av_strlcat(filename, ctx->playpath, len);
00115 }
00116 }
00117
00118 RTMP_Init(r);
00119 if (!RTMP_SetupURL(r, filename)) {
00120 rc = AVERROR_UNKNOWN;
00121 goto fail;
00122 }
00123
00124 if (flags & AVIO_FLAG_WRITE)
00125 RTMP_EnableWrite(r);
00126
00127 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
00128 rc = AVERROR_UNKNOWN;
00129 goto fail;
00130 }
00131
00132 s->is_streamed = 1;
00133 rc = 0;
00134 fail:
00135 if (filename != s->filename)
00136 av_freep(&filename);
00137 return rc;
00138 }
00139
00140 static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
00141 {
00142 LibRTMPContext *ctx = s->priv_data;
00143 RTMP *r = &ctx->rtmp;
00144
00145 return RTMP_Write(r, buf, size);
00146 }
00147
00148 static int rtmp_read(URLContext *s, uint8_t *buf, int size)
00149 {
00150 LibRTMPContext *ctx = s->priv_data;
00151 RTMP *r = &ctx->rtmp;
00152
00153 return RTMP_Read(r, buf, size);
00154 }
00155
00156 static int rtmp_read_pause(URLContext *s, int pause)
00157 {
00158 LibRTMPContext *ctx = s->priv_data;
00159 RTMP *r = &ctx->rtmp;
00160
00161 if (!RTMP_Pause(r, pause))
00162 return AVERROR_UNKNOWN;
00163 return 0;
00164 }
00165
00166 static int64_t rtmp_read_seek(URLContext *s, int stream_index,
00167 int64_t timestamp, int flags)
00168 {
00169 LibRTMPContext *ctx = s->priv_data;
00170 RTMP *r = &ctx->rtmp;
00171
00172 if (flags & AVSEEK_FLAG_BYTE)
00173 return AVERROR(ENOSYS);
00174
00175
00176 if (stream_index < 0)
00177 timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
00178 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
00179
00180 if (!RTMP_SendSeek(r, timestamp))
00181 return AVERROR_UNKNOWN;
00182 return timestamp;
00183 }
00184
00185 static int rtmp_get_file_handle(URLContext *s)
00186 {
00187 LibRTMPContext *ctx = s->priv_data;
00188 RTMP *r = &ctx->rtmp;
00189
00190 return RTMP_Socket(r);
00191 }
00192
00193 #define OFFSET(x) offsetof(LibRTMPContext, x)
00194 #define DEC AV_OPT_FLAG_DECODING_PARAM
00195 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00196 static const AVOption options[] = {
00197 {"rtmp_app", "Name of application to connect to on the RTMP server", OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
00198 {"rtmp_playpath", "Stream identifier to play or to publish", OFFSET(playpath), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
00199 { NULL },
00200 };
00201
00202 #define RTMP_CLASS(flavor)\
00203 static const AVClass lib ## flavor ## _class = {\
00204 .class_name = "lib" #flavor " protocol",\
00205 .item_name = av_default_item_name,\
00206 .option = options,\
00207 .version = LIBAVUTIL_VERSION_INT,\
00208 };
00209
00210 RTMP_CLASS(rtmp)
00211 URLProtocol ff_librtmp_protocol = {
00212 .name = "rtmp",
00213 .url_open = rtmp_open,
00214 .url_read = rtmp_read,
00215 .url_write = rtmp_write,
00216 .url_close = rtmp_close,
00217 .url_read_pause = rtmp_read_pause,
00218 .url_read_seek = rtmp_read_seek,
00219 .url_get_file_handle = rtmp_get_file_handle,
00220 .priv_data_size = sizeof(LibRTMPContext),
00221 .priv_data_class = &librtmp_class,
00222 .flags = URL_PROTOCOL_FLAG_NETWORK,
00223 };
00224
00225 RTMP_CLASS(rtmpt)
00226 URLProtocol ff_librtmpt_protocol = {
00227 .name = "rtmpt",
00228 .url_open = rtmp_open,
00229 .url_read = rtmp_read,
00230 .url_write = rtmp_write,
00231 .url_close = rtmp_close,
00232 .url_read_pause = rtmp_read_pause,
00233 .url_read_seek = rtmp_read_seek,
00234 .url_get_file_handle = rtmp_get_file_handle,
00235 .priv_data_size = sizeof(LibRTMPContext),
00236 .priv_data_class = &librtmpt_class,
00237 .flags = URL_PROTOCOL_FLAG_NETWORK,
00238 };
00239
00240 RTMP_CLASS(rtmpe)
00241 URLProtocol ff_librtmpe_protocol = {
00242 .name = "rtmpe",
00243 .url_open = rtmp_open,
00244 .url_read = rtmp_read,
00245 .url_write = rtmp_write,
00246 .url_close = rtmp_close,
00247 .url_read_pause = rtmp_read_pause,
00248 .url_read_seek = rtmp_read_seek,
00249 .url_get_file_handle = rtmp_get_file_handle,
00250 .priv_data_size = sizeof(LibRTMPContext),
00251 .priv_data_class = &librtmpe_class,
00252 .flags = URL_PROTOCOL_FLAG_NETWORK,
00253 };
00254
00255 RTMP_CLASS(rtmpte)
00256 URLProtocol ff_librtmpte_protocol = {
00257 .name = "rtmpte",
00258 .url_open = rtmp_open,
00259 .url_read = rtmp_read,
00260 .url_write = rtmp_write,
00261 .url_close = rtmp_close,
00262 .url_read_pause = rtmp_read_pause,
00263 .url_read_seek = rtmp_read_seek,
00264 .url_get_file_handle = rtmp_get_file_handle,
00265 .priv_data_size = sizeof(LibRTMPContext),
00266 .priv_data_class = &librtmpte_class,
00267 .flags = URL_PROTOCOL_FLAG_NETWORK,
00268 };
00269
00270 RTMP_CLASS(rtmps)
00271 URLProtocol ff_librtmps_protocol = {
00272 .name = "rtmps",
00273 .url_open = rtmp_open,
00274 .url_read = rtmp_read,
00275 .url_write = rtmp_write,
00276 .url_close = rtmp_close,
00277 .url_read_pause = rtmp_read_pause,
00278 .url_read_seek = rtmp_read_seek,
00279 .url_get_file_handle = rtmp_get_file_handle,
00280 .priv_data_size = sizeof(LibRTMPContext),
00281 .priv_data_class = &librtmps_class,
00282 .flags = URL_PROTOCOL_FLAG_NETWORK,
00283 };