author Jan-Ivar Bruaroey <>
Tue, 12 Jul 2016 23:25:07 -0400
changeset 349153 3b9c5f9d8a429e7b97ba54165934c46bdb777f49
parent 347525 80cae085c9a56b15929d7e4bb03fa3c96daf85fe
child 349154 e3354c94965925a82cf4c2d56c20aa07ad79bd99
permissions -rw-r--r--
Bug 1286096 - Move AllocationHandle used for cameras to MediaEngineSource base class to reuse for microphones. r=padenot MozReview-Commit-ID: BcmlKnHhe0o

/* -*- 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 */


#include "prcvar.h"
#include "prthread.h"
#include "nsIThread.h"
#include "nsIRunnable.h"

#include "mozilla/Mutex.h"
#include "mozilla/Monitor.h"
#include "nsCOMPtr.h"
#include "nsThreadUtils.h"
#include "DOMMediaStream.h"
#include "nsDirectoryServiceDefs.h"
#include "nsComponentManagerUtils.h"

#include "VideoUtils.h"
#include "MediaEngineCameraVideoSource.h"
#include "VideoSegment.h"
#include "AudioSegment.h"
#include "StreamTracks.h"
#include "MediaStreamGraph.h"

#include "MediaEngineWrapper.h"
#include "mozilla/dom/MediaStreamTrackBinding.h"

// WebRTC library includes follow
#include "webrtc/common.h"
#include "webrtc/video_engine/include/vie_capture.h"
#include "webrtc/video_engine/include/vie_render.h"
#include "CamerasChild.h"

#include "NullTransport.h"

namespace webrtc {
class I420VideoFrame;

namespace mozilla {

 * The WebRTC implementation of the MediaEngine interface.
class MediaEngineRemoteVideoSource : public MediaEngineCameraVideoSource,
                                     public webrtc::ExternalRenderer

  // ExternalRenderer
  int FrameSizeChange(unsigned int w, unsigned int h,
                      unsigned int streams) override;
  int DeliverFrame(unsigned char* buffer,
                   size_t size,
                   uint32_t time_stamp,
                   int64_t ntp_time,
                   int64_t render_time,
                   void *handle) override;
  // XXX!!!! FIX THIS
  int DeliverI420Frame(const webrtc::I420VideoFrame& webrtc_frame) override { return 0; };
  bool IsTextureSupported() override { return false; };

  // MediaEngineCameraVideoSource
  MediaEngineRemoteVideoSource(int aIndex, mozilla::camera::CaptureEngine aCapEngine,
                               dom::MediaSourceEnum aMediaSource,
                               const char* aMonitorName = "RemoteVideo.Monitor");

  nsresult Allocate(const dom::MediaTrackConstraints& aConstraints,
                    const MediaEnginePrefs& aPrefs,
                    const nsString& aDeviceId,
                    const nsACString& aOrigin,
                    AllocationHandle** aOutHandle,
                    const char** aOutBadConstraint) override;
  nsresult Deallocate(AllocationHandle* aHandle) override;
  nsresult Start(SourceMediaStream*, TrackID, const PrincipalHandle&) override;
  nsresult Stop(SourceMediaStream*, TrackID) override;
  nsresult Restart(AllocationHandle* aHandle,
                   const dom::MediaTrackConstraints& aConstraints,
                   const MediaEnginePrefs &aPrefs,
                   const nsString& aDeviceId,
                   const char** aOutBadConstraint) override;
  void NotifyPull(MediaStreamGraph* aGraph,
                  SourceMediaStream* aSource,
                  TrackID aId,
                  StreamTime aDesiredTime,
                  const PrincipalHandle& aPrincipalHandle) override;
  dom::MediaSourceEnum GetMediaSource() const override {
    return mMediaSource;

  bool ChooseCapability(const NormalizedConstraints &aConstraints,
                        const MediaEnginePrefs &aPrefs,
                        const nsString& aDeviceId) override;

  void Refresh(int aIndex);

  void Shutdown() override;

  ~MediaEngineRemoteVideoSource() { Shutdown(); }

  // Initialize the needed Video engine interfaces.
  void Init();
  size_t NumCapabilities() const override;
  void GetCapability(size_t aIndex, webrtc::CaptureCapability& aOut) const override;
  void SetLastCapability(const webrtc::CaptureCapability& aCapability);

  /* UpdateExisting - Centralized function to apply constraints and restart
   * device as needed, considering all allocations and changes to one.
   * aHandle           - New or existing handle, or null to update after removal.
   * aNewConstraints   - Constraints to be applied to existing handle, or null.
   * aPrefs            - As passed in (in case of changes in about:config).
   * aDeviceId         - As passed in (origin dependent).
   * aOutBadConstraint - Result: nonzero if failed to apply. Name of culprit.

  UpdateExisting(AllocationHandle* aHandle,
                 NormalizedConstraints* aNewConstraints,
                 const MediaEnginePrefs& aPrefs,
                 const nsString& aDeviceId,
                 const char** aOutBadConstraint);

  UpdateNew(AllocationHandle* aHandle,
            const MediaEnginePrefs& aPrefs,
            const nsString& aDeviceId,
            const char** aOutBadConstraint) {
    return UpdateExisting(aHandle, nullptr, aPrefs, aDeviceId, aOutBadConstraint);

  UpdateRemove(const MediaEnginePrefs& aPrefs,
               const nsString& aDeviceId,
               const char** aOutBadConstraint) {
    return UpdateExisting(nullptr, nullptr, aPrefs, aDeviceId, aOutBadConstraint);

  dom::MediaSourceEnum mMediaSource; // source of media (camera | application | screen)
  mozilla::camera::CaptureEngine mCapEngine;

  nsTArray<RefPtr<AllocationHandle>> mRegisteredHandles;

  // To only restart camera when needed, we keep track previous settings.
  webrtc::CaptureCapability mLastCapability;
  bool mInShutdown;