2012-04-18 22:33:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsError.h"
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2012-11-19 15:11:21 +00:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2012-04-18 22:33:13 +00:00
|
|
|
#include "MediaResource.h"
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "GStreamerReader.h"
|
2012-04-18 22:33:13 +00:00
|
|
|
#include "VideoUtils.h"
|
2013-03-02 19:14:44 +00:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2012-04-18 22:33:13 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace layers;
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
|
|
//#define SEEK_LOGGING
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 19:46:40 +00:00
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2012-04-18 22:33:13 +00:00
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
extern bool
|
|
|
|
IsYV12Format(const VideoData::YCbCrBuffer::Plane& aYPlane,
|
|
|
|
const VideoData::YCbCrBuffer::Plane& aCbPlane,
|
|
|
|
const VideoData::YCbCrBuffer::Plane& aCrPlane);
|
|
|
|
|
2012-04-18 22:33:13 +00:00
|
|
|
static const int MAX_CHANNELS = 4;
|
|
|
|
// Let the demuxer work in pull mode for short files
|
|
|
|
static const int SHORT_FILE_SIZE = 1024 * 1024;
|
|
|
|
// The default resource->Read() size when working in push mode
|
|
|
|
static const int DEFAULT_SOURCE_READ_SIZE = 50 * 1024;
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
G_DEFINE_BOXED_TYPE(BufferData, buffer_data, BufferData::Copy, BufferData::Free);
|
|
|
|
|
2012-04-18 22:33:13 +00:00
|
|
|
typedef enum {
|
|
|
|
GST_PLAY_FLAG_VIDEO = (1 << 0),
|
|
|
|
GST_PLAY_FLAG_AUDIO = (1 << 1),
|
|
|
|
GST_PLAY_FLAG_TEXT = (1 << 2),
|
|
|
|
GST_PLAY_FLAG_VIS = (1 << 3),
|
|
|
|
GST_PLAY_FLAG_SOFT_VOLUME = (1 << 4),
|
|
|
|
GST_PLAY_FLAG_NATIVE_AUDIO = (1 << 5),
|
|
|
|
GST_PLAY_FLAG_NATIVE_VIDEO = (1 << 6),
|
|
|
|
GST_PLAY_FLAG_DOWNLOAD = (1 << 7),
|
|
|
|
GST_PLAY_FLAG_BUFFERING = (1 << 8),
|
|
|
|
GST_PLAY_FLAG_DEINTERLACE = (1 << 9),
|
|
|
|
GST_PLAY_FLAG_SOFT_COLORBALANCE = (1 << 10)
|
|
|
|
} PlayFlags;
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
GStreamerReader::GStreamerReader(AbstractMediaDecoder* aDecoder)
|
2012-11-14 19:46:40 +00:00
|
|
|
: MediaDecoderReader(aDecoder),
|
2013-03-13 20:11:15 +00:00
|
|
|
mPlayBin(nullptr),
|
|
|
|
mBus(nullptr),
|
|
|
|
mSource(nullptr),
|
|
|
|
mVideoSink(nullptr),
|
|
|
|
mVideoAppSink(nullptr),
|
|
|
|
mAudioSink(nullptr),
|
|
|
|
mAudioAppSink(nullptr),
|
2012-04-18 22:33:13 +00:00
|
|
|
mFormat(GST_VIDEO_FORMAT_UNKNOWN),
|
|
|
|
mVideoSinkBufferCount(0),
|
|
|
|
mAudioSinkBufferCount(0),
|
|
|
|
mGstThreadsMonitor("media.gst.threads"),
|
|
|
|
mReachedEos(false),
|
|
|
|
mByteOffset(0),
|
|
|
|
mLastReportedByteOffset(0),
|
|
|
|
fpsNum(0),
|
|
|
|
fpsDen(0)
|
|
|
|
{
|
2012-11-14 19:46:40 +00:00
|
|
|
MOZ_COUNT_CTOR(GStreamerReader);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
mSrcCallbacks.need_data = GStreamerReader::NeedDataCb;
|
|
|
|
mSrcCallbacks.enough_data = GStreamerReader::EnoughDataCb;
|
|
|
|
mSrcCallbacks.seek_data = GStreamerReader::SeekDataCb;
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
mSinkCallbacks.eos = GStreamerReader::EosCb;
|
|
|
|
mSinkCallbacks.new_preroll = GStreamerReader::NewPrerollCb;
|
|
|
|
mSinkCallbacks.new_buffer = GStreamerReader::NewBufferCb;
|
2013-03-13 20:11:15 +00:00
|
|
|
mSinkCallbacks.new_buffer_list = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
gst_segment_init(&mVideoSegment, GST_FORMAT_UNDEFINED);
|
|
|
|
gst_segment_init(&mAudioSegment, GST_FORMAT_UNDEFINED);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
GStreamerReader::~GStreamerReader()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2012-11-14 19:46:40 +00:00
|
|
|
MOZ_COUNT_DTOR(GStreamerReader);
|
2012-04-18 22:33:13 +00:00
|
|
|
ResetDecode();
|
|
|
|
|
|
|
|
if (mPlayBin) {
|
|
|
|
gst_app_src_end_of_stream(mSource);
|
2013-02-13 18:37:49 +00:00
|
|
|
if (mSource)
|
|
|
|
gst_object_unref(mSource);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_element_set_state(mPlayBin, GST_STATE_NULL);
|
|
|
|
gst_object_unref(mPlayBin);
|
2013-03-13 20:11:15 +00:00
|
|
|
mPlayBin = nullptr;
|
|
|
|
mVideoSink = nullptr;
|
|
|
|
mVideoAppSink = nullptr;
|
|
|
|
mAudioSink = nullptr;
|
|
|
|
mAudioAppSink = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_object_unref(mBus);
|
2013-03-13 20:11:15 +00:00
|
|
|
mBus = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult GStreamerReader::Init(MediaDecoderReader* aCloneDonor)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GError* error = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
if (!gst_init_check(0, 0, &error)) {
|
|
|
|
LOG(PR_LOG_ERROR, ("gst initialization failed: %s", error->message));
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
mPlayBin = gst_element_factory_make("playbin2", nullptr);
|
|
|
|
if (!mPlayBin) {
|
2012-04-18 22:33:13 +00:00
|
|
|
LOG(PR_LOG_ERROR, ("couldn't create playbin2"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-03-13 20:11:15 +00:00
|
|
|
g_object_set(mPlayBin, "buffer-size", 0, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
mBus = gst_pipeline_get_bus(GST_PIPELINE(mPlayBin));
|
|
|
|
|
|
|
|
mVideoSink = gst_parse_bin_from_description("capsfilter name=filter ! "
|
|
|
|
"appsink name=videosink sync=true max-buffers=1 "
|
|
|
|
"caps=video/x-raw-yuv,format=(fourcc)I420"
|
2013-03-13 20:11:15 +00:00
|
|
|
, TRUE, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
mVideoAppSink = GST_APP_SINK(gst_bin_get_by_name(GST_BIN(mVideoSink),
|
|
|
|
"videosink"));
|
|
|
|
gst_app_sink_set_callbacks(mVideoAppSink, &mSinkCallbacks,
|
2013-03-13 20:11:15 +00:00
|
|
|
(gpointer) this, nullptr);
|
|
|
|
GstPad* sinkpad = gst_element_get_pad(GST_ELEMENT(mVideoAppSink), "sink");
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_pad_add_event_probe(sinkpad,
|
2012-11-14 19:46:40 +00:00
|
|
|
G_CALLBACK(&GStreamerReader::EventProbeCb), this);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_object_unref(sinkpad);
|
2013-03-13 20:11:15 +00:00
|
|
|
#if GST_VERSION_MICRO >= 36
|
|
|
|
gst_pad_set_bufferalloc_function(sinkpad, GStreamerReader::AllocateVideoBufferCb);
|
|
|
|
#endif
|
|
|
|
gst_pad_set_element_private(sinkpad, this);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
mAudioSink = gst_parse_bin_from_description("capsfilter name=filter ! "
|
2012-07-19 02:46:11 +00:00
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
2012-04-18 22:33:13 +00:00
|
|
|
"appsink name=audiosink sync=true caps=audio/x-raw-float,"
|
2012-07-30 15:57:15 +00:00
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
2013-03-13 20:11:15 +00:00
|
|
|
"channels={1,2},width=32,endianness=1234", TRUE, nullptr);
|
2012-07-30 15:57:15 +00:00
|
|
|
#else
|
2013-03-13 20:11:15 +00:00
|
|
|
"channels={1,2},width=32,endianness=4321", TRUE, nullptr);
|
2012-07-30 15:57:15 +00:00
|
|
|
#endif
|
2012-07-19 02:46:11 +00:00
|
|
|
#else
|
|
|
|
"appsink name=audiosink sync=true caps=audio/x-raw-int,"
|
2012-07-30 15:57:15 +00:00
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
2013-03-13 20:11:15 +00:00
|
|
|
"channels={1,2},width=16,endianness=1234", TRUE, nullptr);
|
2012-07-30 15:57:15 +00:00
|
|
|
#else
|
2013-03-13 20:11:15 +00:00
|
|
|
"channels={1,2},width=16,endianness=4321", TRUE, nullptr);
|
2012-07-30 15:57:15 +00:00
|
|
|
#endif
|
2012-07-19 02:46:11 +00:00
|
|
|
#endif
|
2012-04-18 22:33:13 +00:00
|
|
|
mAudioAppSink = GST_APP_SINK(gst_bin_get_by_name(GST_BIN(mAudioSink),
|
2013-03-13 20:11:15 +00:00
|
|
|
"audiosink"));
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_app_sink_set_callbacks(mAudioAppSink, &mSinkCallbacks,
|
2013-03-13 20:11:15 +00:00
|
|
|
(gpointer) this, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
sinkpad = gst_element_get_pad(GST_ELEMENT(mAudioAppSink), "sink");
|
|
|
|
gst_pad_add_event_probe(sinkpad,
|
2013-03-13 20:11:15 +00:00
|
|
|
G_CALLBACK(&GStreamerReader::EventProbeCb), this);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_object_unref(sinkpad);
|
|
|
|
|
|
|
|
g_object_set(mPlayBin, "uri", "appsrc://",
|
2013-03-13 20:11:15 +00:00
|
|
|
"video-sink", mVideoSink,
|
|
|
|
"audio-sink", mAudioSink,
|
|
|
|
nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
g_object_connect(mPlayBin, "signal::source-setup",
|
|
|
|
GStreamerReader::PlayBinSourceSetupCb, this, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::PlayBinSourceSetupCb(GstElement* aPlayBin,
|
|
|
|
GParamSpec* pspec,
|
|
|
|
gpointer aUserData)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-02-13 18:37:49 +00:00
|
|
|
GstElement *source;
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2013-02-13 18:37:49 +00:00
|
|
|
|
|
|
|
g_object_get(aPlayBin, "source", &source, NULL);
|
|
|
|
reader->PlayBinSourceSetup(GST_APP_SRC(source));
|
2012-04-18 22:33:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::PlayBinSourceSetup(GstAppSrc* aSource)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
mSource = GST_APP_SRC(aSource);
|
2013-03-13 20:11:15 +00:00
|
|
|
gst_app_src_set_callbacks(mSource, &mSrcCallbacks, (gpointer) this, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2013-03-13 20:09:20 +00:00
|
|
|
|
|
|
|
/* do a short read to trigger a network request so that GetLength() below
|
|
|
|
* returns something meaningful and not -1
|
|
|
|
*/
|
|
|
|
char buf[512];
|
|
|
|
unsigned int size = 0;
|
|
|
|
resource->Read(buf, sizeof(buf), &size);
|
|
|
|
resource->Seek(SEEK_SET, 0);
|
|
|
|
|
|
|
|
/* now we should have a length */
|
2013-03-13 20:11:15 +00:00
|
|
|
int64_t resourceLength = resource->GetLength();
|
|
|
|
gst_app_src_set_size(mSource, resourceLength);
|
2012-04-18 22:33:13 +00:00
|
|
|
if (resource->IsDataCachedToEndOfResource(0) ||
|
2013-03-13 20:11:15 +00:00
|
|
|
(resourceLength != -1 && resourceLength <= SHORT_FILE_SIZE)) {
|
2012-04-18 22:33:13 +00:00
|
|
|
/* let the demuxer work in pull mode for local files (or very short files)
|
|
|
|
* so that we get optimal seeking accuracy/performance
|
|
|
|
*/
|
2013-03-13 20:11:15 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("configuring random access, len %lld", resourceLength));
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_app_src_set_stream_type(mSource, GST_APP_STREAM_TYPE_RANDOM_ACCESS);
|
|
|
|
} else {
|
|
|
|
/* make the demuxer work in push mode so that seeking is kept to a minimum
|
|
|
|
*/
|
2013-03-13 20:11:15 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("configuring push mode, len %lld", resourceLength));
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_app_src_set_stream_type(mSource, GST_APP_STREAM_TYPE_SEEKABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:40:07 +00:00
|
|
|
nsresult GStreamerReader::ReadMetadata(VideoInfo* aInfo,
|
2013-03-13 20:11:15 +00:00
|
|
|
MetadataTags** aTags)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
nsresult ret = NS_OK;
|
|
|
|
|
|
|
|
/* We do 3 attempts here: decoding audio and video, decoding video only,
|
|
|
|
* decoding audio only. This allows us to play streams that have one broken
|
|
|
|
* stream but that are otherwise decodeable.
|
|
|
|
*/
|
|
|
|
guint flags[3] = {GST_PLAY_FLAG_VIDEO|GST_PLAY_FLAG_AUDIO,
|
2012-06-22 00:44:39 +00:00
|
|
|
static_cast<guint>(~GST_PLAY_FLAG_AUDIO), static_cast<guint>(~GST_PLAY_FLAG_VIDEO)};
|
2012-04-18 22:33:13 +00:00
|
|
|
guint default_flags, current_flags;
|
2013-03-13 20:11:15 +00:00
|
|
|
g_object_get(mPlayBin, "flags", &default_flags, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
GstMessage* message = nullptr;
|
2013-03-04 07:56:00 +00:00
|
|
|
for (unsigned int i = 0; i < G_N_ELEMENTS(flags); i++) {
|
2012-04-18 22:33:13 +00:00
|
|
|
current_flags = default_flags & flags[i];
|
2013-03-13 20:11:15 +00:00
|
|
|
g_object_set(G_OBJECT(mPlayBin), "flags", current_flags, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
/* reset filter caps to ANY */
|
2013-03-13 20:11:15 +00:00
|
|
|
GstCaps* caps = gst_caps_new_any();
|
|
|
|
GstElement* filter = gst_bin_get_by_name(GST_BIN(mAudioSink), "filter");
|
|
|
|
g_object_set(filter, "caps", caps, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_object_unref(filter);
|
|
|
|
|
|
|
|
filter = gst_bin_get_by_name(GST_BIN(mVideoSink), "filter");
|
2013-03-13 20:11:15 +00:00
|
|
|
g_object_set(filter, "caps", caps, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_object_unref(filter);
|
|
|
|
gst_caps_unref(caps);
|
2013-03-13 20:11:15 +00:00
|
|
|
filter = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
if (!(current_flags & GST_PLAY_FLAG_AUDIO))
|
|
|
|
filter = gst_bin_get_by_name(GST_BIN(mAudioSink), "filter");
|
|
|
|
else if (!(current_flags & GST_PLAY_FLAG_VIDEO))
|
|
|
|
filter = gst_bin_get_by_name(GST_BIN(mVideoSink), "filter");
|
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
/* Little trick: set the target caps to "skip" so that playbin2 fails to
|
|
|
|
* find a decoder for the stream we want to skip.
|
|
|
|
*/
|
2013-03-13 20:11:15 +00:00
|
|
|
GstCaps* filterCaps = gst_caps_new_simple ("skip", nullptr);
|
|
|
|
g_object_set(filter, "caps", filterCaps, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_caps_unref(filterCaps);
|
|
|
|
gst_object_unref(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start the pipeline */
|
|
|
|
gst_element_set_state(mPlayBin, GST_STATE_PAUSED);
|
|
|
|
|
|
|
|
/* Wait for ASYNC_DONE, which is emitted when the pipeline is built,
|
|
|
|
* prerolled and ready to play. Also watch for errors.
|
|
|
|
*/
|
|
|
|
message = gst_bus_timed_pop_filtered(mBus, GST_CLOCK_TIME_NONE,
|
2013-03-13 20:11:15 +00:00
|
|
|
(GstMessageType)(GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR));
|
2012-04-18 22:33:13 +00:00
|
|
|
if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
|
2013-03-13 20:11:15 +00:00
|
|
|
GError* error;
|
|
|
|
gchar* debug;
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
gst_message_parse_error(message, &error, &debug);
|
|
|
|
LOG(PR_LOG_ERROR, ("read metadata error: %s: %s", error->message,
|
2013-03-13 20:11:15 +00:00
|
|
|
debug));
|
2012-04-18 22:33:13 +00:00
|
|
|
g_error_free(error);
|
|
|
|
g_free(debug);
|
|
|
|
gst_element_set_state(mPlayBin, GST_STATE_NULL);
|
|
|
|
gst_message_unref(message);
|
|
|
|
ret = NS_ERROR_FAILURE;
|
|
|
|
} else {
|
|
|
|
gst_message_unref(message);
|
|
|
|
ret = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(ret))
|
|
|
|
/* we couldn't get this to play */
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* FIXME: workaround for a bug in matroskademux. This seek makes matroskademux
|
|
|
|
* parse the index */
|
|
|
|
if (gst_element_seek_simple(mPlayBin, GST_FORMAT_TIME,
|
|
|
|
GST_SEEK_FLAG_FLUSH, 0)) {
|
|
|
|
/* after a seek we need to wait again for ASYNC_DONE */
|
|
|
|
message = gst_bus_timed_pop_filtered(mBus, GST_CLOCK_TIME_NONE,
|
|
|
|
(GstMessageType)(GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR));
|
|
|
|
if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
|
|
|
|
gst_element_set_state(mPlayBin, GST_STATE_NULL);
|
|
|
|
gst_message_unref(message);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* report the duration */
|
|
|
|
gint64 duration;
|
|
|
|
GstFormat format = GST_FORMAT_TIME;
|
|
|
|
if (gst_element_query_duration(GST_ELEMENT(mPlayBin),
|
|
|
|
&format, &duration) && format == GST_FORMAT_TIME) {
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2012-06-04 23:32:36 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("returning duration %" GST_TIME_FORMAT,
|
2012-04-18 22:33:13 +00:00
|
|
|
GST_TIME_ARGS (duration)));
|
|
|
|
duration = GST_TIME_AS_USECONDS (duration);
|
2012-11-19 15:11:21 +00:00
|
|
|
mDecoder->SetMediaDuration(duration);
|
2012-04-18 22:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int n_video = 0, n_audio = 0;
|
2013-03-13 20:11:15 +00:00
|
|
|
g_object_get(mPlayBin, "n-video", &n_video, "n-audio", &n_audio, nullptr);
|
2012-04-18 22:33:13 +00:00
|
|
|
mInfo.mHasVideo = n_video != 0;
|
|
|
|
mInfo.mHasAudio = n_audio != 0;
|
|
|
|
|
|
|
|
*aInfo = mInfo;
|
|
|
|
|
2012-07-31 00:14:29 +00:00
|
|
|
*aTags = nullptr;
|
|
|
|
|
2012-04-18 22:33:13 +00:00
|
|
|
/* set the pipeline to PLAYING so that it starts decoding and queueing data in
|
|
|
|
* the appsinks */
|
|
|
|
gst_element_set_state(mPlayBin, GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult GStreamerReader::ResetDecode()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
if (NS_FAILED(MediaDecoderReader::ResetDecode())) {
|
2012-04-18 22:33:13 +00:00
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoQueue.Reset();
|
|
|
|
mAudioQueue.Reset();
|
|
|
|
|
|
|
|
mVideoSinkBufferCount = 0;
|
|
|
|
mAudioSinkBufferCount = 0;
|
|
|
|
mReachedEos = false;
|
|
|
|
mLastReportedByteOffset = 0;
|
|
|
|
mByteOffset = 0;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void GStreamerReader::NotifyBytesConsumed()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mByteOffset >= mLastReportedByteOffset,
|
|
|
|
"current byte offset less than prev offset");
|
|
|
|
mDecoder->NotifyBytesConsumed(mByteOffset - mLastReportedByteOffset);
|
|
|
|
mLastReportedByteOffset = mByteOffset;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
bool GStreamerReader::WaitForDecodedData(int* aCounter)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
|
|
|
|
/* Report consumed bytes from here as we can't do it from gst threads */
|
|
|
|
NotifyBytesConsumed();
|
|
|
|
while(*aCounter == 0) {
|
|
|
|
if (mReachedEos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mon.Wait();
|
|
|
|
NotifyBytesConsumed();
|
|
|
|
}
|
|
|
|
(*aCounter)--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool GStreamerReader::DecodeAudioData()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
if (!WaitForDecodedData(&mAudioSinkBufferCount)) {
|
|
|
|
mAudioQueue.Finish();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
GstBuffer* buffer = gst_app_sink_pull_buffer(mAudioAppSink);
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t timestamp = GST_BUFFER_TIMESTAMP(buffer);
|
2012-04-18 22:33:13 +00:00
|
|
|
timestamp = gst_segment_to_stream_time(&mAudioSegment,
|
|
|
|
GST_FORMAT_TIME, timestamp);
|
|
|
|
timestamp = GST_TIME_AS_USECONDS(timestamp);
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t duration = 0;
|
2012-04-18 22:33:13 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID(GST_BUFFER_DURATION(buffer)))
|
|
|
|
duration = GST_TIME_AS_USECONDS(GST_BUFFER_DURATION(buffer));
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t offset = GST_BUFFER_OFFSET(buffer);
|
2012-04-18 22:33:13 +00:00
|
|
|
unsigned int size = GST_BUFFER_SIZE(buffer);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t frames = (size / sizeof(AudioDataValue)) / mInfo.mAudioChannels;
|
2012-04-18 22:33:13 +00:00
|
|
|
ssize_t outSize = static_cast<size_t>(size / sizeof(AudioDataValue));
|
|
|
|
nsAutoArrayPtr<AudioDataValue> data(new AudioDataValue[outSize]);
|
|
|
|
memcpy(data, GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
|
2013-03-13 20:11:15 +00:00
|
|
|
AudioData* audio = new AudioData(offset, timestamp, duration,
|
2012-04-18 22:33:13 +00:00
|
|
|
frames, data.forget(), mInfo.mAudioChannels);
|
|
|
|
|
|
|
|
mAudioQueue.Push(audio);
|
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool GStreamerReader::DecodeVideoFrame(bool &aKeyFrameSkip,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aTimeThreshold)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
GstBuffer* buffer = nullptr;
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t timestamp, nextTimestamp;
|
2012-04-18 22:33:13 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!WaitForDecodedData(&mVideoSinkBufferCount)) {
|
|
|
|
mVideoQueue.Finish();
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 15:11:21 +00:00
|
|
|
mDecoder->NotifyDecodedFrames(0, 1);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
buffer = gst_app_sink_pull_buffer(mVideoAppSink);
|
|
|
|
bool isKeyframe = !GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
if ((aKeyFrameSkip && !isKeyframe)) {
|
|
|
|
gst_buffer_unref(buffer);
|
2013-03-13 20:11:15 +00:00
|
|
|
buffer = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp = GST_BUFFER_TIMESTAMP(buffer);
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
timestamp = gst_segment_to_stream_time(&mVideoSegment,
|
|
|
|
GST_FORMAT_TIME, timestamp);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(GST_CLOCK_TIME_IS_VALID(timestamp),
|
|
|
|
"frame has invalid timestamp");
|
|
|
|
timestamp = nextTimestamp = GST_TIME_AS_USECONDS(timestamp);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID(GST_BUFFER_DURATION(buffer)))
|
|
|
|
nextTimestamp += GST_TIME_AS_USECONDS(GST_BUFFER_DURATION(buffer));
|
|
|
|
else if (fpsNum && fpsDen)
|
|
|
|
/* add 1-frame duration */
|
|
|
|
nextTimestamp += gst_util_uint64_scale(GST_USECOND, fpsNum, fpsDen);
|
|
|
|
|
|
|
|
if (timestamp < aTimeThreshold) {
|
2012-06-04 23:32:36 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("skipping frame %" GST_TIME_FORMAT
|
|
|
|
" threshold %" GST_TIME_FORMAT,
|
2012-04-18 22:33:13 +00:00
|
|
|
GST_TIME_ARGS(timestamp), GST_TIME_ARGS(aTimeThreshold)));
|
|
|
|
gst_buffer_unref(buffer);
|
2013-03-13 20:11:15 +00:00
|
|
|
buffer = nullptr;
|
2012-04-18 22:33:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
if (!buffer)
|
2012-04-18 22:33:13 +00:00
|
|
|
/* no more frames */
|
|
|
|
return false;
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
nsRefPtr<PlanarYCbCrImage> image;
|
|
|
|
#if GST_VERSION_MICRO >= 36
|
|
|
|
const GstStructure* structure = gst_buffer_get_qdata(buffer,
|
|
|
|
g_quark_from_string("moz-reader-data"));
|
|
|
|
const GValue* value = gst_structure_get_value(structure, "image");
|
|
|
|
if (value) {
|
|
|
|
BufferData* data = reinterpret_cast<BufferData*>(g_value_get_boxed(value));
|
|
|
|
image = data->mImage;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!image) {
|
|
|
|
/* Ugh, upstream is not calling gst_pad_alloc_buffer(). Fallback to
|
|
|
|
* allocating a PlanarYCbCrImage backed GstBuffer here and memcpy.
|
|
|
|
*/
|
|
|
|
GstBuffer* tmp = nullptr;
|
|
|
|
AllocateVideoBufferFull(nullptr, GST_BUFFER_OFFSET(buffer),
|
|
|
|
GST_BUFFER_SIZE(buffer), nullptr, &tmp, image);
|
|
|
|
|
|
|
|
/* copy */
|
|
|
|
gst_buffer_copy_metadata(tmp, buffer, GST_BUFFER_COPY_ALL);
|
|
|
|
memcpy(GST_BUFFER_DATA(tmp), GST_BUFFER_DATA(buffer),
|
|
|
|
GST_BUFFER_SIZE(tmp));
|
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
buffer = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint8* data = GST_BUFFER_DATA(buffer);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
int width = mPicture.width;
|
|
|
|
int height = mPicture.height;
|
|
|
|
GstVideoFormat format = mFormat;
|
|
|
|
|
|
|
|
VideoData::YCbCrBuffer b;
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
b.mPlanes[i].mData = data + gst_video_format_get_component_offset(format, i,
|
|
|
|
width, height);
|
|
|
|
b.mPlanes[i].mStride = gst_video_format_get_row_stride(format, i, width);
|
|
|
|
b.mPlanes[i].mHeight = gst_video_format_get_component_height(format,
|
|
|
|
i, height);
|
|
|
|
b.mPlanes[i].mWidth = gst_video_format_get_component_width(format,
|
|
|
|
i, width);
|
2012-06-04 23:31:13 +00:00
|
|
|
b.mPlanes[i].mOffset = 0;
|
|
|
|
b.mPlanes[i].mSkip = 0;
|
2012-04-18 22:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isKeyframe = !GST_BUFFER_FLAG_IS_SET(buffer,
|
|
|
|
GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
/* XXX ? */
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t offset = 0;
|
2013-03-13 20:11:15 +00:00
|
|
|
VideoData* video = VideoData::Create(mInfo, image, offset,
|
|
|
|
timestamp, nextTimestamp, b,
|
|
|
|
isKeyframe, -1, mPicture);
|
2012-04-18 22:33:13 +00:00
|
|
|
mVideoQueue.Push(video);
|
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult GStreamerReader::Seek(int64_t aTarget,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aStartTime,
|
|
|
|
int64_t aEndTime,
|
|
|
|
int64_t aCurrentTime)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
gint64 seekPos = aTarget * GST_USECOND;
|
2012-06-04 23:32:36 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p About to seek to %" GST_TIME_FORMAT,
|
2012-04-18 22:33:13 +00:00
|
|
|
mDecoder, GST_TIME_ARGS(seekPos)));
|
|
|
|
|
|
|
|
if (!gst_element_seek_simple(mPlayBin, GST_FORMAT_TIME,
|
|
|
|
static_cast<GstSeekFlags>(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE), seekPos)) {
|
|
|
|
LOG(PR_LOG_ERROR, ("seek failed"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
LOG(PR_LOG_DEBUG, ("seek succeeded"));
|
|
|
|
|
|
|
|
return DecodeToTarget(aTarget);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:14:44 +00:00
|
|
|
nsresult GStreamerReader::GetBuffered(TimeRanges* aBuffered,
|
|
|
|
int64_t aStartTime)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2012-06-06 05:58:07 +00:00
|
|
|
if (!mInfo.mHasVideo && !mInfo.mHasAudio) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-18 22:33:13 +00:00
|
|
|
GstFormat format = GST_FORMAT_TIME;
|
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
|
|
|
nsTArray<MediaByteRange> ranges;
|
|
|
|
resource->GetCachedRanges(ranges);
|
|
|
|
|
|
|
|
if (mDecoder->OnStateMachineThread())
|
|
|
|
/* Report the position from here while buffering as we can't report it from
|
|
|
|
* the gstreamer threads that are actually reading from the resource
|
|
|
|
*/
|
|
|
|
NotifyBytesConsumed();
|
|
|
|
|
|
|
|
if (resource->IsDataCachedToEndOfResource(0)) {
|
|
|
|
/* fast path for local or completely cached files */
|
|
|
|
gint64 duration = 0;
|
|
|
|
|
|
|
|
duration = QueryDuration();
|
|
|
|
double end = (double) duration / GST_MSECOND;
|
|
|
|
LOG(PR_LOG_DEBUG, ("complete range [0, %f] for [0, %li]",
|
2013-03-13 20:11:15 +00:00
|
|
|
end, resource->GetLength()));
|
2012-04-18 22:33:13 +00:00
|
|
|
aBuffered->Add(0, end);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for(uint32_t index = 0; index < ranges.Length(); index++) {
|
|
|
|
int64_t startOffset = ranges[index].mStart;
|
|
|
|
int64_t endOffset = ranges[index].mEnd;
|
2012-04-18 22:33:13 +00:00
|
|
|
gint64 startTime, endTime;
|
|
|
|
|
|
|
|
if (!gst_element_query_convert(GST_ELEMENT(mPlayBin), GST_FORMAT_BYTES,
|
|
|
|
startOffset, &format, &startTime) || format != GST_FORMAT_TIME)
|
|
|
|
continue;
|
|
|
|
if (!gst_element_query_convert(GST_ELEMENT(mPlayBin), GST_FORMAT_BYTES,
|
|
|
|
endOffset, &format, &endTime) || format != GST_FORMAT_TIME)
|
|
|
|
continue;
|
|
|
|
|
2013-03-04 07:56:00 +00:00
|
|
|
double start = (double) GST_TIME_AS_USECONDS (startTime) / GST_MSECOND;
|
2012-04-18 22:33:13 +00:00
|
|
|
double end = (double) GST_TIME_AS_USECONDS (endTime) / GST_MSECOND;
|
|
|
|
LOG(PR_LOG_DEBUG, ("adding range [%f, %f] for [%li %li] size %li",
|
2013-03-13 20:11:15 +00:00
|
|
|
start, end, startOffset, endOffset, resource->GetLength()));
|
2012-04-18 22:33:13 +00:00
|
|
|
aBuffered->Add(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void GStreamerReader::ReadAndPushData(guint aLength)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
GstBuffer* buffer = gst_buffer_new_and_alloc(aLength);
|
|
|
|
guint8* data = GST_BUFFER_DATA(buffer);
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t size = 0, bytesRead = 0;
|
2012-04-18 22:33:13 +00:00
|
|
|
while(bytesRead < aLength) {
|
|
|
|
rv = resource->Read(reinterpret_cast<char*>(data + bytesRead),
|
|
|
|
aLength - bytesRead, &size);
|
|
|
|
if (NS_FAILED(rv) || size == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bytesRead += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_BUFFER_SIZE(buffer) = bytesRead;
|
|
|
|
mByteOffset += bytesRead;
|
|
|
|
|
|
|
|
GstFlowReturn ret = gst_app_src_push_buffer(mSource, gst_buffer_ref(buffer));
|
2013-03-13 20:11:15 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2012-04-18 22:33:13 +00:00
|
|
|
LOG(PR_LOG_ERROR, ("ReadAndPushData push ret %s", gst_flow_get_name(ret)));
|
2013-03-13 20:11:15 +00:00
|
|
|
}
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
if (GST_BUFFER_SIZE (buffer) < aLength) {
|
2012-04-18 22:33:13 +00:00
|
|
|
/* If we read less than what we wanted, we reached the end */
|
|
|
|
gst_app_src_end_of_stream(mSource);
|
2013-03-13 20:11:15 +00:00
|
|
|
}
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
int64_t GStreamerReader::QueryDuration()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
gint64 duration = 0;
|
|
|
|
GstFormat format = GST_FORMAT_TIME;
|
|
|
|
|
|
|
|
if (gst_element_query_duration(GST_ELEMENT(mPlayBin),
|
|
|
|
&format, &duration)) {
|
|
|
|
if (format == GST_FORMAT_TIME) {
|
2012-06-04 23:32:36 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("pipeline duration %" GST_TIME_FORMAT,
|
2012-04-18 22:33:13 +00:00
|
|
|
GST_TIME_ARGS (duration)));
|
|
|
|
duration = GST_TIME_AS_USECONDS (duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:45:13 +00:00
|
|
|
/*if (mDecoder->mDuration != -1 &&
|
2012-04-18 22:33:13 +00:00
|
|
|
mDecoder->mDuration > duration) {
|
2012-11-14 19:45:13 +00:00
|
|
|
// We decoded more than the reported duration (which could be estimated)
|
2012-04-18 22:33:13 +00:00
|
|
|
LOG(PR_LOG_DEBUG, ("mDuration > duration"));
|
|
|
|
duration = mDecoder->mDuration;
|
2012-11-14 19:45:13 +00:00
|
|
|
}*/
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::NeedDataCb(GstAppSrc* aSrc,
|
|
|
|
guint aLength,
|
|
|
|
gpointer aUserData)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
reader->NeedData(aSrc, aLength);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::NeedData(GstAppSrc* aSrc, guint aLength)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
if (aLength == static_cast<guint>(-1))
|
2012-04-18 22:33:13 +00:00
|
|
|
aLength = DEFAULT_SOURCE_READ_SIZE;
|
|
|
|
ReadAndPushData(aLength);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::EnoughDataCb(GstAppSrc* aSrc, gpointer aUserData)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
reader->EnoughData(aSrc);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::EnoughData(GstAppSrc* aSrc)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
gboolean GStreamerReader::SeekDataCb(GstAppSrc* aSrc,
|
|
|
|
guint64 aOffset,
|
|
|
|
gpointer aUserData)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
return reader->SeekData(aSrc, aOffset);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
gboolean GStreamerReader::SeekData(GstAppSrc* aSrc, guint64 aOffset)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2013-03-13 20:11:15 +00:00
|
|
|
int64_t resourceLength = resource->GetLength();
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
if (gst_app_src_get_size(mSource) == -1) {
|
2012-04-18 22:33:13 +00:00
|
|
|
/* It's possible that we didn't know the length when we initialized mSource
|
|
|
|
* but maybe we do now
|
|
|
|
*/
|
2013-03-13 20:11:15 +00:00
|
|
|
gst_app_src_set_size(mSource, resourceLength);
|
|
|
|
}
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2013-03-13 20:11:15 +00:00
|
|
|
if (aOffset < static_cast<guint64>(resourceLength)) {
|
2012-04-18 22:33:13 +00:00
|
|
|
rv = resource->Seek(SEEK_SET, aOffset);
|
2013-03-13 20:11:15 +00:00
|
|
|
}
|
2012-04-18 22:33:13 +00:00
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-04-18 22:33:13 +00:00
|
|
|
mByteOffset = mLastReportedByteOffset = aOffset;
|
2013-03-13 20:11:15 +00:00
|
|
|
} else {
|
2012-04-18 22:33:13 +00:00
|
|
|
LOG(PR_LOG_ERROR, ("seek at %lu failed", aOffset));
|
2013-03-13 20:11:15 +00:00
|
|
|
}
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
gboolean GStreamerReader::EventProbeCb(GstPad* aPad,
|
|
|
|
GstEvent* aEvent,
|
2012-04-18 22:33:13 +00:00
|
|
|
gpointer aUserData)
|
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
return reader->EventProbe(aPad, aEvent);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
gboolean GStreamerReader::EventProbe(GstPad* aPad, GstEvent* aEvent)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GstElement* parent = GST_ELEMENT(gst_pad_get_parent(aPad));
|
2012-04-18 22:33:13 +00:00
|
|
|
switch(GST_EVENT_TYPE(aEvent)) {
|
|
|
|
case GST_EVENT_NEWSEGMENT:
|
|
|
|
{
|
|
|
|
gboolean update;
|
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
gint64 start, stop, position;
|
2013-03-13 20:11:15 +00:00
|
|
|
GstSegment* segment;
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
/* Store the segments so we can convert timestamps to stream time, which
|
|
|
|
* is what the upper layers sync on.
|
|
|
|
*/
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
gst_event_parse_new_segment(aEvent, &update, &rate, &format,
|
|
|
|
&start, &stop, &position);
|
|
|
|
if (parent == GST_ELEMENT(mVideoAppSink))
|
|
|
|
segment = &mVideoSegment;
|
|
|
|
else
|
|
|
|
segment = &mAudioSegment;
|
|
|
|
gst_segment_set_newsegment(segment, update, rate, format,
|
|
|
|
start, stop, position);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
/* Reset on seeks */
|
|
|
|
ResetDecode();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gst_object_unref(parent);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
GstFlowReturn GStreamerReader::AllocateVideoBufferFull(GstPad* aPad,
|
|
|
|
guint64 aOffset,
|
|
|
|
guint aSize,
|
|
|
|
GstCaps* aCaps,
|
|
|
|
GstBuffer** aBuf,
|
|
|
|
nsRefPtr<PlanarYCbCrImage>& aImage)
|
|
|
|
{
|
|
|
|
/* allocate an image using the container */
|
|
|
|
ImageContainer* container = mDecoder->GetImageContainer();
|
|
|
|
ImageFormat format = PLANAR_YCBCR;
|
|
|
|
PlanarYCbCrImage* img = reinterpret_cast<PlanarYCbCrImage*>(container->CreateImage(&format, 1).get());
|
|
|
|
nsRefPtr<PlanarYCbCrImage> image = dont_AddRef(img);
|
|
|
|
|
|
|
|
/* prepare a GstBuffer pointing to the underlying PlanarYCbCrImage buffer */
|
|
|
|
GstBuffer* buf = gst_buffer_new();
|
|
|
|
GST_BUFFER_SIZE(buf) = aSize;
|
|
|
|
/* allocate the actual YUV buffer */
|
|
|
|
GST_BUFFER_DATA(buf) = image->AllocateAndGetNewBuffer(aSize);
|
|
|
|
|
|
|
|
aImage = image;
|
|
|
|
|
|
|
|
#if GST_VERSION_MICRO >= 36
|
|
|
|
/* create a GBoxed handle to hold the image */
|
|
|
|
BufferData* data = new BufferData(image);
|
|
|
|
|
|
|
|
/* store it in a GValue so we can put it in a GstStructure */
|
|
|
|
GValue value = {0,};
|
|
|
|
g_value_init(&value, buffer_data_get_type());
|
|
|
|
g_value_take_boxed(&value, data);
|
|
|
|
|
|
|
|
/* store the value in the structure */
|
|
|
|
GstStructure* structure = gst_structure_new("moz-reader-data", nullptr);
|
|
|
|
gst_structure_take_value(structure, "image", &value);
|
|
|
|
|
|
|
|
/* and attach the structure to the buffer */
|
|
|
|
gst_buffer_set_qdata(buf, g_quark_from_string("moz-reader-data"), structure);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*aBuf = buf;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstFlowReturn GStreamerReader::AllocateVideoBufferCb(GstPad* aPad,
|
|
|
|
guint64 aOffset,
|
|
|
|
guint aSize,
|
|
|
|
GstCaps* aCaps,
|
|
|
|
GstBuffer** aBuf)
|
|
|
|
{
|
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(gst_pad_get_element_private(aPad));
|
|
|
|
return reader->AllocateVideoBuffer(aPad, aOffset, aSize, aCaps, aBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstFlowReturn GStreamerReader::AllocateVideoBuffer(GstPad* aPad,
|
|
|
|
guint64 aOffset,
|
|
|
|
guint aSize,
|
|
|
|
GstCaps* aCaps,
|
|
|
|
GstBuffer** aBuf)
|
|
|
|
{
|
|
|
|
nsRefPtr<PlanarYCbCrImage> image;
|
|
|
|
return AllocateVideoBufferFull(aPad, aOffset, aSize, aCaps, aBuf, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstFlowReturn GStreamerReader::NewPrerollCb(GstAppSink* aSink,
|
2012-04-18 22:33:13 +00:00
|
|
|
gpointer aUserData)
|
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
if (aSink == reader->mVideoAppSink)
|
|
|
|
reader->VideoPreroll();
|
|
|
|
else
|
|
|
|
reader->AudioPreroll();
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void GStreamerReader::AudioPreroll()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
/* The first audio buffer has reached the audio sink. Get rate and channels */
|
|
|
|
LOG(PR_LOG_DEBUG, ("Audio preroll"));
|
2013-03-13 20:11:15 +00:00
|
|
|
GstPad* sinkpad = gst_element_get_pad(GST_ELEMENT(mAudioAppSink), "sink");
|
|
|
|
GstCaps* caps = gst_pad_get_negotiated_caps(sinkpad);
|
|
|
|
GstStructure* s = gst_caps_get_structure(caps, 0);
|
2012-04-18 22:33:13 +00:00
|
|
|
mInfo.mAudioRate = mInfo.mAudioChannels = 0;
|
2013-03-13 20:11:15 +00:00
|
|
|
gst_structure_get_int(s, "rate", (gint*) &mInfo.mAudioRate);
|
|
|
|
gst_structure_get_int(s, "channels", (gint*) &mInfo.mAudioChannels);
|
2012-04-18 22:33:13 +00:00
|
|
|
NS_ASSERTION(mInfo.mAudioRate != 0, ("audio rate is zero"));
|
|
|
|
NS_ASSERTION(mInfo.mAudioChannels != 0, ("audio channels is zero"));
|
|
|
|
NS_ASSERTION(mInfo.mAudioChannels > 0 && mInfo.mAudioChannels <= MAX_CHANNELS,
|
|
|
|
"invalid audio channels number");
|
|
|
|
mInfo.mHasAudio = true;
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
gst_object_unref(sinkpad);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void GStreamerReader::VideoPreroll()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
/* The first video buffer has reached the video sink. Get width and height */
|
|
|
|
LOG(PR_LOG_DEBUG, ("Video preroll"));
|
2013-03-13 20:11:15 +00:00
|
|
|
GstPad* sinkpad = gst_element_get_pad(GST_ELEMENT(mVideoAppSink), "sink");
|
|
|
|
GstCaps* caps = gst_pad_get_negotiated_caps(sinkpad);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_video_format_parse_caps(caps, &mFormat, &mPicture.width, &mPicture.height);
|
2013-03-13 20:11:15 +00:00
|
|
|
GstStructure* structure = gst_caps_get_structure(caps, 0);
|
2012-04-18 22:33:13 +00:00
|
|
|
gst_structure_get_fraction(structure, "framerate", &fpsNum, &fpsDen);
|
|
|
|
NS_ASSERTION(mPicture.width && mPicture.height, "invalid video resolution");
|
|
|
|
mInfo.mDisplay = nsIntSize(mPicture.width, mPicture.height);
|
|
|
|
mInfo.mHasVideo = true;
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
gst_object_unref(sinkpad);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
GstFlowReturn GStreamerReader::NewBufferCb(GstAppSink* aSink,
|
|
|
|
gpointer aUserData)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
|
|
|
|
if (aSink == reader->mVideoAppSink)
|
|
|
|
reader->NewVideoBuffer();
|
|
|
|
else
|
|
|
|
reader->NewAudioBuffer();
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void GStreamerReader::NewVideoBuffer()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
/* We have a new video buffer queued in the video sink. Increment the counter
|
|
|
|
* and notify the decode thread potentially blocked in DecodeVideoFrame
|
|
|
|
*/
|
2012-11-19 15:11:21 +00:00
|
|
|
mDecoder->NotifyDecodedFrames(1, 0);
|
2012-04-18 22:33:13 +00:00
|
|
|
mVideoSinkBufferCount++;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void GStreamerReader::NewAudioBuffer()
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
/* We have a new audio buffer queued in the audio sink. Increment the counter
|
|
|
|
* and notify the decode thread potentially blocked in DecodeAudioData
|
|
|
|
*/
|
|
|
|
mAudioSinkBufferCount++;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::EosCb(GstAppSink* aSink, gpointer aUserData)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
2013-03-13 20:11:15 +00:00
|
|
|
GStreamerReader* reader = reinterpret_cast<GStreamerReader*>(aUserData);
|
2012-04-18 22:33:13 +00:00
|
|
|
reader->Eos(aSink);
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:11:15 +00:00
|
|
|
void GStreamerReader::Eos(GstAppSink* aSink)
|
2012-04-18 22:33:13 +00:00
|
|
|
{
|
|
|
|
/* We reached the end of the stream */
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mGstThreadsMonitor);
|
|
|
|
/* Potentially unblock DecodeVideoFrame and DecodeAudioData */
|
|
|
|
mReachedEos = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
/* Potentially unblock the decode thread in ::DecodeLoop */
|
|
|
|
mVideoQueue.Finish();
|
|
|
|
mAudioQueue.Finish();
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 19:45:33 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|