DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
/* -*- 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
/* 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 "video_engine/browser_capture_impl.h"
#include "video_engine/browser_capture_impl.h"
#include "video_engine/desktop_capture_impl.h"
#include "webrtc/system_wrappers/include/clock.h"
#ifdef WEBRTC_ANDROID
#  include "webrtc/modules/video_capture/video_capture.h"
#endif
#endif

#ifdef MOZ_WIDGET_ANDROID
#  include "mozilla/jni/Utils.h"
#endif


namespace mozilla {
namespace camera {

#undef LOG
#undef LOG_ENABLED
#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)
#if defined(ANDROID)
int VideoEngine::SetAndroidObjects() {
  LOG(("%s", __PRETTY_FUNCTION__));

  JavaVM* const javaVM = mozilla::jni::GetVM();
  if (!javaVM || webrtc::SetCaptureAndroidVM(javaVM) != 0) {
  if (!javaVM || webrtc::SetCaptureAndroidVM(javaVM) != 0) {
    LOG(("Could not set capture Android VM"));
    return -1;
  }
#  ifdef WEBRTC_INCLUDE_INTERNAL_VIDEO_RENDER
  if (webrtc::SetRenderAndroidVM(javaVM) != 0) {
  if (webrtc::SetRenderAndroidVM(javaVM) != 0) {
    LOG(("Could not set render Android VM"));
    return -1;
  }
#  endif
  return 0;
  return 0;
}
#endif

void VideoEngine::CreateVideoCapture(int32_t& id,
                                     const char* deviceUniqueIdUTF8) {
                                     const char* deviceUniqueIdUTF8) {
  LOG(("%s", __PRETTY_FUNCTION__));
  MOZ_ASSERT(deviceUniqueIdUTF8);

  id = GenerateId();
  LOG(("CaptureDeviceInfo.type=%s id=%d", mCaptureDevInfo.TypeName(), id));
  LOG(("CaptureDeviceInfo.type=%s id=%d", mCaptureDevInfo.TypeName(), id));

  for (auto& it : mCaps) {
    if (it.second.VideoCapture() &&
        it.second.VideoCapture()->CurrentDeviceName() &&
        strcmp(it.second.VideoCapture()->CurrentDeviceName(),
        strcmp(it.second.VideoCapture()->CurrentDeviceName(),
               deviceUniqueIdUTF8) == 0) {
      mIdMap.emplace(id, it.first);
      return;
    }
    }
  }

  CaptureEntry entry = {-1, nullptr};

  if (mCaptureDevInfo.type == webrtc::CaptureDeviceType::Camera) {
  if (mCaptureDevInfo.type == webrtc::CaptureDeviceType::Camera) {
    entry = CaptureEntry(
        id, webrtc::VideoCaptureFactory::Create(deviceUniqueIdUTF8));
    if (entry.VideoCapture()) {
      entry.VideoCapture()->SetApplyRotation(true);
    }
  } else {
#ifndef WEBRTC_ANDROID
#ifndef WEBRTC_ANDROID
#  ifdef MOZ_X11
    webrtc::VideoCaptureModule* captureModule;
    auto type = mCaptureDevInfo.type;
    nsresult result = NS_DispatchToMainThread(
        media::NewRunnableFrom([&captureModule, id, deviceUniqueIdUTF8,
        media::NewRunnableFrom([&captureModule, id, deviceUniqueIdUTF8,
                                type]() -> nsresult {
          captureModule =
              webrtc::DesktopCaptureImpl::Create(id, deviceUniqueIdUTF8, type);
          return NS_OK;
        }),
        }),
        nsIEventTarget::DISPATCH_SYNC);

    if (result == NS_OK) {
      entry = CaptureEntry(id, captureModule);
    } else {
    } else {
      return;
    }
#  else
    entry = CaptureEntry(id, webrtc::DesktopCaptureImpl::Create(
                                 id, deviceUniqueIdUTF8, mCaptureDevInfo.type));
                                 id, deviceUniqueIdUTF8, mCaptureDevInfo.type));
#  endif
#else
    MOZ_ASSERT("CreateVideoCapture NO DESKTOP CAPTURE IMPL ON ANDROID" ==
               nullptr);
#endif
  }
  }
  mCaps.emplace(id, std::move(entry));
  mIdMap.emplace(id, id);
}

int VideoEngine::ReleaseVideoCapture(const int32_t id) {
int VideoEngine::ReleaseVideoCapture(const int32_t id) {
  bool found = false;

#ifdef DEBUG
  {
    auto it = mIdMap.find(id);
    auto it = mIdMap.find(id);
    MOZ_ASSERT(it != mIdMap.end());
    Unused << it;
  }
#endif

  for (auto& it : mIdMap) {
    if (it.first != id && it.second == mIdMap[id]) {
    if (it.first != id && it.second == mIdMap[id]) {
      // There are other tracks still using this hardware.
      found = true;
    }
  }


  if (!found) {
    WithEntry(id, [&found](CaptureEntry& cap) {
      cap.mVideoCaptureModule = nullptr;
      found = true;
    });
    });
    MOZ_ASSERT(found);
    if (found) {
      auto it = mCaps.find(mIdMap[id]);
      MOZ_ASSERT(it != mCaps.end());
      mCaps.erase(it);
      mCaps.erase(it);
    }
  }

  mIdMap.erase(id);
  return found ? 0 : (-1);
  return found ? 0 : (-1);
}

std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo>
VideoEngine::GetOrCreateVideoCaptureDeviceInfo() {
VideoEngine::GetOrCreateVideoCaptureDeviceInfo() {
  LOG(("%s", __PRETTY_FUNCTION__));
  int64_t currentTime = 0;

  const char* capDevTypeName =
      webrtc::CaptureDeviceInfo(mCaptureDevInfo.type).TypeName();
      webrtc::CaptureDeviceInfo(mCaptureDevInfo.type).TypeName();

  if (mDeviceInfo) {
    LOG(("Device cache available."));
    // Camera cache is invalidated by HW change detection elsewhere
    if (mCaptureDevInfo.type == webrtc::CaptureDeviceType::Camera) {
    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
    // Screen sharing cache is invalidated after the expiration time
    currentTime = webrtc::Clock::GetRealTimeClock()->TimeInMilliseconds();
    LOG(("Checking expiry, fetched current time of: %" PRId64, currentTime));
    LOG(("device cache expiration is %" PRId64, mExpiryTimeInMs));
    if (currentTime <= mExpiryTimeInMs) {
      LOG(("returning cached CaptureDeviceInfo of type %s", capDevTypeName));
      LOG(("returning cached CaptureDeviceInfo of type %s", capDevTypeName));
      return mDeviceInfo;
    }
  }

  if (currentTime == 0) {
  if (currentTime == 0) {
    currentTime = webrtc::Clock::GetRealTimeClock()->TimeInMilliseconds();
    LOG(("Fetched current time of: %" PRId64, currentTime));
  }
  mExpiryTimeInMs = currentTime + kCacheExpiryPeriodMs;
  LOG(("new device cache expiration is %" PRId64, mExpiryTimeInMs));
  LOG(("new device cache expiration is %" PRId64, mExpiryTimeInMs));
  LOG(("creating a new VideoCaptureDeviceInfo of type %s", capDevTypeName));

  switch (mCaptureDevInfo.type) {
    case webrtc::CaptureDeviceType::Camera: {
#ifdef MOZ_WIDGET_ANDROID
      if (SetAndroidObjects()) {
      if (SetAndroidObjects()) {
        LOG(("VideoEngine::SetAndroidObjects Failed"));
        break;
      }
#endif
      mDeviceInfo.reset(webrtc::VideoCaptureFactory::CreateDeviceInfo());
      mDeviceInfo.reset(webrtc::VideoCaptureFactory::CreateDeviceInfo());
      LOG(("webrtc::CaptureDeviceType::Camera: Finished creating new device."));
      break;
    }
    case webrtc::CaptureDeviceType::Browser: {
      mDeviceInfo.reset(webrtc::BrowserDeviceInfoImpl::CreateDeviceInfo());
      mDeviceInfo.reset(webrtc::BrowserDeviceInfoImpl::CreateDeviceInfo());
      LOG((
          "webrtc::CaptureDeviceType::Browser: Finished creating new device."));
      break;
    }
    // Window and Screen types are handled by DesktopCapture
    // Window and Screen types are handled by DesktopCapture
    case webrtc::CaptureDeviceType::Window:
    case webrtc::CaptureDeviceType::Screen: {
#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
      mDeviceInfo.reset(webrtc::DesktopCaptureImpl::CreateDeviceInfo(
          mId, mCaptureDevInfo.type));
          mId, mCaptureDevInfo.type));
      LOG(("screen capture: Finished creating new device."));
#else
      MOZ_ASSERT(
      MOZ_ASSERT(
          "GetVideoCaptureDeviceInfo NO DESKTOP CAPTURE IMPL ON ANDROID" ==
          "GetVideoCaptureDeviceInfo NO DESKTOP CAPTURE IMPL ON ANDROID" ==
          nullptr);
      mDeviceInfo.reset();
#endif
#endif
      break;
    }
  }
  LOG(("EXIT %s", __PRETTY_FUNCTION__));
  return mDeviceInfo;
}

const UniquePtr<const webrtc::Config>& VideoEngine::GetConfiguration() {
const UniquePtr<const webrtc::Config>& VideoEngine::GetConfiguration() {
  return mConfig;
}

already_AddRefed<VideoEngine> VideoEngine::Create(
    UniquePtr<const webrtc::Config>&& aConfig) {
    UniquePtr<const webrtc::Config>&& aConfig) {
  LOG(("%s", __PRETTY_FUNCTION__));
  LOG(("Creating new VideoEngine with CaptureDeviceType %s",
       aConfig->Get<webrtc::CaptureDeviceInfo>().TypeName()));
  return do_AddRef(new VideoEngine(std::move(aConfig)));
  return do_AddRef(new VideoEngine(std::move(aConfig)));
}

VideoEngine::CaptureEntry::CaptureEntry(
    int32_t aCapnum, rtc::scoped_refptr<webrtc::VideoCaptureModule> aCapture)
    : mCapnum(aCapnum), mVideoCaptureModule(aCapture) {}
    : mCapnum(aCapnum), mVideoCaptureModule(aCapture) {}

rtc::scoped_refptr<webrtc::VideoCaptureModule>
VideoEngine::CaptureEntry::VideoCapture() {
  return mVideoCaptureModule;
}
}

int32_t VideoEngine::CaptureEntry::Capnum() const { return mCapnum; }

bool VideoEngine::WithEntry(
    const int32_t entryCapnum,
    const int32_t entryCapnum,
    const std::function<void(CaptureEntry& entry)>&& fn) {
#ifdef DEBUG
  {
    auto it = mIdMap.find(entryCapnum);
    MOZ_ASSERT(it != mIdMap.end());
    MOZ_ASSERT(it != mIdMap.end());
    Unused << it;
  }
#endif

  auto it = mCaps.find(mIdMap[entryCapnum]);
  auto it = mCaps.find(mIdMap[entryCapnum]);
  MOZ_ASSERT(it != mCaps.end());
  if (it == mCaps.end()) {
    return false;
  }
  fn(it->second);
  fn(it->second);
  return true;
}

int32_t VideoEngine::GenerateId() {
  // XXX Something better than this (a map perhaps, or a simple boolean TArray,
  // XXX Something better than this (a map perhaps, or a simple boolean TArray,
  // given the number in-use is O(1) normally!)
  return mId = sId++;
}

VideoEngine::VideoEngine(UniquePtr<const webrtc::Config>&& aConfig)
VideoEngine::VideoEngine(UniquePtr<const webrtc::Config>&& aConfig)
    : mId(0),
      mCaptureDevInfo(aConfig->Get<webrtc::CaptureDeviceInfo>()),
      mDeviceInfo(nullptr),
      mConfig(std::move(aConfig)) {
  LOG(("%s", __PRETTY_FUNCTION__));
  LOG(("%s", __PRETTY_FUNCTION__));
}

}  // namespace camera
}  // namespace mozilla
}  // namespace mozilla