Bug 1425277 - p7: add apple encoder module and factory. r=jya
authorJohn Lin <jolin@mozilla.com>
Mon, 26 Nov 2018 18:29:08 +0000
changeset 448077 8759840bbdd3ea0285df853f866463a52f4a8597
parent 448076 b86195e2307af27bfe09b6b9b7447f0ac1f718f5
child 448078 74bdaf9e17c01ca9ee6748ee9ddf530776800428
push id35105
push userrmaries@mozilla.com
push dateTue, 27 Nov 2018 09:41:18 +0000
treeherdermozilla-central@b0e69b136883 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1425277
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1425277 - p7: add apple encoder module and factory. r=jya Differential Revision: https://phabricator.services.mozilla.com/D7563
dom/media/platforms/PEMFactory.cpp
dom/media/platforms/PEMFactory.h
dom/media/platforms/apple/AppleEncoderModule.cpp
dom/media/platforms/apple/AppleEncoderModule.h
dom/media/platforms/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/PEMFactory.cpp
@@ -0,0 +1,55 @@
+/* -*- 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 "PEMFactory.h"
+
+#ifdef MOZ_APPLEMEDIA
+#include "AppleEncoderModule.h"
+#endif
+
+namespace mozilla {
+
+PEMFactory::PEMFactory() {
+#ifdef MOZ_APPLEMEDIA
+  RefPtr<PlatformEncoderModule> m(new AppleEncoderModule());
+  mModules.AppendElement(m);
+#endif
+}
+
+bool PEMFactory::SupportsMimeType(const nsACString& aMimeType) const {
+  for (auto m : mModules) {
+    if (m->SupportsMimeType(aMimeType)) {
+      return true;
+    }
+  }
+  return false;
+}
+
+already_AddRefed<MediaDataEncoder> PEMFactory::CreateEncoder(
+    const CreateEncoderParams& aParams) {
+  const TrackInfo& info = aParams.mConfig;
+  RefPtr<PlatformEncoderModule> m = FindPEM(info);
+  if (!m) {
+    return nullptr;
+  }
+
+  return info.IsVideo() ? m->CreateVideoEncoder(aParams) : nullptr;
+}
+
+already_AddRefed<PlatformEncoderModule> PEMFactory::FindPEM(
+    const TrackInfo& aTrackInfo) const {
+  RefPtr<PlatformEncoderModule> found;
+  for (auto m : mModules) {
+    if (m->SupportsMimeType(aTrackInfo.mMimeType)) {
+      found = m;
+      break;
+    }
+  }
+
+  return found.forget();
+}
+
+}  // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/PEMFactory.h
@@ -0,0 +1,40 @@
+/* -*- 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/. */
+
+#if !defined(PEMFactory_h_)
+#define PEMFactory_h_
+
+#include "PlatformEncoderModule.h"
+
+namespace mozilla {
+
+class PEMFactory final {
+ public:
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PEMFactory)
+
+  PEMFactory();
+
+  // Factory method that creates the appropriate PlatformEncoderModule for
+  // the platform we're running on. Caller is responsible for deleting this
+  // instance. It's expected that there will be multiple
+  // PlatformEncoderModules alive at the same time.
+  already_AddRefed<MediaDataEncoder> CreateEncoder(
+      const CreateEncoderParams& aParams);
+
+  bool SupportsMimeType(const nsACString& aMimeType) const;
+
+ private:
+  virtual ~PEMFactory() {}
+  // Returns the first PEM in our list supporting the mimetype.
+  already_AddRefed<PlatformEncoderModule> FindPEM(
+      const TrackInfo& aTrackInfo) const;
+
+  nsTArray<RefPtr<PlatformEncoderModule>> mModules;
+};
+
+}  // namespace mozilla
+
+#endif /* PEMFactory_h_ */
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/apple/AppleEncoderModule.cpp
@@ -0,0 +1,38 @@
+/* -*- 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 "AppleEncoderModule.h"
+
+#include "nsMimeTypes.h"
+
+#include "AppleVTEncoder.h"
+
+namespace mozilla {
+
+bool AppleEncoderModule::SupportsMimeType(const nsACString& aMimeType) const {
+  return aMimeType.EqualsLiteral(VIDEO_MP4) ||
+         aMimeType.EqualsLiteral("video/avc");
+}
+
+already_AddRefed<MediaDataEncoder> AppleEncoderModule::CreateVideoEncoder(
+    const CreateEncoderParams& aParams) const {
+  const VideoInfo* info = aParams.mConfig.GetAsVideoInfo();
+  MOZ_ASSERT(info);
+
+  using Config = AppleVTEncoder::Config;
+  Config config =
+      Config(MediaDataEncoder::CodecType::H264, aParams.mUsage, info->mImage,
+             aParams.mPixelFormat, aParams.mFramerate, aParams.mBitrate);
+  if (aParams.mCodecSpecific) {
+    config.SetCodecSpecific(aParams.mCodecSpecific.ref().mH264);
+  }
+
+  RefPtr<MediaDataEncoder> encoder(
+      new AppleVTEncoder(std::forward<Config>(config), aParams.mTaskQueue));
+  return encoder.forget();
+}
+
+}  // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/apple/AppleEncoderModule.h
@@ -0,0 +1,26 @@
+/* -*- 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 AppleEncoderModule_h_
+#define AppleEncoderModule_h_
+
+#include "PlatformEncoderModule.h"
+
+namespace mozilla {
+class AppleEncoderModule final : public PlatformEncoderModule {
+ public:
+  AppleEncoderModule() {}
+  virtual ~AppleEncoderModule() {}
+
+  bool SupportsMimeType(const nsACString& aMimeType) const override;
+
+  already_AddRefed<MediaDataEncoder> CreateVideoEncoder(
+      const CreateEncoderParams& aParams) const override;
+};
+
+}  // namespace mozilla
+
+#endif /* AppleEncoderModule_h_ */
--- a/dom/media/platforms/moz.build
+++ b/dom/media/platforms/moz.build
@@ -9,16 +9,17 @@ EXPORTS += [
     'agnostic/DummyMediaDataDecoder.h',
     'agnostic/OpusDecoder.h',
     'agnostic/TheoraDecoder.h',
     'agnostic/VorbisDecoder.h',
     'agnostic/VPXDecoder.h',
     'AllocationPolicy.h',
     'MediaTelemetryConstants.h',
     'PDMFactory.h',
+    'PEMFactory.h',
     'PlatformDecoderModule.h',
     'PlatformEncoderModule.h',
     'ReorderQueue.h',
     'SimpleMap.h',
     'wrappers/MediaChangeMonitor.h',
     'wrappers/MediaDataDecoderProxy.h'
 ]
 
@@ -29,16 +30,17 @@ UNIFIED_SOURCES += [
     'agnostic/NullDecoderModule.cpp',
     'agnostic/OpusDecoder.cpp',
     'agnostic/TheoraDecoder.cpp',
     'agnostic/VorbisDecoder.cpp',
     'agnostic/VPXDecoder.cpp',
     'agnostic/WAVDecoder.cpp',
     'AllocationPolicy.cpp',
     'PDMFactory.cpp',
+    'PEMFactory.cpp',
     'wrappers/MediaChangeMonitor.cpp',
     'wrappers/MediaDataDecoderProxy.cpp'
 ]
 
 DIRS += [
     'agnostic/bytestreams',
     'agnostic/eme',
     'agnostic/gmp',
@@ -81,20 +83,22 @@ if CONFIG['MOZ_OMX']:
     ]
     UNIFIED_SOURCES += [
         'omx/OmxCoreLibLinker.cpp',
     ]
 
 if CONFIG['MOZ_APPLEMEDIA']:
   EXPORTS += [
       'apple/AppleDecoderModule.h',
+      'apple/AppleEncoderModule.h',
   ]
   UNIFIED_SOURCES += [
       'apple/AppleATDecoder.cpp',
       'apple/AppleDecoderModule.cpp',
+      'apple/AppleEncoderModule.cpp',
       'apple/AppleVTDecoder.cpp',
       'apple/AppleVTEncoder.cpp',
   ]
   LOCAL_INCLUDES += [
       '/media/libyuv/libyuv/include',
   ]
   OS_LIBS += [
       '-framework AudioToolbox',