10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// libjingle
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Copyright 2004 Google Inc.
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Redistribution and use in source and binary forms, with or without
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// modification, are permitted provided that the following conditions are met:
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//  1. Redistributions of source code must retain the above copyright notice,
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     this list of conditions and the following disclaimer.
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//  2. Redistributions in binary form must reproduce the above copyright notice,
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     this list of conditions and the following disclaimer in the documentation
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     and/or other materials provided with the distribution.
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//  3. The name of the author may not be used to endorse or promote products
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     derived from this software without specific prior written permission.
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifdef HAVE_WEBRTC_VIDEO
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <vector>
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/videocapturer.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/webrtc/webrtcvideoframe.h"
36cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/criticalsection.h"
37cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/messagehandler.h"
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "webrtc/modules/video_capture/include/video_capture.h"
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Factory to allow injection of a VCM impl into WebRtcVideoCapturer.
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// DeviceInfos do not have a Release() and therefore need an explicit Destroy().
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass WebRtcVcmFactoryInterface {
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~WebRtcVcmFactoryInterface() {}
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual webrtc::VideoCaptureModule* Create(
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int id, const char* device) = 0;
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) = 0;
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void DestroyDeviceInfo(
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      webrtc::VideoCaptureModule::DeviceInfo* info) = 0;
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// WebRTC-based implementation of VideoCapturer.
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass WebRtcVideoCapturer : public VideoCapturer,
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                            public webrtc::VideoCaptureDataCallback {
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  WebRtcVideoCapturer();
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  explicit WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory);
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~WebRtcVideoCapturer();
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool Init(const Device& device);
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool Init(webrtc::VideoCaptureModule* module);
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Override virtual methods of the parent class VideoCapturer.
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetBestCaptureFormat(const VideoFormat& desired,
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    VideoFormat* best_format);
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual CaptureState Start(const VideoFormat& capture_format);
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void Stop();
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsRunning();
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsScreencast() const { return false; }
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Override virtual methods of the parent class VideoCapturer.
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetPreferredFourccs(std::vector<uint32>* fourccs);
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Callback when a frame is captured by camera.
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnIncomingCapturedFrame(const int32_t id,
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                       webrtc::I420VideoFrame& frame);
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnIncomingCapturedEncodedFrame(const int32_t id,
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      webrtc::VideoFrame& frame,
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      webrtc::VideoCodecType codec_type) {
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnCaptureDelayChanged(const int32_t id,
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                     const int32_t delay);
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
892a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<WebRtcVcmFactoryInterface> factory_;
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  webrtc::VideoCaptureModule* module_;
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int captured_frames_;
92a39fcda7b56709e660a3d3fd171cb9e963e7e532wu@webrtc.org  std::vector<uint8_t> capture_buffer_;
932a81a3893cea812cfa676ff7553038078c17f56cwu@webrtc.org
942a81a3893cea812cfa676ff7553038078c17f56cwu@webrtc.org  // Critical section to avoid Stop during an OnIncomingCapturedFrame callback.
952a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::CriticalSection critical_section_stopping_;
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstruct WebRtcCapturedFrame : public CapturedFrame {
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  WebRtcCapturedFrame(const webrtc::I420VideoFrame& frame,
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                      void* buffer, int length);
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // HAVE_WEBRTC_VIDEO
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
108