2014-04-14 15:24:00 +04:00
|
|
|
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-09-27 09:22:37 +04:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "MediaSourceDecoder.h"
|
|
|
|
|
2014-08-22 06:14:36 +04:00
|
|
|
#include "prlog.h"
|
2013-09-27 09:22:37 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2013-10-05 12:04:39 +04:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2014-08-13 16:22:00 +04:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2013-10-05 12:04:39 +04:00
|
|
|
#include "MediaSource.h"
|
2014-08-13 16:22:00 +04:00
|
|
|
#include "MediaSourceReader.h"
|
|
|
|
#include "MediaSourceResource.h"
|
2014-08-20 12:07:00 +04:00
|
|
|
#include "MediaSourceUtils.h"
|
2014-07-22 12:20:00 +04:00
|
|
|
|
2013-09-27 09:22:37 +04:00
|
|
|
#ifdef PR_LOGGING
|
2014-08-11 05:21:17 +04:00
|
|
|
extern PRLogModuleInfo* GetMediaSourceLog();
|
|
|
|
extern PRLogModuleInfo* GetMediaSourceAPILog();
|
|
|
|
|
|
|
|
#define MSE_DEBUG(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
|
|
|
#define MSE_DEBUGV(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG+1, (__VA_ARGS__))
|
|
|
|
#define MSE_API(...) PR_LOG(GetMediaSourceAPILog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
2013-09-27 09:22:37 +04:00
|
|
|
#else
|
2014-03-05 07:35:46 +04:00
|
|
|
#define MSE_DEBUG(...)
|
2014-08-11 05:21:17 +04:00
|
|
|
#define MSE_DEBUGV(...)
|
|
|
|
#define MSE_API(...)
|
2013-09-27 09:22:37 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-22 06:14:36 +04:00
|
|
|
class SourceBufferDecoder;
|
2013-09-27 09:22:37 +04:00
|
|
|
|
2013-11-18 08:22:47 +04:00
|
|
|
MediaSourceDecoder::MediaSourceDecoder(dom::HTMLMediaElement* aElement)
|
2013-09-27 09:22:37 +04:00
|
|
|
: mMediaSource(nullptr)
|
2014-11-12 07:11:33 +03:00
|
|
|
, mMediaSourceDuration(UnspecifiedNaN<double>())
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
|
|
|
Init(aElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoder*
|
|
|
|
MediaSourceDecoder::Clone()
|
|
|
|
{
|
|
|
|
// TODO: Sort out cloning.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoderStateMachine*
|
|
|
|
MediaSourceDecoder::CreateStateMachine()
|
|
|
|
{
|
2014-08-25 08:09:44 +04:00
|
|
|
mReader = new MediaSourceReader(this);
|
2014-08-17 15:29:00 +04:00
|
|
|
return new MediaDecoderStateMachine(this, mReader);
|
2013-09-27 09:22:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaSourceDecoder::Load(nsIStreamListener**, MediaDecoder*)
|
|
|
|
{
|
2014-02-18 02:53:51 +04:00
|
|
|
MOZ_ASSERT(!mDecoderStateMachine);
|
|
|
|
mDecoderStateMachine = CreateStateMachine();
|
|
|
|
if (!mDecoderStateMachine) {
|
|
|
|
NS_WARNING("Failed to create state machine!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:55:02 +04:00
|
|
|
nsresult rv = mDecoderStateMachine->Init(nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SetStateMachineParameters();
|
2014-12-22 11:20:31 +03:00
|
|
|
return ScheduleStateMachineThread();
|
2013-09-27 09:22:37 +04:00
|
|
|
}
|
|
|
|
|
2013-10-05 12:04:39 +04:00
|
|
|
nsresult
|
2013-11-18 08:22:47 +04:00
|
|
|
MediaSourceDecoder::GetSeekable(dom::TimeRanges* aSeekable)
|
2013-10-05 12:04:39 +04:00
|
|
|
{
|
2014-08-25 08:09:44 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mMediaSource) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-09-16 07:15:55 +04:00
|
|
|
|
2013-10-05 12:04:39 +04:00
|
|
|
double duration = mMediaSource->Duration();
|
|
|
|
if (IsNaN(duration)) {
|
|
|
|
// Return empty range.
|
|
|
|
} else if (duration > 0 && mozilla::IsInfinite(duration)) {
|
2013-11-18 08:22:47 +04:00
|
|
|
nsRefPtr<dom::TimeRanges> bufferedRanges = new dom::TimeRanges();
|
2014-11-12 07:50:21 +03:00
|
|
|
mReader->GetBuffered(bufferedRanges);
|
2014-04-21 17:30:00 +04:00
|
|
|
aSeekable->Add(bufferedRanges->GetStartTime(), bufferedRanges->GetEndTime());
|
2013-10-05 12:04:39 +04:00
|
|
|
} else {
|
|
|
|
aSeekable->Add(0, duration);
|
|
|
|
}
|
2014-08-20 12:07:00 +04:00
|
|
|
MSE_DEBUG("MediaSourceDecoder(%p)::GetSeekable ranges=%s", this, DumpTimeRanges(aSeekable).get());
|
2013-10-05 12:04:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-25 08:09:44 +04:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::Shutdown()
|
|
|
|
{
|
2014-08-28 07:44:58 +04:00
|
|
|
MSE_DEBUG("MediaSourceDecoder(%p)::Shutdown", this);
|
2014-11-04 07:24:52 +03:00
|
|
|
// Detach first so that TrackBuffers are unused on the main thread when
|
|
|
|
// shut down on the decode task queue.
|
2014-08-28 07:44:58 +04:00
|
|
|
if (mMediaSource) {
|
|
|
|
mMediaSource->Detach();
|
|
|
|
}
|
2014-11-04 07:24:52 +03:00
|
|
|
|
|
|
|
MediaDecoder::Shutdown();
|
2014-08-25 08:09:44 +04:00
|
|
|
// Kick WaitForData out of its slumber.
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2014-04-14 15:24:00 +04:00
|
|
|
/*static*/
|
|
|
|
already_AddRefed<MediaResource>
|
2014-10-15 05:17:03 +04:00
|
|
|
MediaSourceDecoder::CreateResource(nsIPrincipal* aPrincipal)
|
2014-04-14 15:24:00 +04:00
|
|
|
{
|
2014-10-15 05:17:03 +04:00
|
|
|
return nsRefPtr<MediaResource>(new MediaSourceResource(aPrincipal)).forget();
|
2014-04-14 15:24:00 +04:00
|
|
|
}
|
|
|
|
|
2013-09-27 09:22:37 +04:00
|
|
|
void
|
2013-11-18 08:22:47 +04:00
|
|
|
MediaSourceDecoder::AttachMediaSource(dom::MediaSource* aMediaSource)
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
2014-08-25 08:09:44 +04:00
|
|
|
MOZ_ASSERT(!mMediaSource && !mDecoderStateMachine && NS_IsMainThread());
|
2013-09-27 09:22:37 +04:00
|
|
|
mMediaSource = aMediaSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::DetachMediaSource()
|
|
|
|
{
|
2014-08-25 08:09:44 +04:00
|
|
|
MOZ_ASSERT(mMediaSource && NS_IsMainThread());
|
2013-09-27 09:22:37 +04:00
|
|
|
mMediaSource = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 06:14:36 +04:00
|
|
|
already_AddRefed<SourceBufferDecoder>
|
2015-01-08 02:58:55 +03:00
|
|
|
MediaSourceDecoder::CreateSubDecoder(const nsACString& aType, int64_t aTimestampOffset)
|
2013-09-27 09:22:37 +04:00
|
|
|
{
|
2014-08-20 12:07:00 +04:00
|
|
|
MOZ_ASSERT(mReader);
|
2015-01-08 02:58:55 +03:00
|
|
|
return mReader->CreateSubDecoder(aType, aTimestampOffset);
|
2014-04-21 17:30:00 +04:00
|
|
|
}
|
|
|
|
|
2014-09-04 05:57:06 +04:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::AddTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mReader);
|
|
|
|
mReader->AddTrackBuffer(aTrackBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::RemoveTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mReader);
|
|
|
|
mReader->RemoveTrackBuffer(aTrackBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::OnTrackBufferConfigured(TrackBuffer* aTrackBuffer, const MediaInfo& aInfo)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mReader);
|
|
|
|
mReader->OnTrackBufferConfigured(aTrackBuffer, aInfo);
|
|
|
|
}
|
|
|
|
|
2014-08-26 11:25:09 +04:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::Ended()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mReader->Ended();
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
2014-11-12 07:50:20 +03:00
|
|
|
bool
|
|
|
|
MediaSourceDecoder::IsExpectingMoreData()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
return !mReader->IsEnded();
|
|
|
|
}
|
|
|
|
|
2014-11-12 07:11:33 +03:00
|
|
|
class DurationChangedRunnable : public nsRunnable {
|
|
|
|
public:
|
2015-01-16 15:49:02 +03:00
|
|
|
DurationChangedRunnable(MediaSourceDecoder* aDecoder,
|
|
|
|
double aOldDuration,
|
|
|
|
double aNewDuration)
|
2014-11-12 07:11:33 +03:00
|
|
|
: mDecoder(aDecoder)
|
|
|
|
, mOldDuration(aOldDuration)
|
|
|
|
, mNewDuration(aNewDuration)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL {
|
|
|
|
mDecoder->DurationChanged(mOldDuration, mNewDuration);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<MediaSourceDecoder> mDecoder;
|
|
|
|
double mOldDuration;
|
|
|
|
double mNewDuration;
|
|
|
|
};
|
|
|
|
|
2014-08-25 08:09:44 +04:00
|
|
|
void
|
2014-11-12 07:11:33 +03:00
|
|
|
MediaSourceDecoder::DurationChanged(double aOldDuration, double aNewDuration)
|
2014-08-25 08:09:44 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-12 07:11:33 +03:00
|
|
|
// Run the MediaSource duration changed algorithm
|
|
|
|
if (mMediaSource) {
|
|
|
|
mMediaSource->DurationChange(aOldDuration, aNewDuration);
|
|
|
|
}
|
|
|
|
// Run the MediaElement duration changed algorithm
|
|
|
|
MediaDecoder::DurationChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::SetDecodedDuration(int64_t aDuration)
|
|
|
|
{
|
|
|
|
// Only use the decoded duration if one wasn't already
|
|
|
|
// set.
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
if (!mMediaSource || !IsNaN(mMediaSourceDuration)) {
|
2014-08-25 08:09:44 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-11-12 07:11:33 +03:00
|
|
|
double duration = aDuration;
|
2015-01-16 15:49:01 +03:00
|
|
|
// A duration of -1 is +Infinity.
|
|
|
|
if (aDuration >= 0) {
|
|
|
|
duration /= USECS_PER_S;
|
|
|
|
}
|
2015-01-16 15:49:02 +03:00
|
|
|
DoSetMediaSourceDuration(duration);
|
2014-11-12 07:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-16 15:49:02 +03:00
|
|
|
MediaSourceDecoder::SetMediaSourceDuration(double aDuration, MSRangeRemovalAction aAction)
|
2014-11-12 07:11:33 +03:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
double oldDuration = mMediaSourceDuration;
|
2015-01-16 15:49:02 +03:00
|
|
|
DoSetMediaSourceDuration(aDuration);
|
|
|
|
ScheduleDurationChange(oldDuration, aDuration, aAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::DoSetMediaSourceDuration(double aDuration)
|
|
|
|
{
|
2015-01-16 15:49:01 +03:00
|
|
|
if (aDuration >= 0) {
|
|
|
|
mDecoderStateMachine->SetDuration(aDuration * USECS_PER_S);
|
|
|
|
mMediaSourceDuration = aDuration;
|
|
|
|
} else {
|
|
|
|
mDecoderStateMachine->SetDuration(INT64_MAX);
|
|
|
|
mMediaSourceDuration = PositiveInfinity<double>();
|
|
|
|
}
|
2015-01-24 13:46:21 +03:00
|
|
|
if (mReader) {
|
|
|
|
mReader->SetMediaSourceDuration(mMediaSourceDuration);
|
|
|
|
}
|
2015-01-16 15:49:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceDecoder::ScheduleDurationChange(double aOldDuration,
|
|
|
|
double aNewDuration,
|
|
|
|
MSRangeRemovalAction aAction)
|
|
|
|
{
|
2015-01-16 15:49:02 +03:00
|
|
|
if (aAction == MSRangeRemovalAction::SKIP) {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
MediaDecoder::DurationChanged();
|
|
|
|
} else {
|
2015-01-16 15:49:02 +03:00
|
|
|
nsCOMPtr<nsIRunnable> task =
|
|
|
|
NS_NewRunnableMethod(this, &MediaDecoder::DurationChanged);
|
2015-01-16 15:49:02 +03:00
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
}
|
2014-11-12 07:11:33 +03:00
|
|
|
} else {
|
2015-01-16 15:49:02 +03:00
|
|
|
if (NS_IsMainThread()) {
|
2015-01-16 15:49:02 +03:00
|
|
|
DurationChanged(aOldDuration, aNewDuration);
|
2015-01-16 15:49:02 +03:00
|
|
|
} else {
|
|
|
|
nsRefPtr<nsIRunnable> task =
|
2015-01-16 15:49:02 +03:00
|
|
|
new DurationChangedRunnable(this, aOldDuration, aNewDuration);
|
2015-01-16 15:49:02 +03:00
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
}
|
2014-11-12 07:11:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
MediaSourceDecoder::GetMediaSourceDuration()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
return mMediaSourceDuration;
|
2014-08-25 08:09:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-07 15:54:00 +04:00
|
|
|
MediaSourceDecoder::NotifyTimeRangesChanged()
|
2014-08-25 08:09:44 +04:00
|
|
|
{
|
2014-11-05 03:32:26 +03:00
|
|
|
MOZ_ASSERT(mReader);
|
|
|
|
mReader->NotifyTimeRangesChanged();
|
2014-08-25 08:09:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 04:04:54 +04:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::PrepareReaderInitialization()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mReader);
|
|
|
|
mReader->PrepareInitialization();
|
|
|
|
}
|
|
|
|
|
2015-01-29 05:35:58 +03:00
|
|
|
void
|
|
|
|
MediaSourceDecoder::GetMozDebugReaderData(nsAString& aString)
|
|
|
|
{
|
|
|
|
mReader->GetMozDebugReaderData(aString);
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:05:00 +04:00
|
|
|
#ifdef MOZ_EME
|
|
|
|
nsresult
|
|
|
|
MediaSourceDecoder::SetCDMProxy(CDMProxy* aProxy)
|
|
|
|
{
|
|
|
|
nsresult rv = MediaDecoder::SetCDMProxy(aProxy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mReader->SetCDMProxy(aProxy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-16 06:14:56 +03:00
|
|
|
bool
|
|
|
|
MediaSourceDecoder::IsActiveReader(MediaDecoderReader* aReader)
|
|
|
|
{
|
|
|
|
return mReader->IsActiveReader(aReader);
|
|
|
|
}
|
|
|
|
|
2013-09-27 09:22:37 +04:00
|
|
|
} // namespace mozilla
|