aboutsummaryrefslogtreecommitdiff
path: root/media/engine/webrtcvideocapturer.h
blob: a7c49fb14c23c52a6302fb0059ce1e3d642b3368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 *  Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef MEDIA_ENGINE_WEBRTCVIDEOCAPTURER_H_
#define MEDIA_ENGINE_WEBRTCVIDEOCAPTURER_H_

#include <memory>
#include <string>
#include <vector>

#include "common_video/libyuv/include/webrtc_libyuv.h"
#include "media/base/device.h"
#include "media/base/videocapturer.h"
#include "modules/video_capture/video_capture.h"
#include "rtc_base/asyncinvoker.h"
#include "rtc_base/messagehandler.h"
#include "rtc_base/scoped_ref_ptr.h"

namespace cricket {

// Factory to allow injection of a VCM impl into WebRtcVideoCapturer.
// DeviceInfos do not have a Release() and therefore need an explicit Destroy().
class WebRtcVcmFactoryInterface {
 public:
  virtual ~WebRtcVcmFactoryInterface() {}
  virtual rtc::scoped_refptr<webrtc::VideoCaptureModule> Create(
      const char* device) = 0;
  virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo() = 0;
  virtual void DestroyDeviceInfo(
      webrtc::VideoCaptureModule::DeviceInfo* info) = 0;
};

// WebRTC-based implementation of VideoCapturer.
class WebRtcVideoCapturer : public VideoCapturer,
                            public rtc::VideoSinkInterface<webrtc::VideoFrame> {
 public:
  WebRtcVideoCapturer();
  explicit WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory);
  virtual ~WebRtcVideoCapturer();

  bool Init(const Device& device);
  bool Init(const rtc::scoped_refptr<webrtc::VideoCaptureModule>& module);

  // Override virtual methods of the parent class VideoCapturer.
  bool GetBestCaptureFormat(const VideoFormat& desired,
                            VideoFormat* best_format) override;
  CaptureState Start(const VideoFormat& capture_format) override;
  void Stop() override;
  bool IsRunning() override;
  bool IsScreencast() const override { return false; }

 protected:
  void OnSinkWantsChanged(const rtc::VideoSinkWants& wants) override;
  // Override virtual methods of the parent class VideoCapturer.
  bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) override;

 private:
  // Callback when a frame is captured by camera.
  void OnFrame(const webrtc::VideoFrame& frame) override;

  // Used to signal captured frames on the same thread as invoked Start().
  // With WebRTC's current VideoCapturer implementations, this will mean a
  // thread hop, but in other implementations (e.g. Chrome) it will be called
  // directly from OnIncomingCapturedFrame.
  // TODO(tommi): Remove this workaround when we've updated the WebRTC capturers
  // to follow the same contract.
  void SignalFrameCapturedOnStartThread(const webrtc::VideoFrame& frame);

  std::unique_ptr<WebRtcVcmFactoryInterface> factory_;
  rtc::scoped_refptr<webrtc::VideoCaptureModule> module_;
  int captured_frames_;
  std::vector<uint8_t> capture_buffer_;
  rtc::Thread* start_thread_;  // Set in Start(), unset in Stop();
};

}  // namespace cricket

#endif  // MEDIA_ENGINE_WEBRTCVIDEOCAPTURER_H_