Windows: ao_pcm: add io.h include for MinGW64
[mplayer.git] / stream / stream_ffmpeg.c
blob343381afb0a8a085375d5778ae48df63379d18ca
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 av_register_all();
92 if (mode == STREAM_READ)
93 flags = URL_RDONLY;
94 else if (mode == STREAM_WRITE)
95 flags = URL_WRONLY;
96 else {
97 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
98 res = STREAM_UNSUPPORTED;
99 goto out;
102 if (stream->url)
103 filename = stream->url;
104 else {
105 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
106 goto out;
108 if (!strncmp(filename, prefix, strlen(prefix)))
109 filename += strlen(prefix);
110 dummy = !strncmp(filename, "rtsp:", 5);
111 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);
113 if (!dummy && url_open(&ctx, filename, flags) < 0)
114 goto out;
116 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] libavformat URL type: %s\n",
117 ctx->prot->name);
118 if (!strncmp("rtmp", ctx->prot->name, 4)) {
119 *file_format = DEMUXER_TYPE_LAVF;
120 stream->lavf_type = "flv";
122 stream->priv = ctx;
123 size = dummy ? 0 : url_filesize(ctx);
124 if (size >= 0)
125 stream->end_pos = size;
126 stream->type = STREAMTYPE_FILE;
127 stream->seek = seek;
128 if (dummy || ctx->is_streamed) {
129 stream->type = STREAMTYPE_STREAM;
130 stream->seek = NULL;
132 if (!dummy) {
133 stream->fill_buffer = fill_buffer;
134 stream->write_buffer = write_buffer;
135 stream->control = control;
136 stream->close = close_f;
138 res = STREAM_OK;
140 out:
141 return res;
144 const stream_info_t stream_info_ffmpeg = {
145 "FFmpeg",
146 "ffmpeg",
149 open_f,
150 { "ffmpeg", "rtmp", NULL },
151 NULL,
152 1 // Urls are an option string