Bug 1395015 - Remove HLSResource. draft
authorJames Cheng <jacheng@mozilla.com>
Wed, 30 Aug 2017 14:14:32 +0800
changeset 655597 6672d6d30a04d8ff27cccdd033bcaa0c32e07bd2
parent 655385 db7f19e26e571ae1dd309f5d2f387b06ba670c30
child 728894 0cb609215905778b44f1d0c0e6b1a1c4c0da5060
push id76939
push userbmo:jacheng@mozilla.com
push dateWed, 30 Aug 2017 08:12:35 +0000
bugs1395015
milestone57.0a1
Bug 1395015 - Remove HLSResource. Move the dependent method into HLSDecoder. Remove the class entirely. MozReview-Commit-ID: F9eOFQvgeLQ
dom/media/hls/HLSDecoder.cpp
dom/media/hls/HLSDecoder.h
dom/media/hls/HLSResource.cpp
dom/media/hls/HLSResource.h
dom/media/hls/moz.build
--- a/dom/media/hls/HLSDecoder.cpp
+++ b/dom/media/hls/HLSDecoder.cpp
@@ -1,54 +1,105 @@
 /* -*- 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"
+#include "GeneratedJNINatives.h"
+#include "GeneratedJNIWrappers.h"
 #include "HLSDemuxer.h"
-#include "HLSResource.h"
 #include "HLSUtils.h"
 #include "MediaContainerType.h"
 #include "MediaDecoderStateMachine.h"
 #include "MediaFormatReader.h"
 #include "MediaPrefs.h"
 #include "MediaShutdownManager.h"
+#include "nsContentUtils.h"
 #include "nsNetUtil.h"
 
+using namespace mozilla::java;
+
 namespace mozilla {
 
+class HLSResourceCallbacksSupport
+  : public GeckoHLSResourceWrapper::Callbacks::Natives<HLSResourceCallbacksSupport>
+{
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(HLSResourceCallbacksSupport)
+public:
+  typedef GeckoHLSResourceWrapper::Callbacks::Natives<HLSResourceCallbacksSupport> NativeCallbacks;
+  using NativeCallbacks::DisposeNative;
+  using NativeCallbacks::AttachNative;
+
+  HLSResourceCallbacksSupport(HLSDecoder* aResource);
+  void Detach();
+  void OnDataArrived();
+  void OnError(int aErrorCode);
+
+private:
+  ~HLSResourceCallbacksSupport() {}
+  HLSDecoder* mDecoder;
+};
+
+HLSResourceCallbacksSupport::HLSResourceCallbacksSupport(HLSDecoder* aDecoder)
+{
+  MOZ_ASSERT(aDecoder);
+  mDecoder = aDecoder;
+}
+
 void
-HLSDecoder::Shutdown()
+HLSResourceCallbacksSupport::Detach()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (mResource) {
-    mResource->Detach();
+  mDecoder = nullptr;
+}
+
+void
+HLSResourceCallbacksSupport::OnDataArrived()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  if (mDecoder) {
+    HLS_DEBUG("HLSResourceCallbacksSupport", "OnDataArrived");
+    mDecoder->NotifyDataArrived();
   }
-  MediaDecoder::Shutdown();
+}
+
+void
+HLSResourceCallbacksSupport::OnError(int aErrorCode)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  if (mDecoder) {
+    HLS_DEBUG("HLSResourceCallbacksSupport", "onError(%d)", aErrorCode);
+    // Since HLS source should be from the Internet, we treat all resource errors
+    // from GeckoHlsPlayer as network errors.
+    mDecoder->NetworkError();
+  }
+}
+
+HLSDecoder::HLSDecoder(MediaDecoderInit& aInit)
+  : MediaDecoder(aInit)
+{
 }
 
 MediaDecoderStateMachine*
 HLSDecoder::CreateStateMachine()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  MOZ_ASSERT(mResource);
-  auto resourceWrapper = mResource->GetResourceWrapper();
-  MOZ_ASSERT(resourceWrapper);
   MediaFormatReaderInit init;
   init.mVideoFrameContainer = GetVideoFrameContainer();
   init.mKnowsCompositor = GetCompositor();
   init.mCrashHelper = GetOwner()->CreateGMPCrashHelper();
   init.mFrameStats = mFrameStats;
   mReader =
-    new MediaFormatReader(init, new HLSDemuxer(resourceWrapper->GetPlayerId()));
+    new MediaFormatReader(init, new HLSDemuxer(mHLSResourceWrapper->GetPlayerId()));
 
   return new MediaDecoderStateMachine(this, mReader);
 }
 
 bool
 HLSDecoder::IsEnabled()
 {
   return MediaPrefs::HLSEnabled() && (jni::GetAPIVersion() >= 16);
@@ -60,84 +111,110 @@ HLSDecoder::IsSupportedType(const MediaC
   return IsEnabled() &&
          DecoderTraits::IsHttpLiveStreamingType(aContainerType);
 }
 
 nsresult
 HLSDecoder::Load(nsIChannel* aChannel)
 {
   MOZ_ASSERT(NS_IsMainThread());
-  MOZ_ASSERT(!mResource);
 
-  nsCOMPtr<nsIURI> uri;
-  nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
+  nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(mURI));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  mResource = MakeUnique<HLSResource>(this, aChannel, uri);
+  mChannel = aChannel;
+  nsCString spec;
+  Unused << mURI->GetSpec(spec);;
+  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);
 
   rv = MediaShutdownManager::Instance().Register(this);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   SetStateMachine(CreateStateMachine());
   NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
 
   return InitializeStateMachine();
 }
 
 void
 HLSDecoder::AddSizeOfResources(ResourceSizes* aSizes)
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (mResource) {
-    aSizes->mByteSize += mResource->SizeOfIncludingThis(aSizes->mMallocSizeOf);
-  }
+  // TODO: track JAVA wrappers.
 }
 
 already_AddRefed<nsIPrincipal>
 HLSDecoder::GetCurrentPrincipal()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  return mResource ? mResource->GetCurrentPrincipal() : nullptr;
+  nsCOMPtr<nsIPrincipal> principal;
+  nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
+  if (!secMan || !mChannel) {
+    return nullptr;
+  }
+  secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(principal));
+  return principal.forget();
 }
 
 nsresult
 HLSDecoder::Play()
 {
   MOZ_ASSERT(NS_IsMainThread());
   HLS_DEBUG("HLSDecoder", "MediaElement called Play");
-  auto resourceWrapper = mResource->GetResourceWrapper();
-  resourceWrapper->Play();
+  mHLSResourceWrapper->Play();
   return MediaDecoder::Play();
 }
 
 void
 HLSDecoder::Pause()
 {
   MOZ_ASSERT(NS_IsMainThread());
   HLS_DEBUG("HLSDecoder", "MediaElement called Pause");
-  auto resourceWrapper = mResource->GetResourceWrapper();
-  resourceWrapper->Pause();
+  mHLSResourceWrapper->Pause();
   return MediaDecoder::Pause();
 }
 
 void
 HLSDecoder::Suspend()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (mResource) {
-    mResource->Suspend();
-  }
+  HLS_DEBUG("HLSDecoder", "Should suspend the resource fetching.");
+  mHLSResourceWrapper->Suspend();
 }
 
 void
 HLSDecoder::Resume()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (mResource) {
-    mResource->Resume();
+  HLS_DEBUG("HLSDecoder", "Should resume the resource fetching.");
+  mHLSResourceWrapper->Resume();
+}
+
+void
+HLSDecoder::Shutdown()
+{
+  HLS_DEBUG("HLSDecoder", "Shutdown");
+  if (mCallbackSupport) {
+    mCallbackSupport->Detach();
+    mCallbackSupport = nullptr;
   }
+  if (mHLSResourceWrapper) {
+    mHLSResourceWrapper->Destroy();
+    mHLSResourceWrapper = nullptr;
+  }
+  if (mJavaCallbacks) {
+    HLSResourceCallbacksSupport::DisposeNative(mJavaCallbacks);
+    mJavaCallbacks = nullptr;
+  }
+  MediaDecoder::Shutdown();
 }
 
 } // namespace mozilla
--- a/dom/media/hls/HLSDecoder.h
+++ b/dom/media/hls/HLSDecoder.h
@@ -2,31 +2,27 @@
 /* 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/. */
 
 #ifndef HLSDecoder_h_
 #define HLSDecoder_h_
 
-#include "HLSResource.h"
 #include "MediaDecoder.h"
 
 namespace mozilla {
 
+class HLSResourceCallbacksSupport;
+
 class HLSDecoder final : public MediaDecoder
 {
 public:
   // MediaDecoder interface.
-  explicit HLSDecoder(MediaDecoderInit& aInit)
-    : MediaDecoder(aInit)
-  {
-  }
-
-  void Shutdown() override;
+  explicit HLSDecoder(MediaDecoderInit& aInit);
 
   // Returns true if the HLS backend is pref'ed on.
   static bool IsEnabled();
 
   // Returns true if aContainerType is an HLS type that we think we can render
   // with the a platform decoder backend.
   // If provided, codecs are checked for support.
   static bool IsSupportedType(const MediaContainerType& aContainerType);
@@ -37,30 +33,37 @@ public:
 
   void Pause() override;
 
   void AddSizeOfResources(ResourceSizes* aSizes) override;
   already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override;
   bool IsTransportSeekable() override { return true; }
   void Suspend() override;
   void Resume() override;
+  void Shutdown() override;
 
 private:
+  friend class HLSResourceCallbacksSupport;
+
   void PinForSeek() override {}
   void UnpinForSeek() override {}
 
   MediaDecoderStateMachine* CreateStateMachine();
 
   bool CanPlayThroughImpl() override final
   {
     // TODO: We don't know how to estimate 'canplaythrough' for this decoder.
     // For now we just return true for 'autoplay' can work.
     return true;
   }
 
   bool IsLiveStream() override final { return false; }
 
-  UniquePtr<HLSResource> mResource;
+  nsCOMPtr<nsIChannel> mChannel;
+  nsCOMPtr<nsIURI> mURI;
+  java::GeckoHLSResourceWrapper::GlobalRef mHLSResourceWrapper;
+  java::GeckoHLSResourceWrapper::Callbacks::GlobalRef mJavaCallbacks;
+  RefPtr<HLSResourceCallbacksSupport> mCallbackSupport;
 };
 
 } // namespace mozilla
 
 #endif /* HLSDecoder_h_ */
deleted file mode 100644
--- a/dom/media/hls/HLSResource.cpp
+++ /dev/null
@@ -1,117 +0,0 @@
-/* -*- 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 "HLSResource.h"
-#include "HLSUtils.h"
-
-using namespace mozilla::java;
-
-namespace mozilla {
-
-HLSResourceCallbacksSupport::HLSResourceCallbacksSupport(HLSResource* aResource)
-{
-  MOZ_ASSERT(aResource);
-  mResource = aResource;
-}
-
-void
-HLSResourceCallbacksSupport::Detach()
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  mResource = nullptr;
-}
-
-void
-HLSResourceCallbacksSupport::OnDataArrived()
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  if (mResource) {
-    mResource->onDataAvailable();
-  }
-}
-
-void
-HLSResourceCallbacksSupport::OnError(int aErrorCode)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  if (mResource) {
-    mResource->onError(aErrorCode);
-  }
-}
-
-HLSResource::HLSResource(HLSDecoder* aDecoder,
-                         nsIChannel* aChannel,
-                         nsIURI* aURI)
-  : mDecoder(aDecoder)
-  , mChannel(aChannel)
-  , mURI(aURI)
-{
-  nsCString spec;
-  nsresult rv = aURI->GetSpec(spec);
-  (void)rv;
-  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);
-}
-
-void
-HLSResource::onDataAvailable()
-{
-  HLS_DEBUG("HLSResource", "onDataAvailable");
-  if (mDecoder) {
-    mDecoder->NotifyDataArrived();
-  }
-}
-
-void
-HLSResource::onError(int aErrorCode)
-{
-  HLS_DEBUG("HLSResource", "onError(%d)", aErrorCode);
-  // Since HLS source should be from the Internet, we treat all resource errors
-  // from GeckoHlsPlayer as network errors.
-  if (mDecoder) {
-    mDecoder->NetworkError();
-  }
-}
-
-void
-HLSResource::Suspend()
-{
-  MOZ_ASSERT(NS_IsMainThread(), "Don't call on non-main thread");
-  HLS_DEBUG("HLSResource", "Should suspend the resource fetching.");
-  mHLSResourceWrapper->Suspend();
-}
-
-void HLSResource::Resume()
-{
-  MOZ_ASSERT(NS_IsMainThread(), "Don't call on non-main thread");
-  HLS_DEBUG("HLSResource", "Should resume the resource fetching.");
-  mHLSResourceWrapper->Resume();
-}
-
-HLSResource::~HLSResource()
-{
-  HLS_DEBUG("HLSResource", "~HLSResource()");
-  if (mCallbackSupport) {
-    mCallbackSupport->Detach();
-    mCallbackSupport = nullptr;
-  }
-  if (mHLSResourceWrapper) {
-    mHLSResourceWrapper->Destroy();
-    mHLSResourceWrapper = nullptr;
-  }
-  if (mJavaCallbacks) {
-      HLSResourceCallbacksSupport::DisposeNative(mJavaCallbacks);
-      mJavaCallbacks = nullptr;
-  }
-}
-
-} // namespace mozilla
deleted file mode 100644
--- a/dom/media/hls/HLSResource.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/* -*- 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/. */
-
-#ifndef HLSResource_h_
-#define HLSResource_h_
-
-#include "GeneratedJNINatives.h"
-#include "GeneratedJNIWrappers.h"
-#include "HLSUtils.h"
-#include "nsContentUtils.h"
-
-using namespace mozilla::java;
-
-namespace mozilla {
-
-class HLSDecoder;
-class HLSResource;
-
-class HLSResourceCallbacksSupport
-  : public GeckoHLSResourceWrapper::Callbacks::Natives<HLSResourceCallbacksSupport>
-{
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(HLSResourceCallbacksSupport)
-public:
-  typedef GeckoHLSResourceWrapper::Callbacks::Natives<HLSResourceCallbacksSupport> NativeCallbacks;
-  using NativeCallbacks::DisposeNative;
-  using NativeCallbacks::AttachNative;
-
-  HLSResourceCallbacksSupport(HLSResource* aResource);
-  void Detach();
-  void OnDataArrived();
-  void OnError(int aErrorCode);
-
-private:
-  ~HLSResourceCallbacksSupport() {}
-  HLSResource* mResource;
-};
-
-class HLSResource final
-{
-public:
-  HLSResource(HLSDecoder* aDecoder, nsIChannel* aChannel, nsIURI* aURI);
-  ~HLSResource();
-  void Suspend();
-  void Resume();
-
-  already_AddRefed<nsIPrincipal> GetCurrentPrincipal()
-  {
-    NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
-
-    nsCOMPtr<nsIPrincipal> principal;
-    nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
-    if (!secMan || !mChannel)
-      return nullptr;
-    secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(principal));
-    return principal.forget();
-  }
-
-  java::GeckoHLSResourceWrapper::GlobalRef GetResourceWrapper() {
-    return mHLSResourceWrapper;
-  }
-
-  void Detach() { mDecoder = nullptr; }
-
-  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
-  {
-    // TODO: track JAVA wrappers.
-    return 0;
-  }
-
-  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
-  {
-    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
-  }
-
-private:
-  friend class HLSResourceCallbacksSupport;
-
-  void onDataAvailable();
-  void onError(int aErrorCode);
-
-  HLSDecoder* mDecoder;
-  nsCOMPtr<nsIChannel> mChannel;
-  nsCOMPtr<nsIURI> mURI;
-  java::GeckoHLSResourceWrapper::GlobalRef mHLSResourceWrapper;
-  java::GeckoHLSResourceWrapper::Callbacks::GlobalRef mJavaCallbacks;
-  RefPtr<HLSResourceCallbacksSupport> mCallbackSupport;
-};
-
-} // namespace mozilla
-#endif /* HLSResource_h_ */
--- a/dom/media/hls/moz.build
+++ b/dom/media/hls/moz.build
@@ -2,24 +2,22 @@
 # 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/.
 
 
 EXPORTS += [
     'HLSDecoder.h',
     'HLSDemuxer.h',
-    'HLSResource.h',
     'HLSUtils.h',
 ]
 
 UNIFIED_SOURCES += [
     'HLSDecoder.cpp',
     'HLSDemuxer.cpp',
-    'HLSResource.cpp',
     'HLSUtils.cpp',
 ]
 
 include('/ipc/chromium/chromium-config.mozbuild')
 
 FINAL_LIBRARY = 'xul'
 
 if CONFIG['GNU_CXX']: