Merge svn changes up to r30475
[mplayer/glamo.git] / stream / stream_ffmpeg.c
blobf8ca8f6e20bc136ab53796d18d09b746003dfd27
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;
79 av_register_all();
80 if (mode == STREAM_READ)
81 flags = URL_RDONLY;
82 else if (mode == STREAM_WRITE)
83 flags = URL_WRONLY;
84 else {
85 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
86 res = STREAM_UNSUPPORTED;
87 goto out;
90 if (stream->url)
91 filename = stream->url;
92 else {
93 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
94 goto out;
96 if (!strncmp(filename, prefix, strlen(prefix)))
97 filename += strlen(prefix);
98 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);
100 if (url_open(&ctx, filename, flags) < 0)
101 goto out;
103 stream->priv = ctx;
104 size = url_filesize(ctx);
105 if (size >= 0)
106 stream->end_pos = size;
107 stream->type = STREAMTYPE_FILE;
108 stream->seek = seek;
109 if (ctx->is_streamed) {
110 stream->type = STREAMTYPE_STREAM;
111 stream->seek = NULL;
113 stream->fill_buffer = fill_buffer;
114 stream->write_buffer = write_buffer;
115 stream->control = control;
116 stream->close = close_f;
117 res = STREAM_OK;
119 out:
120 return res;
123 const stream_info_t stream_info_ffmpeg = {
124 "FFmpeg",
125 "ffmpeg",
128 open_f,
129 { "ffmpeg", "rtmp", NULL },
130 NULL,
131 1 // Urls are an option string