Bug 1350246 - [Part2] Implement a HLSResource as a MediaResource. draft
authorJames Cheng <jacheng@mozilla.com>
Fri, 05 May 2017 16:42:34 +0800
changeset 592554 c08f418676e12be06be7736594d8cb5cf6186df3
parent 592553 1c869788b0c254abea17f49e0f0e3da50a13c3f5
child 592555 2e4304f84cdf4257cac60921b0a3b679b9e832b7
push id63431
push userbmo:jacheng@mozilla.com
push dateMon, 12 Jun 2017 12:38:14 +0000
bugs1350246
milestone55.0a1
Bug 1350246 - [Part2] Implement a HLSResource as a MediaResource. MozReview-Commit-ID: BOH0AvScYfK
dom/media/hls/HLSResource.cpp
dom/media/hls/HLSResource.h
new file mode 100644
--- /dev/null
+++ b/dom/media/hls/HLSResource.cpp
@@ -0,0 +1,72 @@
+/* -*- 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 "HLSResource.h"
+#include "HLSUtils.h"
+
+using namespace mozilla::java;
+
+namespace mozilla {
+
+HlsResourceCallbacksSupport::HlsResourceCallbacksSupport(HLSResource* aResource)
+{
+  MOZ_ASSERT(aResource);
+  mResource = aResource;
+}
+
+void
+HlsResourceCallbacksSupport::OnDataArrived()
+{
+  MOZ_ASSERT(mResource);
+  mResource->onDataAvailable();
+}
+
+void
+HlsResourceCallbacksSupport::OnError(int aErrorCode)
+{
+  MOZ_ASSERT(mResource);
+}
+
+HLSResource::HLSResource(MediaResourceCallback* aCallback,
+                         nsIChannel* aChannel,
+                         nsIURI* aURI,
+                         const MediaContainerType& aContainerType)
+  : BaseMediaResource(aCallback, aChannel, aURI, aContainerType)
+{
+  nsCString spec;
+  nsresult rv = aURI->GetSpec(spec);
+  (void)rv;
+  HlsResourceCallbacksSupport::Init();
+  mJavaCallbacks = GeckoHlsResourceWrapper::HlsResourceCallbacks::New();
+  HlsResourceCallbacksSupport::AttachNative(mJavaCallbacks,
+                                            mozilla::MakeUnique<HlsResourceCallbacksSupport>(this));
+  mHlsResourceWrapper = java::GeckoHlsResourceWrapper::Create(NS_ConvertUTF8toUTF16(spec),
+                                                              mJavaCallbacks);
+  MOZ_ASSERT(mHlsResourceWrapper);
+}
+
+void
+HLSResource::onDataAvailable()
+{
+  MOZ_ASSERT(mCallback);
+  HLS_DEBUG("HLSResource", "onDataAvailable");
+  mCallback->NotifyDataArrived();
+}
+
+HLSResource::~HLSResource()
+{
+  if (mJavaCallbacks) {
+    HlsResourceCallbacksSupport::DisposeNative(mJavaCallbacks);
+    mJavaCallbacks = nullptr;
+  }
+  if (mHlsResourceWrapper) {
+    mHlsResourceWrapper->Destroy();
+    mHlsResourceWrapper = nullptr;
+  }
+  HLS_DEBUG("HLSResource", "Destroy");
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/media/hls/HLSResource.h
@@ -0,0 +1,128 @@
+/* -*- 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"
+
+#define UNIMPLEMENTED() HLS_DEBUG("HLSResource", "UNIMPLEMENTED FUNCTION")
+
+using namespace mozilla::java;
+
+namespace mozilla {
+
+class HLSResource;
+
+class HlsResourceCallbacksSupport
+  : public GeckoHlsResourceWrapper::HlsResourceCallbacks::Natives<HlsResourceCallbacksSupport>
+{
+public:
+  typedef GeckoHlsResourceWrapper::HlsResourceCallbacks::Natives<HlsResourceCallbacksSupport> NativeCallbacks;
+  using NativeCallbacks::DisposeNative;
+  using NativeCallbacks::AttachNative;
+
+  HlsResourceCallbacksSupport(HLSResource* aResource);
+  void OnDataArrived();
+  void OnError(int aErrorCode);
+
+private:
+  HLSResource* mResource;
+};
+
+class HLSResource final : public BaseMediaResource
+{
+public:
+  HLSResource(MediaResourceCallback* aCallback,
+              nsIChannel* aChannel,
+              nsIURI* aURI,
+              const MediaContainerType& aContainerType);
+  ~HLSResource();
+  nsresult Close() override { return NS_OK; }
+  void Suspend(bool aCloseImmediately) override { UNIMPLEMENTED(); }
+  void Resume() override { UNIMPLEMENTED(); }
+  bool CanClone() override { UNIMPLEMENTED(); return false; }
+  already_AddRefed<MediaResource> CloneData(MediaResourceCallback*) override { UNIMPLEMENTED(); return nullptr; }
+  void SetReadMode(MediaCacheStream::ReadMode aMode) override { UNIMPLEMENTED(); }
+  void SetPlaybackRate(uint32_t aBytesPerSecond) override  { UNIMPLEMENTED(); }
+  nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
+  bool ShouldCacheReads() override { UNIMPLEMENTED(); return false; }
+  int64_t Tell() override { UNIMPLEMENTED(); return -1; }
+  void Pin() override { UNIMPLEMENTED(); }
+  void Unpin() override { UNIMPLEMENTED(); }
+  double GetDownloadRate(bool* aIsReliable) override { UNIMPLEMENTED(); *aIsReliable = false; return 0; }
+  int64_t GetLength() override { UNIMPLEMENTED(); return -1; }
+  int64_t GetNextCachedData(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
+  int64_t GetCachedDataEnd(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
+  bool IsDataCachedToEndOfResource(int64_t aOffset) override { UNIMPLEMENTED(); return false; }
+  bool IsSuspendedByCache() override { UNIMPLEMENTED(); return false; }
+  bool IsSuspended() override { UNIMPLEMENTED(); return false; }
+  nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
+  nsresult Open(nsIStreamListener** aStreamListener) override { UNIMPLEMENTED(); return NS_OK; }
+
+  already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
+  {
+    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();
+  }
+
+  nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override
+  {
+    UNIMPLEMENTED();
+    return NS_OK;
+  }
+
+  bool IsTransportSeekable() override { return true; }
+
+  const MediaContainerType& GetContentType() const override { return mContainerType; }
+
+  bool IsLiveStream() override
+  {
+    return false;
+  }
+
+  bool IsExpectingMoreData() override
+  {
+    return false;
+  }
+
+  java::GeckoHlsResourceWrapper::GlobalRef GetResourceWrapper() {
+    return mHlsResourceWrapper;
+  }
+
+private:
+  friend class HlsResourceCallbacksSupport;
+
+  void onDataAvailable();
+
+  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
+  {
+    size_t size = MediaResource::SizeOfExcludingThis(aMallocSizeOf);
+    size += mContainerType.SizeOfExcludingThis(aMallocSizeOf);
+
+    return size;
+  }
+
+  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
+  {
+    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
+  }
+
+  java::GeckoHlsResourceWrapper::GlobalRef mHlsResourceWrapper;
+  java::GeckoHlsResourceWrapper::HlsResourceCallbacks::GlobalRef mJavaCallbacks;
+};
+
+} // namespace mozilla
+#endif /* HLSResource_h_ */