1// libjingle
2// Copyright 2004 Google Inc.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7//  1. Redistributions of source code must retain the above copyright notice,
8//     this list of conditions and the following disclaimer.
9//  2. Redistributions in binary form must reproduce the above copyright notice,
10//     this list of conditions and the following disclaimer in the documentation
11//     and/or other materials provided with the distribution.
12//  3. The name of the author may not be used to endorse or promote products
13//     derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26#ifndef TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
27#define TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
28
29#ifdef HAVE_WEBRTC_VIDEO
30
31#include <string>
32#include <vector>
33
34#include "talk/base/messagehandler.h"
35#include "talk/media/base/videocapturer.h"
36#include "talk/media/webrtc/webrtcvideoframe.h"
37#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
38#include "webrtc/modules/video_capture/include/video_capture.h"
39
40namespace cricket {
41
42// Factory to allow injection of a VCM impl into WebRtcVideoCapturer.
43// DeviceInfos do not have a Release() and therefore need an explicit Destroy().
44class WebRtcVcmFactoryInterface {
45 public:
46  virtual ~WebRtcVcmFactoryInterface() {}
47  virtual webrtc::VideoCaptureModule* Create(
48      int id, const char* device) = 0;
49  virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) = 0;
50  virtual void DestroyDeviceInfo(
51      webrtc::VideoCaptureModule::DeviceInfo* info) = 0;
52};
53
54// WebRTC-based implementation of VideoCapturer.
55class WebRtcVideoCapturer : public VideoCapturer,
56                            public webrtc::VideoCaptureDataCallback {
57 public:
58  WebRtcVideoCapturer();
59  explicit WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory);
60  virtual ~WebRtcVideoCapturer();
61
62  bool Init(const Device& device);
63  bool Init(webrtc::VideoCaptureModule* module);
64
65  // Override virtual methods of the parent class VideoCapturer.
66  virtual bool GetBestCaptureFormat(const VideoFormat& desired,
67                                    VideoFormat* best_format);
68  virtual CaptureState Start(const VideoFormat& capture_format);
69  virtual void Stop();
70  virtual bool IsRunning();
71  virtual bool IsScreencast() const { return false; }
72
73 protected:
74  // Override virtual methods of the parent class VideoCapturer.
75  virtual bool GetPreferredFourccs(std::vector<uint32>* fourccs);
76
77 private:
78  // Callback when a frame is captured by camera.
79  virtual void OnIncomingCapturedFrame(const int32_t id,
80                                       webrtc::I420VideoFrame& frame);
81  virtual void OnIncomingCapturedEncodedFrame(const int32_t id,
82      webrtc::VideoFrame& frame,
83      webrtc::VideoCodecType codec_type) {
84  }
85  virtual void OnCaptureDelayChanged(const int32_t id,
86                                     const int32_t delay);
87
88  talk_base::scoped_ptr<WebRtcVcmFactoryInterface> factory_;
89  webrtc::VideoCaptureModule* module_;
90  int captured_frames_;
91  std::vector<uint8_t> capture_buffer_;
92};
93
94struct WebRtcCapturedFrame : public CapturedFrame {
95 public:
96  WebRtcCapturedFrame(const webrtc::I420VideoFrame& frame,
97                      void* buffer, int length);
98};
99
100}  // namespace cricket
101
102#endif  // HAVE_WEBRTC_VIDEO
103#endif  // TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
104