Bug 1371000 - add expiration to noncamera device info;r=jib
authorNico Grunbaum
Tue, 13 Jun 2017 17:24:54 -0700
changeset 418746 3b3d54ec727beff868387935d691bdcef7658740
parent 418745 23da6b9178da539cd231678cd7dc197b91657fd2
child 418747 6c77cacaf82d169e8e7f557f2f14cae275f3410f
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1371000
milestone56.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 1371000 - add expiration to noncamera device info;r=jib MozReview-Commit-ID: 406sbEpJfbk
dom/media/systemservices/VideoEngine.cpp
dom/media/systemservices/VideoEngine.h
--- a/dom/media/systemservices/VideoEngine.cpp
+++ b/dom/media/systemservices/VideoEngine.cpp
@@ -1,32 +1,32 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set sw=2 ts=8 et ft=cpp : */
 /* 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 "VideoEngine.h"
 #include "webrtc/video_engine/browser_capture_impl.h"
+#include "webrtc/system_wrappers/include/clock.h"
 #ifdef WEBRTC_ANDROID
 #include "webrtc/modules/video_capture/video_capture.h"
 #endif
 
 
 namespace mozilla {
 namespace camera {
 
 #undef LOG
 #undef LOG_ENABLED
 mozilla::LazyLogModule gVideoEngineLog("VideoEngine");
 #define LOG(args) MOZ_LOG(gVideoEngineLog, mozilla::LogLevel::Debug, args)
 #define LOG_ENABLED() MOZ_LOG_TEST(gVideoEngineLog, mozilla::LogLevel::Debug)
 
 int VideoEngine::sId = 0;
-
 #if defined(ANDROID)
 int VideoEngine::SetAndroidObjects(JavaVM* javaVM) {
   LOG((__PRETTY_FUNCTION__));
 
   if (webrtc::SetCaptureAndroidVM(javaVM) != 0) {
     LOG(("Could not set capture Android VM"));
     return -1;
   }
@@ -54,34 +54,57 @@ VideoEngine::CreateVideoCapture(int32_t&
 #ifndef WEBRTC_ANDROID
     entry = CaptureEntry(
 	      id,
 	      webrtc::DesktopCaptureImpl::Create(id, deviceUniqueIdUTF8, mCaptureDevInfo.type));
 #else
     MOZ_ASSERT("CreateVideoCapture NO DESKTOP CAPTURE IMPL ON ANDROID" == nullptr);
 #endif
   }
-  mCaps.emplace(id,std::move(entry));
+  mCaps.emplace(id, std::move(entry));
 }
 
 int
 VideoEngine::ReleaseVideoCapture(const int32_t id) {
   bool found = false;
   WithEntry(id, [&found](CaptureEntry& cap) {
          cap.mVideoCaptureModule = nullptr;
         found = true;
    });
   return found ? 0 : (-1);
 }
 
 std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo>
 VideoEngine::GetOrCreateVideoCaptureDeviceInfo() {
+  LOG((__PRETTY_FUNCTION__));
+  int64_t currentTime = 0;
+
+  const char * capDevTypeName =
+    webrtc::CaptureDeviceInfo(mCaptureDevInfo.type).TypeName();
+
   if (mDeviceInfo) {
-    return mDeviceInfo;
+    // Camera cache is invalidated by HW change detection elsewhere
+    if (mCaptureDevInfo.type == webrtc::CaptureDeviceType::Camera) {
+      LOG(("returning cached CaptureDeviceInfo of type %s", capDevTypeName));
+      return mDeviceInfo;
+    }
+    // Screen sharing cache is invalidated after the expiration time
+    currentTime = webrtc::Clock::GetRealTimeClock()->TimeInMilliseconds();
+    if (currentTime <= mExpiryTimeInMs) {
+      LOG(("returning cached CaptureDeviceInfo of type %s", capDevTypeName));
+      return mDeviceInfo;
+    }
   }
+
+  if (currentTime == 0) {
+   currentTime = webrtc::Clock::GetRealTimeClock()->TimeInMilliseconds();
+  }
+  mExpiryTimeInMs = currentTime + kCacheExpiryPeriodMs;
+  LOG(("creating a new VideoCaptureDeviceInfo of type %s", capDevTypeName));
+
   switch (mCaptureDevInfo.type) {
     case webrtc::CaptureDeviceType::Camera: {
       mDeviceInfo.reset(webrtc::VideoCaptureFactory::CreateDeviceInfo());
       break;
     }
     case webrtc::CaptureDeviceType::Browser: {
       mDeviceInfo.reset(webrtc::BrowserDeviceInfoImpl::CreateDeviceInfo());
       break;
--- a/dom/media/systemservices/VideoEngine.h
+++ b/dom/media/systemservices/VideoEngine.h
@@ -21,37 +21,43 @@ namespace mozilla {
 namespace camera {
 
 // Historically the video engine was part of webrtc
 // it was removed (and reimplemented in Talk)
 class VideoEngine
 {
 private:
   virtual ~VideoEngine (){};
+  // Base cache expiration period
+  // Note because cameras use HW plug event detection, this
+  // only applies to screen based modes.
+  static const int64_t kCacheExpiryPeriodMs = 1000;
 
 public:
   VideoEngine (){};
   NS_INLINE_DECL_REFCOUNTING(VideoEngine)
 
   static RefPtr<VideoEngine> Create(UniquePtr<const webrtc::Config>&& aConfig);
 #if defined(ANDROID)
   static int SetAndroidObjects(JavaVM* javaVM);
 #endif
   void CreateVideoCapture(int32_t& id, const char* deviceUniqueIdUTF8);
 
   int ReleaseVideoCapture(const int32_t id);
 
   // VideoEngine is responsible for any cleanup in its modules
   static void Delete(VideoEngine * engine) { }
 
-  /** Returns or creates a new new DeviceInfo.
-  *   It is cached to prevent repeated lengthy polling for "realness"
-  *   of the hardware devices.  This could be handled in a more elegant
-  *   way in the future.
-  *   @return on failure the shared_ptr will be null, otherwise it will contain a DeviceInfo.
+  /** Returns an existing or creates a new new DeviceInfo.
+  *   Camera info is cached to prevent repeated lengthy polling for "realness"
+  *   of the hardware devices.  Other types of capture, e.g. screen share info,
+  *   are cached for 1 second. This could be handled in a more elegant way in
+  *   the future.
+  *   @return on failure the shared_ptr will be null, otherwise it will contain
+  *   a DeviceInfo.
   *   @see bug 1305212 https://bugzilla.mozilla.org/show_bug.cgi?id=1305212
   */
   std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo> GetOrCreateVideoCaptureDeviceInfo();
 
   const UniquePtr<const webrtc::Config>& GetConfiguration();
 
   void Startup() {
     mIsRunning = true;
@@ -83,15 +89,16 @@ public:
 private:
   explicit VideoEngine(UniquePtr<const webrtc::Config>&& aConfig);
   bool mIsRunning;
   int32_t mId;
   webrtc::CaptureDeviceInfo mCaptureDevInfo;
   std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo> mDeviceInfo;
   UniquePtr<const webrtc::Config> mConfig;
   std::map<int32_t, CaptureEntry> mCaps;
-
+  // The validity period for non-camera capture device infos`
+  int64_t mExpiryTimeInMs = 0;
   int32_t GenerateId();
   static int32_t sId;
 };
 }
 }
 #endif