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