blob: 6fbfcf2ca2a9da73822fc5ba5d756e91e4a6c480 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010011#ifndef WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOCAPTURER_H_
12#define WEBRTC_MEDIA_ENGINE_WEBRTCVIDEOCAPTURER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
kwiberg686a8ef2016-02-26 03:00:35 -080014#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <string>
16#include <vector>
17
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
solenberg8ad582d2016-03-16 09:34:56 -070019#include "webrtc/media/base/device.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/media/base/videocapturer.h"
Henrik Kjellander5dda80a2015-11-12 12:46:09 +010021#include "webrtc/modules/video_capture/video_capture.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020022#include "webrtc/rtc_base/asyncinvoker.h"
23#include "webrtc/rtc_base/messagehandler.h"
24#include "webrtc/rtc_base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26namespace cricket {
27
28// Factory to allow injection of a VCM impl into WebRtcVideoCapturer.
29// DeviceInfos do not have a Release() and therefore need an explicit Destroy().
30class WebRtcVcmFactoryInterface {
31 public:
32 virtual ~WebRtcVcmFactoryInterface() {}
Peter Boström09c3a1e2016-03-22 17:17:39 +010033 virtual rtc::scoped_refptr<webrtc::VideoCaptureModule> Create(
Peter Boström09c3a1e2016-03-22 17:17:39 +010034 const char* device) = 0;
nisseb29b9c82016-12-12 00:22:56 -080035 virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 virtual void DestroyDeviceInfo(
37 webrtc::VideoCaptureModule::DeviceInfo* info) = 0;
38};
39
40// WebRTC-based implementation of VideoCapturer.
41class WebRtcVideoCapturer : public VideoCapturer,
nisseb29b9c82016-12-12 00:22:56 -080042 public rtc::VideoSinkInterface<webrtc::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 public:
44 WebRtcVideoCapturer();
45 explicit WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory);
46 virtual ~WebRtcVideoCapturer();
47
48 bool Init(const Device& device);
Peter Boström09c3a1e2016-03-22 17:17:39 +010049 bool Init(const rtc::scoped_refptr<webrtc::VideoCaptureModule>& module);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51 // Override virtual methods of the parent class VideoCapturer.
Pera5092412016-02-12 13:30:57 +010052 bool GetBestCaptureFormat(const VideoFormat& desired,
53 VideoFormat* best_format) override;
54 CaptureState Start(const VideoFormat& capture_format) override;
55 void Stop() override;
56 bool IsRunning() override;
57 bool IsScreencast() const override { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59 protected:
Pera5092412016-02-12 13:30:57 +010060 void OnSinkWantsChanged(const rtc::VideoSinkWants& wants) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 // Override virtual methods of the parent class VideoCapturer.
nisseef8b61e2016-04-29 06:09:15 -070062 bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 private:
65 // Callback when a frame is captured by camera.
nisseb29b9c82016-12-12 00:22:56 -080066 void OnFrame(const webrtc::VideoFrame& frame) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000068 // Used to signal captured frames on the same thread as invoked Start().
69 // With WebRTC's current VideoCapturer implementations, this will mean a
70 // thread hop, but in other implementations (e.g. Chrome) it will be called
71 // directly from OnIncomingCapturedFrame.
72 // TODO(tommi): Remove this workaround when we've updated the WebRTC capturers
73 // to follow the same contract.
noahric5d9b92b2015-10-24 11:14:46 -070074 void SignalFrameCapturedOnStartThread(const webrtc::VideoFrame& frame);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000075
kwiberg686a8ef2016-02-26 03:00:35 -080076 std::unique_ptr<WebRtcVcmFactoryInterface> factory_;
Peter Boström09c3a1e2016-03-22 17:17:39 +010077 rtc::scoped_refptr<webrtc::VideoCaptureModule> module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 int captured_frames_;
wu@webrtc.org16d62542013-11-05 23:45:14 +000079 std::vector<uint8_t> capture_buffer_;
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000080 rtc::Thread* start_thread_; // Set in Start(), unset in Stop();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081};
82
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083} // namespace cricket
84
Pera5092412016-02-12 13:30:57 +010085#endif // WEBRTC_MEDIA_WEBRTC_WEBRTCVIDEOCAPTURER_H_