talloc.[ch]: remove "type safety" hack that violates C types
[mplayer.git] / stream / stream_ffmpeg.c
blob101b1124486a822d1cf76ab83ccd79074cb00b6a
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include "libavformat/avformat.h"
22 #include "libavformat/avio.h"
23 #include "mp_msg.h"
24 #include "stream.h"
25 #include "m_option.h"
26 #include "m_struct.h"
27 #include "libmpdemux/demuxer.h"
29 static int fill_buffer(stream_t *s, char *buffer, int max_len)
31 int r = url_read_complete(s->priv, buffer, max_len);
32 return (r <= 0) ? -1 : r;
35 static int write_buffer(stream_t *s, char *buffer, int len)
37 /* url_write retries internally on short writes and EAGAIN */
38 int r = url_write(s->priv, buffer, len);
39 return (r <= 0) ? -1 : r;
42 static int seek(stream_t *s, off_t newpos)
44 s->pos = newpos;
45 if (url_seek(s->priv, s->pos, SEEK_SET) < 0) {
46 s->eof = 1;
47 return 0;
49 return 1;
52 static int control(stream_t *s, int cmd, void *arg)
54 int64_t size, ts;
55 double pts;
56 switch(cmd) {
57 case STREAM_CTRL_GET_SIZE:
58 size = url_filesize(s->priv);
59 if(size >= 0) {
60 *(off_t *)arg = size;
61 return 1;
63 break;
64 case STREAM_CTRL_SEEK_TO_TIME:
65 pts = *(double *)arg;
66 ts = pts * AV_TIME_BASE;
67 ts = av_url_read_seek(s->priv, -1, ts, 0);
68 if (ts >= 0)
69 return 1;
70 break;
72 return STREAM_UNSUPPORTED;
75 static void close_f(stream_t *stream)
77 url_close(stream->priv);
80 static const char prefix[] = "ffmpeg://";
82 static int open_f(stream_t *stream, int mode, void *opts, int *file_format)
84 int flags = 0;
85 const char *filename;
86 URLContext *ctx = NULL;
87 int res = STREAM_ERROR;
88 int64_t size;
89 int dummy;
91 if (mode == STREAM_READ)
92 flags = URL_RDONLY;
93 else if (mode == STREAM_WRITE)
94 flags = URL_WRONLY;
95 else {
96 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
97 res = STREAM_UNSUPPORTED;
98 goto out;
101 if (stream->url)
102 filename = stream->url;
103 else {
104 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
105 goto out;
107 if (!strncmp(filename, prefix, strlen(prefix)))
108 filename += strlen(prefix);
109 dummy = !strncmp(filename, "rtsp:", 5);
110 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);
112 if (!dummy && url_open(&ctx, filename, flags) < 0)
113 goto out;
115 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] libavformat URL type: %s\n",
116 ctx->prot->name);
117 if (!strncmp("rtmp", ctx->prot->name, 4)) {
118 *file_format = DEMUXER_TYPE_LAVF;
119 stream->lavf_type = "flv";
121 stream->priv = ctx;
122 size = dummy ? 0 : url_filesize(ctx);
123 if (size >= 0)
124 stream->end_pos = size;
125 stream->type = STREAMTYPE_FILE;
126 stream->seek = seek;
127 if (dummy || ctx->is_streamed) {
128 stream->type = STREAMTYPE_STREAM;
129 stream->seek = NULL;
131 if (!dummy) {
132 stream->fill_buffer = fill_buffer;
133 stream->write_buffer = write_buffer;
134 stream->control = control;
135 stream->close = close_f;
137 res = STREAM_OK;
139 out:
140 return res;
143 const stream_info_t stream_info_ffmpeg = {
144 "FFmpeg",
145 "ffmpeg",
148 open_f,
149 { "ffmpeg", "rtmp", NULL },
150 NULL,
151 1 // Urls are an option string