Bug 1336792 - part 1: extract DurationMap and make it thread safe. r?jya draft
authorJohn Lin <jolin@mozilla.com>
Fri, 10 Feb 2017 11:39:18 +0800
changeset 481657 55baa5ec01af16d12233d5b7cc0919ce058341d6
parent 481645 a9e69e8269851726a9967d0cd89efd4de6eadade
child 481658 ca32ae36352f5aa67fcdf9f75a56bc893264e1ab
push id44897
push userbmo:jolin@mozilla.com
push dateFri, 10 Feb 2017 09:25:54 +0000
reviewersjya
bugs1336792
milestone54.0a1
Bug 1336792 - part 1: extract DurationMap and make it thread safe. r?jya MozReview-Commit-ID: EGSc5sMfxvL
dom/media/platforms/DurationMap.h
dom/media/platforms/ffmpeg/FFmpegVideoDecoder.h
dom/media/platforms/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/platforms/DurationMap.h
@@ -0,0 +1,56 @@
+/* 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 mozilla_DurationMap_h
+#define mozilla_DurationMap_h
+
+#include "mozilla/Pair.h"
+#include "nsTArray.h"
+
+namespace mozilla {
+
+class DurationMap
+{
+public:
+  typedef Pair<int64_t, int64_t> DurationElement;
+
+  DurationMap() : mMutex("DurationMap") { }
+
+  // Insert Key and Duration pair at the end of our map.
+  void Insert(int64_t aKey, int64_t aDuration)
+  {
+    MutexAutoLock lock(mMutex);
+    mMap.AppendElement(MakePair(aKey, aDuration));
+  }
+  // Sets aDuration matching aKey and remove it from the map if found.
+  // The element returned is the first one found.
+  // Returns true if found, false otherwise.
+  bool Find(int64_t aKey, int64_t& aDuration)
+  {
+    MutexAutoLock lock(mMutex);
+    for (uint32_t i = 0; i < mMap.Length(); i++) {
+      DurationElement& element = mMap[i];
+      if (element.first() == aKey) {
+        aDuration = element.second();
+        mMap.RemoveElementAt(i);
+        return true;
+      }
+    }
+    return false;
+  }
+  // Remove all elements of the map.
+  void Clear()
+  {
+    MutexAutoLock lock(mMutex);
+    mMap.Clear();
+  }
+
+private:
+  Mutex mMutex; // To protect mMap.
+  AutoTArray<DurationElement, 16> mMap;
+};
+
+} // namespace mozilla
+
+#endif // mozilla_DurationMap_h
--- a/dom/media/platforms/ffmpeg/FFmpegVideoDecoder.h
+++ b/dom/media/platforms/ffmpeg/FFmpegVideoDecoder.h
@@ -4,18 +4,17 @@
  * 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 __FFmpegVideoDecoder_h__
 #define __FFmpegVideoDecoder_h__
 
 #include "FFmpegLibWrapper.h"
 #include "FFmpegDataDecoder.h"
-#include "mozilla/Pair.h"
-#include "nsTArray.h"
+#include "DurationMap.h"
 
 namespace mozilla
 {
 
 template <int V>
 class FFmpegVideoDecoder : public FFmpegDataDecoder<V>
 {
 };
@@ -82,49 +81,14 @@ private:
     int64_t mNumFaultyDts; /// Number of incorrect DTS values so far
     int64_t mLastPts;      /// PTS of the last frame
     int64_t mLastDts;      /// DTS of the last frame
   };
 
   PtsCorrectionContext mPtsContext;
   int64_t mLastInputDts;
 
-  class DurationMap
-  {
-  public:
-    typedef Pair<int64_t, int64_t> DurationElement;
-
-    // Insert Key and Duration pair at the end of our map.
-    void Insert(int64_t aKey, int64_t aDuration)
-    {
-      mMap.AppendElement(MakePair(aKey, aDuration));
-    }
-    // Sets aDuration matching aKey and remove it from the map if found.
-    // The element returned is the first one found.
-    // Returns true if found, false otherwise.
-    bool Find(int64_t aKey, int64_t& aDuration)
-    {
-      for (uint32_t i = 0; i < mMap.Length(); i++) {
-        DurationElement& element = mMap[i];
-        if (element.first() == aKey) {
-          aDuration = element.second();
-          mMap.RemoveElementAt(i);
-          return true;
-        }
-      }
-      return false;
-    }
-    // Remove all elements of the map.
-    void Clear()
-    {
-      mMap.Clear();
-    }
-
-  private:
-    AutoTArray<DurationElement, 16> mMap;
-  };
-
   DurationMap mDurationMap;
 };
 
 } // namespace mozilla
 
 #endif // __FFmpegVideoDecoder_h__
--- a/dom/media/platforms/moz.build
+++ b/dom/media/platforms/moz.build
@@ -5,16 +5,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXPORTS += [
     'agnostic/AgnosticDecoderModule.h',
     'agnostic/OpusDecoder.h',
     'agnostic/TheoraDecoder.h',
     'agnostic/VorbisDecoder.h',
     'agnostic/VPXDecoder.h',
+    'DurationMap.h',
     'MediaTelemetryConstants.h',
     'PDMFactory.h',
     'PlatformDecoderModule.h',
     'wrappers/H264Converter.h'
 ]
 
 UNIFIED_SOURCES += [
     'agnostic/AgnosticDecoderModule.cpp',