1 /*
2  * libjingle
3  * Copyright 2011, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 // This class implements the VideoCaptureModule interface. Instead of capturing
29 // frames from a camera it captures frames from a file or a static frame.
30 
31 #ifndef TALK_APP_WEBRTC_TEST_FAKEVIDEOCAPTUREMODULE_H_
32 #define TALK_APP_WEBRTC_TEST_FAKEVIDEOCAPTUREMODULE_H_
33 
34 #include <string>
35 
36 #include "talk/base/basictypes.h"
37 #include "talk/base/messagehandler.h"
38 #include "talk/base/scoped_ptr.h"
39 #include "talk/base/scoped_ref_ptr.h"
40 
41 #ifdef WEBRTC_RELATIVE_PATH
42 #include "common_types.h"
43 #include "modules/video_capture/main/interface/video_capture.h"
44 #else
45 #include "third_party/webrtc/files/include/common_types.h"
46 #include "third_party/webrtc/files/include/video_capture.h"
47 #endif
48 
49 namespace talk_base {
50 
51 class Thread;
52 
53 }  // namespace talk_base
54 
55 namespace webrtc {
56 
57 class VideoCaptureExternal;
58 
59 }  // namespace webrtc
60 
61 class I420FrameSource;
62 
63 class FakeVideoCaptureModule
64     : public webrtc::VideoCaptureModule,
65       public talk_base::MessageHandler {
66  public:
67   virtual ~FakeVideoCaptureModule();
68 
69   static FakeVideoCaptureModule* Create(talk_base::Thread* camera_thread);
70   static FakeVideoCaptureModule* Create(talk_base::Thread* camera_thread,
71                                         const std::string& file_name);
72 
73   void StartCapturing();
74   void StopCapturing();
75 
76   bool SetFrameRate(int fps);
77   void SetSize(int width, int height);
sent_frames()78   int sent_frames() const { return sent_frames_; }
79 
ChangeUniqueId(const int32_t id)80   virtual int32_t ChangeUniqueId(const int32_t id) {
81     return impl_->ChangeUniqueId(id);
82   }
83 
TimeUntilNextProcess()84   virtual int32_t TimeUntilNextProcess() {
85     return impl_->TimeUntilNextProcess();
86   }
87 
Process()88   virtual int32_t Process() {
89     return impl_->Process();
90   }
91 
RegisterCaptureDataCallback(webrtc::VideoCaptureDataCallback & dataCallback)92   virtual WebRtc_Word32 RegisterCaptureDataCallback(
93       webrtc::VideoCaptureDataCallback& dataCallback) {
94     return impl_->RegisterCaptureDataCallback(dataCallback);
95   }
96 
DeRegisterCaptureDataCallback()97   virtual WebRtc_Word32 DeRegisterCaptureDataCallback() {
98     return impl_->DeRegisterCaptureDataCallback();
99   }
100 
RegisterCaptureCallback(webrtc::VideoCaptureFeedBack & callBack)101   virtual WebRtc_Word32 RegisterCaptureCallback(
102       webrtc::VideoCaptureFeedBack& callBack) {
103     return impl_->RegisterCaptureCallback(callBack);
104   }
105 
DeRegisterCaptureCallback()106   virtual WebRtc_Word32 DeRegisterCaptureCallback() {
107     return impl_->DeRegisterCaptureCallback();
108   }
109 
StartCapture(const webrtc::VideoCaptureCapability & capability)110   virtual WebRtc_Word32 StartCapture(
111       const webrtc::VideoCaptureCapability& capability) {
112     capture_started_ = true;
113     return 0;
114   }
115 
StopCapture()116   virtual WebRtc_Word32 StopCapture() {
117     capture_started_ = false;
118     return 0;
119   }
120 
121   virtual WebRtc_Word32 StartSendImage(const webrtc::VideoFrame& videoFrame,
122                                        WebRtc_Word32 frameRate = 1) {
123     return impl_->StartSendImage(videoFrame, frameRate = 1);
124   }
125 
StopSendImage()126   virtual WebRtc_Word32 StopSendImage() {
127     return impl_->StopSendImage();
128   }
129 
CurrentDeviceName()130   virtual const char* CurrentDeviceName() const {
131     return impl_->CurrentDeviceName();
132   }
133 
CaptureStarted()134   virtual bool CaptureStarted() {
135     return capture_started_;
136   }
137 
CaptureSettings(webrtc::VideoCaptureCapability & settings)138   virtual WebRtc_Word32 CaptureSettings(
139       webrtc::VideoCaptureCapability& settings) {
140     return impl_->CaptureSettings(settings);
141   }
142 
SetCaptureDelay(WebRtc_Word32 delayMS)143   virtual WebRtc_Word32 SetCaptureDelay(WebRtc_Word32 delayMS) {
144     return impl_->SetCaptureDelay(delayMS);
145   }
146 
CaptureDelay()147   virtual WebRtc_Word32 CaptureDelay() {
148     return impl_->CaptureDelay();
149   }
150 
SetCaptureRotation(webrtc::VideoCaptureRotation rotation)151   virtual WebRtc_Word32 SetCaptureRotation(
152       webrtc::VideoCaptureRotation rotation) {
153     return impl_->SetCaptureRotation(rotation);
154   }
155 
GetEncodeInterface(const webrtc::VideoCodec & codec)156   virtual VideoCaptureEncodeInterface* GetEncodeInterface(
157       const webrtc::VideoCodec& codec) {
158     return impl_->GetEncodeInterface(codec);
159   }
160 
EnableFrameRateCallback(const bool enable)161   virtual WebRtc_Word32 EnableFrameRateCallback(const bool enable) {
162     return impl_->EnableFrameRateCallback(enable);
163   }
EnableNoPictureAlarm(const bool enable)164   virtual WebRtc_Word32 EnableNoPictureAlarm(const bool enable) {
165     return impl_->EnableNoPictureAlarm(enable);
166   }
167 
168   // Inherited from MessageHandler.
OnMessage(talk_base::Message * msg)169   virtual void OnMessage(talk_base::Message* msg) {
170     GenerateNewFrame();
171   }
172 
173  protected:
174   FakeVideoCaptureModule(talk_base::Thread* camera_thread);
175 
176  private:
177   bool Init(I420FrameSource* frame_source);
178   bool RegisterFrameSource(I420FrameSource* frame_source);
179 
180   void GenerateNewFrame();
181   size_t GetI420FrameLengthInBytes();
182   uint32 GetTimestamp();
183 
184   // Module interface implementation.
185   talk_base::scoped_refptr<VideoCaptureModule> impl_;
186 
187   // Class that generates the frames from e.g. file or staticly.
188   I420FrameSource* frame_source_;
189 
190   talk_base::Thread* camera_thread_;
191   webrtc::VideoCaptureExternal* video_capture_;
192 
193   bool started_;
194   bool capture_started_;
195   int sent_frames_;
196   uint32 next_frame_time_;
197   uint32 time_per_frame_ms_;
198 
199   int fps_;
200   int width_;
201   int height_;
202   talk_base::scoped_array<uint8> image_;
203 };
204 
205 #endif  // TALK_APP_WEBRTC_TEST_FAKEVIDEOCAPTUREMODULE_H_
206