2010-01-30 23:26:47 +01:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-11-17 17:09:17 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
#include "libavformat/avio.h"
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "core/mp_msg.h"
|
2009-11-17 17:09:17 +01:00
|
|
|
#include "stream.h"
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "core/m_option.h"
|
|
|
|
#include "core/m_struct.h"
|
|
|
|
#include "demux/demux.h"
|
2009-11-17 17:09:17 +01:00
|
|
|
|
|
|
|
static int fill_buffer(stream_t *s, char *buffer, int max_len)
|
|
|
|
{
|
2011-12-25 00:20:12 +01:00
|
|
|
AVIOContext *avio = s->priv;
|
|
|
|
int r = avio_read(avio, buffer, max_len);
|
2009-11-17 17:09:17 +01:00
|
|
|
return (r <= 0) ? -1 : r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_buffer(stream_t *s, char *buffer, int len)
|
|
|
|
{
|
2011-12-25 00:20:12 +01:00
|
|
|
AVIOContext *avio = s->priv;
|
|
|
|
avio_write(avio, buffer, len);
|
|
|
|
avio_flush(avio);
|
|
|
|
if (avio->error)
|
|
|
|
return -1;
|
|
|
|
return len;
|
2009-11-17 17:09:17 +01:00
|
|
|
}
|
|
|
|
|
2012-11-18 20:46:12 +01:00
|
|
|
static int seek(stream_t *s, int64_t newpos)
|
2009-11-17 17:09:17 +01:00
|
|
|
{
|
2011-12-25 00:20:12 +01:00
|
|
|
AVIOContext *avio = s->priv;
|
2009-11-17 17:09:17 +01:00
|
|
|
s->pos = newpos;
|
2011-12-25 00:20:12 +01:00
|
|
|
if (avio_seek(avio, s->pos, SEEK_SET) < 0) {
|
2009-11-17 17:09:17 +01:00
|
|
|
s->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control(stream_t *s, int cmd, void *arg)
|
|
|
|
{
|
2012-04-18 00:27:55 +02:00
|
|
|
AVIOContext *avio = s->priv;
|
2010-04-23 22:50:34 +02:00
|
|
|
int64_t size, ts;
|
|
|
|
double pts;
|
2009-11-17 17:09:17 +01:00
|
|
|
switch(cmd) {
|
|
|
|
case STREAM_CTRL_GET_SIZE:
|
2011-12-25 00:20:12 +01:00
|
|
|
size = avio_size(avio);
|
2009-11-17 17:09:17 +01:00
|
|
|
if(size >= 0) {
|
2012-08-18 21:51:58 +02:00
|
|
|
*(uint64_t *)arg = size;
|
2009-11-17 17:09:17 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-04-23 22:50:34 +02:00
|
|
|
break;
|
|
|
|
case STREAM_CTRL_SEEK_TO_TIME:
|
|
|
|
pts = *(double *)arg;
|
|
|
|
ts = pts * AV_TIME_BASE;
|
2011-12-25 00:20:12 +01:00
|
|
|
ts = avio_seek_time(avio, -1, ts, 0);
|
2010-04-23 22:50:34 +02:00
|
|
|
if (ts >= 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
2009-11-17 17:09:17 +01:00
|
|
|
}
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_f(stream_t *stream)
|
|
|
|
{
|
2011-12-25 00:20:12 +01:00
|
|
|
AVIOContext *avio = stream->priv;
|
|
|
|
/* NOTE: As of 2011 write streams must be manually flushed before close.
|
|
|
|
* Currently write_buffer() always flushes them after writing.
|
|
|
|
* avio_close() could return an error, but we have no way to return that
|
|
|
|
* with the current stream API.
|
|
|
|
*/
|
|
|
|
avio_close(avio);
|
2009-11-17 17:09:17 +01:00
|
|
|
}
|
|
|
|
|
2012-09-07 18:39:46 +02:00
|
|
|
static const char * const prefix[] = { "lavf://", "ffmpeg://" };
|
2009-11-17 17:09:17 +01:00
|
|
|
|
|
|
|
static int open_f(stream_t *stream, int mode, void *opts, int *file_format)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
const char *filename;
|
2011-12-25 00:20:12 +01:00
|
|
|
AVIOContext *avio = NULL;
|
2009-11-17 17:09:17 +01:00
|
|
|
int res = STREAM_ERROR;
|
|
|
|
|
|
|
|
if (mode == STREAM_READ)
|
2012-01-28 12:41:36 +01:00
|
|
|
flags = AVIO_FLAG_READ;
|
2009-11-17 17:09:17 +01:00
|
|
|
else if (mode == STREAM_WRITE)
|
2012-01-28 12:41:36 +01:00
|
|
|
flags = AVIO_FLAG_WRITE;
|
2009-11-17 17:09:17 +01:00
|
|
|
else {
|
|
|
|
mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
|
|
|
|
res = STREAM_UNSUPPORTED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-01-06 17:11:29 +01:00
|
|
|
if (stream->url)
|
|
|
|
filename = stream->url;
|
2009-11-17 17:09:17 +01:00
|
|
|
else {
|
|
|
|
mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2012-09-07 18:39:46 +02:00
|
|
|
for (int i = 0; i < sizeof(prefix) / sizeof(prefix[0]); i++)
|
|
|
|
if (!strncmp(filename, prefix[i], strlen(prefix[i])))
|
|
|
|
filename += strlen(prefix[i]);
|
|
|
|
if (!strncmp(filename, "rtsp:", 5)) {
|
|
|
|
/* This is handled as a special demuxer, without a separate
|
|
|
|
* stream layer. demux_lavf will do all the real work.
|
|
|
|
*/
|
|
|
|
stream->type = STREAMTYPE_STREAM;
|
|
|
|
stream->seek = NULL;
|
|
|
|
*file_format = DEMUXER_TYPE_LAVF;
|
|
|
|
stream->lavf_type = "rtsp";
|
|
|
|
return STREAM_OK;
|
|
|
|
}
|
2009-11-17 17:09:17 +01:00
|
|
|
mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);
|
|
|
|
|
2012-09-07 18:39:46 +02:00
|
|
|
if (avio_open(&avio, filename, flags) < 0)
|
2009-11-17 17:09:17 +01:00
|
|
|
goto out;
|
|
|
|
|
2011-12-25 00:20:12 +01:00
|
|
|
char *rtmp[] = {"rtmp:", "rtmpt:", "rtmpe:", "rtmpte:", "rtmps:"};
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(rtmp); i++)
|
|
|
|
if (!strncmp(filename, rtmp[i], strlen(rtmp[i]))) {
|
|
|
|
*file_format = DEMUXER_TYPE_LAVF;
|
|
|
|
stream->lavf_type = "flv";
|
|
|
|
}
|
|
|
|
stream->priv = avio;
|
2012-09-07 18:39:46 +02:00
|
|
|
int64_t size = avio_size(avio);
|
2009-11-17 17:09:17 +01:00
|
|
|
if (size >= 0)
|
|
|
|
stream->end_pos = size;
|
|
|
|
stream->type = STREAMTYPE_FILE;
|
2009-11-17 19:30:33 +01:00
|
|
|
stream->seek = seek;
|
2012-09-07 18:39:46 +02:00
|
|
|
if (!avio->seekable) {
|
2009-11-17 17:09:17 +01:00
|
|
|
stream->type = STREAMTYPE_STREAM;
|
2009-11-17 19:30:33 +01:00
|
|
|
stream->seek = NULL;
|
2009-11-17 17:09:17 +01:00
|
|
|
}
|
2012-09-07 18:39:46 +02:00
|
|
|
stream->fill_buffer = fill_buffer;
|
|
|
|
stream->write_buffer = write_buffer;
|
|
|
|
stream->control = control;
|
|
|
|
stream->close = close_f;
|
2009-11-17 17:09:17 +01:00
|
|
|
res = STREAM_OK;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stream_info_t stream_info_ffmpeg = {
|
|
|
|
"FFmpeg",
|
|
|
|
"ffmpeg",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
open_f,
|
2012-09-07 18:39:46 +02:00
|
|
|
{ "lavf", "ffmpeg", "rtmp", "rtsp", NULL },
|
2010-01-06 17:11:29 +01:00
|
|
|
NULL,
|
2009-11-17 17:09:17 +01:00
|
|
|
1 // Urls are an option string
|
|
|
|
};
|