blob: f0808e9eaa75dd6459b9a248ae2f6861e686b56f [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
Henrik Boströmcbe408a2015-05-27 10:11:34 +020018#include "webrtc/base/asyncinvoker.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000019#include "webrtc/base/messagehandler.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
kjellandera96e2d72016-02-04 23:52:28 -080021#include "webrtc/media/base/videocapturer.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010022#include "webrtc/media/engine/webrtcvideoframe.h"
Henrik Kjellander5dda80a2015-11-12 12:46:09 +010023#include "webrtc/modules/video_capture/video_capture.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25namespace cricket {
26
27// Factory to allow injection of a VCM impl into WebRtcVideoCapturer.
28// DeviceInfos do not have a Release() and therefore need an explicit Destroy().
29class WebRtcVcmFactoryInterface {
30 public:
31 virtual ~WebRtcVcmFactoryInterface() {}
32 virtual webrtc::VideoCaptureModule* Create(
33 int id, const char* device) = 0;
34 virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) = 0;
35 virtual void DestroyDeviceInfo(
36 webrtc::VideoCaptureModule::DeviceInfo* info) = 0;
37};
38
39// WebRTC-based implementation of VideoCapturer.
40class WebRtcVideoCapturer : public VideoCapturer,
41 public webrtc::VideoCaptureDataCallback {
42 public:
43 WebRtcVideoCapturer();
44 explicit WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory);
45 virtual ~WebRtcVideoCapturer();
46
47 bool Init(const Device& device);
48 bool Init(webrtc::VideoCaptureModule* module);
49
50 // Override virtual methods of the parent class VideoCapturer.
Pera5092412016-02-12 13:30:57 +010051 bool GetBestCaptureFormat(const VideoFormat& desired,
52 VideoFormat* best_format) override;
53 CaptureState Start(const VideoFormat& capture_format) override;
54 void Stop() override;
55 bool IsRunning() override;
56 bool IsScreencast() const override { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58 protected:
Pera5092412016-02-12 13:30:57 +010059 void OnSinkWantsChanged(const rtc::VideoSinkWants& wants) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 // Override virtual methods of the parent class VideoCapturer.
Peter Boström0c4e06b2015-10-07 12:23:21 +020061 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
63 private:
64 // Callback when a frame is captured by camera.
65 virtual void OnIncomingCapturedFrame(const int32_t id,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070066 const webrtc::VideoFrame& frame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 virtual void OnCaptureDelayChanged(const int32_t id,
68 const int32_t delay);
69
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000070 // Used to signal captured frames on the same thread as invoked Start().
71 // With WebRTC's current VideoCapturer implementations, this will mean a
72 // thread hop, but in other implementations (e.g. Chrome) it will be called
73 // directly from OnIncomingCapturedFrame.
74 // TODO(tommi): Remove this workaround when we've updated the WebRTC capturers
75 // to follow the same contract.
noahric5d9b92b2015-10-24 11:14:46 -070076 void SignalFrameCapturedOnStartThread(const webrtc::VideoFrame& frame);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000077
kwiberg686a8ef2016-02-26 03:00:35 -080078 std::unique_ptr<WebRtcVcmFactoryInterface> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 webrtc::VideoCaptureModule* module_;
80 int captured_frames_;
wu@webrtc.org16d62542013-11-05 23:45:14 +000081 std::vector<uint8_t> capture_buffer_;
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000082 rtc::Thread* start_thread_; // Set in Start(), unset in Stop();
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000083
kwiberg686a8ef2016-02-26 03:00:35 -080084 std::unique_ptr<rtc::AsyncInvoker> async_invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085};
86
87struct WebRtcCapturedFrame : public CapturedFrame {
88 public:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070089 WebRtcCapturedFrame(const webrtc::VideoFrame& frame,
90 void* buffer,
91 size_t length);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092};
93
94} // namespace cricket
95
Pera5092412016-02-12 13:30:57 +010096#endif // WEBRTC_MEDIA_WEBRTC_WEBRTCVIDEOCAPTURER_H_