av: Port to new avio protocol handler

This commit is contained in:
Sebastian Dröge 2012-12-04 17:47:20 +01:00
parent 5f03a7d869
commit 06ed3e4060
3 changed files with 69 additions and 81 deletions

View file

@ -30,6 +30,7 @@
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavformat/url.h>
#include "gstav.h" #include "gstav.h"
#include "gstavutils.h" #include "gstavutils.h"
@ -149,8 +150,8 @@ plugin_init (GstPlugin * plugin)
gst_ffmpegaudioresample_register (plugin); gst_ffmpegaudioresample_register (plugin);
#endif #endif
av_register_protocol2 (&gstreamer_protocol, sizeof (URLProtocol)); ffurl_register_protocol (&gstreamer_protocol, sizeof (URLProtocol));
av_register_protocol2 (&gstpipe_protocol, sizeof (URLProtocol)); ffurl_register_protocol (&gstpipe_protocol, sizeof (URLProtocol));
/* Now we can return the pointer to the newly created Plugin object. */ /* Now we can return the pointer to the newly created Plugin object. */
return TRUE; return TRUE;

View file

@ -26,6 +26,7 @@
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavformat/url.h>
#include <gst/gst.h> #include <gst/gst.h>

View file

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavformat/url.h>
#include <gst/gst.h> #include <gst/gst.h>
@ -57,7 +58,7 @@ gst_ffmpegdata_open (URLContext * h, const char *filename, int flags)
h->flags &= ~GST_FFMPEG_URL_STREAMHEADER; h->flags &= ~GST_FFMPEG_URL_STREAMHEADER;
/* we don't support R/W together */ /* we don't support R/W together */
if (flags != URL_RDONLY && flags != URL_WRONLY) { if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
GST_WARNING ("Only read-only or write-only are supported"); GST_WARNING ("Only read-only or write-only are supported");
return -EINVAL; return -EINVAL;
} }
@ -70,13 +71,11 @@ gst_ffmpegdata_open (URLContext * h, const char *filename, int flags)
/* make sure we're a pad and that we're of the right type */ /* make sure we're a pad and that we're of the right type */
g_return_val_if_fail (GST_IS_PAD (pad), -EINVAL); g_return_val_if_fail (GST_IS_PAD (pad), -EINVAL);
switch (flags) { if ((flags & AVIO_FLAG_READ)) {
case URL_RDONLY: g_return_val_if_fail (GST_PAD_IS_SINK (pad), -EINVAL);
g_return_val_if_fail (GST_PAD_IS_SINK (pad), -EINVAL); }
break; if ((flags & AVIO_FLAG_WRITE)) {
case URL_WRONLY: g_return_val_if_fail (GST_PAD_IS_SRC (pad), -EINVAL);
g_return_val_if_fail (GST_PAD_IS_SRC (pad), -EINVAL);
break;
} }
info->eos = FALSE; info->eos = FALSE;
@ -98,7 +97,7 @@ gst_ffmpegdata_peek (URLContext * h, unsigned char *buf, int size)
GstFlowReturn ret; GstFlowReturn ret;
int total = 0; int total = 0;
g_return_val_if_fail (h->flags == URL_RDONLY, AVERROR (EIO)); g_return_val_if_fail ((h->flags & AVIO_FLAG_READ), AVERROR (EIO));
info = (GstProtocolInfo *) h->priv_data; info = (GstProtocolInfo *) h->priv_data;
GST_DEBUG ("Pulling %d bytes at position %" G_GUINT64_FORMAT, size, GST_DEBUG ("Pulling %d bytes at position %" G_GUINT64_FORMAT, size,
@ -159,7 +158,7 @@ gst_ffmpegdata_write (URLContext * h, const unsigned char *buf, int size)
GST_DEBUG ("Writing %d bytes", size); GST_DEBUG ("Writing %d bytes", size);
info = (GstProtocolInfo *) h->priv_data; info = (GstProtocolInfo *) h->priv_data;
g_return_val_if_fail (h->flags != URL_RDONLY, -EIO); g_return_val_if_fail ((h->flags & AVIO_FLAG_WRITE), -EIO);
/* create buffer and push data further */ /* create buffer and push data further */
outbuf = gst_buffer_new_and_alloc (size); outbuf = gst_buffer_new_and_alloc (size);
@ -185,73 +184,64 @@ gst_ffmpegdata_seek (URLContext * h, int64_t pos, int whence)
info = (GstProtocolInfo *) h->priv_data; info = (GstProtocolInfo *) h->priv_data;
/* TODO : if we are push-based, we need to return sensible info */ /* TODO : if we are push-based, we need to return sensible info */
if ((h->flags & AVIO_FLAG_READ)) {
/* sinkpad */
switch (whence) {
case SEEK_SET:
newpos = (guint64) pos;
break;
case SEEK_CUR:
newpos = info->offset + pos;
break;
case SEEK_END:
case AVSEEK_SIZE:
/* ffmpeg wants to know the current end position in bytes ! */
{
gint64 duration;
switch (h->flags) { GST_DEBUG ("Seek end");
case URL_RDONLY:
{
/* sinkpad */
switch (whence) {
case SEEK_SET:
newpos = (guint64) pos;
break;
case SEEK_CUR:
newpos = info->offset + pos;
break;
case SEEK_END:
case AVSEEK_SIZE:
/* ffmpeg wants to know the current end position in bytes ! */
{
gint64 duration;
GST_DEBUG ("Seek end"); if (gst_pad_is_linked (info->pad))
if (gst_pad_query_duration (GST_PAD_PEER (info->pad),
if (gst_pad_is_linked (info->pad)) GST_FORMAT_BYTES, &duration))
if (gst_pad_query_duration (GST_PAD_PEER (info->pad), newpos = ((guint64) duration) + pos;
GST_FORMAT_BYTES, &duration))
newpos = ((guint64) duration) + pos;
}
break;
default:
g_assert (0);
break;
} }
/* FIXME : implement case for push-based behaviour */ break;
if (whence != AVSEEK_SIZE) default:
info->offset = newpos; g_assert (0);
break;
} }
break; /* FIXME : implement case for push-based behaviour */
case URL_WRONLY: if (whence != AVSEEK_SIZE)
{ info->offset = newpos;
GstSegment segment; }
oldpos = info->offset; if ((h->flags & AVIO_FLAG_WRITE)) {
GstSegment segment;
/* srcpad */ oldpos = info->offset;
switch (whence) {
case SEEK_SET: /* srcpad */
{ switch (whence) {
info->offset = (guint64) pos; case SEEK_SET:
break; {
} info->offset = (guint64) pos;
case SEEK_CUR: break;
info->offset += pos;
break;
default:
break;
} }
newpos = info->offset; case SEEK_CUR:
info->offset += pos;
if (newpos != oldpos) { break;
gst_segment_init (&segment, GST_FORMAT_BYTES); default:
segment.start = newpos; break;
segment.time = newpos; }
gst_pad_push_event (info->pad, gst_event_new_segment (&segment)); newpos = info->offset;
}
break; if (newpos != oldpos) {
gst_segment_init (&segment, GST_FORMAT_BYTES);
segment.start = newpos;
segment.time = newpos;
gst_pad_push_event (info->pad, gst_event_new_segment (&segment));
} }
default:
g_assert (0);
break;
} }
GST_DEBUG ("Now at offset %" G_GUINT64_FORMAT " (returning %" G_GUINT64_FORMAT GST_DEBUG ("Now at offset %" G_GUINT64_FORMAT " (returning %" G_GUINT64_FORMAT
@ -270,15 +260,9 @@ gst_ffmpegdata_close (URLContext * h)
GST_LOG ("Closing file"); GST_LOG ("Closing file");
switch (h->flags) { if ((h->flags & AVIO_FLAG_WRITE)) {
case URL_WRONLY: /* send EOS - that closes down the stream */
{ gst_pad_push_event (info->pad, gst_event_new_eos ());
/* send EOS - that closes down the stream */
gst_pad_push_event (info->pad, gst_event_new_eos ());
break;
}
default:
break;
} }
/* clean up data */ /* clean up data */
@ -292,6 +276,7 @@ gst_ffmpegdata_close (URLContext * h)
URLProtocol gstreamer_protocol = { URLProtocol gstreamer_protocol = {
/*.name = */ "gstreamer", /*.name = */ "gstreamer",
/*.url_open = */ gst_ffmpegdata_open, /*.url_open = */ gst_ffmpegdata_open,
/*.url_open2 = */ NULL,
/*.url_read = */ gst_ffmpegdata_read, /*.url_read = */ gst_ffmpegdata_read,
/*.url_write = */ gst_ffmpegdata_write, /*.url_write = */ gst_ffmpegdata_write,
/*.url_seek = */ gst_ffmpegdata_seek, /*.url_seek = */ gst_ffmpegdata_seek,
@ -310,7 +295,7 @@ gst_ffmpeg_pipe_open (URLContext * h, const char *filename, int flags)
GST_LOG ("Opening %s", filename); GST_LOG ("Opening %s", filename);
/* we don't support W together */ /* we don't support W together */
if (flags != URL_RDONLY) { if ((flags & AVIO_FLAG_WRITE)) {
GST_WARNING ("Only read-only is supported"); GST_WARNING ("Only read-only is supported");
return -EINVAL; return -EINVAL;
} }
@ -379,6 +364,7 @@ gst_ffmpeg_pipe_close (URLContext * h)
URLProtocol gstpipe_protocol = { URLProtocol gstpipe_protocol = {
"gstpipe", "gstpipe",
gst_ffmpeg_pipe_open, gst_ffmpeg_pipe_open,
NULL,
gst_ffmpeg_pipe_read, gst_ffmpeg_pipe_read,
NULL, NULL,
NULL, NULL,