summaryrefslogtreecommitdiff
path: root/chromium/media/video/capture/mac/video_capture_device_mac.mm
blob: eea861481fe8a00608429eaa2fbcd22660b4fdf1 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// 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/mac/video_capture_device_mac.h"

#import <QTKit/QTKit.h>

#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/time/time.h"
#include "media/video/capture/mac/video_capture_device_qtkit_mac.h"

namespace {

const int kMinFrameRate = 1;
const int kMaxFrameRate = 30;

// In QT device identifiers, the USB VID and PID are stored in 4 bytes each.
const size_t kVidPidSize = 4;

struct Resolution {
  int width;
  int height;
};

const Resolution kQVGA = { 320, 240 },
                 kVGA = { 640, 480 },
                 kHD = { 1280, 720 };

const Resolution* const kWellSupportedResolutions[] = {
   &kQVGA,
   &kVGA,
   &kHD,
};

// TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability.
void GetBestMatchSupportedResolution(int* width, int* height) {
  int min_diff = kint32max;
  int matched_width = *width;
  int matched_height = *height;
  int desired_res_area = *width * *height;
  for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) {
    int area = kWellSupportedResolutions[i]->width *
               kWellSupportedResolutions[i]->height;
    int diff = std::abs(desired_res_area - area);
    if (diff < min_diff) {
      min_diff = diff;
      matched_width = kWellSupportedResolutions[i]->width;
      matched_height = kWellSupportedResolutions[i]->height;
    }
  }
  *width = matched_width;
  *height = matched_height;
}

}

namespace media {

void VideoCaptureDevice::GetDeviceNames(Names* device_names) {
  // Loop through all available devices and add to |device_names|.
  device_names->clear();

  NSDictionary* capture_devices = [VideoCaptureDeviceQTKit deviceNames];
  for (NSString* key in capture_devices) {
    Name name([[capture_devices valueForKey:key] UTF8String],
              [key UTF8String]);
    device_names->push_back(name);
  }
}

const std::string VideoCaptureDevice::Name::GetModel() const {
  // Both PID and VID are 4 characters.
  if (unique_id_.size() < 2 * kVidPidSize) {
    return "";
  }

  // The last characters of device id is a concatenation of VID and then PID.
  const size_t vid_location = unique_id_.size() - 2 * kVidPidSize;
  std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize);
  const size_t pid_location = unique_id_.size() - kVidPidSize;
  std::string id_product = unique_id_.substr(pid_location, kVidPidSize);

  return id_vendor + ":" + id_product;
}

VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) {
  VideoCaptureDeviceMac* capture_device =
      new VideoCaptureDeviceMac(device_name);
  if (!capture_device->Init()) {
    LOG(ERROR) << "Could not initialize VideoCaptureDevice.";
    delete capture_device;
    capture_device = NULL;
  }
  return capture_device;
}

VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name)
    : device_name_(device_name),
      observer_(NULL),
      sent_frame_info_(false),
      loop_proxy_(base::MessageLoopProxy::current()),
      state_(kNotInitialized),
      weak_factory_(this),
      weak_this_(weak_factory_.GetWeakPtr()),
      capture_device_(nil) {
}

VideoCaptureDeviceMac::~VideoCaptureDeviceMac() {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  [capture_device_ release];
}

void VideoCaptureDeviceMac::Allocate(
    const VideoCaptureCapability& capture_format,
    EventHandler* observer) {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  if (state_ != kIdle) {
    return;
  }
  int width = capture_format.width;
  int height = capture_format.height;
  int frame_rate = capture_format.frame_rate;

  // QTKit can scale captured frame to any size requested, which would lead to
  // undesired aspect ratio change. Tries to open the camera with a natively
  // supported format and let the client to crop/pad the captured frames.
  GetBestMatchSupportedResolution(&width,
                                  &height);

  observer_ = observer;
  NSString* deviceId =
      [NSString stringWithUTF8String:device_name_.id().c_str()];

  [capture_device_ setFrameReceiver:this];

  if (![capture_device_ setCaptureDevice:deviceId]) {
    SetErrorState("Could not open capture device.");
    return;
  }
  if (frame_rate < kMinFrameRate)
    frame_rate = kMinFrameRate;
  else if (frame_rate > kMaxFrameRate)
    frame_rate = kMaxFrameRate;

  current_settings_.color = PIXEL_FORMAT_UYVY;
  current_settings_.width = width;
  current_settings_.height = height;
  current_settings_.frame_rate = frame_rate;
  current_settings_.expected_capture_delay = 0;
  current_settings_.interlaced = false;

  if (width != kHD.width || height != kHD.height) {
    // If the resolution is VGA or QVGA, set the capture resolution to the
    // target size. For most cameras (though not all), at these resolutions
    // QTKit produces frames with square pixels.
    if (!UpdateCaptureResolution())
      return;

    sent_frame_info_ = true;
    observer_->OnFrameInfo(current_settings_);
  }

  // If the resolution is HD, start capturing without setting a resolution.
  // QTKit will produce frames at the native resolution, allowing us to
  // identify cameras whose native resolution is too low for HD.  This
  // additional information comes at a cost in startup latency, because the
  // webcam will need to be reopened if its default resolution is not HD or VGA.

  if (![capture_device_ startCapture]) {
    SetErrorState("Could not start capture device.");
    return;
  }

  state_ = kAllocated;
}

void VideoCaptureDeviceMac::Start() {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  DCHECK_EQ(state_, kAllocated);
  state_ = kCapturing;

  // This method no longer has any effect.  Capturing is triggered by
  // the call to Allocate.
  // TODO(bemasc, ncarter): Remove this method.
}

void VideoCaptureDeviceMac::Stop() {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  DCHECK(state_ == kCapturing || state_ == kError) << state_;
  [capture_device_ stopCapture];
  state_ = kAllocated;
}

void VideoCaptureDeviceMac::DeAllocate() {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  if (state_ != kAllocated && state_ != kCapturing) {
    return;
  }
  if (state_ == kCapturing) {
    [capture_device_ stopCapture];
  }
  [capture_device_ setCaptureDevice:nil];
  [capture_device_ setFrameReceiver:nil];

  state_ = kIdle;
}

const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() {
  return device_name_;
}

bool VideoCaptureDeviceMac::Init() {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  DCHECK_EQ(state_, kNotInitialized);

  Names device_names;
  GetDeviceNames(&device_names);
  Name* found = device_names.FindById(device_name_.id());
  if (!found)
    return false;

  capture_device_ =
      [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this];
  if (!capture_device_)
    return false;

  state_ = kIdle;
  return true;
}

void VideoCaptureDeviceMac::ReceiveFrame(
    const uint8* video_frame,
    int video_frame_length,
    const VideoCaptureCapability& frame_info,
    int aspect_numerator,
    int aspect_denominator) {
  // This method is safe to call from a device capture thread,
  // i.e. any thread controlled by QTKit.

  if (!sent_frame_info_) {
    if (current_settings_.width == kHD.width &&
        current_settings_.height == kHD.height) {
      bool changeToVga = false;
      if (frame_info.width < kHD.width || frame_info.height < kHD.height) {
        // These are the default capture settings, not yet configured to match
        // |current_settings_|.
        DCHECK(frame_info.frame_rate == 0);
        DVLOG(1) << "Switching to VGA because the default resolution is " <<
            frame_info.width << "x" << frame_info.height;
        changeToVga = true;
      }
      if (frame_info.width == kHD.width && frame_info.height == kHD.height &&
          aspect_numerator != aspect_denominator) {
        DVLOG(1) << "Switching to VGA because HD has nonsquare pixel " <<
            "aspect ratio " << aspect_numerator << ":" << aspect_denominator;
        changeToVga = true;
      }

      if (changeToVga) {
        current_settings_.width = kVGA.width;
        current_settings_.height = kVGA.height;
      }
    }

    if (current_settings_.width == frame_info.width &&
        current_settings_.height == frame_info.height) {
      sent_frame_info_ = true;
      observer_->OnFrameInfo(current_settings_);
    } else {
      UpdateCaptureResolution();
      // The current frame does not have the right width and height, so it
      // must not be passed to |observer_|.
      return;
    }
  }

  DCHECK(current_settings_.width == frame_info.width &&
         current_settings_.height == frame_info.height);

  observer_->OnIncomingCapturedFrame(
      video_frame, video_frame_length, base::Time::Now(), 0, false, false);
}

void VideoCaptureDeviceMac::ReceiveError(const std::string& reason) {
  loop_proxy_->PostTask(FROM_HERE,
      base::Bind(&VideoCaptureDeviceMac::SetErrorState, weak_this_,
          reason));
}

void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) {
  DCHECK_EQ(loop_proxy_, base::MessageLoopProxy::current());
  DLOG(ERROR) << reason;
  state_ = kError;
  observer_->OnError();
}

bool VideoCaptureDeviceMac::UpdateCaptureResolution() {
 if (![capture_device_ setCaptureHeight:current_settings_.height
                                  width:current_settings_.width
                              frameRate:current_settings_.frame_rate]) {
   ReceiveError("Could not configure capture device.");
   return false;
 }
 return true;
}

} // namespace media