Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / stream_ffmpeg.c
blob37c0800369dad987de15efdf3c24b984d6d85d3d
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"
28 static int fill_buffer(stream_t *s, char *buffer, int max_len)
30 int r = url_read(s->priv, buffer, max_len);
31 return (r <= 0) ? -1 : r;
34 static int write_buffer(stream_t *s, char *buffer, int len)
36 int r = url_write(s->priv, buffer, len);
37 return (r <= 0) ? -1 : r;
40 static int seek(stream_t *s, off_t newpos)
42 s->pos = newpos;
43 if (url_seek(s->priv, s->pos, SEEK_SET) < 0) {
44 s->eof = 1;
45 return 0;
47 return 1;
50 static int control(stream_t *s, int cmd, void *arg)
52 int64_t size;
53 switch(cmd) {
54 case STREAM_CTRL_GET_SIZE:
55 size = url_filesize(s->priv);
56 if(size >= 0) {
57 *(off_t *)arg = size;
58 return 1;
61 return STREAM_UNSUPPORTED;
64 static void close_f(stream_t *stream)
66 url_close(stream->priv);
69 static const char prefix[] = "ffmpeg://";
71 static int open_f(stream_t *stream, int mode, void *opts, int *file_format)
73 int flags = 0;
74 const char *filename;
75 URLContext *ctx = NULL;
76 int res = STREAM_ERROR;
77 int64_t size;
78 int dummy;
80 av_register_all();
81 if (mode == STREAM_READ)
82 flags = URL_RDONLY;
83 else if (mode == STREAM_WRITE)
84 flags = URL_WRONLY;
85 else {
86 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
87 res = STREAM_UNSUPPORTED;
88 goto out;
91 if (stream->url)
92 filename = stream->url;
93 else {
94 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
95 goto out;
97 if (!strncmp(filename, prefix, strlen(prefix)))
98 filename += strlen(prefix);
99 dummy = !strncmp(filename, "rtsp:", 5);
100 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);
102 if (!dummy && url_open(&ctx, filename, flags) < 0)
103 goto out;
105 stream->priv = ctx;
106 size = dummy ? 0 : url_filesize(ctx);
107 if (size >= 0)
108 stream->end_pos = size;
109 stream->type = STREAMTYPE_FILE;
110 stream->seek = seek;
111 if (dummy || ctx->is_streamed) {
112 stream->type = STREAMTYPE_STREAM;
113 stream->seek = NULL;
115 if (!dummy) {
116 stream->fill_buffer = fill_buffer;
117 stream->write_buffer = write_buffer;
118 stream->control = control;
119 stream->close = close_f;
121 res = STREAM_OK;
123 out:
124 return res;
127 const stream_info_t stream_info_ffmpeg = {
128 "FFmpeg",
129 "ffmpeg",
132 open_f,
133 { "ffmpeg", "rtmp", NULL },
134 NULL,
135 1 // Urls are an option string