Bug 1341497 - Move WidevineBuffer and WidevineDecryptedBlock into WidevineUtils. r=gerald
authorChris Pearce <cpearce@mozilla.com>
Wed, 22 Feb 2017 15:55:38 +1300
changeset 373164 1bc86128b84e1927eae32ca33f77b2ab5eb93488
parent 373163 6a1aabfac7566ee9fecde00c92412b97fc73a6fc
child 373165 3cf38f4d73957eac7d71fae5fbecee1c9a4ac973
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgerald
bugs1341497
milestone54.0a1
Bug 1341497 - Move WidevineBuffer and WidevineDecryptedBlock into WidevineUtils. r=gerald This makes it easier to reuse in the ChromiumCDM code. Also add an ExtractBuffer() method, which allows us to Move() the contained nsTArray out without needing to copy the data. MozReview-Commit-ID: 9suJSfXTVYy
dom/media/gmp/widevine-adapter/WidevineDecryptor.cpp
dom/media/gmp/widevine-adapter/WidevineUtils.cpp
dom/media/gmp/widevine-adapter/WidevineUtils.h
dom/media/gmp/widevine-adapter/moz.build
--- a/dom/media/gmp/widevine-adapter/WidevineDecryptor.cpp
+++ b/dom/media/gmp/widevine-adapter/WidevineDecryptor.cpp
@@ -4,17 +4,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "WidevineDecryptor.h"
 
 #include "WidevineAdapter.h"
 #include "WidevineUtils.h"
 #include "WidevineFileIO.h"
 #include "GMPPlatform.h"
-#include <mozilla/SizePrintfMacros.h>
 #include <stdarg.h>
 #include "TimeUnits.h"
 
 using namespace cdm;
 using namespace std;
 
 namespace mozilla {
 
@@ -156,53 +155,16 @@ void
 WidevineDecryptor::SetServerCertificate(uint32_t aPromiseId,
                                         const uint8_t* aServerCert,
                                         uint32_t aServerCertSize)
 {
   CDM_LOG("Decryptor::SetServerCertificate()");
   CDM()->SetServerCertificate(aPromiseId, aServerCert, aServerCertSize);
 }
 
-class WidevineDecryptedBlock : public cdm::DecryptedBlock {
-public:
-
-  WidevineDecryptedBlock()
-    : mBuffer(nullptr)
-    , mTimestamp(0)
-  {
-  }
-
-  ~WidevineDecryptedBlock() override {
-    if (mBuffer) {
-      mBuffer->Destroy();
-      mBuffer = nullptr;
-    }
-  }
-
-  void SetDecryptedBuffer(cdm::Buffer* aBuffer) override {
-    mBuffer = aBuffer;
-  }
-
-  cdm::Buffer* DecryptedBuffer() override {
-    return mBuffer;
-  }
-
-  void SetTimestamp(int64_t aTimestamp) override {
-    mTimestamp = aTimestamp;
-  }
-
-  int64_t Timestamp() const override {
-    return mTimestamp;
-  }
-
-private:
-  cdm::Buffer* mBuffer;
-  int64_t mTimestamp;
-};
-
 cdm::Time
 WidevineDecryptor::ThrottleDecrypt(cdm::Time aWallTime, cdm::Time aSampleDuration)
 {
   const cdm::Time WindowSize = 1.0;
   const cdm::Time MaxThroughput = 2.0;
 
   // Forget decrypts that happened before the start of our window.
   while (!mDecrypts.empty() && mDecrypts.front().mWallTime < aWallTime - WindowSize) {
@@ -322,38 +284,16 @@ WidevineDecryptor::DecryptingComplete()
   // WidevineVideoDecoder, or a runnable), the CDMWrapper destroys
   // the CDM.
   mCDM = nullptr;
   sDecryptors.erase(mInstanceId);
   mCallback = nullptr;
   Release();
 }
 
-class WidevineBuffer : public cdm::Buffer {
-public:
-  explicit WidevineBuffer(size_t aSize) {
-    CDM_LOG("WidevineBuffer(size=%" PRIuSIZE ") created", aSize);
-    mBuffer.SetLength(aSize);
-  }
-  ~WidevineBuffer() override {
-    CDM_LOG("WidevineBuffer(size=%" PRIu32 ") destroyed", Size());
-  }
-  void Destroy() override { delete this; }
-  uint32_t Capacity() const override { return mBuffer.Length(); };
-  uint8_t* Data() override { return mBuffer.Elements(); }
-  void SetSize(uint32_t aSize) override { mBuffer.SetLength(aSize); }
-  uint32_t Size() const override { return mBuffer.Length(); }
-
-private:
-  WidevineBuffer(const WidevineBuffer&);
-  void operator=(const WidevineBuffer&);
-
-  nsTArray<uint8_t> mBuffer;
-};
-
 Buffer*
 WidevineDecryptor::Allocate(uint32_t aCapacity)
 {
   CDM_LOG("Decryptor::Allocate(capacity=%u)", aCapacity);
   return new WidevineBuffer(aCapacity);
 }
 
 class TimerTask : public GMPTask {
--- a/dom/media/gmp/widevine-adapter/WidevineUtils.cpp
+++ b/dom/media/gmp/widevine-adapter/WidevineUtils.cpp
@@ -1,19 +1,21 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 "WidevineUtils.h"
 #include "WidevineDecryptor.h"
+#include <mozilla/SizePrintfMacros.h>
 
 #include "gmp-api/gmp-errors.h"
 #include <stdarg.h>
 #include <stdio.h>
+#include <inttypes.h>
 
 namespace mozilla {
 
 namespace detail {
 LogModule* GetCDMLog()
 {
   static LazyLogModule sLog("CDM");
   return sLog;
@@ -71,9 +73,95 @@ CDMWrapper::CDMWrapper(cdm::ContentDecry
 
 CDMWrapper::~CDMWrapper()
 {
   CDM_LOG("CDMWrapper destroying CDM=%p", mCDM);
   mCDM->Destroy();
   mCDM = nullptr;
 }
 
+WidevineBuffer::WidevineBuffer(size_t aSize)
+{
+  CDM_LOG("WidevineBuffer(size=%" PRIuSIZE ") created", aSize);
+  mBuffer.SetLength(aSize);
+}
+
+WidevineBuffer::~WidevineBuffer()
+{
+  CDM_LOG("WidevineBuffer(size=%" PRIu32 ") destroyed", Size());
+}
+
+void
+WidevineBuffer::Destroy()
+{
+  delete this;
+}
+
+uint32_t
+WidevineBuffer::Capacity() const
+{
+  return mBuffer.Length();
+}
+
+uint8_t*
+WidevineBuffer::Data()
+{
+  return mBuffer.Elements();
+}
+
+void
+WidevineBuffer::SetSize(uint32_t aSize)
+{
+  mBuffer.SetLength(aSize);
+}
+
+uint32_t
+WidevineBuffer::Size() const
+{
+  return mBuffer.Length();
+}
+
+nsTArray<uint8_t>
+WidevineBuffer::ExtractBuffer() {
+  nsTArray<uint8_t> out;
+  out.SwapElements(mBuffer);
+  return out;
+}
+
+WidevineDecryptedBlock::WidevineDecryptedBlock()
+  : mBuffer(nullptr)
+  , mTimestamp(0)
+{
+}
+
+WidevineDecryptedBlock::~WidevineDecryptedBlock()
+{
+  if (mBuffer) {
+    mBuffer->Destroy();
+    mBuffer = nullptr;
+  }
+}
+
+void
+WidevineDecryptedBlock::SetDecryptedBuffer(cdm::Buffer* aBuffer)
+{
+  mBuffer = aBuffer;
+}
+
+cdm::Buffer*
+WidevineDecryptedBlock::DecryptedBuffer()
+{
+  return mBuffer;
+}
+
+void
+WidevineDecryptedBlock::SetTimestamp(int64_t aTimestamp)
+{
+  mTimestamp = aTimestamp;
+}
+
+int64_t
+WidevineDecryptedBlock::Timestamp() const
+{
+  return mTimestamp;
+}
+
 } // namespace mozilla
--- a/dom/media/gmp/widevine-adapter/WidevineUtils.h
+++ b/dom/media/gmp/widevine-adapter/WidevineUtils.h
@@ -56,11 +56,48 @@ private:
 
 void InitInputBuffer(const GMPEncryptedBufferMetadata* aCrypto,
                      int64_t aTimestamp,
                      const uint8_t* aData,
                      size_t aDataSize,
                      cdm::InputBuffer &aInputBuffer,
                      nsTArray<cdm::SubsampleEntry> &aSubsamples);
 
+class WidevineBuffer : public cdm::Buffer
+{
+public:
+  explicit WidevineBuffer(size_t aSize);
+  ~WidevineBuffer() override;
+  void Destroy() override;
+  uint32_t Capacity() const override;
+  uint8_t* Data() override;
+  void SetSize(uint32_t aSize) override;
+  uint32_t Size() const override;
+
+  // Moves contents of buffer out into temporary.
+  // Note: This empties the buffer.
+  nsTArray<uint8_t> ExtractBuffer();
+
+private:
+  nsTArray<uint8_t> mBuffer;
+  WidevineBuffer(const WidevineBuffer&);
+  void operator=(const WidevineBuffer&);
+};
+
+class WidevineDecryptedBlock : public cdm::DecryptedBlock
+{
+public:
+
+  WidevineDecryptedBlock();
+  ~WidevineDecryptedBlock() override;
+  void SetDecryptedBuffer(cdm::Buffer* aBuffer) override;
+  cdm::Buffer* DecryptedBuffer() override;
+  void SetTimestamp(int64_t aTimestamp) override;
+  int64_t Timestamp() const override;
+
+private:
+  cdm::Buffer* mBuffer;
+  int64_t mTimestamp;
+};
+
 } // namespace mozilla
 
 #endif // WidevineUtils_h_
--- a/dom/media/gmp/widevine-adapter/moz.build
+++ b/dom/media/gmp/widevine-adapter/moz.build
@@ -9,16 +9,21 @@ SOURCES += [
     'WidevineDecryptor.cpp',
     'WidevineDummyDecoder.cpp',
     'WidevineFileIO.cpp',
     'WidevineUtils.cpp',
     'WidevineVideoDecoder.cpp',
     'WidevineVideoFrame.cpp',
 ]
 
+EXPORTS += [
+    'WidevineDecryptor.h',
+    'WidevineUtils.h'
+]
+
 FINAL_LIBRARY = 'xul'
 
 LOCAL_INCLUDES += [
     '/dom/media/gmp',
 ]
 
 if CONFIG['CLANG_CXX']:
     CXXFLAGS += ['-Wno-error=shadow']