From a3ede6c5107a6d232ee115114a4130bfd0adac0c Mon Sep 17 00:00:00 2001 From: perkj Date: Tue, 8 Mar 2016 01:27:48 +0100 Subject: [PATCH] Renamed VideoSourceInterface to VideoTrackSourceInterface. Moved VideoSourceInterface to MediaStreamInterface.h Renamed VideoSourceTest to VideoCapturerTrackSourceTest Renamed VideoSource to VideoCaptureTrackSource and cl lint and cl format. BUG=webrtc:5426 TBR=pthatcher@webrtc.org Review URL: https://codereview.webrtc.org/1770003002 . Cr-Commit-Position: refs/heads/master@{#11893} --- .../webrtc/objc/RTCAVFoundationVideoSource.mm | 5 +- .../webrtc/objc/RTCPeerConnectionFactory.mm | 5 +- .../app/webrtc/objc/RTCVideoSource+Internal.h | 4 +- talk/app/webrtc/objc/RTCVideoSource.mm | 4 +- talk/app/webrtc/objc/RTCVideoTrack.mm | 2 +- webrtc/api/api.gyp | 4 +- webrtc/api/api_tests.gyp | 2 +- webrtc/api/java/jni/peerconnection_jni.cc | 17 +- webrtc/api/mediastreaminterface.h | 32 +- webrtc/api/mediastreamtrackproxy.h | 2 +- webrtc/api/objc/RTCAVFoundationVideoSource.mm | 6 +- webrtc/api/objc/RTCVideoSource+Private.h | 9 +- webrtc/api/objc/RTCVideoSource.mm | 6 +- webrtc/api/objc/RTCVideoTrack.mm | 2 +- webrtc/api/peerconnection.cc | 6 +- webrtc/api/peerconnection_unittest.cc | 2 +- webrtc/api/peerconnectionfactory.cc | 24 +- webrtc/api/peerconnectionfactory.h | 10 +- webrtc/api/peerconnectionfactory_unittest.cc | 4 +- webrtc/api/peerconnectionfactoryproxy.h | 11 +- webrtc/api/peerconnectioninterface.h | 12 +- .../api/peerconnectioninterface_unittest.cc | 6 +- webrtc/api/rtpsender.cc | 6 +- webrtc/api/rtpsenderreceiver_unittest.cc | 6 +- webrtc/api/test/peerconnectiontestwrapper.cc | 2 +- webrtc/api/videocapturertracksource.cc | 436 +++++++++++++++++- webrtc/api/videocapturertracksource.h | 87 +++- ...c => videocapturertracksource_unittest.cc} | 212 ++++----- webrtc/api/videosource.h | 91 +--- webrtc/api/videosourceinterface.h | 32 +- webrtc/api/videosourceproxy.h | 30 +- webrtc/api/videotrack.cc | 4 +- webrtc/api/videotrack.h | 11 +- webrtc/api/videotrack_unittest.cc | 10 +- 34 files changed, 750 insertions(+), 352 deletions(-) rename webrtc/api/{videosource_unittest.cc => videocapturertracksource_unittest.cc} (72%) diff --git a/talk/app/webrtc/objc/RTCAVFoundationVideoSource.mm b/talk/app/webrtc/objc/RTCAVFoundationVideoSource.mm index 525cead7da..9d82283142 100644 --- a/talk/app/webrtc/objc/RTCAVFoundationVideoSource.mm +++ b/talk/app/webrtc/objc/RTCAVFoundationVideoSource.mm @@ -39,9 +39,8 @@ NSParameterAssert(factory); rtc::scoped_ptr capturer; capturer.reset(new webrtc::AVFoundationVideoCapturer()); - rtc::scoped_refptr source = - factory.nativeFactory->CreateVideoSource(capturer.release(), - constraints.constraints); + rtc::scoped_refptr source = + factory.nativeFactory->CreateVideoSource(capturer.release(), constraints.constraints); return [super initWithMediaSource:source]; } diff --git a/talk/app/webrtc/objc/RTCPeerConnectionFactory.mm b/talk/app/webrtc/objc/RTCPeerConnectionFactory.mm index 3393eca16c..a60263a12f 100644 --- a/talk/app/webrtc/objc/RTCPeerConnectionFactory.mm +++ b/talk/app/webrtc/objc/RTCPeerConnectionFactory.mm @@ -125,9 +125,8 @@ if (!capturer) { return nil; } - rtc::scoped_refptr source = - self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer], - constraints.constraints); + rtc::scoped_refptr source = + self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer], constraints.constraints); return [[RTCVideoSource alloc] initWithMediaSource:source]; } diff --git a/talk/app/webrtc/objc/RTCVideoSource+Internal.h b/talk/app/webrtc/objc/RTCVideoSource+Internal.h index a13e71ed83..b1e47d3ab1 100644 --- a/talk/app/webrtc/objc/RTCVideoSource+Internal.h +++ b/talk/app/webrtc/objc/RTCVideoSource+Internal.h @@ -31,7 +31,7 @@ @interface RTCVideoSource (Internal) -@property(nonatomic, assign, readonly) - rtc::scoped_refptrvideoSource; +@property(nonatomic, assign, readonly) rtc::scoped_refptr + videoSource; @end diff --git a/talk/app/webrtc/objc/RTCVideoSource.mm b/talk/app/webrtc/objc/RTCVideoSource.mm index 5a1ed7415b..d106fe1e95 100644 --- a/talk/app/webrtc/objc/RTCVideoSource.mm +++ b/talk/app/webrtc/objc/RTCVideoSource.mm @@ -37,8 +37,8 @@ @implementation RTCVideoSource (Internal) -- (rtc::scoped_refptr)videoSource { - return static_cast(self.mediaSource.get()); +- (rtc::scoped_refptr)videoSource { + return static_cast(self.mediaSource.get()); } @end diff --git a/talk/app/webrtc/objc/RTCVideoTrack.mm b/talk/app/webrtc/objc/RTCVideoTrack.mm index e7d9f52ea7..b32f05f35e 100644 --- a/talk/app/webrtc/objc/RTCVideoTrack.mm +++ b/talk/app/webrtc/objc/RTCVideoTrack.mm @@ -63,7 +63,7 @@ (rtc::scoped_refptr)mediaTrack { if (self = [super initWithMediaTrack:mediaTrack]) { [self configure]; - rtc::scoped_refptr source = + rtc::scoped_refptr source = self.nativeVideoTrack->GetSource(); if (source) { _source = [[RTCVideoSource alloc] initWithMediaSource:source.get()]; diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp index 7f571c041d..1e32e36f15 100644 --- a/webrtc/api/api.gyp +++ b/webrtc/api/api.gyp @@ -307,8 +307,8 @@ 'statstypes.cc', 'statstypes.h', 'streamcollection.h', - 'videosource.cc', - 'videosource.h', + 'videocapturertracksource.cc', + 'videocapturertracksource.h', 'videosourceinterface.h', 'videosourceproxy.h', 'videotrack.cc', diff --git a/webrtc/api/api_tests.gyp b/webrtc/api/api_tests.gyp index 814784fe98..53285f90df 100644 --- a/webrtc/api/api_tests.gyp +++ b/webrtc/api/api_tests.gyp @@ -59,7 +59,7 @@ 'test/peerconnectiontestwrapper.h', 'test/peerconnectiontestwrapper.cc', 'test/testsdpstrings.h', - 'videosource_unittest.cc', + 'videocapturertracksource_unittest.cc', 'videotrack_unittest.cc', 'webrtcsdp_unittest.cc', 'webrtcsession_unittest.cc', diff --git a/webrtc/api/java/jni/peerconnection_jni.cc b/webrtc/api/java/jni/peerconnection_jni.cc index f7b9e90202..7a95737c9a 100644 --- a/webrtc/api/java/jni/peerconnection_jni.cc +++ b/webrtc/api/java/jni/peerconnection_jni.cc @@ -107,7 +107,7 @@ using webrtc::StatsObserver; using webrtc::StatsReport; using webrtc::StatsReports; using webrtc::VideoRendererInterface; -using webrtc::VideoSourceInterface; +using webrtc::VideoTrackSourceInterface; using webrtc::VideoTrackInterface; using webrtc::VideoTrackVector; using webrtc::kVideoCodecVP8; @@ -1225,13 +1225,13 @@ JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource)( jni, j_video_capturer, j_surface_texture_helper); rtc::scoped_ptr capturer( new webrtc::AndroidVideoCapturer(delegate)); - // Create a webrtc::VideoSourceInterface from the cricket::VideoCapturer, + // Create a webrtc::VideoTrackSourceInterface from the cricket::VideoCapturer, // native factory and constraints. scoped_ptr constraints( new ConstraintsWrapper(jni, j_constraints)); rtc::scoped_refptr factory( factoryFromJava(native_factory)); - rtc::scoped_refptr source( + rtc::scoped_refptr source( factory->CreateVideoSource(capturer.release(), constraints.get())); return (jlong)source.release(); } @@ -1241,10 +1241,9 @@ JOW(jlong, PeerConnectionFactory_nativeCreateVideoTrack)( jlong native_source) { rtc::scoped_refptr factory( factoryFromJava(native_factory)); - rtc::scoped_refptr track( - factory->CreateVideoTrack( - JavaToStdString(jni, id), - reinterpret_cast(native_source))); + rtc::scoped_refptr track(factory->CreateVideoTrack( + JavaToStdString(jni, id), + reinterpret_cast(native_source))); return (jlong)track.release(); } @@ -1887,12 +1886,12 @@ JOW(void, VideoRenderer_nativeCopyPlane)( } JOW(void, VideoSource_stop)(JNIEnv* jni, jclass, jlong j_p) { - reinterpret_cast(j_p)->Stop(); + reinterpret_cast(j_p)->Stop(); } JOW(void, VideoSource_restart)( JNIEnv* jni, jclass, jlong j_p_source, jlong j_p_format) { - reinterpret_cast(j_p_source)->Restart(); + reinterpret_cast(j_p_source)->Restart(); } JOW(jstring, MediaStreamTrack_nativeId)(JNIEnv* jni, jclass, jlong j_p) { diff --git a/webrtc/api/mediastreaminterface.h b/webrtc/api/mediastreaminterface.h index 100db08d04..c9ce4a5a7b 100644 --- a/webrtc/api/mediastreaminterface.h +++ b/webrtc/api/mediastreaminterface.h @@ -23,6 +23,7 @@ #include "webrtc/base/basictypes.h" #include "webrtc/base/refcount.h" #include "webrtc/base/scoped_ref_ptr.h" +#include "webrtc/media/base/mediachannel.h" #include "webrtc/media/base/videosinkinterface.h" #include "webrtc/media/base/videosourceinterface.h" @@ -120,17 +121,34 @@ class VideoRendererInterface virtual ~VideoRendererInterface() {} }; -class VideoSourceInterface; +// VideoTrackSourceInterface is a reference counted source used for VideoTracks. +// The same source can be used in multiple VideoTracks. +class VideoTrackSourceInterface + : public MediaSourceInterface, + public rtc::VideoSourceInterface { + public: + // Get access to the source implementation of cricket::VideoCapturer. + // This can be used for receiving frames and state notifications. + // But it should not be used for starting or stopping capturing. + // TODO(perkj): We are currently trying to replace all internal use of + // cricket::VideoCapturer with rtc::VideoSourceInterface. Once that + // refactoring is done, + // remove this method. + virtual cricket::VideoCapturer* GetVideoCapturer() = 0; + + virtual void Stop() = 0; + virtual void Restart() = 0; + + virtual const cricket::VideoOptions* options() const = 0; + + protected: + virtual ~VideoTrackSourceInterface() {} +}; class VideoTrackInterface : public MediaStreamTrackInterface, public rtc::VideoSourceInterface { public: - // Make an unqualified VideoSourceInterface resolve to - // webrtc::VideoSourceInterface, not our base class - // rtc::VideoSourceInterface. - using VideoSourceInterface = webrtc::VideoSourceInterface; - // AddRenderer and RemoveRenderer are for backwards compatibility // only. They are obsoleted by the methods of // rtc::VideoSourceInterface. @@ -147,7 +165,7 @@ class VideoTrackInterface void RemoveSink( rtc::VideoSinkInterface* sink) override{}; - virtual VideoSourceInterface* GetSource() const = 0; + virtual VideoTrackSourceInterface* GetSource() const = 0; // Return the track input sink. I.e., frames sent to this sink are // propagated to all renderers registered with the track. The diff --git a/webrtc/api/mediastreamtrackproxy.h b/webrtc/api/mediastreamtrackproxy.h index dca2d15561..ada82919b0 100644 --- a/webrtc/api/mediastreamtrackproxy.h +++ b/webrtc/api/mediastreamtrackproxy.h @@ -53,7 +53,7 @@ BEGIN_PROXY_MAP(VideoTrack) rtc::VideoSinkInterface*, const rtc::VideoSinkWants&) PROXY_METHOD1(void, RemoveSink, rtc::VideoSinkInterface*) - PROXY_CONSTMETHOD0(VideoSourceInterface*, GetSource) + PROXY_CONSTMETHOD0(VideoTrackSourceInterface*, GetSource) PROXY_METHOD0(rtc::VideoSinkInterface*, GetSink) PROXY_METHOD1(void, RegisterObserver, ObserverInterface*) diff --git a/webrtc/api/objc/RTCAVFoundationVideoSource.mm b/webrtc/api/objc/RTCAVFoundationVideoSource.mm index 868f626949..0cd015550c 100644 --- a/webrtc/api/objc/RTCAVFoundationVideoSource.mm +++ b/webrtc/api/objc/RTCAVFoundationVideoSource.mm @@ -21,9 +21,9 @@ NSParameterAssert(factory); rtc::scoped_ptr capturer; capturer.reset(new webrtc::AVFoundationVideoCapturer()); - rtc::scoped_refptr source = - factory.nativeFactory->CreateVideoSource(capturer.release(), - constraints.nativeConstraints.get()); + rtc::scoped_refptr source = + factory.nativeFactory->CreateVideoSource( + capturer.release(), constraints.nativeConstraints.get()); return [super initWithNativeVideoSource:source]; } diff --git a/webrtc/api/objc/RTCVideoSource+Private.h b/webrtc/api/objc/RTCVideoSource+Private.h index c363d3390e..be064bdd9b 100644 --- a/webrtc/api/objc/RTCVideoSource+Private.h +++ b/webrtc/api/objc/RTCVideoSource+Private.h @@ -17,15 +17,16 @@ NS_ASSUME_NONNULL_BEGIN @interface RTCVideoSource () /** - * The VideoSourceInterface object passed to this RTCVideoSource during + * The VideoTrackSourceInterface object passed to this RTCVideoSource during * construction. */ @property(nonatomic, readonly) - rtc::scoped_refptr nativeVideoSource; + rtc::scoped_refptr + nativeVideoSource; -/** Initialize an RTCVideoSource from a native VideoSourceInterface. */ +/** Initialize an RTCVideoSource from a native VideoTrackSourceInterface. */ - (instancetype)initWithNativeVideoSource: - (rtc::scoped_refptr)nativeVideoSource + (rtc::scoped_refptr)nativeVideoSource NS_DESIGNATED_INITIALIZER; + (webrtc::MediaSourceInterface::SourceState)nativeSourceStateForState: diff --git a/webrtc/api/objc/RTCVideoSource.mm b/webrtc/api/objc/RTCVideoSource.mm index 35f30e3b38..8e9c39d3dd 100644 --- a/webrtc/api/objc/RTCVideoSource.mm +++ b/webrtc/api/objc/RTCVideoSource.mm @@ -13,7 +13,7 @@ #import "webrtc/api/objc/RTCVideoSource+Private.h" @implementation RTCVideoSource { - rtc::scoped_refptr _nativeVideoSource; + rtc::scoped_refptr _nativeVideoSource; } - (RTCSourceState)state { @@ -27,12 +27,12 @@ #pragma mark - Private -- (rtc::scoped_refptr)nativeVideoSource { +- (rtc::scoped_refptr)nativeVideoSource { return _nativeVideoSource; } - (instancetype)initWithNativeVideoSource: - (rtc::scoped_refptr)nativeVideoSource { + (rtc::scoped_refptr)nativeVideoSource { NSParameterAssert(nativeVideoSource); if (self = [super init]) { _nativeVideoSource = nativeVideoSource; diff --git a/webrtc/api/objc/RTCVideoTrack.mm b/webrtc/api/objc/RTCVideoTrack.mm index 8a9e3d7fd6..5834af761b 100644 --- a/webrtc/api/objc/RTCVideoTrack.mm +++ b/webrtc/api/objc/RTCVideoTrack.mm @@ -58,7 +58,7 @@ - (RTCVideoSource *)source { if (!_source) { - rtc::scoped_refptr source = + rtc::scoped_refptr source = self.nativeVideoTrack->GetSource(); if (source) { _source = [[RTCVideoSource alloc] initWithNativeVideoSource:source.get()]; diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc index 6ea852d37e..c2039aeea4 100644 --- a/webrtc/api/peerconnection.cc +++ b/webrtc/api/peerconnection.cc @@ -29,7 +29,7 @@ #include "webrtc/api/rtpreceiver.h" #include "webrtc/api/rtpsender.h" #include "webrtc/api/streamcollection.h" -#include "webrtc/api/videosource.h" +#include "webrtc/api/videocapturertracksource.h" #include "webrtc/api/videotrack.h" #include "webrtc/base/arraysize.h" #include "webrtc/base/logging.h" @@ -404,8 +404,8 @@ class RemoteMediaStreamFactory { const std::string& track_id) { return AddTrack( stream, track_id, - VideoSource::Create(worker_thread_, new RemoteVideoCapturer(), - nullptr, true) + VideoCapturerTrackSource::Create( + worker_thread_, new RemoteVideoCapturer(), nullptr, true) .get()); } diff --git a/webrtc/api/peerconnection_unittest.cc b/webrtc/api/peerconnection_unittest.cc index 788a98181e..d0fb9ad4c5 100644 --- a/webrtc/api/peerconnection_unittest.cc +++ b/webrtc/api/peerconnection_unittest.cc @@ -403,7 +403,7 @@ class PeerConnectionTestClient : public webrtc::PeerConnectionObserver, cricket::FakeVideoCapturer* fake_capturer = new webrtc::FakePeriodicVideoCapturer(); video_capturers_.push_back(fake_capturer); - rtc::scoped_refptr source = + rtc::scoped_refptr source = peer_connection_factory_->CreateVideoSource(fake_capturer, &source_constraints); std::string label = stream_label + kVideoTrackLabelBase; diff --git a/webrtc/api/peerconnectionfactory.cc b/webrtc/api/peerconnectionfactory.cc index 40e845f4d6..22e450ca4e 100644 --- a/webrtc/api/peerconnectionfactory.cc +++ b/webrtc/api/peerconnectionfactory.cc @@ -21,7 +21,7 @@ #include "webrtc/api/peerconnection.h" #include "webrtc/api/peerconnectionfactoryproxy.h" #include "webrtc/api/peerconnectionproxy.h" -#include "webrtc/api/videosource.h" +#include "webrtc/api/videocapturertracksource.h" #include "webrtc/api/videosourceproxy.h" #include "webrtc/api/videotrack.h" #include "webrtc/base/bind.h" @@ -208,22 +208,23 @@ PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) { return source; } -rtc::scoped_refptr +rtc::scoped_refptr PeerConnectionFactory::CreateVideoSource( cricket::VideoCapturer* capturer, const MediaConstraintsInterface* constraints) { RTC_DCHECK(signaling_thread_->IsCurrent()); - rtc::scoped_refptr source(VideoSource::Create( - worker_thread_, capturer, constraints, false)); - return VideoSourceProxy::Create(signaling_thread_, source); + rtc::scoped_refptr source( + VideoCapturerTrackSource::Create(worker_thread_, capturer, constraints, + false)); + return VideoTrackSourceProxy::Create(signaling_thread_, source); } -rtc::scoped_refptr +rtc::scoped_refptr PeerConnectionFactory::CreateVideoSource(cricket::VideoCapturer* capturer) { RTC_DCHECK(signaling_thread_->IsCurrent()); - rtc::scoped_refptr source( - VideoSource::Create(worker_thread_, capturer, false)); - return VideoSourceProxy::Create(signaling_thread_, source); + rtc::scoped_refptr source( + VideoCapturerTrackSource::Create(worker_thread_, capturer, false)); + return VideoTrackSourceProxy::Create(signaling_thread_, source); } bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file, @@ -319,10 +320,9 @@ PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) { MediaStream::Create(label)); } -rtc::scoped_refptr -PeerConnectionFactory::CreateVideoTrack( +rtc::scoped_refptr PeerConnectionFactory::CreateVideoTrack( const std::string& id, - VideoSourceInterface* source) { + VideoTrackSourceInterface* source) { RTC_DCHECK(signaling_thread_->IsCurrent()); rtc::scoped_refptr track( VideoTrack::Create(id, source)); diff --git a/webrtc/api/peerconnectionfactory.h b/webrtc/api/peerconnectionfactory.h index 1bc40ed84a..b47f75a7ce 100644 --- a/webrtc/api/peerconnectionfactory.h +++ b/webrtc/api/peerconnectionfactory.h @@ -63,19 +63,19 @@ class PeerConnectionFactory : public PeerConnectionFactoryInterface { rtc::scoped_refptr CreateAudioSource( const MediaConstraintsInterface* constraints) override; - virtual rtc::scoped_refptr CreateVideoSource( + virtual rtc::scoped_refptr CreateVideoSource( cricket::VideoCapturer* capturer) override; // This version supports filtering on width, height and frame rate. // For the "constraints=null" case, use the version without constraints. // TODO(hta): Design a version without MediaConstraintsInterface. // https://bugs.chromium.org/p/webrtc/issues/detail?id=5617 - rtc::scoped_refptr CreateVideoSource( + rtc::scoped_refptr CreateVideoSource( cricket::VideoCapturer* capturer, const MediaConstraintsInterface* constraints) override; - rtc::scoped_refptr - CreateVideoTrack(const std::string& id, - VideoSourceInterface* video_source) override; + rtc::scoped_refptr CreateVideoTrack( + const std::string& id, + VideoTrackSourceInterface* video_source) override; rtc::scoped_refptr CreateAudioTrack(const std::string& id, diff --git a/webrtc/api/peerconnectionfactory_unittest.cc b/webrtc/api/peerconnectionfactory_unittest.cc index b874f95a79..2165937945 100644 --- a/webrtc/api/peerconnectionfactory_unittest.cc +++ b/webrtc/api/peerconnectionfactory_unittest.cc @@ -34,7 +34,7 @@ using webrtc::MediaStreamInterface; using webrtc::PeerConnectionFactoryInterface; using webrtc::PeerConnectionInterface; using webrtc::PeerConnectionObserver; -using webrtc::VideoSourceInterface; +using webrtc::VideoTrackSourceInterface; using webrtc::VideoTrackInterface; namespace { @@ -332,7 +332,7 @@ TEST_F(PeerConnectionFactoryTest, CreatePCUsingIPLiteralAddress) { TEST_F(PeerConnectionFactoryTest, LocalRendering) { cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer(); // The source take ownership of |capturer|. - rtc::scoped_refptr source( + rtc::scoped_refptr source( factory_->CreateVideoSource(capturer, NULL)); ASSERT_TRUE(source.get() != NULL); rtc::scoped_refptr track( diff --git a/webrtc/api/peerconnectionfactoryproxy.h b/webrtc/api/peerconnectionfactoryproxy.h index d83c300406..829bf8177e 100644 --- a/webrtc/api/peerconnectionfactoryproxy.h +++ b/webrtc/api/peerconnectionfactoryproxy.h @@ -50,14 +50,17 @@ BEGIN_PROXY_MAP(PeerConnectionFactory) PROXY_METHOD1(rtc::scoped_refptr, CreateAudioSource, const cricket::AudioOptions&) - PROXY_METHOD2(rtc::scoped_refptr, - CreateVideoSource, cricket::VideoCapturer*, + PROXY_METHOD2(rtc::scoped_refptr, + CreateVideoSource, + cricket::VideoCapturer*, const MediaConstraintsInterface*) - PROXY_METHOD1(rtc::scoped_refptr, + PROXY_METHOD1(rtc::scoped_refptr, CreateVideoSource, cricket::VideoCapturer*) PROXY_METHOD2(rtc::scoped_refptr, - CreateVideoTrack, const std::string&, VideoSourceInterface*) + CreateVideoTrack, + const std::string&, + VideoTrackSourceInterface*) PROXY_METHOD2(rtc::scoped_refptr, CreateAudioTrack, const std::string&, AudioSourceInterface*) PROXY_METHOD2(bool, StartAecDump, rtc::PlatformFile, int64_t) diff --git a/webrtc/api/peerconnectioninterface.h b/webrtc/api/peerconnectioninterface.h index 7a83343862..f6c8cbaf00 100644 --- a/webrtc/api/peerconnectioninterface.h +++ b/webrtc/api/peerconnectioninterface.h @@ -563,23 +563,23 @@ class PeerConnectionFactoryInterface : public rtc::RefCountInterface { virtual rtc::scoped_refptr CreateAudioSource( const MediaConstraintsInterface* constraints) = 0; - // Creates a VideoSourceInterface. The new source take ownership of + // Creates a VideoTrackSourceInterface. The new source take ownership of // |capturer|. - virtual rtc::scoped_refptr CreateVideoSource( + virtual rtc::scoped_refptr CreateVideoSource( cricket::VideoCapturer* capturer) = 0; // A video source creator that allows selection of resolution and frame rate. // |constraints| decides video resolution and frame rate but can // be NULL. // In the NULL case, use the version above. - virtual rtc::scoped_refptr CreateVideoSource( + virtual rtc::scoped_refptr CreateVideoSource( cricket::VideoCapturer* capturer, const MediaConstraintsInterface* constraints) = 0; // Creates a new local VideoTrack. The same |source| can be used in several // tracks. - virtual rtc::scoped_refptr - CreateVideoTrack(const std::string& label, - VideoSourceInterface* source) = 0; + virtual rtc::scoped_refptr CreateVideoTrack( + const std::string& label, + VideoTrackSourceInterface* source) = 0; // Creates an new AudioTrack. At the moment |source| can be NULL. virtual rtc::scoped_refptr diff --git a/webrtc/api/peerconnectioninterface_unittest.cc b/webrtc/api/peerconnectioninterface_unittest.cc index 6d324c0a6f..0448526753 100644 --- a/webrtc/api/peerconnectioninterface_unittest.cc +++ b/webrtc/api/peerconnectioninterface_unittest.cc @@ -27,7 +27,7 @@ #include "webrtc/api/test/fakedtlsidentitystore.h" #include "webrtc/api/test/mockpeerconnectionobservers.h" #include "webrtc/api/test/testsdpstrings.h" -#include "webrtc/api/videosource.h" +#include "webrtc/api/videocapturertracksource.h" #include "webrtc/api/videotrack.h" #include "webrtc/base/gunit.h" #include "webrtc/base/scoped_ptr.h" @@ -262,7 +262,7 @@ using webrtc::SdpParseError; using webrtc::SessionDescriptionInterface; using webrtc::StreamCollection; using webrtc::StreamCollectionInterface; -using webrtc::VideoSourceInterface; +using webrtc::VideoTrackSourceInterface; using webrtc::VideoTrack; using webrtc::VideoTrackInterface; @@ -597,7 +597,7 @@ class PeerConnectionInterfaceTest : public testing::Test { // Create a local stream. scoped_refptr stream( pc_factory_->CreateLocalMediaStream(label)); - scoped_refptr video_source( + scoped_refptr video_source( pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL)); scoped_refptr video_track( pc_factory_->CreateVideoTrack(label + "v0", video_source)); diff --git a/webrtc/api/rtpsender.cc b/webrtc/api/rtpsender.cc index 1ca8fc1d0a..c5db92946d 100644 --- a/webrtc/api/rtpsender.cc +++ b/webrtc/api/rtpsender.cc @@ -268,7 +268,7 @@ bool VideoRtpSender::SetTrack(MediaStreamTrackInterface* track) { // Update video provider. if (can_send_track()) { - VideoSourceInterface* source = track_->GetSource(); + VideoTrackSourceInterface* source = track_->GetSource(); // TODO(deadbeef): If SetTrack is called with a disabled track, and the // previous track was enabled, this could cause a frame from the new track // to slip out. Really, what we need is for SetCaptureDevice and @@ -296,7 +296,7 @@ void VideoRtpSender::SetSsrc(uint32_t ssrc) { } ssrc_ = ssrc; if (can_send_track()) { - VideoSourceInterface* source = track_->GetSource(); + VideoTrackSourceInterface* source = track_->GetSource(); provider_->SetCaptureDevice(ssrc_, source ? source->GetVideoCapturer() : nullptr); SetVideoSend(); @@ -321,7 +321,7 @@ void VideoRtpSender::Stop() { void VideoRtpSender::SetVideoSend() { RTC_DCHECK(!stopped_ && can_send_track()); const cricket::VideoOptions* options = nullptr; - VideoSourceInterface* source = track_->GetSource(); + VideoTrackSourceInterface* source = track_->GetSource(); if (track_->enabled() && source) { options = source->options(); } diff --git a/webrtc/api/rtpsenderreceiver_unittest.cc b/webrtc/api/rtpsenderreceiver_unittest.cc index ca88a3bc72..5dc97c64ea 100644 --- a/webrtc/api/rtpsenderreceiver_unittest.cc +++ b/webrtc/api/rtpsenderreceiver_unittest.cc @@ -19,7 +19,7 @@ #include "webrtc/api/rtpreceiver.h" #include "webrtc/api/rtpsender.h" #include "webrtc/api/streamcollection.h" -#include "webrtc/api/videosource.h" +#include "webrtc/api/videocapturertracksource.h" #include "webrtc/api/videotrack.h" #include "webrtc/base/gunit.h" #include "webrtc/media/base/fakevideocapturer.h" @@ -78,7 +78,7 @@ class MockVideoProvider : public VideoProviderInterface { const cricket::VideoOptions* options)); }; -class FakeVideoSource : public Notifier { +class FakeVideoSource : public Notifier { public: static rtc::scoped_refptr Create(bool remote) { return new rtc::RefCountedObject(remote); @@ -113,7 +113,7 @@ class RtpSenderReceiverTest : public testing::Test { } void AddVideoTrack(bool remote) { - rtc::scoped_refptr source( + rtc::scoped_refptr source( FakeVideoSource::Create(remote)); video_track_ = VideoTrack::Create(kVideoTrackId, source); EXPECT_TRUE(stream_->AddTrack(video_track_)); diff --git a/webrtc/api/test/peerconnectiontestwrapper.cc b/webrtc/api/test/peerconnectiontestwrapper.cc index 42671567b1..353f0ee349 100644 --- a/webrtc/api/test/peerconnectiontestwrapper.cc +++ b/webrtc/api/test/peerconnectiontestwrapper.cc @@ -267,7 +267,7 @@ rtc::scoped_refptr FakeConstraints constraints = video_constraints; constraints.SetMandatoryMaxFrameRate(10); - rtc::scoped_refptr source = + rtc::scoped_refptr source = peer_connection_factory_->CreateVideoSource( new webrtc::FakePeriodicVideoCapturer(), &constraints); std::string videotrack_label = label + kVideoTrackLabelBase; diff --git a/webrtc/api/videocapturertracksource.cc b/webrtc/api/videocapturertracksource.cc index aeb2516b36..c505e86bd5 100644 --- a/webrtc/api/videocapturertracksource.cc +++ b/webrtc/api/videocapturertracksource.cc @@ -1,5 +1,4 @@ /* - /* * Copyright 2012 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license @@ -11,5 +10,436 @@ #include "webrtc/api/videocapturertracksource.h" -// TODO(perkj): This file is added to prepare for changing name of VideoSource -// to VideoCapturerTrackSource. +#include +#include +#include + +#include "webrtc/api/mediaconstraintsinterface.h" +#include "webrtc/base/arraysize.h" + +using cricket::CaptureState; +using webrtc::MediaConstraintsInterface; +using webrtc::MediaSourceInterface; + +namespace { + +const double kRoundingTruncation = 0.0005; + +// Default resolution. If no constraint is specified, this is the resolution we +// will use. +static const cricket::VideoFormatPod kDefaultFormat = { + 640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}; + +// List of formats used if the camera doesn't support capability enumeration. +static const cricket::VideoFormatPod kVideoFormats[] = { + {1920, 1080, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, + {1280, 720, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, + {960, 720, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, + {640, 360, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, + {640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, + {320, 240, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, + {320, 180, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}}; + +MediaSourceInterface::SourceState GetReadyState(cricket::CaptureState state) { + switch (state) { + case cricket::CS_STARTING: + return MediaSourceInterface::kInitializing; + case cricket::CS_RUNNING: + return MediaSourceInterface::kLive; + case cricket::CS_FAILED: + case cricket::CS_STOPPED: + return MediaSourceInterface::kEnded; + case cricket::CS_PAUSED: + return MediaSourceInterface::kMuted; + default: + ASSERT(false && "GetReadyState unknown state"); + } + return MediaSourceInterface::kEnded; +} + +void SetUpperLimit(int new_limit, int* original_limit) { + if (*original_limit < 0 || new_limit < *original_limit) + *original_limit = new_limit; +} + +// Updates |format_upper_limit| from |constraint|. +// If constraint.maxFoo is smaller than format_upper_limit.foo, +// set format_upper_limit.foo to constraint.maxFoo. +void SetUpperLimitFromConstraint( + const MediaConstraintsInterface::Constraint& constraint, + cricket::VideoFormat* format_upper_limit) { + if (constraint.key == MediaConstraintsInterface::kMaxWidth) { + int value = rtc::FromString(constraint.value); + SetUpperLimit(value, &(format_upper_limit->width)); + } else if (constraint.key == MediaConstraintsInterface::kMaxHeight) { + int value = rtc::FromString(constraint.value); + SetUpperLimit(value, &(format_upper_limit->height)); + } +} + +// Fills |format_out| with the max width and height allowed by |constraints|. +void FromConstraintsForScreencast( + const MediaConstraintsInterface::Constraints& constraints, + cricket::VideoFormat* format_out) { + typedef MediaConstraintsInterface::Constraints::const_iterator + ConstraintsIterator; + + cricket::VideoFormat upper_limit(-1, -1, 0, 0); + for (ConstraintsIterator constraints_it = constraints.begin(); + constraints_it != constraints.end(); ++constraints_it) + SetUpperLimitFromConstraint(*constraints_it, &upper_limit); + + if (upper_limit.width >= 0) + format_out->width = upper_limit.width; + if (upper_limit.height >= 0) + format_out->height = upper_limit.height; +} + +// Returns true if |constraint| is fulfilled. |format_out| can differ from +// |format_in| if the format is changed by the constraint. Ie - the frame rate +// can be changed by setting maxFrameRate. +bool NewFormatWithConstraints( + const MediaConstraintsInterface::Constraint& constraint, + const cricket::VideoFormat& format_in, + bool mandatory, + cricket::VideoFormat* format_out) { + ASSERT(format_out != NULL); + *format_out = format_in; + + if (constraint.key == MediaConstraintsInterface::kMinWidth) { + int value = rtc::FromString(constraint.value); + return (value <= format_in.width); + } else if (constraint.key == MediaConstraintsInterface::kMaxWidth) { + int value = rtc::FromString(constraint.value); + return (value >= format_in.width); + } else if (constraint.key == MediaConstraintsInterface::kMinHeight) { + int value = rtc::FromString(constraint.value); + return (value <= format_in.height); + } else if (constraint.key == MediaConstraintsInterface::kMaxHeight) { + int value = rtc::FromString(constraint.value); + return (value >= format_in.height); + } else if (constraint.key == MediaConstraintsInterface::kMinFrameRate) { + int value = rtc::FromString(constraint.value); + return (value <= cricket::VideoFormat::IntervalToFps(format_in.interval)); + } else if (constraint.key == MediaConstraintsInterface::kMaxFrameRate) { + int value = rtc::FromString(constraint.value); + if (value == 0) { + if (mandatory) { + // TODO(ronghuawu): Convert the constraint value to float when sub-1fps + // is supported by the capturer. + return false; + } else { + value = 1; + } + } + if (value <= cricket::VideoFormat::IntervalToFps(format_in.interval)) + format_out->interval = cricket::VideoFormat::FpsToInterval(value); + return true; + } else if (constraint.key == MediaConstraintsInterface::kMinAspectRatio) { + double value = rtc::FromString(constraint.value); + // The aspect ratio in |constraint.value| has been converted to a string and + // back to a double, so it may have a rounding error. + // E.g if the value 1/3 is converted to a string, the string will not have + // infinite length. + // We add a margin of 0.0005 which is high enough to detect the same aspect + // ratio but small enough to avoid matching wrong aspect ratios. + double ratio = static_cast(format_in.width) / format_in.height; + return (value <= ratio + kRoundingTruncation); + } else if (constraint.key == MediaConstraintsInterface::kMaxAspectRatio) { + double value = rtc::FromString(constraint.value); + double ratio = static_cast(format_in.width) / format_in.height; + // Subtract 0.0005 to avoid rounding problems. Same as above. + const double kRoundingTruncation = 0.0005; + return (value >= ratio - kRoundingTruncation); + } else if (constraint.key == MediaConstraintsInterface::kNoiseReduction) { + // These are actually options, not constraints, so they can be satisfied + // regardless of the format. + return true; + } + LOG(LS_WARNING) << "Found unknown MediaStream constraint. Name:" + << constraint.key << " Value:" << constraint.value; + return false; +} + +// Removes cricket::VideoFormats from |formats| that don't meet |constraint|. +void FilterFormatsByConstraint( + const MediaConstraintsInterface::Constraint& constraint, + bool mandatory, + std::vector* formats) { + std::vector::iterator format_it = formats->begin(); + while (format_it != formats->end()) { + // Modify the format_it to fulfill the constraint if possible. + // Delete it otherwise. + if (!NewFormatWithConstraints(constraint, (*format_it), mandatory, + &(*format_it))) { + format_it = formats->erase(format_it); + } else { + ++format_it; + } + } +} + +// Returns a vector of cricket::VideoFormat that best match |constraints|. +std::vector FilterFormats( + const MediaConstraintsInterface::Constraints& mandatory, + const MediaConstraintsInterface::Constraints& optional, + const std::vector& supported_formats) { + typedef MediaConstraintsInterface::Constraints::const_iterator + ConstraintsIterator; + std::vector candidates = supported_formats; + + for (ConstraintsIterator constraints_it = mandatory.begin(); + constraints_it != mandatory.end(); ++constraints_it) + FilterFormatsByConstraint(*constraints_it, true, &candidates); + + if (candidates.size() == 0) + return candidates; + + // Ok - all mandatory checked and we still have a candidate. + // Let's try filtering using the optional constraints. + for (ConstraintsIterator constraints_it = optional.begin(); + constraints_it != optional.end(); ++constraints_it) { + std::vector current_candidates = candidates; + FilterFormatsByConstraint(*constraints_it, false, ¤t_candidates); + if (current_candidates.size() > 0) { + candidates = current_candidates; + } + } + + // We have done as good as we can to filter the supported resolutions. + return candidates; +} + +// Find the format that best matches the default video size. +// Constraints are optional and since the performance of a video call +// might be bad due to bitrate limitations, CPU, and camera performance, +// it is better to select a resolution that is as close as possible to our +// default and still meets the contraints. +const cricket::VideoFormat& GetBestCaptureFormat( + const std::vector& formats) { + ASSERT(formats.size() > 0); + + int default_area = kDefaultFormat.width * kDefaultFormat.height; + + std::vector::const_iterator it = formats.begin(); + std::vector::const_iterator best_it = formats.begin(); + int best_diff_area = std::abs(default_area - it->width * it->height); + int64_t best_diff_interval = kDefaultFormat.interval; + for (; it != formats.end(); ++it) { + int diff_area = std::abs(default_area - it->width * it->height); + int64_t diff_interval = std::abs(kDefaultFormat.interval - it->interval); + if (diff_area < best_diff_area || + (diff_area == best_diff_area && diff_interval < best_diff_interval)) { + best_diff_area = diff_area; + best_diff_interval = diff_interval; + best_it = it; + } + } + return *best_it; +} + +// Set |option| to the highest-priority value of |key| in the constraints. +// Return false if the key is mandatory, and the value is invalid. +bool ExtractOption(const MediaConstraintsInterface* all_constraints, + const std::string& key, + rtc::Optional* option) { + size_t mandatory = 0; + bool value; + if (FindConstraint(all_constraints, key, &value, &mandatory)) { + *option = rtc::Optional(value); + return true; + } + + return mandatory == 0; +} + +// Search |all_constraints| for known video options. Apply all options that are +// found with valid values, and return false if any mandatory video option was +// found with an invalid value. +bool ExtractVideoOptions(const MediaConstraintsInterface* all_constraints, + cricket::VideoOptions* options) { + bool all_valid = true; + + all_valid &= + ExtractOption(all_constraints, MediaConstraintsInterface::kNoiseReduction, + &(options->video_noise_reduction)); + + return all_valid; +} + +} // anonymous namespace + +namespace webrtc { + +rtc::scoped_refptr VideoCapturerTrackSource::Create( + rtc::Thread* worker_thread, + cricket::VideoCapturer* capturer, + const webrtc::MediaConstraintsInterface* constraints, + bool remote) { + RTC_DCHECK(worker_thread != NULL); + RTC_DCHECK(capturer != NULL); + rtc::scoped_refptr source( + new rtc::RefCountedObject(worker_thread, + capturer, remote)); + source->Initialize(constraints); + return source; +} + +rtc::scoped_refptr VideoCapturerTrackSource::Create( + rtc::Thread* worker_thread, + cricket::VideoCapturer* capturer, + bool remote) { + RTC_DCHECK(worker_thread != NULL); + RTC_DCHECK(capturer != NULL); + rtc::scoped_refptr source( + new rtc::RefCountedObject(worker_thread, + capturer, remote)); + source->Initialize(nullptr); + return source; +} + +VideoCapturerTrackSource::VideoCapturerTrackSource( + rtc::Thread* worker_thread, + cricket::VideoCapturer* capturer, + bool remote) + : signaling_thread_(rtc::Thread::Current()), + worker_thread_(worker_thread), + video_capturer_(capturer), + started_(false), + state_(kInitializing), + remote_(remote) { + video_capturer_->SignalStateChange.connect( + this, &VideoCapturerTrackSource::OnStateChange); +} + +VideoCapturerTrackSource::~VideoCapturerTrackSource() { + video_capturer_->SignalStateChange.disconnect(this); + Stop(); +} + +void VideoCapturerTrackSource::Initialize( + const webrtc::MediaConstraintsInterface* constraints) { + std::vector formats = + *video_capturer_->GetSupportedFormats(); + if (formats.empty()) { + if (video_capturer_->IsScreencast()) { + // The screen capturer can accept any resolution and we will derive the + // format from the constraints if any. + // Note that this only affects tab capturing, not desktop capturing, + // since the desktop capturer does not respect the VideoFormat passed in. + formats.push_back(cricket::VideoFormat(kDefaultFormat)); + } else { + // The VideoCapturer implementation doesn't support capability + // enumeration. We need to guess what the camera supports. + for (int i = 0; i < arraysize(kVideoFormats); ++i) { + formats.push_back(cricket::VideoFormat(kVideoFormats[i])); + } + } + } + + if (constraints) { + MediaConstraintsInterface::Constraints mandatory_constraints = + constraints->GetMandatory(); + MediaConstraintsInterface::Constraints optional_constraints; + optional_constraints = constraints->GetOptional(); + + if (video_capturer_->IsScreencast()) { + // Use the maxWidth and maxHeight allowed by constraints for screencast. + FromConstraintsForScreencast(mandatory_constraints, &(formats[0])); + } + + formats = + FilterFormats(mandatory_constraints, optional_constraints, formats); + } + + if (formats.size() == 0) { + LOG(LS_WARNING) << "Failed to find a suitable video format."; + SetState(kEnded); + return; + } + + cricket::VideoOptions options; + if (!ExtractVideoOptions(constraints, &options)) { + LOG(LS_WARNING) << "Could not satisfy mandatory options."; + SetState(kEnded); + return; + } + options_.SetAll(options); + options_.is_screencast = rtc::Optional(video_capturer_->IsScreencast()); + + format_ = GetBestCaptureFormat(formats); + // Start the camera with our best guess. + // TODO(perkj): Should we try again with another format it it turns out that + // the camera doesn't produce frames with the correct format? Or will + // cricket::VideCapturer be able to re-scale / crop to the requested + // resolution? + if (!worker_thread_->Invoke( + rtc::Bind(&cricket::VideoCapturer::StartCapturing, + video_capturer_.get(), format_))) { + SetState(kEnded); + return; + } + started_ = true; + // Initialize hasn't succeeded until a successful state change has occurred. +} + +void VideoCapturerTrackSource::Stop() { + if (!started_) { + return; + } + started_ = false; + worker_thread_->Invoke( + rtc::Bind(&cricket::VideoCapturer::Stop, video_capturer_.get())); +} + +void VideoCapturerTrackSource::Restart() { + if (started_) { + return; + } + if (!worker_thread_->Invoke( + rtc::Bind(&cricket::VideoCapturer::StartCapturing, + video_capturer_.get(), format_))) { + SetState(kEnded); + return; + } + started_ = true; +} + +void VideoCapturerTrackSource::AddOrUpdateSink( + rtc::VideoSinkInterface* sink, + const rtc::VideoSinkWants& wants) { + worker_thread_->Invoke( + rtc::Bind(&cricket::VideoCapturer::AddOrUpdateSink, video_capturer_.get(), + sink, wants)); +} + +void VideoCapturerTrackSource::RemoveSink( + rtc::VideoSinkInterface* output) { + worker_thread_->Invoke(rtc::Bind(&cricket::VideoCapturer::RemoveSink, + video_capturer_.get(), output)); +} + +// OnStateChange listens to the cricket::VideoCapturer::SignalStateChange. +void VideoCapturerTrackSource::OnStateChange( + cricket::VideoCapturer* capturer, + cricket::CaptureState capture_state) { + if (rtc::Thread::Current() != signaling_thread_) { + invoker_.AsyncInvoke( + signaling_thread_, rtc::Bind(&VideoCapturerTrackSource::OnStateChange, + this, capturer, capture_state)); + return; + } + + if (capturer == video_capturer_.get()) { + SetState(GetReadyState(capture_state)); + } +} + +void VideoCapturerTrackSource::SetState(SourceState new_state) { + if (state_ != new_state) { + state_ = new_state; + FireOnChanged(); + } +} + +} // namespace webrtc diff --git a/webrtc/api/videocapturertracksource.h b/webrtc/api/videocapturertracksource.h index 15f8bf137e..f086d793e7 100644 --- a/webrtc/api/videocapturertracksource.h +++ b/webrtc/api/videocapturertracksource.h @@ -11,7 +11,88 @@ #ifndef WEBRTC_API_VIDEOCAPTURERTRACKSOURCE_H_ #define WEBRTC_API_VIDEOCAPTURERTRACKSOURCE_H_ -// TODO(perkj): This file is added to prepare for changing name of VideoSource -// to VideoCapturerTrackSource. +#include -#endif // WEBRTC_API_VIDEOCAPTURERTRACKSOURCE_H_ +#include "webrtc/api/mediastreaminterface.h" +#include "webrtc/api/notifier.h" +#include "webrtc/api/videosourceinterface.h" +#include "webrtc/api/videotrackrenderers.h" +#include "webrtc/base/asyncinvoker.h" +#include "webrtc/base/scoped_ptr.h" +#include "webrtc/base/sigslot.h" +#include "webrtc/media/base/videosinkinterface.h" +#include "webrtc/media/base/videocapturer.h" +#include "webrtc/media/base/videocommon.h" + +// VideoCapturerTrackSource implements VideoTrackSourceInterface. It owns a +// cricket::VideoCapturer and make sure the camera is started at a resolution +// that honors the constraints. +// The state is set depending on the result of starting the capturer. +// If the constraint can't be met or the capturer fails to start, the state +// transition to kEnded, otherwise it transitions to kLive. +namespace webrtc { + +class MediaConstraintsInterface; + +class VideoCapturerTrackSource : public Notifier, + public sigslot::has_slots<> { + public: + // Creates an instance of VideoCapturerTrackSource. + // VideoCapturerTrackSource take ownership of |capturer|. + // |constraints| can be NULL and in that case the camera is opened using a + // default resolution. + static rtc::scoped_refptr Create( + rtc::Thread* worker_thread, + cricket::VideoCapturer* capturer, + const webrtc::MediaConstraintsInterface* constraints, + bool remote); + + static rtc::scoped_refptr Create( + rtc::Thread* worker_thread, + cricket::VideoCapturer* capturer, + bool remote); + + SourceState state() const override { return state_; } + bool remote() const override { return remote_; } + + virtual const cricket::VideoOptions* options() const { return &options_; } + + virtual cricket::VideoCapturer* GetVideoCapturer() { + return video_capturer_.get(); + } + + void Stop() override; + void Restart() override; + + void AddOrUpdateSink(rtc::VideoSinkInterface* sink, + const rtc::VideoSinkWants& wants) override; + void RemoveSink(rtc::VideoSinkInterface* sink) override; + + protected: + VideoCapturerTrackSource(rtc::Thread* worker_thread, + cricket::VideoCapturer* capturer, + bool remote); + virtual ~VideoCapturerTrackSource(); + void Initialize(const webrtc::MediaConstraintsInterface* constraints); + + private: + void OnStateChange(cricket::VideoCapturer* capturer, + cricket::CaptureState capture_state); + void SetState(SourceState new_state); + + rtc::Thread* signaling_thread_; + rtc::Thread* worker_thread_; + rtc::AsyncInvoker invoker_; + rtc::scoped_ptr video_capturer_; + bool started_; + rtc::scoped_ptr frame_input_; + + cricket::VideoFormat format_; + cricket::VideoOptions options_; + SourceState state_; + const bool remote_; +}; + +} // namespace webrtc + +#endif // WEBRTC_API_VIDEOCAPTURERTRACKSOURCE_H_ diff --git a/webrtc/api/videosource_unittest.cc b/webrtc/api/videocapturertracksource_unittest.cc similarity index 72% rename from webrtc/api/videosource_unittest.cc rename to webrtc/api/videocapturertracksource_unittest.cc index cfa1e721b8..e059ef312c 100644 --- a/webrtc/api/videosource_unittest.cc +++ b/webrtc/api/videocapturertracksource_unittest.cc @@ -13,7 +13,7 @@ #include "webrtc/api/remotevideocapturer.h" #include "webrtc/api/test/fakeconstraints.h" -#include "webrtc/api/videosource.h" +#include "webrtc/api/videocapturertracksource.h" #include "webrtc/base/gunit.h" #include "webrtc/media/base/fakemediaengine.h" #include "webrtc/media/base/fakevideocapturer.h" @@ -21,11 +21,11 @@ #include "webrtc/media/engine/webrtcvideoframe.h" using webrtc::FakeConstraints; -using webrtc::VideoSource; +using webrtc::VideoCapturerTrackSource; using webrtc::MediaConstraintsInterface; using webrtc::MediaSourceInterface; using webrtc::ObserverInterface; -using webrtc::VideoSourceInterface; +using webrtc::VideoTrackSourceInterface; namespace { @@ -34,27 +34,30 @@ const int kMaxWaitMs = 100; } // anonymous namespace - // TestVideoCapturer extends cricket::FakeVideoCapturer so it can be used for // testing without known camera formats. // It keeps its own lists of cricket::VideoFormats for the unit tests in this // file. class TestVideoCapturer : public cricket::FakeVideoCapturer { public: - TestVideoCapturer(bool is_screencast) - : FakeVideoCapturer(is_screencast), - test_without_formats_(false) { + explicit TestVideoCapturer(bool is_screencast) + : FakeVideoCapturer(is_screencast), test_without_formats_(false) { std::vector formats; - formats.push_back(cricket::VideoFormat(1280, 720, - cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420)); - formats.push_back(cricket::VideoFormat(640, 480, - cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420)); - formats.push_back(cricket::VideoFormat(640, 400, - cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420)); - formats.push_back(cricket::VideoFormat(320, 240, - cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420)); - formats.push_back(cricket::VideoFormat(352, 288, - cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420)); + formats.push_back( + cricket::VideoFormat(1280, 720, cricket::VideoFormat::FpsToInterval(30), + cricket::FOURCC_I420)); + formats.push_back( + cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30), + cricket::FOURCC_I420)); + formats.push_back( + cricket::VideoFormat(640, 400, cricket::VideoFormat::FpsToInterval(30), + cricket::FOURCC_I420)); + formats.push_back( + cricket::VideoFormat(320, 240, cricket::VideoFormat::FpsToInterval(30), + cricket::FOURCC_I420)); + formats.push_back( + cricket::VideoFormat(352, 288, cricket::VideoFormat::FpsToInterval(30), + cricket::FOURCC_I420)); ResetSupportedFormats(formats); } @@ -84,8 +87,7 @@ class TestVideoCapturer : public cricket::FakeVideoCapturer { *best_format = desired; return true; } - return FakeVideoCapturer::GetBestCaptureFormat(desired, - best_format); + return FakeVideoCapturer::GetBestCaptureFormat(desired, best_format); } private: @@ -94,25 +96,19 @@ class TestVideoCapturer : public cricket::FakeVideoCapturer { class StateObserver : public ObserverInterface { public: - explicit StateObserver(VideoSourceInterface* source) - : state_(source->state()), - source_(source) { - } - virtual void OnChanged() { - state_ = source_->state(); - } + explicit StateObserver(VideoTrackSourceInterface* source) + : state_(source->state()), source_(source) {} + virtual void OnChanged() { state_ = source_->state(); } MediaSourceInterface::SourceState state() const { return state_; } private: MediaSourceInterface::SourceState state_; - rtc::scoped_refptr source_; + rtc::scoped_refptr source_; }; -class VideoSourceTest : public testing::Test { +class VideoCapturerTrackSourceTest : public testing::Test { protected: - VideoSourceTest() { - InitCapturer(false); - } + VideoCapturerTrackSourceTest() { InitCapturer(false); } void InitCapturer(bool is_screencast) { capturer_cleanup_ = rtc::scoped_ptr( new TestVideoCapturer(is_screencast)); @@ -121,16 +117,14 @@ class VideoSourceTest : public testing::Test { void InitScreencast() { InitCapturer(true); } - void CreateVideoSource() { - CreateVideoSource(NULL); - } + void CreateVideoCapturerSource() { CreateVideoCapturerSource(NULL); } - void CreateVideoSource( + void CreateVideoCapturerSource( const webrtc::MediaConstraintsInterface* constraints) { // VideoSource take ownership of |capturer_| - source_ = - VideoSource::Create(rtc::Thread::Current(), capturer_cleanup_.release(), - constraints, false); + source_ = VideoCapturerTrackSource::Create(rtc::Thread::Current(), + capturer_cleanup_.release(), + constraints, false); ASSERT_TRUE(source_.get() != NULL); EXPECT_EQ(capturer_, source_->GetVideoCapturer()); @@ -144,16 +138,15 @@ class VideoSourceTest : public testing::Test { TestVideoCapturer* capturer_; cricket::FakeVideoRenderer renderer_; rtc::scoped_ptr state_observer_; - rtc::scoped_refptr source_; + rtc::scoped_refptr source_; }; - // Test that a VideoSource transition to kLive state when the capture // device have started and kEnded if it is stopped. // It also test that an output can receive video frames. -TEST_F(VideoSourceTest, CapturerStartStop) { +TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) { // Initialize without constraints. - CreateVideoSource(); + CreateVideoCapturerSource(); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); @@ -166,9 +159,9 @@ TEST_F(VideoSourceTest, CapturerStartStop) { } // Test that a VideoSource can be stopped and restarted. -TEST_F(VideoSourceTest, StopRestart) { +TEST_F(VideoCapturerTrackSourceTest, StopRestart) { // Initialize without constraints. - CreateVideoSource(); + CreateVideoCapturerSource(); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); @@ -191,9 +184,9 @@ TEST_F(VideoSourceTest, StopRestart) { // Test start stop with a remote VideoSource - the video source that has a // RemoteVideoCapturer and takes video frames from FrameInput. -TEST_F(VideoSourceTest, StartStopRemote) { - source_ = VideoSource::Create(rtc::Thread::Current(), - new webrtc::RemoteVideoCapturer(), NULL, true); +TEST_F(VideoCapturerTrackSourceTest, StartStopRemote) { + source_ = VideoCapturerTrackSource::Create( + rtc::Thread::Current(), new webrtc::RemoteVideoCapturer(), NULL, true); ASSERT_TRUE(source_.get() != NULL); EXPECT_TRUE(NULL != source_->GetVideoCapturer()); @@ -212,8 +205,8 @@ TEST_F(VideoSourceTest, StartStopRemote) { // Test that a VideoSource transition to kEnded if the capture device // fails. -TEST_F(VideoSourceTest, CameraFailed) { - CreateVideoSource(); +TEST_F(VideoCapturerTrackSourceTest, CameraFailed) { + CreateVideoCapturerSource(); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); @@ -224,13 +217,13 @@ TEST_F(VideoSourceTest, CameraFailed) { // Test that the capture output is CIF if we set max constraints to CIF. // and the capture device support CIF. -TEST_F(VideoSourceTest, MandatoryConstraintCif5Fps) { +TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 5); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -242,7 +235,7 @@ TEST_F(VideoSourceTest, MandatoryConstraintCif5Fps) { // Test that the capture output is 720P if the camera support it and the // optional constraint is set to 720P. -TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) { +TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); @@ -250,7 +243,7 @@ TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) { constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 1280.0 / 720); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -263,7 +256,7 @@ TEST_F(VideoSourceTest, MandatoryMinVgaOptional720P) { // Test that the capture output have aspect ratio 4:3 if a mandatory constraint // require it even if an optional constraint request a higher resolution // that don't have this aspect ratio. -TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) { +TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMinWidth, 640); constraints.AddMandatory(MediaConstraintsInterface::kMinHeight, 480); @@ -271,7 +264,7 @@ TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) { 640.0 / 480); constraints.AddOptional(MediaConstraintsInterface::kMinWidth, 1280); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -281,23 +274,22 @@ TEST_F(VideoSourceTest, MandatoryAspectRatio4To3) { EXPECT_EQ(30, format->framerate()); } - // Test that the source state transition to kEnded if the mandatory aspect ratio // is set higher than supported. -TEST_F(VideoSourceTest, MandatoryAspectRatioTooHigh) { +TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatioTooHigh) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, 2); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), kMaxWaitMs); } // Test that the source ignores an optional aspect ratio that is higher than // supported. -TEST_F(VideoSourceTest, OptionalAspectRatioTooHigh) { +TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) { FakeConstraints constraints; constraints.AddOptional(MediaConstraintsInterface::kMinAspectRatio, 2); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -308,10 +300,10 @@ TEST_F(VideoSourceTest, OptionalAspectRatioTooHigh) { // Test that the source starts video with the default resolution if the // camera doesn't support capability enumeration and there are no constraints. -TEST_F(VideoSourceTest, NoCameraCapability) { +TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) { capturer_->TestWithoutCameraFormats(); - CreateVideoSource(); + CreateVideoCapturerSource(); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -324,7 +316,7 @@ TEST_F(VideoSourceTest, NoCameraCapability) { // Test that the source can start the video and get the requested aspect ratio // if the camera doesn't support capability enumeration and the aspect ratio is // set. -TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) { +TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) { capturer_->TestWithoutCameraFormats(); FakeConstraints constraints; @@ -333,7 +325,7 @@ TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) { constraints.AddMandatory(MediaConstraintsInterface::kMinAspectRatio, requested_aspect_ratio); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -343,121 +335,113 @@ TEST_F(VideoSourceTest, NoCameraCapability16To9Ratio) { // Test that the source state transitions to kEnded if an unknown mandatory // constraint is found. -TEST_F(VideoSourceTest, InvalidMandatoryConstraint) { +TEST_F(VideoCapturerTrackSourceTest, InvalidMandatoryConstraint) { FakeConstraints constraints; constraints.AddMandatory("weird key", 640); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), kMaxWaitMs); } // Test that the source ignores an unknown optional constraint. -TEST_F(VideoSourceTest, InvalidOptionalConstraint) { +TEST_F(VideoCapturerTrackSourceTest, InvalidOptionalConstraint) { FakeConstraints constraints; constraints.AddOptional("weird key", 640); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); } -TEST_F(VideoSourceTest, SetValidOptionValues) { +TEST_F(VideoCapturerTrackSourceTest, SetValidOptionValues) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "false"); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ(rtc::Optional(false), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, OptionNotSet) { +TEST_F(VideoCapturerTrackSourceTest, OptionNotSet) { FakeConstraints constraints; - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ(rtc::Optional(), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, MandatoryOptionOverridesOptional) { +TEST_F(VideoCapturerTrackSourceTest, MandatoryOptionOverridesOptional) { FakeConstraints constraints; - constraints.AddMandatory( - MediaConstraintsInterface::kNoiseReduction, true); - constraints.AddOptional( - MediaConstraintsInterface::kNoiseReduction, false); + constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, true); + constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, false); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ(rtc::Optional(true), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, InvalidOptionKeyOptional) { +TEST_F(VideoCapturerTrackSourceTest, InvalidOptionKeyOptional) { FakeConstraints constraints; - constraints.AddOptional( - MediaConstraintsInterface::kNoiseReduction, false); + constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, false); constraints.AddOptional("invalidKey", false); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), - kMaxWaitMs); + kMaxWaitMs); EXPECT_EQ(rtc::Optional(false), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, InvalidOptionKeyMandatory) { +TEST_F(VideoCapturerTrackSourceTest, InvalidOptionKeyMandatory) { FakeConstraints constraints; - constraints.AddMandatory( - MediaConstraintsInterface::kNoiseReduction, false); + constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); constraints.AddMandatory("invalidKey", false); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), - kMaxWaitMs); + kMaxWaitMs); EXPECT_EQ(rtc::Optional(), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, InvalidOptionValueOptional) { +TEST_F(VideoCapturerTrackSourceTest, InvalidOptionValueOptional) { FakeConstraints constraints; - constraints.AddOptional( - MediaConstraintsInterface::kNoiseReduction, "not a boolean"); + constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, + "not a boolean"); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), - kMaxWaitMs); + kMaxWaitMs); EXPECT_EQ(rtc::Optional(), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, InvalidOptionValueMandatory) { +TEST_F(VideoCapturerTrackSourceTest, InvalidOptionValueMandatory) { FakeConstraints constraints; // Optional constraints should be ignored if the mandatory constraints fail. - constraints.AddOptional( - MediaConstraintsInterface::kNoiseReduction, "false"); + constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, "false"); // Values are case-sensitive and must be all lower-case. - constraints.AddMandatory( - MediaConstraintsInterface::kNoiseReduction, "True"); + constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, "True"); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), - kMaxWaitMs); + kMaxWaitMs); EXPECT_EQ(rtc::Optional(), source_->options()->video_noise_reduction); } -TEST_F(VideoSourceTest, MixedOptionsAndConstraints) { +TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 352); constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 288); constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 5); - constraints.AddMandatory( - MediaConstraintsInterface::kNoiseReduction, false); - constraints.AddOptional( - MediaConstraintsInterface::kNoiseReduction, true); + constraints.AddMandatory(MediaConstraintsInterface::kNoiseReduction, false); + constraints.AddOptional(MediaConstraintsInterface::kNoiseReduction, true); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -472,11 +456,11 @@ TEST_F(VideoSourceTest, MixedOptionsAndConstraints) { // Tests that the source starts video with the default resolution for // screencast if no constraint is set. -TEST_F(VideoSourceTest, ScreencastResolutionNoConstraint) { +TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) { InitScreencast(); capturer_->TestWithoutCameraFormats(); - CreateVideoSource(); + CreateVideoCapturerSource(); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -488,7 +472,7 @@ TEST_F(VideoSourceTest, ScreencastResolutionNoConstraint) { // Tests that the source starts video with the max width and height set by // constraints for screencast. -TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) { +TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMaxWidth, 480); constraints.AddMandatory(MediaConstraintsInterface::kMaxHeight, 270); @@ -496,7 +480,7 @@ TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) { InitScreencast(); capturer_->TestWithoutCameraFormats(); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); @@ -506,21 +490,21 @@ TEST_F(VideoSourceTest, ScreencastResolutionWithConstraint) { EXPECT_EQ(30, format->framerate()); } -TEST_F(VideoSourceTest, MandatorySubOneFpsConstraints) { +TEST_F(VideoCapturerTrackSourceTest, MandatorySubOneFpsConstraints) { FakeConstraints constraints; constraints.AddMandatory(MediaConstraintsInterface::kMaxFrameRate, 0.5); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kEnded, state_observer_->state(), kMaxWaitMs); ASSERT_TRUE(capturer_->GetCaptureFormat() == NULL); } -TEST_F(VideoSourceTest, OptionalSubOneFpsConstraints) { +TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) { FakeConstraints constraints; constraints.AddOptional(MediaConstraintsInterface::kMaxFrameRate, 0.5); - CreateVideoSource(&constraints); + CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); diff --git a/webrtc/api/videosource.h b/webrtc/api/videosource.h index eb4646336d..08e5422aae 100644 --- a/webrtc/api/videosource.h +++ b/webrtc/api/videosource.h @@ -8,93 +8,4 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef WEBRTC_API_VIDEOSOURCE_H_ -#define WEBRTC_API_VIDEOSOURCE_H_ - -#include - -#include "webrtc/api/mediastreaminterface.h" -#include "webrtc/api/notifier.h" -#include "webrtc/api/videosourceinterface.h" -#include "webrtc/api/videotrackrenderers.h" -#include "webrtc/base/asyncinvoker.h" -#include "webrtc/base/scoped_ptr.h" -#include "webrtc/base/sigslot.h" -#include "webrtc/media/base/videosinkinterface.h" -#include "webrtc/media/base/videocapturer.h" -#include "webrtc/media/base/videocommon.h" - -// VideoSource implements VideoSourceInterface. It owns a -// cricket::VideoCapturer and make sure the camera is started at a resolution -// that honors the constraints. -// The state is set depending on the result of starting the capturer. -// If the constraint can't be met or the capturer fails to start, the state -// transition to kEnded, otherwise it transitions to kLive. -namespace webrtc { - -class MediaConstraintsInterface; - -class VideoSource : public Notifier, - public sigslot::has_slots<> { - public: - // Creates an instance of VideoSource. - // VideoSource take ownership of |capturer|. - // |constraints| can be NULL and in that case the camera is opened using a - // default resolution. - static rtc::scoped_refptr Create( - rtc::Thread* worker_thread, - cricket::VideoCapturer* capturer, - const webrtc::MediaConstraintsInterface* constraints, - bool remote); - - // Note that the non-constraints version does not have the ability to - // select configuration based on width, height, aspect ratio or frame rate. - static rtc::scoped_refptr Create( - rtc::Thread* worker_thread, - cricket::VideoCapturer* capturer, - bool remote); - - SourceState state() const override { return state_; } - bool remote() const override { return remote_; } - - virtual const cricket::VideoOptions* options() const { return &options_; } - - virtual cricket::VideoCapturer* GetVideoCapturer() { - return video_capturer_.get(); - } - - void Stop() override; - void Restart() override; - - void AddOrUpdateSink(rtc::VideoSinkInterface* sink, - const rtc::VideoSinkWants& wants) override; - void RemoveSink(rtc::VideoSinkInterface* sink) override; - - protected: - VideoSource(rtc::Thread* worker_thread, - cricket::VideoCapturer* capturer, - bool remote); - virtual ~VideoSource(); - void Initialize(const webrtc::MediaConstraintsInterface* constraints); - - private: - void OnStateChange(cricket::VideoCapturer* capturer, - cricket::CaptureState capture_state); - void SetState(SourceState new_state); - - rtc::Thread* signaling_thread_; - rtc::Thread* worker_thread_; - rtc::AsyncInvoker invoker_; - rtc::scoped_ptr video_capturer_; - bool started_; - rtc::scoped_ptr frame_input_; - - cricket::VideoFormat format_; - cricket::VideoOptions options_; - SourceState state_; - const bool remote_; -}; - -} // namespace webrtc - -#endif // WEBRTC_API_VIDEOSOURCE_H_ +// TODO(perkj): Remove this file once Chrome build files doesn't depend on it. diff --git a/webrtc/api/videosourceinterface.h b/webrtc/api/videosourceinterface.h index d55f0aaa52..10ded41411 100644 --- a/webrtc/api/videosourceinterface.h +++ b/webrtc/api/videosourceinterface.h @@ -12,37 +12,7 @@ #define WEBRTC_API_VIDEOSOURCEINTERFACE_H_ #include "webrtc/api/mediastreaminterface.h" -#include "webrtc/media/base/mediachannel.h" -#include "webrtc/media/base/videosourceinterface.h" -namespace webrtc { - -// VideoSourceInterface is a reference counted source used for VideoTracks. -// The same source can be used in multiple VideoTracks. -// The methods are only supposed to be called by the PeerConnection -// implementation. -class VideoSourceInterface : - public MediaSourceInterface, - public rtc::VideoSourceInterface { - public: - // Get access to the source implementation of cricket::VideoCapturer. - // This can be used for receiving frames and state notifications. - // But it should not be used for starting or stopping capturing. - virtual cricket::VideoCapturer* GetVideoCapturer() = 0; - - virtual void Stop() = 0; - virtual void Restart() = 0; - - virtual const cricket::VideoOptions* options() const = 0; - - protected: - virtual ~VideoSourceInterface() {} -}; - -// TODO(perkj): Rename webrtc::VideoSourceInterface to -// webrtc::VideoTrackSourceInterface -using VideoTrackSourceInterface = VideoSourceInterface; - -} // namespace webrtc +// TODO(perkj): Remove this file once Chrome build files doesn't depend on it. #endif // WEBRTC_API_VIDEOSOURCEINTERFACE_H_ diff --git a/webrtc/api/videosourceproxy.h b/webrtc/api/videosourceproxy.h index e0f94dc881..59adf89094 100644 --- a/webrtc/api/videosourceproxy.h +++ b/webrtc/api/videosourceproxy.h @@ -16,22 +16,24 @@ namespace webrtc { -// VideoSourceProxy makes sure the real VideoSourceInterface implementation is +// VideoTrackSourceInterface makes sure the real VideoTrackSourceInterface +// implementation is // destroyed on the signaling thread and marshals all method calls to the // signaling thread. -BEGIN_PROXY_MAP(VideoSource) - PROXY_CONSTMETHOD0(SourceState, state) - PROXY_CONSTMETHOD0(bool, remote) - PROXY_METHOD0(cricket::VideoCapturer*, GetVideoCapturer) - PROXY_METHOD0(void, Stop) - PROXY_METHOD0(void, Restart) - PROXY_METHOD2(void, AddOrUpdateSink, - rtc::VideoSinkInterface*, - const rtc::VideoSinkWants&) - PROXY_METHOD1(void, RemoveSink, rtc::VideoSinkInterface*) - PROXY_CONSTMETHOD0(const cricket::VideoOptions*, options) - PROXY_METHOD1(void, RegisterObserver, ObserverInterface*) - PROXY_METHOD1(void, UnregisterObserver, ObserverInterface*) +BEGIN_PROXY_MAP(VideoTrackSource) +PROXY_CONSTMETHOD0(SourceState, state) +PROXY_CONSTMETHOD0(bool, remote) +PROXY_METHOD0(cricket::VideoCapturer*, GetVideoCapturer) +PROXY_METHOD0(void, Stop) +PROXY_METHOD0(void, Restart) +PROXY_METHOD2(void, + AddOrUpdateSink, + rtc::VideoSinkInterface*, + const rtc::VideoSinkWants&) +PROXY_METHOD1(void, RemoveSink, rtc::VideoSinkInterface*) +PROXY_CONSTMETHOD0(const cricket::VideoOptions*, options) +PROXY_METHOD1(void, RegisterObserver, ObserverInterface*) +PROXY_METHOD1(void, UnregisterObserver, ObserverInterface*) END_PROXY() } // namespace webrtc diff --git a/webrtc/api/videotrack.cc b/webrtc/api/videotrack.cc index 0f933a0dab..36d256f1e4 100644 --- a/webrtc/api/videotrack.cc +++ b/webrtc/api/videotrack.cc @@ -17,7 +17,7 @@ namespace webrtc { const char MediaStreamTrackInterface::kVideoKind[] = "video"; VideoTrack::VideoTrack(const std::string& label, - VideoSourceInterface* video_source) + VideoTrackSourceInterface* video_source) : MediaStreamTrack(label), video_source_(video_source) { // TODO(perkj): Sinks should register directly to the source so that @@ -61,7 +61,7 @@ bool VideoTrack::set_enabled(bool enable) { rtc::scoped_refptr VideoTrack::Create( const std::string& id, - VideoSourceInterface* source) { + VideoTrackSourceInterface* source) { rtc::RefCountedObject* track = new rtc::RefCountedObject(id, source); return track; diff --git a/webrtc/api/videotrack.h b/webrtc/api/videotrack.h index 1964cb4898..066aa90a36 100644 --- a/webrtc/api/videotrack.h +++ b/webrtc/api/videotrack.h @@ -22,14 +22,15 @@ namespace webrtc { class VideoTrack : public MediaStreamTrack { public: - static rtc::scoped_refptr Create(const std::string& label, - VideoSourceInterface* source); + static rtc::scoped_refptr Create( + const std::string& label, + VideoTrackSourceInterface* source); void AddOrUpdateSink(rtc::VideoSinkInterface* sink, const rtc::VideoSinkWants& wants) override; void RemoveSink(rtc::VideoSinkInterface* sink) override; - virtual VideoSourceInterface* GetSource() const { + virtual VideoTrackSourceInterface* GetSource() const { return video_source_.get(); } rtc::VideoSinkInterface* GetSink() override; @@ -37,12 +38,12 @@ class VideoTrack : public MediaStreamTrack { virtual std::string kind() const; protected: - VideoTrack(const std::string& id, VideoSourceInterface* video_source); + VideoTrack(const std::string& id, VideoTrackSourceInterface* video_source); ~VideoTrack(); private: VideoTrackRenderers renderers_; - rtc::scoped_refptr video_source_; + rtc::scoped_refptr video_source_; }; } // namespace webrtc diff --git a/webrtc/api/videotrack_unittest.cc b/webrtc/api/videotrack_unittest.cc index 0c3e8b0534..9f44b81ea6 100644 --- a/webrtc/api/videotrack_unittest.cc +++ b/webrtc/api/videotrack_unittest.cc @@ -12,7 +12,7 @@ #include "webrtc/api/remotevideocapturer.h" #include "webrtc/api/test/fakevideotrackrenderer.h" -#include "webrtc/api/videosource.h" +#include "webrtc/api/videocapturertracksource.h" #include "webrtc/api/videotrack.h" #include "webrtc/base/gunit.h" #include "webrtc/base/scoped_ptr.h" @@ -22,7 +22,7 @@ using webrtc::FakeVideoTrackRenderer; using webrtc::FakeVideoTrackRendererOld; -using webrtc::VideoSource; +using webrtc::VideoCapturerTrackSource; using webrtc::VideoTrack; using webrtc::VideoTrackInterface; @@ -40,9 +40,9 @@ class VideoTrackTest : public testing::Test { VideoTrackTest() { static const char kVideoTrackId[] = "track_id"; video_track_ = VideoTrack::Create( - kVideoTrackId, - VideoSource::Create(rtc::Thread::Current(), - new webrtc::RemoteVideoCapturer(), NULL, true)); + kVideoTrackId, VideoCapturerTrackSource::Create( + rtc::Thread::Current(), + new webrtc::RemoteVideoCapturer(), NULL, true)); } protected: