comparison stream/stream_ffmpeg.c @ 29881:5797dc687a8e

Add preliminary support for streaming via FFmpeg's URProtocol functions. Basic playback tested for file and http protocols.
author reimar
date Tue, 17 Nov 2009 16:09:17 +0000
parents
children 988c42cb595a
comparison
equal deleted inserted replaced
29880:ec407332afae 29881:5797dc687a8e
1 #include "config.h"
2
3 #include "libavformat/avformat.h"
4 #include "libavformat/avio.h"
5 #include "mp_msg.h"
6 #include "stream.h"
7 #include "m_option.h"
8 #include "m_struct.h"
9
10 static struct stream_priv_s {
11 char *filename;
12 char *filename2;
13 } stream_priv_dflts = {
14 NULL, NULL
15 };
16
17 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
18 /// URL definition
19 static const m_option_t stream_opts_fields[] = {
20 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
21 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL},
22 {NULL}
23 };
24
25 static const struct m_struct_st stream_opts = {
26 "ffmpeg",
27 sizeof(struct stream_priv_s),
28 &stream_priv_dflts,
29 stream_opts_fields
30 };
31
32 static int fill_buffer(stream_t *s, char *buffer, int max_len)
33 {
34 int r = url_read_complete(s->priv, buffer, max_len);
35 return (r <= 0) ? -1 : r;
36 }
37
38 static int write_buffer(stream_t *s, char *buffer, int len)
39 {
40 int r = url_write(s->priv, buffer, len);
41 return (r <= 0) ? -1 : r;
42 }
43
44 static int seek(stream_t *s, off_t newpos)
45 {
46 s->pos = newpos;
47 if (url_seek(s->priv, s->pos, SEEK_SET) < 0) {
48 s->eof = 1;
49 return 0;
50 }
51 return 1;
52 }
53
54 static int control(stream_t *s, int cmd, void *arg)
55 {
56 int64_t size;
57 switch(cmd) {
58 case STREAM_CTRL_GET_SIZE:
59 size = url_filesize(s->priv);
60 if(size >= 0) {
61 *(off_t *)arg = size;
62 return 1;
63 }
64 }
65 return STREAM_UNSUPPORTED;
66 }
67
68 static void close_f(stream_t *stream)
69 {
70 url_close(stream->priv);
71 }
72
73 static const char prefix[] = "ffmpeg://";
74
75 static int open_f(stream_t *stream, int mode, void *opts, int *file_format)
76 {
77 int flags = 0;
78 const char *filename;
79 struct stream_priv_s *p = opts;
80 URLContext *ctx = NULL;
81 int res = STREAM_ERROR;
82 int64_t size;
83
84 av_register_all();
85 if (mode == STREAM_READ)
86 flags = URL_RDONLY;
87 else if (mode == STREAM_WRITE)
88 flags = URL_WRONLY;
89 else {
90 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
91 res = STREAM_UNSUPPORTED;
92 goto out;
93 }
94
95 if (p->filename)
96 filename = p->filename;
97 else if (p->filename2)
98 filename = p->filename2;
99 else {
100 mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
101 goto out;
102 }
103 if (!strncmp(filename, prefix, strlen(prefix)))
104 filename += strlen(prefix);
105 mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);
106
107 if (url_open(&ctx, filename, flags) < 0)
108 goto out;
109
110 stream->priv = ctx;
111 size = url_filesize(ctx);
112 if (size >= 0)
113 stream->end_pos = size;
114 stream->type = STREAMTYPE_FILE;
115 if (ctx->is_streamed) {
116 stream->type = STREAMTYPE_STREAM;
117 stream->flags |= STREAM_SEEK_FW;
118 }
119 stream->seek = seek;
120 stream->fill_buffer = fill_buffer;
121 stream->write_buffer = write_buffer;
122 stream->control = control;
123 stream->close = close_f;
124 res = STREAM_OK;
125
126 out:
127 m_struct_free(&stream_opts,opts);
128 return res;
129 }
130
131 const stream_info_t stream_info_ffmpeg = {
132 "FFmpeg",
133 "ffmpeg",
134 "",
135 "",
136 open_f,
137 { "ffmpeg", NULL },
138 &stream_opts,
139 1 // Urls are an option string
140 };