video_capture_fake.cc (2752B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "video_capture_fake.h" 8 9 #include "FakeVideoSource.h" 10 #include "device_info_fake.h" 11 #include "libwebrtcglue/WebrtcImageBuffer.h" 12 13 using mozilla::FakeVideoSource; 14 using mozilla::ImageBuffer; 15 using mozilla::MakeRefPtr; 16 using mozilla::TimeDuration; 17 using mozilla::TimeStamp; 18 using mozilla::layers::Image; 19 20 namespace webrtc::videocapturemodule { 21 webrtc::scoped_refptr<webrtc::VideoCaptureModule> VideoCaptureFake::Create( 22 nsISerialEventTarget* aTarget) { 23 return webrtc::make_ref_counted<VideoCaptureFake>( 24 Clock::GetRealTimeClockRaw(), aTarget); 25 } 26 27 VideoCaptureFake::VideoCaptureFake(Clock* clock, nsISerialEventTarget* aTarget) 28 : VideoCaptureImpl(clock), 29 mTarget(aTarget), 30 mSource(MakeRefPtr<FakeVideoSource>(aTarget)) { 31 size_t len = strlen(DeviceInfoFake::kId); 32 _deviceUniqueId = new (std::nothrow) char[len + 1]; 33 if (_deviceUniqueId) { 34 memcpy(_deviceUniqueId, DeviceInfoFake::kId, len + 1); 35 } 36 } 37 38 VideoCaptureFake::~VideoCaptureFake() { StopCapture(); } 39 40 int32_t VideoCaptureFake::StartCapture( 41 const VideoCaptureCapability& aCapability) { 42 if (!CaptureStarted()) { 43 mGeneratedImageListener = mSource->GeneratedImageEvent().Connect( 44 mTarget, this, &VideoCaptureFake::OnGeneratedImage); 45 } 46 return mSource->StartCapture( 47 aCapability.width, aCapability.height, 48 TimeDuration::FromSeconds(1.0 / aCapability.maxFPS)); 49 } 50 51 int32_t VideoCaptureFake::StopCapture() { 52 mGeneratedImageListener.DisconnectIfExists(); 53 return mSource->StopCapture(); 54 } 55 56 bool VideoCaptureFake::CaptureStarted() { return mSource->CaptureStarted(); } 57 58 int32_t VideoCaptureFake::CaptureSettings(VideoCaptureCapability& aSettings) { 59 return {}; 60 } 61 62 void VideoCaptureFake::SetTrackingId(uint32_t aTrackingIdProcId) { 63 mSource->SetTrackingId(aTrackingIdProcId); 64 } 65 66 void VideoCaptureFake::OnGeneratedImage(const RefPtr<Image>& aImage, 67 TimeStamp aTime) { 68 webrtc::scoped_refptr<ImageBuffer> buffer( 69 new webrtc::RefCountedObject<ImageBuffer>(RefPtr(aImage))); 70 if (!mStart) { 71 mStart = Some(aTime); 72 } 73 auto videoFrame = webrtc::VideoFrame::Builder() 74 .set_video_frame_buffer(buffer) 75 .set_timestamp_us((aTime - *mStart).ToMicroseconds()) 76 .build(); 77 webrtc::MutexLock lock(&api_lock_); 78 DeliverCapturedFrame(videoFrame); 79 } 80 81 } // namespace webrtc::videocapturemodule