Bug 1278198 - Add Widevine FileIO. r?gerald draft
authorChris Pearce <cpearce@mozilla.com>
Thu, 07 Jul 2016 17:28:14 +1200
changeset 388814 f6ab00f364b94e5b9890d4fcb30a34cf26b6b461
parent 388813 aba40aeae84cb45c333c4fc4baca6ffe81f3b2a6
child 388815 c1c5c08d3551e06bf422f3d124d9eceb546ce361
push id23239
push usercpearce@mozilla.com
push dateMon, 18 Jul 2016 02:10:32 +0000
reviewersgerald
bugs1278198
milestone50.0a1
Bug 1278198 - Add Widevine FileIO. r?gerald MozReview-Commit-ID: IXgSobtF24L
dom/media/gmp/widevine-adapter/WidevineFileIO.cpp
dom/media/gmp/widevine-adapter/WidevineFileIO.h
dom/media/gmp/widevine-adapter/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/gmp/widevine-adapter/WidevineFileIO.cpp
@@ -0,0 +1,99 @@
+#include "WidevineFileIO.h"
+#include "WidevineUtils.h"
+#include "WidevineAdapter.h"
+
+#include <string>
+
+using namespace cdm;
+
+namespace mozilla {
+
+void
+WidevineFileIO::Open(const char* aFilename, uint32_t aFilenameLength)
+{
+  mName = std::string(aFilename, aFilename + aFilenameLength);
+  GMPRecord* record = nullptr;
+  GMPErr err = GMPCreateRecord(aFilename, aFilenameLength, &record, static_cast<GMPRecordClient*>(this));
+  if (GMP_FAILED(err)) {
+    Log("WidevineFileIO::Open() '%s' GMPCreateRecord failed", mName.c_str());
+    mClient->OnOpenComplete(FileIOClient::kError);
+    return;
+  }
+  if (GMP_FAILED(record->Open())) {
+    Log("WidevineFileIO::Open() '%s' record open failed", mName.c_str());
+    mClient->OnOpenComplete(FileIOClient::kError);
+    return;
+  }
+
+  Log("WidevineFileIO::Open() '%s'", mName.c_str());
+  mRecord = record;
+}
+
+void
+WidevineFileIO::Read()
+{
+  if (!mRecord) {
+    Log("WidevineFileIO::Read() '%s' used uninitialized!", mName.c_str());
+    mClient->OnReadComplete(FileIOClient::kError, nullptr, 0);
+    return;
+  }
+  Log("WidevineFileIO::Read() '%s'", mName.c_str());
+  mRecord->Read();
+}
+
+void
+WidevineFileIO::Write(const uint8_t* aData, uint32_t aDataSize)
+{
+  if (!mRecord) {
+    Log("WidevineFileIO::Write() '%s' used uninitialized!", mName.c_str());
+    mClient->OnWriteComplete(FileIOClient::kError);
+    return;
+  }
+  mRecord->Write(aData, aDataSize);
+}
+
+void
+WidevineFileIO::Close()
+{
+  Log("WidevineFileIO::Close() '%s'", mName.c_str());
+  if (mRecord) {
+    mRecord->Close();
+    mRecord = nullptr;
+  }
+  delete this;
+}
+
+static FileIOClient::Status
+GMPToWidevineFileStatus(GMPErr aStatus)
+{
+  switch (aStatus) {
+    case GMPRecordInUse: return FileIOClient::kInUse;
+    case GMPNoErr: return FileIOClient::kSuccess;
+    default: return FileIOClient::kError;
+  }
+}
+
+void
+WidevineFileIO::OpenComplete(GMPErr aStatus)
+{
+  Log("WidevineFileIO::OpenComplete() '%s' status=%d", mName.c_str(), aStatus);
+  mClient->OnOpenComplete(GMPToWidevineFileStatus(aStatus));
+}
+
+void
+WidevineFileIO::ReadComplete(GMPErr aStatus,
+                             const uint8_t* aData,
+                             uint32_t aDataSize)
+{
+  Log("WidevineFileIO::OnReadComplete() '%s' status=%d", mName.c_str(), aStatus);
+  mClient->OnReadComplete(GMPToWidevineFileStatus(aStatus), aData, aDataSize);
+}
+
+void
+WidevineFileIO::WriteComplete(GMPErr aStatus)
+{
+  Log("WidevineFileIO::WriteComplete() '%s' status=%d", mName.c_str(), aStatus);
+  mClient->OnWriteComplete(GMPToWidevineFileStatus(aStatus));
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/media/gmp/widevine-adapter/WidevineFileIO.h
@@ -0,0 +1,46 @@
+/* -*- 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/. */
+
+#ifndef WidevineFileIO_h_
+#define WidevineFileIO_h_
+
+#include <stddef.h>
+#include "content_decryption_module.h"
+#include "gmp-api/gmp-storage.h"
+#include <string>
+
+namespace mozilla {
+
+class WidevineFileIO : public cdm::FileIO
+                     , public GMPRecordClient
+{
+public:
+  explicit WidevineFileIO(cdm::FileIOClient* aClient)
+    : mClient(aClient)
+    , mRecord(nullptr)
+  {}
+
+  // cdm::FileIO
+  void Open(const char* aFilename, uint32_t aFilenameLength) override;
+  void Read() override;
+  void Write(const uint8_t* aData, uint32_t aDataSize) override;
+  void Close() override;
+
+  // GMPRecordClient
+  void OpenComplete(GMPErr aStatus) override;
+  void ReadComplete(GMPErr aStatus,
+                    const uint8_t* aData,
+                    uint32_t aDataSize) override;
+  void WriteComplete(GMPErr aStatus) override;
+
+private:
+  cdm::FileIOClient* mClient;
+  GMPRecord* mRecord;
+  std::string mName;
+};
+
+} // namespace mozilla
+
+#endif // WidevineFileIO_h_
\ No newline at end of file
--- a/dom/media/gmp/widevine-adapter/moz.build
+++ b/dom/media/gmp/widevine-adapter/moz.build
@@ -2,16 +2,17 @@
 # vim: set filetype=python:
 # 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/.
 
 SOURCES += [
     'WidevineAdapter.cpp',
     'WidevineDecryptor.cpp',
+    'WidevineFileIO.cpp',
     'WidevineUtils.cpp',
     'WidevineVideoDecoder.cpp',
     'WidevineVideoFrame.cpp',
 ]
 
 FINAL_LIBRARY = 'xul'
 
 LOCAL_INCLUDES += [