forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavio.c
349 lines (295 loc) · 9.48 KB
/
avio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*****************************************************************************
* avio.c: access using libavformat library
*****************************************************************************
* Copyright (C) 2009 Laurent Aimar
*
* Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <vlc_common.h>
#include <vlc_configuration.h>
#include <vlc_plugin.h>
#include <vlc_access.h>
#include <vlc_sout.h>
#include <vlc_avcodec.h>
#include <vlc_interrupt.h>
#include "avio.h"
#include "../codec/avcodec/avcommon.h"
/*****************************************************************************
* Module descriptor
*****************************************************************************/
#ifndef MERGE_FFMPEG
vlc_module_begin()
AVIO_MODULE
vlc_module_end()
#endif
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static ssize_t Read (stream_t *, void *, size_t);
static int Seek (stream_t *, uint64_t);
static int Control(stream_t *, int, va_list);
static ssize_t Write(sout_access_out_t *, block_t *);
static int OutControl(sout_access_out_t *, int, va_list);
static int OutSeek (sout_access_out_t *, uint64_t);
static int UrlInterruptCallback(void *access)
{
/* NOTE: This works so long as libavformat invokes the callback from the
* same thread that invokes libavformat. Currently libavformat does not
* create internal threads at all. This is not proper event handling in any
* case; libavformat needs fixing. */
(void) access;
return vlc_killed();
}
typedef struct
{
AVIOContext *context;
int64_t size;
} access_sys_t;
typedef struct
{
AVIOContext *context;
} sout_access_out_sys_t;
/* */
/* */
int OpenAvio(vlc_object_t *object)
{
stream_t *access = (stream_t*)object;
access_sys_t *sys = vlc_obj_malloc(object, sizeof(*sys));
if (!sys)
return VLC_ENOMEM;
sys->context = NULL;
/* We accept:
* - avio://full_url
* - url (only a subset of available protocols).
*/
char *url;
if (!strcmp(access->psz_name, "avio"))
url = strdup(access->psz_location);
else if (asprintf(&url, "%s://%s", access->psz_name,
access->psz_location) < 0)
url = NULL;
if (!url)
return VLC_ENOMEM;
/* */
vlc_init_avformat(object);
int ret;
AVIOInterruptCB cb = {
.callback = UrlInterruptCallback,
.opaque = access,
};
AVDictionary *options = NULL;
char *psz_opts = var_InheritString(access, "avio-options");
if (psz_opts) {
vlc_av_get_options(psz_opts, &options);
free(psz_opts);
}
ret = avio_open2(&sys->context, url, AVIO_FLAG_READ, &cb, &options);
AVDictionaryEntry *t = NULL;
while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
msg_Err( access, "unknown option \"%s\"", t->key );
av_dict_free(&options);
if (ret < 0) {
msg_Err(access, "Failed to open %s: %s", url,
vlc_strerror_c(AVUNERROR(ret)));
free(url);
return VLC_EGENERIC;
}
free(url);
sys->size = avio_size(sys->context);
bool seekable;
seekable = sys->context->seekable;
msg_Dbg(access, "%sseekable, size=%"PRIi64, seekable ? "" : "not ",
sys->size);
/* */
access->pf_read = Read;
access->pf_block = NULL;
access->pf_control = Control;
access->pf_seek = Seek;
access->p_sys = sys;
return VLC_SUCCESS;
}
/* */
static const char *const ppsz_sout_options[] = {
"options",
NULL,
};
int OutOpenAvio(vlc_object_t *object)
{
sout_access_out_t *access = (sout_access_out_t*)object;
config_ChainParse( access, "sout-avio-", ppsz_sout_options, access->p_cfg );
sout_access_out_sys_t *sys = vlc_obj_malloc(object, sizeof(*sys));
if (!sys)
return VLC_ENOMEM;
sys->context = NULL;
/* */
vlc_init_avformat(object);
if (!access->psz_path)
return VLC_EGENERIC;
int ret;
AVDictionary *options = NULL;
char *psz_opts = var_InheritString(access, "sout-avio-options");
if (psz_opts) {
vlc_av_get_options(psz_opts, &options);
free(psz_opts);
}
ret = avio_open2(&sys->context, access->psz_path, AVIO_FLAG_WRITE,
NULL, &options);
AVDictionaryEntry *t = NULL;
while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX)))
msg_Err( access, "unknown option \"%s\"", t->key );
av_dict_free(&options);
if (ret < 0) {
errno = AVUNERROR(ret);
msg_Err(access, "Failed to open %s", access->psz_path);
return VLC_EGENERIC;
}
access->pf_write = Write;
access->pf_control = OutControl;
access->pf_seek = OutSeek;
access->p_sys = sys;
return VLC_SUCCESS;
}
void CloseAvio(vlc_object_t *object)
{
stream_t *access = (stream_t*)object;
access_sys_t *sys = access->p_sys;
avio_close(sys->context);
}
void OutCloseAvio(vlc_object_t *object)
{
sout_access_out_t *access = (sout_access_out_t*)object;
sout_access_out_sys_t *sys = access->p_sys;
avio_close(sys->context);
}
static ssize_t Read(stream_t *access, void *data, size_t size)
{
access_sys_t *sys = access->p_sys;
int r = avio_read(sys->context, data, size);
if (r < 0)
r = 0;
return r;
}
/*****************************************************************************
* Write:
*****************************************************************************/
static ssize_t Write(sout_access_out_t *p_access, block_t *p_buffer)
{
sout_access_out_sys_t *p_sys = (sout_access_out_sys_t*)p_access->p_sys;
size_t i_write = 0;
int val;
while (p_buffer != NULL) {
block_t *p_next = p_buffer->p_next;
avio_write(p_sys->context, p_buffer->p_buffer, p_buffer->i_buffer);
avio_flush(p_sys->context);
if ((val = p_sys->context->error) != 0) {
p_sys->context->error = 0; /* FIXME? */
goto error;
}
i_write += p_buffer->i_buffer;
block_Release(p_buffer);
p_buffer = p_next;
}
return i_write;
error:
msg_Err(p_access, "Wrote only %zu bytes: %s", i_write,
vlc_strerror_c(AVUNERROR(val)));
block_ChainRelease( p_buffer );
return i_write;
}
static int Seek(stream_t *access, uint64_t position)
{
access_sys_t *sys = access->p_sys;
int ret;
#ifndef EOVERFLOW
# define EOVERFLOW EFBIG
#endif
if (position > INT64_MAX)
ret = AVERROR(EOVERFLOW);
else
ret = avio_seek(sys->context, position, SEEK_SET);
if (ret < 0) {
msg_Err(access, "Seek to %"PRIu64" failed: %s", position,
vlc_strerror_c(AVUNERROR(ret)));
if (sys->size < 0 || position != (uint64_t)sys->size)
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
static int OutSeek(sout_access_out_t *p_access, uint64_t i_pos)
{
sout_access_out_sys_t *sys = p_access->p_sys;
if (avio_seek(sys->context, i_pos, SEEK_SET) < 0)
return VLC_EGENERIC;
return VLC_SUCCESS;
}
static int OutControl(sout_access_out_t *p_access, int i_query, va_list args)
{
sout_access_out_sys_t *p_sys = p_access->p_sys;
VLC_UNUSED(p_sys);
switch (i_query) {
case ACCESS_OUT_CONTROLS_PACE: {
bool *pb = va_arg(args, bool *);
//*pb = strcmp(p_access->psz_name, "stream");
*pb = false;
break;
}
default:
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
static int Control(stream_t *access, int query, va_list args)
{
access_sys_t *sys = access->p_sys;
bool *b;
switch (query) {
case STREAM_CAN_SEEK:
case STREAM_CAN_FASTSEEK: /* FIXME how to do that ? */
b = va_arg(args, bool *);
*b = sys->context->seekable;
return VLC_SUCCESS;
case STREAM_CAN_PAUSE:
b = va_arg(args, bool *);
*b = sys->context->read_pause != NULL;
return VLC_SUCCESS;
case STREAM_CAN_CONTROL_PACE:
b = va_arg(args, bool *);
*b = true; /* FIXME */
return VLC_SUCCESS;
case STREAM_GET_SIZE:
if (sys->size < 0)
return VLC_EGENERIC;
*va_arg(args, uint64_t *) = sys->size;
return VLC_SUCCESS;
case STREAM_GET_PTS_DELAY:
*va_arg(args, vlc_tick_t *) =
VLC_TICK_FROM_MS(var_InheritInteger(access, "network-caching"));
return VLC_SUCCESS;
case STREAM_SET_PAUSE_STATE: {
bool is_paused = va_arg(args, int);
if (avio_pause(sys->context, is_paused)< 0)
return VLC_EGENERIC;
return VLC_SUCCESS;
}
default:
return VLC_EGENERIC;
}
}