1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2006> Edward Hervey <bilboed@bilboed.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25 #include <errno.h>
26
27 #include <libavformat/avformat.h>
28
29 #include <gst/gst.h>
30
31 #include "gstav.h"
32 #include "gstavprotocol.h"
33
34 typedef struct _GstProtocolInfo GstProtocolInfo;
35
36 struct _GstProtocolInfo
37 {
38 GstPad *pad;
39
40 guint64 offset;
41 gboolean eos;
42 gint set_streamheader;
43 };
44
45 static int
gst_ffmpegdata_peek(void * priv_data,unsigned char * buf,int size)46 gst_ffmpegdata_peek (void *priv_data, unsigned char *buf, int size)
47 {
48 GstProtocolInfo *info;
49 GstBuffer *inbuf = NULL;
50 GstFlowReturn ret;
51 int total = 0;
52
53 info = (GstProtocolInfo *) priv_data;
54
55 GST_DEBUG ("Pulling %d bytes at position %" G_GUINT64_FORMAT, size,
56 info->offset);
57
58 ret = gst_pad_pull_range (info->pad, info->offset, (guint) size, &inbuf);
59
60 switch (ret) {
61 case GST_FLOW_OK:
62 total = (gint) gst_buffer_get_size (inbuf);
63 gst_buffer_extract (inbuf, 0, buf, total);
64 gst_buffer_unref (inbuf);
65 break;
66 case GST_FLOW_EOS:
67 total = 0;
68 break;
69 case GST_FLOW_FLUSHING:
70 total = -1;
71 break;
72 default:
73 case GST_FLOW_ERROR:
74 total = -2;
75 break;
76 }
77
78 GST_DEBUG ("Got %d (%s) return result %d", ret, gst_flow_get_name (ret),
79 total);
80
81 return total;
82 }
83
84 static int
gst_ffmpegdata_read(void * priv_data,unsigned char * buf,int size)85 gst_ffmpegdata_read (void *priv_data, unsigned char *buf, int size)
86 {
87 gint res;
88 GstProtocolInfo *info;
89
90 info = (GstProtocolInfo *) priv_data;
91
92 GST_DEBUG ("Reading %d bytes of data at position %" G_GUINT64_FORMAT, size,
93 info->offset);
94
95 res = gst_ffmpegdata_peek (priv_data, buf, size);
96 if (res >= 0)
97 info->offset += res;
98
99 GST_DEBUG ("Returning %d bytes", res);
100
101 return res;
102 }
103
104 static int
gst_ffmpegdata_write(void * priv_data,uint8_t * buf,int size)105 gst_ffmpegdata_write (void *priv_data, uint8_t * buf, int size)
106 {
107 GstProtocolInfo *info;
108 GstBuffer *outbuf;
109
110 GST_DEBUG ("Writing %d bytes", size);
111 info = (GstProtocolInfo *) priv_data;
112
113 /* create buffer and push data further */
114 outbuf = gst_buffer_new_and_alloc (size);
115
116 gst_buffer_fill (outbuf, 0, buf, size);
117
118 if (gst_pad_push (info->pad, outbuf) != GST_FLOW_OK)
119 return 0;
120
121 info->offset += size;
122 return size;
123 }
124
125 static int64_t
gst_ffmpegdata_seek(void * priv_data,int64_t pos,int whence)126 gst_ffmpegdata_seek (void *priv_data, int64_t pos, int whence)
127 {
128 GstProtocolInfo *info;
129 guint64 newpos = 0, oldpos;
130
131 GST_DEBUG ("Seeking to %" G_GINT64_FORMAT ", whence=%d",
132 (gint64) pos, whence);
133
134 info = (GstProtocolInfo *) priv_data;
135
136 /* TODO : if we are push-based, we need to return sensible info */
137
138 if (GST_PAD_IS_SINK (info->pad)) {
139 /* sinkpad */
140 switch (whence) {
141 case SEEK_SET:
142 newpos = (guint64) pos;
143 break;
144 case SEEK_CUR:
145 newpos = info->offset + pos;
146 break;
147 case SEEK_END:
148 case AVSEEK_SIZE:
149 /* ffmpeg wants to know the current end position in bytes ! */
150 {
151 gint64 duration;
152
153 GST_DEBUG ("Seek end");
154
155 if (gst_pad_is_linked (info->pad))
156 if (gst_pad_query_duration (GST_PAD_PEER (info->pad),
157 GST_FORMAT_BYTES, &duration))
158 newpos = ((guint64) duration) + pos;
159 }
160 break;
161 default:
162 g_assert (0);
163 break;
164 }
165 /* FIXME : implement case for push-based behaviour */
166 if (whence != AVSEEK_SIZE)
167 info->offset = newpos;
168 } else if (GST_PAD_IS_SRC (info->pad)) {
169 GstSegment segment;
170
171 oldpos = info->offset;
172
173 /* srcpad */
174 switch (whence) {
175 case SEEK_SET:
176 {
177 info->offset = (guint64) pos;
178 break;
179 }
180 case SEEK_CUR:
181 info->offset += pos;
182 break;
183 default:
184 break;
185 }
186 newpos = info->offset;
187
188 if (newpos != oldpos) {
189 gst_segment_init (&segment, GST_FORMAT_BYTES);
190 segment.start = newpos;
191 segment.time = newpos;
192 gst_pad_push_event (info->pad, gst_event_new_segment (&segment));
193 }
194 } else {
195 g_assert_not_reached ();
196 }
197
198 GST_DEBUG ("Now at offset %" G_GUINT64_FORMAT " (returning %" G_GUINT64_FORMAT
199 ")", info->offset, newpos);
200 return newpos;
201 }
202
203 int
gst_ffmpegdata_close(AVIOContext * h)204 gst_ffmpegdata_close (AVIOContext * h)
205 {
206 GstProtocolInfo *info;
207
208 if (h == NULL)
209 return 0;
210
211 info = (GstProtocolInfo *) h->opaque;
212 if (info == NULL)
213 return 0;
214
215 GST_LOG ("Closing file");
216
217 if (GST_PAD_IS_SRC (info->pad)) {
218 /* send EOS - that closes down the stream */
219 gst_pad_push_event (info->pad, gst_event_new_eos ());
220 }
221
222 /* clean up data */
223 g_free (info);
224 h->opaque = NULL;
225
226 av_freep (&h->buffer);
227 av_free (h);
228
229 return 0;
230 }
231
232 int
gst_ffmpegdata_open(GstPad * pad,int flags,AVIOContext ** context)233 gst_ffmpegdata_open (GstPad * pad, int flags, AVIOContext ** context)
234 {
235 GstProtocolInfo *info;
236 static const int buffer_size = 4096;
237 unsigned char *buffer = NULL;
238
239 info = g_new0 (GstProtocolInfo, 1);
240
241 info->set_streamheader = flags & GST_FFMPEG_URL_STREAMHEADER;
242 flags &= ~GST_FFMPEG_URL_STREAMHEADER;
243
244 /* we don't support R/W together */
245 if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
246 GST_WARNING ("Only read-only or write-only are supported");
247 g_free (info);
248 return -EINVAL;
249 }
250
251 /* make sure we're a pad and that we're of the right type */
252 g_return_val_if_fail (GST_IS_PAD (pad), -EINVAL);
253
254 if ((flags & AVIO_FLAG_READ))
255 g_return_val_if_fail (GST_PAD_IS_SINK (pad), -EINVAL);
256 if ((flags & AVIO_FLAG_WRITE))
257 g_return_val_if_fail (GST_PAD_IS_SRC (pad), -EINVAL);
258
259 info->eos = FALSE;
260 info->pad = pad;
261 info->offset = 0;
262
263 buffer = av_malloc (buffer_size);
264 if (buffer == NULL) {
265 GST_WARNING ("Failed to allocate buffer");
266 g_free (info);
267 return -ENOMEM;
268 }
269
270 *context =
271 avio_alloc_context (buffer, buffer_size, flags, (void *) info,
272 gst_ffmpegdata_read, gst_ffmpegdata_write, gst_ffmpegdata_seek);
273 if (*context == NULL) {
274 GST_WARNING ("Failed to allocate memory");
275 g_free (info);
276 av_free (buffer);
277 return -ENOMEM;
278 }
279 (*context)->seekable = AVIO_SEEKABLE_NORMAL;
280 if (!(flags & AVIO_FLAG_WRITE)) {
281 (*context)->buf_ptr = (*context)->buf_end;
282 (*context)->write_flag = 0;
283 }
284
285 return 0;
286 }
287
288 /* specialized protocol for cross-thread pushing,
289 * based on ffmpeg's pipe protocol */
290
291 static int
gst_ffmpeg_pipe_read(void * priv_data,uint8_t * buf,int size)292 gst_ffmpeg_pipe_read (void *priv_data, uint8_t * buf, int size)
293 {
294 GstFFMpegPipe *ffpipe;
295 guint available;
296
297 ffpipe = (GstFFMpegPipe *) priv_data;
298
299 GST_LOG ("requested size %d", size);
300
301 GST_FFMPEG_PIPE_MUTEX_LOCK (ffpipe);
302
303 GST_LOG ("requested size %d", size);
304
305 while ((available = gst_adapter_available (ffpipe->adapter)) < size
306 && !ffpipe->eos) {
307 GST_DEBUG ("Available:%d, requested:%d", available, size);
308 ffpipe->needed = size;
309 GST_FFMPEG_PIPE_SIGNAL (ffpipe);
310 GST_FFMPEG_PIPE_WAIT (ffpipe);
311 }
312
313 size = MIN (available, size);
314 if (size) {
315 GST_LOG ("Getting %d bytes", size);
316 gst_adapter_copy (ffpipe->adapter, buf, 0, size);
317 gst_adapter_flush (ffpipe->adapter, size);
318 GST_LOG ("%" G_GSIZE_FORMAT " bytes left in adapter",
319 gst_adapter_available (ffpipe->adapter));
320 ffpipe->needed = 0;
321 }
322 GST_FFMPEG_PIPE_MUTEX_UNLOCK (ffpipe);
323
324 return size;
325 }
326
327 int
gst_ffmpeg_pipe_close(AVIOContext * h)328 gst_ffmpeg_pipe_close (AVIOContext * h)
329 {
330 GST_LOG ("Closing pipe");
331
332 if (h == NULL)
333 return 0;
334
335 h->opaque = NULL;
336 av_freep (&h->buffer);
337 av_free (h);
338
339 return 0;
340 }
341
342 int
gst_ffmpeg_pipe_open(GstFFMpegPipe * ffpipe,int flags,AVIOContext ** context)343 gst_ffmpeg_pipe_open (GstFFMpegPipe * ffpipe, int flags, AVIOContext ** context)
344 {
345 static const int buffer_size = 4096;
346 unsigned char *buffer = NULL;
347
348 /* sanity check */
349 g_return_val_if_fail (GST_IS_ADAPTER (ffpipe->adapter), -EINVAL);
350
351 buffer = av_malloc (buffer_size);
352 if (buffer == NULL) {
353 GST_WARNING ("Failed to allocate buffer");
354 return -ENOMEM;
355 }
356
357 *context =
358 avio_alloc_context (buffer, buffer_size, 0, (void *) ffpipe,
359 gst_ffmpeg_pipe_read, NULL, NULL);
360 if (*context == NULL) {
361 GST_WARNING ("Failed to allocate memory");
362 av_free (buffer);
363 return -ENOMEM;
364 }
365 (*context)->seekable = 0;
366 (*context)->buf_ptr = (*context)->buf_end;
367
368 return 0;
369 }
370