summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/video_capture_controller.cc
blob: 923208719c11ede3a4ff440f7e273b393cbb1180 (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
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2011 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 "content/browser/renderer_host/video_capture_controller.h"

#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "content/browser/browser_thread.h"
#include "content/browser/media_stream/video_capture_manager.h"
#include "media/base/yuv_convert.h"

#if defined(OS_WIN)
#include "content/common/section_util_win.h"
#endif

// The number of TransportDIBs VideoCaptureController allocate.
static const size_t kNoOfDIBS = 3;

VideoCaptureController::VideoCaptureController(
    ControllerId id,
    base::ProcessHandle render_process,
    EventHandler* event_handler)
    : render_handle_(render_process),
      report_ready_to_delete_(false),
      event_handler_(event_handler),
      id_(id) {}

VideoCaptureController::~VideoCaptureController() {
  // Delete all TransportDIBs.
  STLDeleteContainerPairSecondPointers(owned_dibs_.begin(),
                                       owned_dibs_.end());
}

void VideoCaptureController::StartCapture(
    const media::VideoCaptureParams& params) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  params_ = params;
  media_stream::VideoCaptureManager* manager =
      media_stream::VideoCaptureManager::Get();
  // Order the manager to start the actual capture.
  manager->Start(params, this);
}

void VideoCaptureController::StopCapture(Task* stopped_task) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  media_stream::VideoCaptureManager* manager =
      media_stream::VideoCaptureManager::Get();
  manager->Stop(params_.session_id,
                NewRunnableMethod(this,
                                  &VideoCaptureController::OnDeviceStopped,
                                  stopped_task));
}

void VideoCaptureController::ReturnTransportDIB(TransportDIB::Handle handle) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  bool ready_to_delete;
  {
    base::AutoLock lock(lock_);
    free_dibs_.push_back(handle);
    ready_to_delete = (free_dibs_.size() == owned_dibs_.size());
  }
  if (report_ready_to_delete_ && ready_to_delete) {
    event_handler_->OnReadyToDelete(id_);
  }
}

///////////////////////////////////////////////////////////////////////////////
// Implements VideoCaptureDevice::EventHandler.
// OnIncomingCapturedFrame is called the thread running the capture device.
// I.e.- DirectShow thread on windows and v4l2_thread on Linux.
void VideoCaptureController::OnIncomingCapturedFrame(const uint8* data,
                                                     int length,
                                                     base::Time timestamp) {
  TransportDIB::Handle handle;
  TransportDIB* dib = NULL;
  // Check if there is a TransportDIB to fill.
  bool buffer_exist = false;
  {
    base::AutoLock lock(lock_);
    if (!report_ready_to_delete_ && free_dibs_.size() > 0) {
      handle = free_dibs_.back();
      free_dibs_.pop_back();
      DIBMap::iterator it = owned_dibs_.find(handle);
      if (it != owned_dibs_.end()) {
        dib = it->second;
        buffer_exist = true;
      }
    }
  }

  if (!buffer_exist) {
    return;
  }

  if (!dib->Map()) {
    VLOG(1) << "OnIncomingCapturedFrame - Failed to map handle.";
    base::AutoLock lock(lock_);
    free_dibs_.push_back(handle);
    return;
  }
  uint8* target = static_cast<uint8*>(dib->memory());
  CHECK(dib->size() >= static_cast<size_t> (frame_info_.width *
                                            frame_info_.height * 3) /
                                            2);

  // Do color conversion from the camera format to I420.
  switch (frame_info_.color) {
    case media::VideoCaptureDevice::kColorUnknown:  // Color format not set.
      break;
    case media::VideoCaptureDevice::kI420: {
      memcpy(target, data, (frame_info_.width * frame_info_.height * 3) / 2);
      break;
    }
    case media::VideoCaptureDevice::kYUY2: {
      uint8* yplane = target;
      uint8* uplane = target + frame_info_.width * frame_info_.height;
      uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
      media::ConvertYUY2ToYUV(data, yplane, uplane, vplane, frame_info_.width,
                              frame_info_.height);
      break;
    }
    case media::VideoCaptureDevice::kRGB24: {
#if defined(OS_WIN)  // RGB on Windows start at the bottom line.
      uint8* yplane = target;
      uint8* uplane = target + frame_info_.width * frame_info_.height;
      uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
      int ystride = frame_info_.width;
      int uvstride = frame_info_.width / 2;
      int rgb_stride = - 3 * frame_info_.width;
      const uint8* rgb_src = data + 3 * frame_info_.width *
          (frame_info_.height -1);
#else
      uint8* yplane = target;
      uint8* uplane = target + frame_info_.width * frame_info_.height;
      uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
      int ystride = frame_info_.width;
      int uvstride = frame_info_.width / 2;
      int rgb_stride = 3 * frame_info_.width;
      const uint8* rgb_src = data;
#endif
      media::ConvertRGB24ToYUV(rgb_src, yplane, uplane, vplane,
                               frame_info_.width, frame_info_.height,
                               rgb_stride, ystride, uvstride);
      break;
    }
    case media::VideoCaptureDevice::kARGB: {
      uint8* yplane = target;
      uint8* uplane = target +  frame_info_.width * frame_info_.height;
      uint8* vplane = uplane +  (frame_info_.width * frame_info_.height) / 4;
      media::ConvertRGB32ToYUV(data, yplane, uplane, vplane, frame_info_.width,
                               frame_info_.height, frame_info_.width * 4,
                               frame_info_.width, frame_info_.width / 2);
      break;
    }
    default:
      NOTREACHED();
  }

  event_handler_->OnBufferReady(id_, handle, timestamp);
}

void VideoCaptureController::OnError() {
  event_handler_->OnError(id_);
}

void VideoCaptureController::OnFrameInfo(
    const media::VideoCaptureDevice::Capability& info) {
  DCHECK(owned_dibs_.empty());
  bool frames_created = true;
  const size_t needed_size = (info.width * info.height * 3) / 2;
  for (size_t i = 0; i < kNoOfDIBS; ++i) {
    TransportDIB* dib = TransportDIB::Create(needed_size, i);
    if (!dib) {
      frames_created = false;
      break;
    }
    // Lock needed since the buffers are used in OnIncomingFrame
    // and we need to use it there in order to avoid memcpy of complete frames.
    base::AutoLock lock(lock_);
#if defined(OS_WIN)
    // On Windows we need to get a handle the can be used in the render process.
    TransportDIB::Handle handle = chrome::GetSectionForProcess(
        dib->handle(), render_handle_, false);
#else
    TransportDIB::Handle handle = dib->handle();
#endif
    owned_dibs_.insert(std::make_pair(handle, dib));
    free_dibs_.push_back(handle);
  }
  frame_info_= info;

  // Check that all DIBs where created successfully.
  if (!frames_created) {
    event_handler_->OnError(id_);
  }
  event_handler_->OnFrameInfo(id_, info.width, info.height,
                              info.frame_rate);
}

///////////////////////////////////////////////////////////////////////////////
// Called by VideoCaptureManager when a device have been stopped.
// This will report to the event handler that this object is ready to be deleted
// if all DIBS have been returned.
void VideoCaptureController::OnDeviceStopped(Task* stopped_task) {
  bool ready_to_delete_now;

  // Set flag to indicate we need to report when all DIBs have been returned.
  report_ready_to_delete_ = true;
  {
    base::AutoLock lock(lock_);
    ready_to_delete_now = (free_dibs_.size() == owned_dibs_.size());
  }

  if (ready_to_delete_now) {
    event_handler_->OnReadyToDelete(id_);
  }
  if (stopped_task) {
    stopped_task->Run();
    delete stopped_task;
  }
}