1/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11#ifndef PC_VIDEOCAPTURERTRACKSOURCE_H_
12#define PC_VIDEOCAPTURERTRACKSOURCE_H_
13
14#include <memory>
15
16#include "api/mediastreaminterface.h"
17#include "media/base/videocapturer.h"
18#include "media/base/videocommon.h"
19#include "pc/videotracksource.h"
20#include "rtc_base/asyncinvoker.h"
21#include "rtc_base/third_party/sigslot/sigslot.h"
22
23// VideoCapturerTrackSource implements VideoTrackSourceInterface. It owns a
24// cricket::VideoCapturer and make sure the camera is started at a resolution
25// that honors the constraints.
26// The state is set depending on the result of starting the capturer.
27// If the constraint can't be met or the capturer fails to start, the state
28// transition to kEnded, otherwise it transitions to kLive.
29namespace webrtc {
30
31class MediaConstraintsInterface;
32
33class VideoCapturerTrackSource : public VideoTrackSource,
34 public sigslot::has_slots<> {
35 public:
36 // Creates an instance of VideoCapturerTrackSource from |capturer|.
37 // |constraints| can be NULL and in that case the camera is opened using a
38 // default resolution.
39 static rtc::scoped_refptr<VideoTrackSourceInterface> Create(
40 rtc::Thread* worker_thread,
41 std::unique_ptr<cricket::VideoCapturer> capturer,
42 const webrtc::MediaConstraintsInterface* constraints,
43 bool remote);
44
45 static rtc::scoped_refptr<VideoTrackSourceInterface> Create(
46 rtc::Thread* worker_thread,
47 std::unique_ptr<cricket::VideoCapturer> capturer,
48 bool remote);
49
50 bool is_screencast() const final { return video_capturer_->IsScreencast(); }
51 absl::optional<bool> needs_denoising() const final {
52 return needs_denoising_;
53 }
54
55 bool GetStats(Stats* stats) final;
56
57 protected:
58 VideoCapturerTrackSource(rtc::Thread* worker_thread,
59 std::unique_ptr<cricket::VideoCapturer> capturer,
60 bool remote);
61 virtual ~VideoCapturerTrackSource();
62
63 rtc::VideoSourceInterface<VideoFrame>* source() override {
64 return video_capturer_.get();
65 }
66
67 void Initialize(const webrtc::MediaConstraintsInterface* constraints);
68
69 private:
70 void Stop();
71
72 void OnStateChange(cricket::VideoCapturer* capturer,
73 cricket::CaptureState capture_state);
74
75 rtc::Thread* signaling_thread_;
76 rtc::Thread* worker_thread_;
77 rtc::AsyncInvoker invoker_;
78 std::unique_ptr<cricket::VideoCapturer> video_capturer_;
79 bool started_;
80 cricket::VideoFormat format_;
81 absl::optional<bool> needs_denoising_;
82};
83
84} // namespace webrtc
85
86#endif // PC_VIDEOCAPTURERTRACKSOURCE_H_
87