2017-05-05 11:40:29 +03: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 "HLSDecoder.h"
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#include "DecoderTraits.h"
|
2017-08-30 09:14:32 +03:00
|
|
|
#include "GeneratedJNINatives.h"
|
|
|
|
#include "GeneratedJNIWrappers.h"
|
2017-05-05 11:40:29 +03:00
|
|
|
#include "HLSDemuxer.h"
|
|
|
|
#include "HLSUtils.h"
|
|
|
|
#include "MediaContainerType.h"
|
|
|
|
#include "MediaDecoderStateMachine.h"
|
|
|
|
#include "MediaFormatReader.h"
|
2017-06-30 05:11:42 +03:00
|
|
|
#include "MediaShutdownManager.h"
|
2017-08-30 09:14:32 +03:00
|
|
|
#include "nsContentUtils.h"
|
2017-06-30 05:11:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
2017-11-03 09:22:41 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2018-03-20 01:48:56 +03:00
|
|
|
#include "mozilla/StaticPrefs.h"
|
2017-05-05 11:40:29 +03:00
|
|
|
|
2017-08-30 09:14:32 +03:00
|
|
|
using namespace mozilla::java;
|
|
|
|
|
2017-05-05 11:40:29 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2017-08-30 09:14:32 +03:00
|
|
|
class HLSResourceCallbacksSupport
|
|
|
|
: public GeckoHLSResourceWrapper::Callbacks::Natives<
|
|
|
|
HLSResourceCallbacksSupport> {
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(HLSResourceCallbacksSupport)
|
|
|
|
public:
|
|
|
|
typedef GeckoHLSResourceWrapper::Callbacks::Natives<
|
|
|
|
HLSResourceCallbacksSupport>
|
|
|
|
NativeCallbacks;
|
|
|
|
using NativeCallbacks::AttachNative;
|
|
|
|
using NativeCallbacks::DisposeNative;
|
|
|
|
|
2018-03-27 17:51:32 +03:00
|
|
|
explicit HLSResourceCallbacksSupport(HLSDecoder* aResource);
|
2017-08-30 09:14:32 +03:00
|
|
|
void Detach();
|
|
|
|
void OnDataArrived();
|
|
|
|
void OnError(int aErrorCode);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~HLSResourceCallbacksSupport() {}
|
2017-11-03 09:22:41 +03:00
|
|
|
Mutex mMutex;
|
2017-08-30 09:14:32 +03:00
|
|
|
HLSDecoder* mDecoder;
|
|
|
|
};
|
|
|
|
|
|
|
|
HLSResourceCallbacksSupport::HLSResourceCallbacksSupport(HLSDecoder* aDecoder)
|
2017-11-03 09:22:41 +03:00
|
|
|
: mMutex("HLSResourceCallbacksSupport"), mDecoder(aDecoder) {
|
|
|
|
MOZ_ASSERT(mDecoder);
|
2017-08-30 09:14:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HLSResourceCallbacksSupport::Detach() {
|
2017-11-03 09:22:41 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-08-30 09:14:32 +03:00
|
|
|
mDecoder = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HLSResourceCallbacksSupport::OnDataArrived() {
|
2017-11-03 09:22:41 +03:00
|
|
|
HLS_DEBUG("HLSResourceCallbacksSupport", "OnDataArrived.");
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<HLSResourceCallbacksSupport> self = this;
|
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction(
|
|
|
|
"HLSResourceCallbacksSupport::OnDataArrived", [self]() -> void {
|
|
|
|
MutexAutoLock lock(self->mMutex);
|
|
|
|
if (self->mDecoder) {
|
|
|
|
self->mDecoder->NotifyDataArrived();
|
|
|
|
}
|
|
|
|
}));
|
2017-08-30 09:14:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HLSResourceCallbacksSupport::OnError(int aErrorCode) {
|
2017-11-03 09:22:41 +03:00
|
|
|
HLS_DEBUG("HLSResourceCallbacksSupport", "onError(%d)", aErrorCode);
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<HLSResourceCallbacksSupport> self = this;
|
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction(
|
|
|
|
"HLSResourceCallbacksSupport::OnDataArrived", [self]() -> void {
|
|
|
|
MutexAutoLock lock(self->mMutex);
|
|
|
|
if (self->mDecoder) {
|
|
|
|
// Since HLS source should be from the Internet, we treat all resource
|
|
|
|
// errors from GeckoHlsPlayer as network errors.
|
2017-11-17 06:07:30 +03:00
|
|
|
self->mDecoder->NetworkError(
|
|
|
|
MediaResult(NS_ERROR_FAILURE, "HLS error"));
|
2017-11-03 09:22:41 +03:00
|
|
|
}
|
|
|
|
}));
|
2017-08-30 09:14:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
HLSDecoder::HLSDecoder(MediaDecoderInit& aInit) : MediaDecoder(aInit) {}
|
2017-07-26 18:26:17 +03:00
|
|
|
|
2017-05-05 11:40:29 +03:00
|
|
|
MediaDecoderStateMachine* HLSDecoder::CreateStateMachine() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-07-13 10:13:12 +03:00
|
|
|
MediaFormatReaderInit init;
|
2017-07-06 11:59:22 +03:00
|
|
|
init.mVideoFrameContainer = GetVideoFrameContainer();
|
2017-07-12 10:37:02 +03:00
|
|
|
init.mKnowsCompositor = GetCompositor();
|
2017-07-13 05:19:36 +03:00
|
|
|
init.mCrashHelper = GetOwner()->CreateGMPCrashHelper();
|
2017-07-07 06:05:03 +03:00
|
|
|
init.mFrameStats = mFrameStats;
|
2017-08-31 12:31:51 +03:00
|
|
|
init.mMediaDecoderOwnerID = mOwner;
|
2017-08-30 09:14:32 +03:00
|
|
|
mReader = new MediaFormatReader(
|
|
|
|
init, new HLSDemuxer(mHLSResourceWrapper->GetPlayerId()));
|
2017-05-05 11:40:29 +03:00
|
|
|
|
|
|
|
return new MediaDecoderStateMachine(this, mReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HLSDecoder::IsEnabled() {
|
2018-03-20 01:48:56 +03:00
|
|
|
return StaticPrefs::MediaHlsEnabled() && (jni::GetAPIVersion() >= 16);
|
2017-05-05 11:40:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HLSDecoder::IsSupportedType(const MediaContainerType& aContainerType) {
|
2017-05-31 13:37:23 +03:00
|
|
|
return IsEnabled() && DecoderTraits::IsHttpLiveStreamingType(aContainerType);
|
2017-05-05 11:40:29 +03:00
|
|
|
}
|
|
|
|
|
2017-07-26 21:54:53 +03:00
|
|
|
nsresult HLSDecoder::Load(nsIChannel* aChannel) {
|
2017-06-30 05:11:42 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-12-06 11:00:20 +03:00
|
|
|
AbstractThread::AutoEnter context(AbstractMainThread());
|
2017-06-30 05:11:42 +03:00
|
|
|
|
2017-08-30 09:14:32 +03:00
|
|
|
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(mURI));
|
2017-06-30 05:11:42 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-08-30 09:14:32 +03:00
|
|
|
mChannel = aChannel;
|
|
|
|
nsCString spec;
|
|
|
|
Unused << mURI->GetSpec(spec);
|
2018-11-19 16:25:37 +03:00
|
|
|
;
|
2017-08-30 09:14:32 +03:00
|
|
|
HLSResourceCallbacksSupport::Init();
|
|
|
|
mJavaCallbacks = GeckoHLSResourceWrapper::Callbacks::New();
|
|
|
|
mCallbackSupport = new HLSResourceCallbacksSupport(this);
|
|
|
|
HLSResourceCallbacksSupport::AttachNative(mJavaCallbacks, mCallbackSupport);
|
|
|
|
mHLSResourceWrapper = java::GeckoHLSResourceWrapper::Create(
|
|
|
|
NS_ConvertUTF8toUTF16(spec), mJavaCallbacks);
|
|
|
|
MOZ_ASSERT(mHLSResourceWrapper);
|
2017-06-30 05:11:42 +03:00
|
|
|
|
|
|
|
rv = MediaShutdownManager::Instance().Register(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
|
|
|
|
|
2017-12-06 11:00:20 +03:00
|
|
|
GetStateMachine()->DispatchIsLiveStream(false);
|
|
|
|
|
2017-06-30 05:11:42 +03:00
|
|
|
return InitializeStateMachine();
|
|
|
|
}
|
|
|
|
|
2017-08-24 12:35:24 +03:00
|
|
|
void HLSDecoder::AddSizeOfResources(ResourceSizes* aSizes) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-08-30 09:14:32 +03:00
|
|
|
// TODO: track JAVA wrappers.
|
2017-08-24 12:35:24 +03:00
|
|
|
}
|
|
|
|
|
2017-08-24 11:58:06 +03:00
|
|
|
already_AddRefed<nsIPrincipal> HLSDecoder::GetCurrentPrincipal() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-07-30 11:07:34 +03:00
|
|
|
// Bug 1478843
|
|
|
|
return nullptr;
|
2017-08-24 11:58:06 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 12:47:54 +03:00
|
|
|
void HLSDecoder::Play() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
HLS_DEBUG("HLSDecoder", "MediaElement called Play");
|
2017-08-30 09:14:32 +03:00
|
|
|
mHLSResourceWrapper->Play();
|
2017-07-20 12:47:54 +03:00
|
|
|
return MediaDecoder::Play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HLSDecoder::Pause() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
HLS_DEBUG("HLSDecoder", "MediaElement called Pause");
|
2017-08-30 09:14:32 +03:00
|
|
|
mHLSResourceWrapper->Pause();
|
2017-07-20 12:47:54 +03:00
|
|
|
return MediaDecoder::Pause();
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:09:56 +03:00
|
|
|
void HLSDecoder::Suspend() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-08-30 09:14:32 +03:00
|
|
|
HLS_DEBUG("HLSDecoder", "Should suspend the resource fetching.");
|
|
|
|
mHLSResourceWrapper->Suspend();
|
2017-08-07 13:09:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HLSDecoder::Resume() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-08-30 09:14:32 +03:00
|
|
|
HLS_DEBUG("HLSDecoder", "Should resume the resource fetching.");
|
|
|
|
mHLSResourceWrapper->Resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HLSDecoder::Shutdown() {
|
|
|
|
HLS_DEBUG("HLSDecoder", "Shutdown");
|
|
|
|
if (mCallbackSupport) {
|
|
|
|
mCallbackSupport->Detach();
|
|
|
|
}
|
|
|
|
if (mHLSResourceWrapper) {
|
|
|
|
mHLSResourceWrapper->Destroy();
|
|
|
|
mHLSResourceWrapper = nullptr;
|
2017-08-07 13:09:56 +03:00
|
|
|
}
|
2017-08-30 09:14:32 +03:00
|
|
|
if (mJavaCallbacks) {
|
|
|
|
HLSResourceCallbacksSupport::DisposeNative(mJavaCallbacks);
|
|
|
|
mJavaCallbacks = nullptr;
|
|
|
|
}
|
|
|
|
MediaDecoder::Shutdown();
|
2017-08-07 13:09:56 +03:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:07:13 +03:00
|
|
|
void HLSDecoder::NotifyDataArrived() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-30 06:21:14 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
|
|
|
|
AbstractThread::AutoEnter context(AbstractMainThread());
|
2017-11-30 06:07:13 +03:00
|
|
|
NotifyReaderDataArrived();
|
2017-11-30 06:21:14 +03:00
|
|
|
GetOwner()->DownloadProgressed();
|
2017-11-30 06:07:13 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 11:40:29 +03:00
|
|
|
} // namespace mozilla
|