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.
21 #include "libavformat/avformat.h"
22 #include "libavformat/avio.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
)
45 if (url_seek(s
->priv
, s
->pos
, SEEK_SET
) < 0) {
52 static int control(stream_t
*s
, int cmd
, void *arg
)
57 case STREAM_CTRL_GET_SIZE
:
58 size
= url_filesize(s
->priv
);
64 case STREAM_CTRL_SEEK_TO_TIME
:
66 ts
= pts
* AV_TIME_BASE
;
67 ts
= av_url_read_seek(s
->priv
, -1, ts
, 0);
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
)
86 URLContext
*ctx
= NULL
;
87 int res
= STREAM_ERROR
;
92 if (mode
== STREAM_READ
)
94 else if (mode
== STREAM_WRITE
)
97 mp_msg(MSGT_OPEN
, MSGL_ERR
, "[ffmpeg] Unknown open mode %d\n", mode
);
98 res
= STREAM_UNSUPPORTED
;
103 filename
= stream
->url
;
105 mp_msg(MSGT_OPEN
, MSGL_ERR
, "[ffmpeg] No URL\n");
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)
116 mp_msg(MSGT_OPEN
, MSGL_V
, "[ffmpeg] libavformat URL type: %s\n",
118 if (!strncmp("rtmp", ctx
->prot
->name
, 4)) {
119 *file_format
= DEMUXER_TYPE_LAVF
;
120 stream
->lavf_type
= "flv";
123 size
= dummy
? 0 : url_filesize(ctx
);
125 stream
->end_pos
= size
;
126 stream
->type
= STREAMTYPE_FILE
;
128 if (dummy
|| ctx
->is_streamed
) {
129 stream
->type
= STREAMTYPE_STREAM
;
133 stream
->fill_buffer
= fill_buffer
;
134 stream
->write_buffer
= write_buffer
;
135 stream
->control
= control
;
136 stream
->close
= close_f
;
144 const stream_info_t stream_info_ffmpeg
= {
150 { "ffmpeg", "rtmp", NULL
},
152 1 // Urls are an option string