Bug 946865 - Move Load Adaption preference to the main thread. r=jesup
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Fri, 06 Dec 2013 20:34:40 +0100
changeset 159276 d701e36c7ca8baaa878fc8d9b1f6c7cc6c1023f1
parent 159275 06c8949f78a0e9257c5c9c406289cbda3487558b
child 159277 1d9356c6c769ae40332b478fa3f867a12ed1966d
push id25779
push userkwierso@gmail.com
push dateSat, 07 Dec 2013 02:26:14 +0000
treeherdermozilla-central@2099f78b7dea [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs946865
milestone28.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 946865 - Move Load Adaption preference to the main thread. r=jesup
content/media/webrtc/LoadMonitor.cpp
content/media/webrtc/MediaEngine.h
content/media/webrtc/MediaEngineWebRTC.cpp
content/media/webrtc/MediaEngineWebRTC.h
dom/media/MediaManager.cpp
dom/media/MediaManager.h
--- a/content/media/webrtc/LoadMonitor.cpp
+++ b/content/media/webrtc/LoadMonitor.cpp
@@ -16,17 +16,16 @@
 #include "nsNetUtil.h"
 #include "nsILineInputStream.h"
 #include "nsIObserverService.h"
 #include "nsIServiceManager.h"
 
 #include "mozilla/TimeStamp.h"
 #include "mozilla/Util.h"
 #include "mozilla/Services.h"
-#include "mozilla/Preferences.h"
 
 #if defined(ANDROID) || defined(LINUX) || defined(XP_MACOSX)
 #include <sys/time.h>
 #include <sys/resource.h>
 #endif
 
 // NSPR_LOG_MODULES=LoadMonitor:5
 PRLogModuleInfo *gLoadMonitorLog = nullptr;
@@ -318,20 +317,16 @@ LoadMonitor::GetSystemLoad() {
   MutexAutoLock lock(mLock);
   float load = mSystemLoad;
   return load;
 }
 
 nsresult
 LoadMonitor::Init(nsRefPtr<LoadMonitor> &self)
 {
-  if (!Preferences::GetBool("media.navigator.load_adapt", false)) {
-    return NS_OK;
-  }
-
 #if defined(PR_LOGGING)
   if (!gLoadMonitorLog)
     gLoadMonitorLog = PR_NewLogModule("LoadMonitor");
   LOG(("Initializing LoadMonitor"));
 #endif
 
 #if defined(ANDROID) || defined(LINUX)
   nsRefPtr<LoadMonitorAddObserver> addObsRunner = new LoadMonitorAddObserver(self);
--- a/content/media/webrtc/MediaEngine.h
+++ b/content/media/webrtc/MediaEngine.h
@@ -40,16 +40,17 @@ class MediaEngine
 public:
   virtual ~MediaEngine() {}
 
   static const int DEFAULT_VIDEO_FPS = 30;
   static const int DEFAULT_VIDEO_MIN_FPS = 10;
   static const int DEFAULT_VIDEO_WIDTH = 640;
   static const int DEFAULT_VIDEO_HEIGHT = 480;
   static const int DEFAULT_AUDIO_TIMER_MS = 10;
+  static const bool DEFAULT_LOAD_ADAPT = false;
 
   /* Populate an array of video sources in the nsTArray. Also include devices
    * that are currently unavailable. */
   virtual void EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >*) = 0;
 
   /* Populate an array of audio sources in the nsTArray. Also include devices
    * that are currently unavailable. */
   virtual void EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >*) = 0;
@@ -125,16 +126,17 @@ protected:
 /**
  * Video source and friends.
  */
 struct MediaEnginePrefs {
   int32_t mWidth;
   int32_t mHeight;
   int32_t mFPS;
   int32_t mMinFPS;
+  bool mLoadAdapt;
 };
 
 class MediaEngineVideoSource : public MediaEngineSource
 {
 public:
   virtual ~MediaEngineVideoSource() {}
 };
 
--- a/content/media/webrtc/MediaEngineWebRTC.cpp
+++ b/content/media/webrtc/MediaEngineWebRTC.cpp
@@ -38,31 +38,33 @@ GetUserMediaLog()
 #include "AndroidBridge.h"
 #endif
 
 #undef LOG
 #define LOG(args) PR_LOG(GetUserMediaLog(), PR_LOG_DEBUG, args)
 
 namespace mozilla {
 #ifndef MOZ_B2G_CAMERA
-MediaEngineWebRTC::MediaEngineWebRTC()
+MediaEngineWebRTC::MediaEngineWebRTC(MediaEnginePrefs &aPrefs)
   : mMutex("mozilla::MediaEngineWebRTC")
   , mVideoEngine(nullptr)
   , mVoiceEngine(nullptr)
   , mVideoEngineInit(false)
   , mAudioEngineInit(false)
   , mHasTabVideoSource(false)
 {
   nsCOMPtr<nsIComponentRegistrar> compMgr;
   NS_GetComponentRegistrar(getter_AddRefs(compMgr));
   if (compMgr) {
     compMgr->IsContractIDRegistered(NS_TABSOURCESERVICE_CONTRACTID, &mHasTabVideoSource);
   }
-  mLoadMonitor = new LoadMonitor();
-  mLoadMonitor->Init(mLoadMonitor);
+  if (aPrefs.mLoadAdapt) {
+      mLoadMonitor = new LoadMonitor();
+      mLoadMonitor->Init(mLoadMonitor);
+  }
 }
 #endif
 
 
 void
 MediaEngineWebRTC::EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >* aVSources)
 {
 #ifdef MOZ_B2G_CAMERA
@@ -354,12 +356,13 @@ MediaEngineWebRTC::Shutdown()
   if (mVoiceEngine) {
     mAudioSources.Clear();
     webrtc::VoiceEngine::Delete(mVoiceEngine);
   }
 
   mVideoEngine = nullptr;
   mVoiceEngine = nullptr;
 
-  mLoadMonitor->Shutdown();
+  if (mLoadMonitor)
+    mLoadMonitor->Shutdown();
 }
 
 }
--- a/content/media/webrtc/MediaEngineWebRTC.h
+++ b/content/media/webrtc/MediaEngineWebRTC.h
@@ -362,17 +362,17 @@ public:
     , mWindowId(aWindowId)
     , mHasTabVideoSource(false)
   {
     AsyncLatencyLogger::Get(true)->AddRef();
     mLoadMonitor = new LoadMonitor();
     mLoadMonitor->Init(mLoadMonitor);
   }
 #else
-  MediaEngineWebRTC();
+  MediaEngineWebRTC(MediaEnginePrefs &aPrefs);
 #endif
   ~MediaEngineWebRTC() {
     Shutdown();
 #ifdef MOZ_B2G_CAMERA
     AsyncLatencyLogger::Get()->Release();
 #endif
   }
 
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -1039,16 +1039,17 @@ private:
 MediaManager::MediaManager()
   : mMediaThread(nullptr)
   , mMutex("mozilla::MediaManager")
   , mBackend(nullptr) {
   mPrefs.mWidth  = MediaEngine::DEFAULT_VIDEO_WIDTH;
   mPrefs.mHeight = MediaEngine::DEFAULT_VIDEO_HEIGHT;
   mPrefs.mFPS    = MediaEngine::DEFAULT_VIDEO_FPS;
   mPrefs.mMinFPS = MediaEngine::DEFAULT_VIDEO_MIN_FPS;
+  mPrefs.mLoadAdapt = MediaEngine::DEFAULT_LOAD_ADAPT;
 
   nsresult rv;
   nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1", &rv);
   if (NS_SUCCEEDED(rv)) {
     nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs);
     if (branch) {
       GetPrefs(branch, nullptr);
     }
@@ -1083,16 +1084,17 @@ MediaManager::Get() {
     }
     // else MediaManager won't work properly and will leak (see bug 837874)
     nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
     if (prefs) {
       prefs->AddObserver("media.navigator.video.default_width", sSingleton, false);
       prefs->AddObserver("media.navigator.video.default_height", sSingleton, false);
       prefs->AddObserver("media.navigator.video.default_fps", sSingleton, false);
       prefs->AddObserver("media.navigator.video.default_minfps", sSingleton, false);
+      prefs->AddObserver("media.navigator.load_adapt", sSingleton, false);
     }
   }
   return sSingleton;
 }
 
 /* static */ already_AddRefed<MediaManager>
 MediaManager::GetInstance()
 {
@@ -1406,17 +1408,17 @@ MediaManager::GetBackend(uint64_t aWindo
 {
   // Plugin backends as appropriate. The default engine also currently
   // includes picture support for Android.
   // This IS called off main-thread.
   MutexAutoLock lock(mMutex);
   if (!mBackend) {
 #if defined(MOZ_WEBRTC)
   #ifndef MOZ_B2G_CAMERA
-    mBackend = new MediaEngineWebRTC();
+    mBackend = new MediaEngineWebRTC(mPrefs);
   #else
     mBackend = new MediaEngineWebRTC(mCameraManager, aWindowId);
   #endif
 #else
     mBackend = new MediaEngineDefault();
 #endif
   }
   return mBackend;
@@ -1505,22 +1507,35 @@ MediaManager::GetPref(nsIPrefBranch *aBr
   if (aData == nullptr || strcmp(aPref,aData) == 0) {
     if (NS_SUCCEEDED(aBranch->GetIntPref(aPref, &temp))) {
       *aVal = temp;
     }
   }
 }
 
 void
+MediaManager::GetPrefBool(nsIPrefBranch *aBranch, const char *aPref,
+                          const char *aData, bool *aVal)
+{
+  bool temp;
+  if (aData == nullptr || strcmp(aPref,aData) == 0) {
+    if (NS_SUCCEEDED(aBranch->GetBoolPref(aPref, &temp))) {
+      *aVal = temp;
+    }
+  }
+}
+
+void
 MediaManager::GetPrefs(nsIPrefBranch *aBranch, const char *aData)
 {
   GetPref(aBranch, "media.navigator.video.default_width", aData, &mPrefs.mWidth);
   GetPref(aBranch, "media.navigator.video.default_height", aData, &mPrefs.mHeight);
   GetPref(aBranch, "media.navigator.video.default_fps", aData, &mPrefs.mFPS);
   GetPref(aBranch, "media.navigator.video.default_minfps", aData, &mPrefs.mMinFPS);
+  GetPrefBool(aBranch, "media.navigator.load_adapt", aData, &mPrefs.mLoadAdapt);
 }
 
 nsresult
 MediaManager::Observe(nsISupports* aSubject, const char* aTopic,
   const PRUnichar* aData)
 {
   NS_ASSERTION(NS_IsMainThread(), "Observer invoked off the main thread");
   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
@@ -1539,16 +1554,17 @@ MediaManager::Observe(nsISupports* aSubj
     obs->RemoveObserver(this, "getUserMedia:revoke");
 
     nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
     if (prefs) {
       prefs->RemoveObserver("media.navigator.video.default_width", this);
       prefs->RemoveObserver("media.navigator.video.default_height", this);
       prefs->RemoveObserver("media.navigator.video.default_fps", this);
       prefs->RemoveObserver("media.navigator.video.default_minfps", this);
+      prefs->RemoveObserver("media.navigator.load_adapt", this);
     }
 
     // Close off any remaining active windows.
     {
       MutexAutoLock lock(mMutex);
       GetActiveWindows()->Clear();
       mActiveCallbacks.Clear();
       LOG(("Releasing MediaManager singleton and thread"));
--- a/dom/media/MediaManager.h
+++ b/dom/media/MediaManager.h
@@ -501,16 +501,18 @@ public:
 private:
   WindowTable *GetActiveWindows() {
     NS_ASSERTION(NS_IsMainThread(), "Only access windowlist on main thread");
     return &mActiveWindows;
   }
 
   void GetPref(nsIPrefBranch *aBranch, const char *aPref,
                const char *aData, int32_t *aVal);
+  void GetPrefBool(nsIPrefBranch *aBranch, const char *aPref,
+                   const char *aData, bool *aVal);
   void GetPrefs(nsIPrefBranch *aBranch, const char *aData);
 
   // Make private because we want only one instance of this class
   MediaManager();
 
   ~MediaManager() {
     delete mBackend;
   }