summaryrefslogtreecommitdiffstats
path: root/media/video/capture/fake_video_capture_device.cc
blob: 5e5c387427c3c3060dff0a35f2865f638c0ab097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/video/capture/fake_video_capture_device.h"

#include <string>

#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "media/audio/fake_audio_input_stream.h"
#include "media/base/video_frame.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"

namespace media {

static const int kFakeCaptureBeepCycle = 10;  // Visual beep every 0.5s.

void DrawPacman(bool use_argb,
                uint8_t* const data,
                int frame_count,
                int frame_interval,
                const gfx::Size& frame_size) {
  // |kN32_SkColorType| stands for the appropriiate RGBA/BGRA format.
  const SkColorType colorspace =
      use_argb ? kN32_SkColorType : kAlpha_8_SkColorType;
  const SkImageInfo info = SkImageInfo::Make(frame_size.width(),
                                             frame_size.height(),
                                             colorspace,
                                             kOpaque_SkAlphaType);
  SkBitmap bitmap;
  bitmap.setInfo(info);
  bitmap.setPixels(data);
  SkPaint paint;
  paint.setStyle(SkPaint::kFill_Style);
  SkCanvas canvas(bitmap);

  // Equalize Alpha_8 that has light green background while RGBA has white.
  if (use_argb) {
    const SkRect full_frame = SkRect::MakeWH(frame_size.width(),
                                             frame_size.height());
    paint.setARGB(255, 0, 127, 0);
    canvas.drawRect(full_frame, paint);
  }
  paint.setColor(SK_ColorGREEN);

  // Draw a sweeping circle to show an animation.
  const int end_angle = (3 * kFakeCaptureBeepCycle * frame_count % 361);
  const int radius = std::min(frame_size.width(), frame_size.height()) / 4;
  const SkRect rect = SkRect::MakeXYWH(frame_size.width() / 2 - radius,
                                       frame_size.height() / 2 - radius,
                                       2 * radius,
                                       2 * radius);
  canvas.drawArc(rect, 0, end_angle, true, paint);

  // Draw current time.
  const int elapsed_ms = frame_interval * frame_count;
  const int milliseconds = elapsed_ms % 1000;
  const int seconds = (elapsed_ms / 1000) % 60;
  const int minutes = (elapsed_ms / 1000 / 60) % 60;
  const int hours = (elapsed_ms / 1000 / 60 / 60) % 60;

  const std::string time_string = base::StringPrintf("%d:%02d:%02d:%03d %d",
      hours, minutes, seconds, milliseconds, frame_count);
  canvas.scale(3, 3);
  canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint);
}

FakeVideoCaptureDevice::FakeVideoCaptureDevice(
    FakeVideoCaptureDeviceType device_type)
    : device_type_(device_type),
      frame_count_(0),
      weak_factory_(this) {}

FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
  DCHECK(thread_checker_.CalledOnValidThread());
}

void FakeVideoCaptureDevice::AllocateAndStart(
    const VideoCaptureParams& params,
    scoped_ptr<VideoCaptureDevice::Client> client) {
  DCHECK(thread_checker_.CalledOnValidThread());

  client_ = client.Pass();

  // Incoming |params| can be none of the supported formats, so we get the
  // closest thing rounded up. TODO(mcasas): Use the |params|, if they belong to
  // the supported ones, when http://crbug.com/309554 is verified.
  DCHECK_EQ(params.requested_format.pixel_format, PIXEL_FORMAT_I420);
  capture_format_.pixel_format = params.requested_format.pixel_format;
  capture_format_.frame_rate = 30.0;
  if (params.requested_format.frame_size.width() > 1280)
      capture_format_.frame_size.SetSize(1920, 1080);
  else if (params.requested_format.frame_size.width() > 640)
      capture_format_.frame_size.SetSize(1280, 720);
  else if (params.requested_format.frame_size.width() > 320)
    capture_format_.frame_size.SetSize(640, 480);
  else
    capture_format_.frame_size.SetSize(320, 240);

  switch (device_type_) {
    case USING_OWN_BUFFERS:
      fake_frame_.reset(new uint8[VideoFrame::AllocationSize(
          VideoFrame::I420, capture_format_.frame_size)]);
      BeepAndScheduleNextCapture(
          base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
                     weak_factory_.GetWeakPtr()));
      break;
    case USING_CLIENT_BUFFERS:
      BeepAndScheduleNextCapture(
          base::Bind(&FakeVideoCaptureDevice::CaptureUsingClientBuffers,
                     weak_factory_.GetWeakPtr()));
      break;
    case USING_GPU_MEMORY_BUFFERS:
      BeepAndScheduleNextCapture(
          base::Bind(&FakeVideoCaptureDevice::CaptureUsingGpuMemoryBuffers,
                     weak_factory_.GetWeakPtr()));
      break;
    default:
      client_->OnError("Unknown Fake Video Capture Device type.");
  }
}

void FakeVideoCaptureDevice::StopAndDeAllocate() {
  DCHECK(thread_checker_.CalledOnValidThread());
  client_.reset();
}

void FakeVideoCaptureDevice::CaptureUsingOwnBuffers() {
  DCHECK(thread_checker_.CalledOnValidThread());
  const size_t frame_size =
      VideoFrame::AllocationSize(VideoFrame::I420, capture_format_.frame_size);
  memset(fake_frame_.get(), 0, frame_size);

  DrawPacman(false  /* use_argb */,
             fake_frame_.get(),
             frame_count_,
             kFakeCapturePeriodMs,
             capture_format_.frame_size);

  // Give the captured frame to the client.
  client_->OnIncomingCapturedData(fake_frame_.get(),
                                  frame_size,
                                  capture_format_,
                                  0,
                                  base::TimeTicks::Now());
  BeepAndScheduleNextCapture(
      base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
                 weak_factory_.GetWeakPtr()));
}

void FakeVideoCaptureDevice::CaptureUsingClientBuffers() {
  DCHECK(thread_checker_.CalledOnValidThread());

  const scoped_refptr<VideoCaptureDevice::Client::Buffer> capture_buffer =
      client_->ReserveOutputBuffer(VideoFrame::I420,
                                   capture_format_.frame_size);
  DLOG_IF(ERROR, !capture_buffer) << "Couldn't allocate Capture Buffer";
  if (!capture_buffer)
    return;

  uint8_t* const data_ptr = static_cast<uint8_t*>(capture_buffer->data());
  memset(data_ptr, 0, capture_buffer->size());
  DCHECK(data_ptr) << "Buffer has NO backing memory";

  DrawPacman(false  /* use_argb */,
             data_ptr,
             frame_count_,
             kFakeCapturePeriodMs,
             capture_format_.frame_size);

  scoped_refptr<VideoFrame> video_frame =
      VideoFrame::WrapExternalPackedMemory(
          VideoFrame::I420,
          capture_format_.frame_size,
          gfx::Rect(capture_format_.frame_size),
          capture_format_.frame_size,
          static_cast<uint8*>(capture_buffer->data()),
          capture_buffer->size(),
          base::SharedMemory::NULLHandle(),
          0,
          base::TimeDelta(),
          base::Closure());

  // Give the captured frame to the client.
  client_->OnIncomingCapturedVideoFrame(capture_buffer,
                                        video_frame,
                                        base::TimeTicks::Now());
  BeepAndScheduleNextCapture(
      base::Bind(&FakeVideoCaptureDevice::CaptureUsingClientBuffers,
                 weak_factory_.GetWeakPtr()));
}

void FakeVideoCaptureDevice::CaptureUsingGpuMemoryBuffers() {
  DCHECK(thread_checker_.CalledOnValidThread());

  NOTIMPLEMENTED();
}

void FakeVideoCaptureDevice::BeepAndScheduleNextCapture(
    const base::Closure& next_capture) {
  // Generate a synchronized beep sound every so many frames.
  if (frame_count_++ % kFakeCaptureBeepCycle == 0)
    FakeAudioInputStream::BeepOnce();

  // Reschedule next CaptureTask.
  base::MessageLoop::current()->PostDelayedTask(FROM_HERE, next_capture,
      base::TimeDelta::FromMilliseconds(kFakeCapturePeriodMs));
}

}  // namespace media