Home | History | Annotate | Download | only in webrtc
      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/criticalsection.h"
     35 #include "talk/base/messagehandler.h"
     36 #include "talk/media/base/videocapturer.h"
     37 #include "talk/media/webrtc/webrtcvideoframe.h"
     38 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
     39 #include "webrtc/modules/video_capture/include/video_capture.h"
     40 
     41 namespace cricket {
     42 
     43 // Factory to allow injection of a VCM impl into WebRtcVideoCapturer.
     44 // DeviceInfos do not have a Release() and therefore need an explicit Destroy().
     45 class WebRtcVcmFactoryInterface {
     46  public:
     47   virtual ~WebRtcVcmFactoryInterface() {}
     48   virtual webrtc::VideoCaptureModule* Create(
     49       int id, const char* device) = 0;
     50   virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) = 0;
     51   virtual void DestroyDeviceInfo(
     52       webrtc::VideoCaptureModule::DeviceInfo* info) = 0;
     53 };
     54 
     55 // WebRTC-based implementation of VideoCapturer.
     56 class WebRtcVideoCapturer : public VideoCapturer,
     57                             public webrtc::VideoCaptureDataCallback {
     58  public:
     59   WebRtcVideoCapturer();
     60   explicit WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory);
     61   virtual ~WebRtcVideoCapturer();
     62 
     63   bool Init(const Device& device);
     64   bool Init(webrtc::VideoCaptureModule* module);
     65 
     66   // Override virtual methods of the parent class VideoCapturer.
     67   virtual bool GetBestCaptureFormat(const VideoFormat& desired,
     68                                     VideoFormat* best_format);
     69   virtual CaptureState Start(const VideoFormat& capture_format);
     70   virtual void Stop();
     71   virtual bool IsRunning();
     72   virtual bool IsScreencast() const { return false; }
     73 
     74  protected:
     75   // Override virtual methods of the parent class VideoCapturer.
     76   virtual bool GetPreferredFourccs(std::vector<uint32>* fourccs);
     77 
     78  private:
     79   // Callback when a frame is captured by camera.
     80   virtual void OnIncomingCapturedFrame(const int32_t id,
     81                                        webrtc::I420VideoFrame& frame);
     82   virtual void OnIncomingCapturedEncodedFrame(const int32_t id,
     83       webrtc::VideoFrame& frame,
     84       webrtc::VideoCodecType codec_type) {
     85   }
     86   virtual void OnCaptureDelayChanged(const int32_t id,
     87                                      const int32_t delay);
     88 
     89   talk_base::scoped_ptr<WebRtcVcmFactoryInterface> factory_;
     90   webrtc::VideoCaptureModule* module_;
     91   int captured_frames_;
     92   std::vector<uint8_t> capture_buffer_;
     93 
     94   // Critical section to avoid Stop during an OnIncomingCapturedFrame callback.
     95   talk_base::CriticalSection critical_section_stopping_;
     96 };
     97 
     98 struct WebRtcCapturedFrame : public CapturedFrame {
     99  public:
    100   WebRtcCapturedFrame(const webrtc::I420VideoFrame& frame,
    101                       void* buffer, int length);
    102 };
    103 
    104 }  // namespace cricket
    105 
    106 #endif  // HAVE_WEBRTC_VIDEO
    107 #endif  // TALK_MEDIA_WEBRTCVIDEOCAPTURER_H_
    108