fakevideocapturer.h revision 20e8f227664a6747cea11e1fc1de4c018ebcc8e9
1/*
2 * libjingle
3 * Copyright 2004 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#ifndef TALK_MEDIA_BASE_FAKEVIDEOCAPTURER_H_
29#define TALK_MEDIA_BASE_FAKEVIDEOCAPTURER_H_
30
31#include <string.h>
32
33#include <vector>
34
35#include "talk/media/base/videocapturer.h"
36#include "talk/media/base/videocommon.h"
37#include "talk/media/base/videoframe.h"
38#include "webrtc/base/timeutils.h"
39#ifdef HAVE_WEBRTC_VIDEO
40#include "talk/media/webrtc/webrtcvideoframefactory.h"
41#endif
42
43namespace cricket {
44
45// Fake video capturer that allows the test to manually pump in frames.
46class FakeVideoCapturer : public cricket::VideoCapturer {
47 public:
48  FakeVideoCapturer()
49      : running_(false),
50        initial_unix_timestamp_(time(NULL) * rtc::kNumNanosecsPerSec),
51        next_timestamp_(rtc::kNumNanosecsPerMillisec),
52        is_screencast_(false),
53        rotation_(webrtc::kVideoRotation_0) {
54#ifdef HAVE_WEBRTC_VIDEO
55    set_frame_factory(new cricket::WebRtcVideoFrameFactory());
56#endif
57    // Default supported formats. Use ResetSupportedFormats to over write.
58    std::vector<cricket::VideoFormat> formats;
59    formats.push_back(cricket::VideoFormat(1280, 720,
60        cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
61    formats.push_back(cricket::VideoFormat(640, 480,
62        cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
63    formats.push_back(cricket::VideoFormat(320, 240,
64        cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
65    formats.push_back(cricket::VideoFormat(160, 120,
66        cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420));
67    ResetSupportedFormats(formats);
68  }
69  ~FakeVideoCapturer() {
70    SignalDestroyed(this);
71  }
72
73  void ResetSupportedFormats(const std::vector<cricket::VideoFormat>& formats) {
74    SetSupportedFormats(formats);
75  }
76  bool CaptureFrame() {
77    if (!GetCaptureFormat()) {
78      return false;
79    }
80    return CaptureCustomFrame(GetCaptureFormat()->width,
81                              GetCaptureFormat()->height,
82                              GetCaptureFormat()->fourcc);
83  }
84  bool CaptureCustomFrame(int width, int height, uint32 fourcc) {
85    if (!running_) {
86      return false;
87    }
88    // Currently, |fourcc| is always I420 or ARGB.
89    // TODO(fbarchard): Extend SizeOf to take fourcc.
90    uint32 size = 0u;
91    if (fourcc == cricket::FOURCC_ARGB) {
92      size = width * 4 * height;
93    } else if (fourcc == cricket::FOURCC_I420) {
94      size = static_cast<uint32>(cricket::VideoFrame::SizeOf(width, height));
95    } else {
96      return false;  // Unsupported FOURCC.
97    }
98    if (size == 0u) {
99      return false;  // Width and/or Height were zero.
100    }
101
102    cricket::CapturedFrame frame;
103    frame.width = width;
104    frame.height = height;
105    frame.fourcc = fourcc;
106    frame.data_size = size;
107    frame.elapsed_time = next_timestamp_;
108    frame.time_stamp = initial_unix_timestamp_ + next_timestamp_;
109    next_timestamp_ += 33333333;  // 30 fps
110
111    rtc::scoped_ptr<char[]> data(new char[size]);
112    frame.data = data.get();
113    // Copy something non-zero into the buffer so Validate wont complain that
114    // the frame is all duplicate.
115    memset(frame.data, 1, size / 2);
116    memset(reinterpret_cast<uint8*>(frame.data) + (size / 2), 2,
117         size - (size / 2));
118    memcpy(frame.data, reinterpret_cast<const uint8*>(&fourcc), 4);
119    frame.rotation = rotation_;
120    // TODO(zhurunz): SignalFrameCaptured carry returned value to be able to
121    // capture results from downstream.
122    SignalFrameCaptured(this, &frame);
123    return true;
124  }
125
126  sigslot::signal1<FakeVideoCapturer*> SignalDestroyed;
127
128  virtual cricket::CaptureState Start(const cricket::VideoFormat& format) {
129    cricket::VideoFormat supported;
130    if (GetBestCaptureFormat(format, &supported)) {
131      SetCaptureFormat(&supported);
132    }
133    running_ = true;
134    SetCaptureState(cricket::CS_RUNNING);
135    return cricket::CS_RUNNING;
136  }
137  virtual void Stop() {
138    running_ = false;
139    SetCaptureFormat(NULL);
140    SetCaptureState(cricket::CS_STOPPED);
141  }
142  virtual bool IsRunning() { return running_; }
143  void SetScreencast(bool is_screencast) {
144    is_screencast_ = is_screencast;
145  }
146  virtual bool IsScreencast() const { return is_screencast_; }
147  bool GetPreferredFourccs(std::vector<uint32>* fourccs) {
148    fourccs->push_back(cricket::FOURCC_I420);
149    fourccs->push_back(cricket::FOURCC_MJPG);
150    return true;
151  }
152
153  void SetRotation(webrtc::VideoRotation rotation) {
154    rotation_ = rotation;
155  }
156
157  webrtc::VideoRotation GetRotation() { return rotation_; }
158
159 private:
160  bool running_;
161  int64 initial_unix_timestamp_;
162  int64 next_timestamp_;
163  bool is_screencast_;
164  webrtc::VideoRotation rotation_;
165};
166
167}  // namespace cricket
168
169#endif  // TALK_MEDIA_BASE_FAKEVIDEOCAPTURER_H_
170