2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2006> Edward Hervey <bilboed@bilboed.com>
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.
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.
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.
27 #include <libavformat/avformat.h>
32 #include "gstavprotocol.h"
34 typedef struct _GstProtocolInfo GstProtocolInfo;
36 struct _GstProtocolInfo
42 gint set_streamheader;
46 gst_ffmpegdata_peek (void *priv_data, unsigned char *buf, int size)
48 GstProtocolInfo *info;
49 GstBuffer *inbuf = NULL;
53 info = (GstProtocolInfo *) priv_data;
55 GST_DEBUG ("Pulling %d bytes at position %" G_GUINT64_FORMAT, size,
58 ret = gst_pad_pull_range (info->pad, info->offset, (guint) size, &inbuf);
62 total = (gint) gst_buffer_get_size (inbuf);
63 gst_buffer_extract (inbuf, 0, buf, total);
64 gst_buffer_unref (inbuf);
69 case GST_FLOW_FLUSHING:
78 GST_DEBUG ("Got %d (%s) return result %d", ret, gst_flow_get_name (ret),
85 gst_ffmpegdata_read (void *priv_data, unsigned char *buf, int size)
88 GstProtocolInfo *info;
90 info = (GstProtocolInfo *) priv_data;
92 GST_DEBUG ("Reading %d bytes of data at position %" G_GUINT64_FORMAT, size,
95 res = gst_ffmpegdata_peek (priv_data, buf, size);
99 GST_DEBUG ("Returning %d bytes", res);
105 gst_ffmpegdata_write (void *priv_data, uint8_t * buf, int size)
107 GstProtocolInfo *info;
110 GST_DEBUG ("Writing %d bytes", size);
111 info = (GstProtocolInfo *) priv_data;
113 /* create buffer and push data further */
114 outbuf = gst_buffer_new_and_alloc (size);
116 gst_buffer_fill (outbuf, 0, buf, size);
118 if (gst_pad_push (info->pad, outbuf) != GST_FLOW_OK)
121 info->offset += size;
126 gst_ffmpegdata_seek (void *priv_data, int64_t pos, int whence)
128 GstProtocolInfo *info;
129 guint64 newpos = 0, oldpos;
131 GST_DEBUG ("Seeking to %" G_GINT64_FORMAT ", whence=%d",
132 (gint64) pos, whence);
134 info = (GstProtocolInfo *) priv_data;
136 /* TODO : if we are push-based, we need to return sensible info */
138 if (GST_PAD_IS_SINK (info->pad)) {
142 newpos = (guint64) pos;
145 newpos = info->offset + pos;
149 /* ffmpeg wants to know the current end position in bytes ! */
153 GST_DEBUG ("Seek end");
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;
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)) {
171 oldpos = info->offset;
177 info->offset = (guint64) pos;
186 newpos = info->offset;
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));
195 g_assert_not_reached ();
198 GST_DEBUG ("Now at offset %" G_GUINT64_FORMAT " (returning %" G_GUINT64_FORMAT
199 ")", info->offset, newpos);
204 gst_ffmpegdata_close (AVIOContext * h)
206 GstProtocolInfo *info;
211 info = (GstProtocolInfo *) h->opaque;
215 GST_LOG ("Closing file");
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 ());
226 av_freep (&h->buffer);
233 gst_ffmpegdata_open (GstPad * pad, int flags, AVIOContext ** context)
235 GstProtocolInfo *info;
236 static const int buffer_size = 4096;
237 unsigned char *buffer = NULL;
239 info = g_new0 (GstProtocolInfo, 1);
241 info->set_streamheader = flags & GST_FFMPEG_URL_STREAMHEADER;
242 flags &= ~GST_FFMPEG_URL_STREAMHEADER;
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");
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);
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);
263 buffer = av_malloc (buffer_size);
264 if (buffer == NULL) {
265 GST_WARNING ("Failed to allocate buffer");
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");
279 (*context)->seekable = AVIO_SEEKABLE_NORMAL;
280 if (!(flags & AVIO_FLAG_WRITE)) {
281 (*context)->buf_ptr = (*context)->buf_end;
282 (*context)->write_flag = 0;
288 /* specialized protocol for cross-thread pushing,
289 * based on ffmpeg's pipe protocol */
292 gst_ffmpeg_pipe_read (void *priv_data, uint8_t * buf, int size)
294 GstFFMpegPipe *ffpipe;
297 ffpipe = (GstFFMpegPipe *) priv_data;
299 GST_LOG ("requested size %d", size);
301 GST_FFMPEG_PIPE_MUTEX_LOCK (ffpipe);
303 GST_LOG ("requested size %d", size);
305 while ((available = gst_adapter_available (ffpipe->adapter)) < size
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);
313 size = MIN (available, 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));
322 GST_FFMPEG_PIPE_MUTEX_UNLOCK (ffpipe);
328 gst_ffmpeg_pipe_close (AVIOContext * h)
330 GST_LOG ("Closing pipe");
336 av_freep (&h->buffer);
343 gst_ffmpeg_pipe_open (GstFFMpegPipe * ffpipe, int flags, AVIOContext ** context)
345 static const int buffer_size = 4096;
346 unsigned char *buffer = NULL;
349 g_return_val_if_fail (GST_IS_ADAPTER (ffpipe->adapter), -EINVAL);
351 buffer = av_malloc (buffer_size);
352 if (buffer == NULL) {
353 GST_WARNING ("Failed to allocate buffer");
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");
365 (*context)->seekable = 0;
366 (*context)->buf_ptr = (*context)->buf_end;