Bug 1061475: fix crash in Loop calls on Macs due to Refresh() change r=gcp
authorRandell Jesup <rjesup@jesup.org>
Tue, 02 Sep 2014 04:52:45 -0400
changeset 202976 e567693fa55fa2a072606b7faeecfad4798d83c5
parent 202975 0a76964b8d8dc4f4e06748335731c1f02e8711e1
child 202977 bb6876120191ca14dea7500609d2c1cebddac39c
push id48538
push userrjesup@wgate.com
push dateTue, 02 Sep 2014 10:29:58 +0000
treeherdermozilla-inbound@e567693fa55f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1061475
milestone34.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 1061475: fix crash in Loop calls on Macs due to Refresh() change r=gcp
content/media/webrtc/MediaEngine.h
content/media/webrtc/MediaEngineWebRTC.cpp
content/media/webrtc/MediaEngineWebRTC.h
content/media/webrtc/MediaEngineWebRTCVideo.cpp
--- a/content/media/webrtc/MediaEngine.h
+++ b/content/media/webrtc/MediaEngine.h
@@ -78,16 +78,21 @@ protected:
 };
 
 /**
  * Common abstract base class for audio and video sources.
  */
 class MediaEngineSource : public nsISupports
 {
 public:
+  // code inside webrtc.org assumes these sizes; don't use anything smaller
+  // without verifying it's ok
+  static const unsigned int kMaxDeviceNameLength = 128;
+  static const unsigned int kMaxUniqueIdLength = 256;
+
   virtual ~MediaEngineSource() {}
 
   /* Populate the human readable name of this device in the nsAString */
   virtual void GetName(nsAString&) = 0;
 
   /* Populate the UUID of this device in the nsAString */
   virtual void GetUUID(nsAString&) = 0;
 
--- a/content/media/webrtc/MediaEngineWebRTC.cpp
+++ b/content/media/webrtc/MediaEngineWebRTC.cpp
@@ -220,20 +220,18 @@ MediaEngineWebRTC::EnumerateVideoDevices
    * mVideoSources must be updated.
    */
   int num = ptrViECapture->NumberOfCaptureDevices();
   if (num <= 0) {
     return;
   }
 
   for (int i = 0; i < num; i++) {
-    const unsigned int kMaxDeviceNameLength = 128; // XXX FIX!
-    const unsigned int kMaxUniqueIdLength = 256;
-    char deviceName[kMaxDeviceNameLength];
-    char uniqueId[kMaxUniqueIdLength];
+    char deviceName[MediaEngineSource::kMaxDeviceNameLength];
+    char uniqueId[MediaEngineSource::kMaxUniqueIdLength];
 
     // paranoia
     deviceName[0] = '\0';
     uniqueId[0] = '\0';
     int error = ptrViECapture->GetCaptureDevice(i, deviceName,
                                                 sizeof(deviceName), uniqueId,
                                                 sizeof(uniqueId));
 
@@ -241,20 +239,22 @@ MediaEngineWebRTC::EnumerateVideoDevices
       LOG((" VieCapture:GetCaptureDevice: Failed %d",
            ptrViEBase->LastError() ));
       continue;
     }
 #ifdef DEBUG
     LOG(("  Capture Device Index %d, Name %s", i, deviceName));
 
     webrtc::CaptureCapability cap;
-    int numCaps = ptrViECapture->NumberOfCapabilities(uniqueId, kMaxUniqueIdLength);
+    int numCaps = ptrViECapture->NumberOfCapabilities(uniqueId,
+                                                      MediaEngineSource::kMaxUniqueIdLength);
     LOG(("Number of Capabilities %d", numCaps));
     for (int j = 0; j < numCaps; j++) {
-      if (ptrViECapture->GetCaptureCapability(uniqueId, kMaxUniqueIdLength,
+      if (ptrViECapture->GetCaptureCapability(uniqueId,
+                                              MediaEngineSource::kMaxUniqueIdLength,
                                               j, cap ) != 0 ) {
         break;
       }
       LOG(("type=%d width=%d height=%d maxFPS=%d",
            cap.rawType, cap.width, cap.height, cap.maxFPS ));
     }
 #endif
 
--- a/content/media/webrtc/MediaEngineWebRTC.h
+++ b/content/media/webrtc/MediaEngineWebRTC.h
@@ -215,19 +215,16 @@ public:
   }
 
   void Refresh(int aIndex);
 
 protected:
   ~MediaEngineWebRTCVideoSource() { Shutdown(); }
 
 private:
-  static const unsigned int KMaxDeviceNameLength = 128;
-  static const unsigned int KMaxUniqueIdLength = 256;
-
   // Initialize the needed Video engine interfaces.
   void Init();
   void Shutdown();
 
   // Engine variables.
 #ifdef MOZ_B2G_CAMERA
   mozilla::ReentrantMonitor mCallbackMonitor; // Monitor for camera callback handling
   // This is only modified on MainThread (AllocImpl and DeallocImpl)
@@ -345,19 +342,16 @@ protected:
 
   // mSamples is an int to avoid conversions when comparing/etc to
   // samplingFreq & length. Making mSamples protected instead of private is a
   // silly way to avoid -Wunused-private-field warnings when PR_LOGGING is not
   // #defined. mSamples is not actually expected to be used by a derived class.
   int mSamples;
 
 private:
-  static const unsigned int KMaxDeviceNameLength = 128;
-  static const unsigned int KMaxUniqueIdLength = 256;
-
   void Init();
   void Shutdown();
 
   webrtc::VoiceEngine* mVoiceEngine;
   ScopedCustomReleasePtr<webrtc::VoEBase> mVoEBase;
   ScopedCustomReleasePtr<webrtc::VoEExternalMedia> mVoERender;
   ScopedCustomReleasePtr<webrtc::VoENetwork> mVoENetwork;
   ScopedCustomReleasePtr<webrtc::VoEAudioProcessing> mVoEProcessing;
--- a/content/media/webrtc/MediaEngineWebRTCVideo.cpp
+++ b/content/media/webrtc/MediaEngineWebRTCVideo.cpp
@@ -213,17 +213,17 @@ void
 MediaEngineWebRTCVideoSource::ChooseCapability(
     const VideoTrackConstraintsN &aConstraints,
     const MediaEnginePrefs &aPrefs)
 {
 #ifdef MOZ_B2G_CAMERA
   return GuessCapability(aConstraints, aPrefs);
 #else
   NS_ConvertUTF16toUTF8 uniqueId(mUniqueId);
-  int num = mViECapture->NumberOfCapabilities(uniqueId.get(), KMaxUniqueIdLength);
+  int num = mViECapture->NumberOfCapabilities(uniqueId.get(), kMaxUniqueIdLength);
   if (num <= 0) {
     // Mac doesn't support capabilities.
     return GuessCapability(aConstraints, aPrefs);
   }
 
   // The rest is the full algorithm for cameras that can list their capabilities.
 
   LOG(("ChooseCapability: prefs: %dx%d @%d-%dfps",
@@ -235,17 +235,17 @@ MediaEngineWebRTCVideoSource::ChooseCapa
   for (int i = 0; i < num; i++) {
     candidateSet.AppendElement(i);
   }
 
   // Pick among capabilities: First apply required constraints.
 
   for (uint32_t i = 0; i < candidateSet.Length();) {
     webrtc::CaptureCapability cap;
-    mViECapture->GetCaptureCapability(uniqueId.get(), KMaxUniqueIdLength,
+    mViECapture->GetCaptureCapability(uniqueId.get(), kMaxUniqueIdLength,
                                       candidateSet[i], cap);
     if (!SatisfyConstraintSet(aConstraints.mRequired, cap)) {
       candidateSet.RemoveElementAt(i);
     } else {
       ++i;
     }
   }
 
@@ -255,17 +255,17 @@ MediaEngineWebRTCVideoSource::ChooseCapa
 
   if (aConstraints.mAdvanced.WasPassed()) {
     auto &array = aConstraints.mAdvanced.Value();
 
     for (uint32_t i = 0; i < array.Length(); i++) {
       SourceSet rejects;
       for (uint32_t j = 0; j < candidateSet.Length();) {
         webrtc::CaptureCapability cap;
-        mViECapture->GetCaptureCapability(uniqueId.get(), KMaxUniqueIdLength,
+        mViECapture->GetCaptureCapability(uniqueId.get(), kMaxUniqueIdLength,
                                           candidateSet[j], cap);
         if (!SatisfyConstraintSet(array[i], cap)) {
           rejects.AppendElement(candidateSet[j]);
           candidateSet.RemoveElementAt(j);
         } else {
           ++j;
         }
       }
@@ -283,17 +283,17 @@ MediaEngineWebRTCVideoSource::ChooseCapa
   // Default is closest to available capability but equal to or below;
   // otherwise closest above.  Since we handle the num=0 case above and
   // take the first entry always, we can never exit uninitialized.
 
   webrtc::CaptureCapability cap;
   bool higher = true;
   for (uint32_t i = 0; i < candidateSet.Length(); i++) {
     mViECapture->GetCaptureCapability(NS_ConvertUTF16toUTF8(mUniqueId).get(),
-                                      KMaxUniqueIdLength, candidateSet[i], cap);
+                                      kMaxUniqueIdLength, candidateSet[i], cap);
     if (higher) {
       if (i == 0 ||
           (mCapability.width > cap.width && mCapability.height > cap.height)) {
         // closer than the current choice
         mCapability = cap;
         // FIXME: expose expected capture delay?
       }
       if (cap.width <= (uint32_t) prefWidth && cap.height <= (uint32_t) prefHeight) {
@@ -446,17 +446,17 @@ MediaEngineWebRTCVideoSource::Allocate(c
 #else
   if (mState == kReleased && mInitDone) {
     // Note: if shared, we don't allow a later opener to affect the resolution.
     // (This may change depending on spec changes for Constraints/settings)
 
     ChooseCapability(aConstraints, aPrefs);
 
     if (mViECapture->AllocateCaptureDevice(NS_ConvertUTF16toUTF8(mUniqueId).get(),
-                                           KMaxUniqueIdLength, mCaptureIndex)) {
+                                           kMaxUniqueIdLength, mCaptureIndex)) {
       return NS_ERROR_FAILURE;
     }
     mState = kAllocated;
     LOG(("Video device %d allocated", mCaptureIndex));
   } else if (mSources.IsEmpty()) {
     LOG(("Video device %d reallocated", mCaptureIndex));
   } else {
     LOG(("Video device %d allocated shared", mCaptureIndex));
@@ -649,23 +649,21 @@ MediaEngineWebRTCVideoSource::Init()
   // Get interfaces for capture, render for now
   mViECapture = webrtc::ViECapture::GetInterface(mVideoEngine);
   mViERender = webrtc::ViERender::GetInterface(mVideoEngine);
 
   if (mViECapture == nullptr || mViERender == nullptr) {
     return;
   }
 
-  const uint32_t KMaxDeviceNameLength = 128;
-  const uint32_t KMaxUniqueIdLength = 256;
-  char deviceName[KMaxDeviceNameLength];
-  char uniqueId[KMaxUniqueIdLength];
+  char deviceName[kMaxDeviceNameLength];
+  char uniqueId[kMaxUniqueIdLength];
   if (mViECapture->GetCaptureDevice(mCaptureIndex,
-                                    deviceName, KMaxDeviceNameLength,
-                                    uniqueId, KMaxUniqueIdLength)) {
+                                    deviceName, kMaxDeviceNameLength,
+                                    uniqueId, kMaxUniqueIdLength)) {
     return;
   }
 
   CopyUTF8toUTF16(deviceName, mDeviceName);
   CopyUTF8toUTF16(uniqueId, mUniqueId);
 #endif
 
   mInitDone = true;
@@ -702,25 +700,31 @@ MediaEngineWebRTCVideoSource::Shutdown()
 
 void MediaEngineWebRTCVideoSource::Refresh(int aIndex) {
   // NOTE: mCaptureIndex might have changed when allocated!
   // Use aIndex to update information, but don't change mCaptureIndex!!
 #ifdef MOZ_B2G_CAMERA
   // Caller looked up this source by uniqueId; since deviceName == uniqueId nothing else changes
 #else
   // Caller looked up this source by uniqueId, so it shouldn't change
-  const uint32_t KMaxDeviceNameLength = 128;
-  char deviceName[KMaxDeviceNameLength];
+  char deviceName[kMaxDeviceNameLength];
+  char uniqueId[kMaxUniqueIdLength];
+
   if (mViECapture->GetCaptureDevice(aIndex,
-                                    deviceName, KMaxDeviceNameLength,
-                                    nullptr, 0)) {
+                                    deviceName, sizeof(deviceName),
+                                    uniqueId, sizeof(uniqueId))) {
     return;
   }
 
   CopyUTF8toUTF16(deviceName, mDeviceName);
+#ifdef DEBUG
+  nsString temp;
+  CopyUTF8toUTF16(uniqueId, temp);
+  MOZ_ASSERT(temp.Equals(mUniqueId));
+#endif
 #endif
 }
 
 #ifdef MOZ_B2G_CAMERA
 
 // All these functions must be run on MainThread!
 void
 MediaEngineWebRTCVideoSource::AllocImpl() {