blob: 32f105dc11d475b406f3b7cdeaa9db3668de4710 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001// libjingle
2// Copyright 2011 Google Inc.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// 3. The name of the author may not be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26// Implementation of class WebRtcVideoCapturer.
27
28#include "talk/media/webrtc/webrtcvideocapturer.h"
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
34#ifdef HAVE_WEBRTC_VIDEO
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000035#include "talk/media/webrtc/webrtcvideoframe.h"
36#include "talk/media/webrtc/webrtcvideoframefactory.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037#include "webrtc/base/criticalsection.h"
38#include "webrtc/base/logging.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000039#include "webrtc/base/safe_conversions.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040#include "webrtc/base/thread.h"
41#include "webrtc/base/timeutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043#include "webrtc/base/win32.h" // Need this to #include the impl files.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044#include "webrtc/modules/video_capture/include/video_capture_factory.h"
45
46namespace cricket {
47
48struct kVideoFourCCEntry {
49 uint32 fourcc;
50 webrtc::RawVideoType webrtc_type;
51};
52
53// This indicates our format preferences and defines a mapping between
54// webrtc::RawVideoType (from video_capture_defines.h) to our FOURCCs.
55static kVideoFourCCEntry kSupportedFourCCs[] = {
56 { FOURCC_I420, webrtc::kVideoI420 }, // 12 bpp, no conversion.
57 { FOURCC_YV12, webrtc::kVideoYV12 }, // 12 bpp, no conversion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 { FOURCC_YUY2, webrtc::kVideoYUY2 }, // 16 bpp, fast conversion.
59 { FOURCC_UYVY, webrtc::kVideoUYVY }, // 16 bpp, fast conversion.
buildbot@webrtc.orgfa5fcd62014-07-21 21:55:43 +000060 { FOURCC_NV12, webrtc::kVideoNV12 }, // 12 bpp, fast conversion.
61 { FOURCC_NV21, webrtc::kVideoNV21 }, // 12 bpp, fast conversion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 { FOURCC_MJPG, webrtc::kVideoMJPEG }, // compressed, slow conversion.
63 { FOURCC_ARGB, webrtc::kVideoARGB }, // 32 bpp, slow conversion.
64 { FOURCC_24BG, webrtc::kVideoRGB24 }, // 24 bpp, slow conversion.
65};
66
67class WebRtcVcmFactory : public WebRtcVcmFactoryInterface {
68 public:
69 virtual webrtc::VideoCaptureModule* Create(int id, const char* device) {
70 return webrtc::VideoCaptureFactory::Create(id, device);
71 }
72 virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) {
73 return webrtc::VideoCaptureFactory::CreateDeviceInfo(id);
74 }
75 virtual void DestroyDeviceInfo(webrtc::VideoCaptureModule::DeviceInfo* info) {
76 delete info;
77 }
78};
79
80static bool CapabilityToFormat(const webrtc::VideoCaptureCapability& cap,
81 VideoFormat* format) {
82 uint32 fourcc = 0;
83 for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) {
84 if (kSupportedFourCCs[i].webrtc_type == cap.rawType) {
85 fourcc = kSupportedFourCCs[i].fourcc;
86 break;
87 }
88 }
89 if (fourcc == 0) {
90 return false;
91 }
92
93 format->fourcc = fourcc;
94 format->width = cap.width;
95 format->height = cap.height;
96 format->interval = VideoFormat::FpsToInterval(cap.maxFPS);
97 return true;
98}
99
100static bool FormatToCapability(const VideoFormat& format,
101 webrtc::VideoCaptureCapability* cap) {
102 webrtc::RawVideoType webrtc_type = webrtc::kVideoUnknown;
103 for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) {
104 if (kSupportedFourCCs[i].fourcc == format.fourcc) {
105 webrtc_type = kSupportedFourCCs[i].webrtc_type;
106 break;
107 }
108 }
109 if (webrtc_type == webrtc::kVideoUnknown) {
110 return false;
111 }
112
113 cap->width = format.width;
114 cap->height = format.height;
115 cap->maxFPS = VideoFormat::IntervalToFps(format.interval);
116 cap->expectedCaptureDelay = 0;
117 cap->rawType = webrtc_type;
118 cap->codecType = webrtc::kVideoCodecUnknown;
119 cap->interlaced = false;
120 return true;
121}
122
123///////////////////////////////////////////////////////////////////////////
124// Implementation of class WebRtcVideoCapturer
125///////////////////////////////////////////////////////////////////////////
126
127WebRtcVideoCapturer::WebRtcVideoCapturer()
128 : factory_(new WebRtcVcmFactory),
129 module_(NULL),
130 captured_frames_(0) {
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000131 set_frame_factory(new WebRtcVideoFrameFactory());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132}
133
134WebRtcVideoCapturer::WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory)
135 : factory_(factory),
136 module_(NULL),
137 captured_frames_(0) {
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000138 set_frame_factory(new WebRtcVideoFrameFactory());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139}
140
141WebRtcVideoCapturer::~WebRtcVideoCapturer() {
142 if (module_) {
143 module_->Release();
144 }
145}
146
147bool WebRtcVideoCapturer::Init(const Device& device) {
148 if (module_) {
149 LOG(LS_ERROR) << "The capturer is already initialized";
150 return false;
151 }
152
153 webrtc::VideoCaptureModule::DeviceInfo* info = factory_->CreateDeviceInfo(0);
154 if (!info) {
155 return false;
156 }
157
158 // Find the desired camera, by name.
159 // In the future, comparing IDs will be more robust.
160 // TODO(juberti): Figure what's needed to allow this.
161 int num_cams = info->NumberOfDevices();
162 char vcm_id[256] = "";
163 bool found = false;
164 for (int index = 0; index < num_cams; ++index) {
165 char vcm_name[256];
166 if (info->GetDeviceName(index, vcm_name, ARRAY_SIZE(vcm_name),
167 vcm_id, ARRAY_SIZE(vcm_id)) != -1) {
168 if (device.name == reinterpret_cast<char*>(vcm_name)) {
169 found = true;
170 break;
171 }
172 }
173 }
174 if (!found) {
175 LOG(LS_WARNING) << "Failed to find capturer for id: " << device.id;
176 factory_->DestroyDeviceInfo(info);
177 return false;
178 }
179
180 // Enumerate the supported formats.
181 // TODO(juberti): Find out why this starts/stops the camera...
182 std::vector<VideoFormat> supported;
183 int32_t num_caps = info->NumberOfCapabilities(vcm_id);
184 for (int32_t i = 0; i < num_caps; ++i) {
185 webrtc::VideoCaptureCapability cap;
186 if (info->GetCapability(vcm_id, i, cap) != -1) {
187 VideoFormat format;
188 if (CapabilityToFormat(cap, &format)) {
189 supported.push_back(format);
190 } else {
191 LOG(LS_WARNING) << "Ignoring unsupported WebRTC capture format "
192 << cap.rawType;
193 }
194 }
195 }
196 factory_->DestroyDeviceInfo(info);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000197// TODO(fischman): Remove the following check
198// when capabilities for iOS are implemented
199// https://code.google.com/p/webrtc/issues/detail?id=2968
200#if !defined(IOS)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 if (supported.empty()) {
202 LOG(LS_ERROR) << "Failed to find usable formats for id: " << device.id;
203 return false;
204 }
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000205#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 module_ = factory_->Create(0, vcm_id);
207 if (!module_) {
208 LOG(LS_ERROR) << "Failed to create capturer for id: " << device.id;
209 return false;
210 }
211
212 // It is safe to change member attributes now.
213 module_->AddRef();
214 SetId(device.id);
215 SetSupportedFormats(supported);
216 return true;
217}
218
219bool WebRtcVideoCapturer::Init(webrtc::VideoCaptureModule* module) {
220 if (module_) {
221 LOG(LS_ERROR) << "The capturer is already initialized";
222 return false;
223 }
224 if (!module) {
225 LOG(LS_ERROR) << "Invalid VCM supplied";
226 return false;
227 }
228 // TODO(juberti): Set id and formats.
229 (module_ = module)->AddRef();
230 return true;
231}
232
233bool WebRtcVideoCapturer::GetBestCaptureFormat(const VideoFormat& desired,
234 VideoFormat* best_format) {
235 if (!best_format) {
236 return false;
237 }
238
239 if (!VideoCapturer::GetBestCaptureFormat(desired, best_format)) {
240 // We maybe using a manually injected VCM which doesn't support enum.
241 // Use the desired format as the best format.
242 best_format->width = desired.width;
243 best_format->height = desired.height;
244 best_format->fourcc = FOURCC_I420;
245 best_format->interval = desired.interval;
246 LOG(LS_INFO) << "Failed to find best capture format,"
247 << " fall back to the requested format "
248 << best_format->ToString();
249 }
250 return true;
251}
252
253CaptureState WebRtcVideoCapturer::Start(const VideoFormat& capture_format) {
254 if (!module_) {
255 LOG(LS_ERROR) << "The capturer has not been initialized";
256 return CS_NO_DEVICE;
257 }
258
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259 rtc::CritScope cs(&critical_section_stopping_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 // TODO(hellner): weird to return failure when it is in fact actually running.
261 if (IsRunning()) {
262 LOG(LS_ERROR) << "The capturer is already running";
263 return CS_FAILED;
264 }
265
266 SetCaptureFormat(&capture_format);
267
268 webrtc::VideoCaptureCapability cap;
269 if (!FormatToCapability(capture_format, &cap)) {
270 LOG(LS_ERROR) << "Invalid capture format specified";
271 return CS_FAILED;
272 }
273
274 std::string camera_id(GetId());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275 uint32 start = rtc::Time();
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000276 module_->RegisterCaptureDataCallback(*this);
277 if (module_->StartCapture(cap) != 0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 LOG(LS_ERROR) << "Camera '" << camera_id << "' failed to start";
279 return CS_FAILED;
280 }
281
282 LOG(LS_INFO) << "Camera '" << camera_id << "' started with format "
283 << capture_format.ToString() << ", elapsed time "
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 << rtc::TimeSince(start) << " ms";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285
286 captured_frames_ = 0;
287 SetCaptureState(CS_RUNNING);
288 return CS_STARTING;
289}
290
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000291// Critical section blocks Stop from shutting down during callbacks from capture
292// thread to OnIncomingCapturedFrame. Note that the crit is try-locked in
293// OnFrameCaptured, as the lock ordering between this and the system component
294// controlling the camera is reversed: system frame -> OnIncomingCapturedFrame;
295// Stop -> system stop camera).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296void WebRtcVideoCapturer::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 rtc::CritScope cs(&critical_section_stopping_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 if (IsRunning()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299 rtc::Thread::Current()->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 module_->StopCapture();
301 module_->DeRegisterCaptureDataCallback();
302
303 // TODO(juberti): Determine if the VCM exposes any drop stats we can use.
304 double drop_ratio = 0.0;
305 std::string camera_id(GetId());
306 LOG(LS_INFO) << "Camera '" << camera_id << "' stopped after capturing "
307 << captured_frames_ << " frames and dropping "
308 << drop_ratio << "%";
309 }
310 SetCaptureFormat(NULL);
311}
312
313bool WebRtcVideoCapturer::IsRunning() {
314 return (module_ != NULL && module_->CaptureStarted());
315}
316
317bool WebRtcVideoCapturer::GetPreferredFourccs(
318 std::vector<uint32>* fourccs) {
319 if (!fourccs) {
320 return false;
321 }
322
323 fourccs->clear();
324 for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) {
325 fourccs->push_back(kSupportedFourCCs[i].fourcc);
326 }
327 return true;
328}
329
330void WebRtcVideoCapturer::OnIncomingCapturedFrame(const int32_t id,
331 webrtc::I420VideoFrame& sample) {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000332 // This would be a normal CritScope, except that it's possible that:
333 // (1) whatever system component producing this frame has taken a lock, and
334 // (2) Stop() probably calls back into that system component, which may take
335 // the same lock. Due to the reversed order, we have to try-lock in order to
336 // avoid a potential deadlock. Besides, if we can't enter because we're
337 // stopping, we may as well drop the frame.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000338 rtc::TryCritScope cs(&critical_section_stopping_);
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000339 if (!cs.locked() || !IsRunning()) {
340 // Capturer has been stopped or is in the process of stopping.
341 return;
342 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343
344 ++captured_frames_;
345 // Log the size and pixel aspect ratio of the first captured frame.
346 if (1 == captured_frames_) {
347 LOG(LS_INFO) << "Captured frame size "
348 << sample.width() << "x" << sample.height()
349 << ". Expected format " << GetCaptureFormat()->ToString();
350 }
351
352 // Signal down stream components on captured frame.
353 // The CapturedFrame class doesn't support planes. We have to ExtractBuffer
354 // to one block for it.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000355 size_t length =
356 webrtc::CalcBufferSize(webrtc::kI420, sample.width(), sample.height());
wu@webrtc.org16d62542013-11-05 23:45:14 +0000357 capture_buffer_.resize(length);
358 // TODO(ronghuawu): Refactor the WebRtcCapturedFrame to avoid memory copy.
359 webrtc::ExtractBuffer(sample, length, &capture_buffer_[0]);
360 WebRtcCapturedFrame frame(sample, &capture_buffer_[0], length);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 SignalFrameCaptured(this, &frame);
362}
363
364void WebRtcVideoCapturer::OnCaptureDelayChanged(const int32_t id,
365 const int32_t delay) {
366 LOG(LS_INFO) << "Capture delay changed to " << delay << " ms";
367}
368
369// WebRtcCapturedFrame
370WebRtcCapturedFrame::WebRtcCapturedFrame(const webrtc::I420VideoFrame& sample,
371 void* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000372 size_t length) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 width = sample.width();
374 height = sample.height();
375 fourcc = FOURCC_I420;
376 // TODO(hellner): Support pixel aspect ratio (for OSX).
377 pixel_width = 1;
378 pixel_height = 1;
379 // Convert units from VideoFrame RenderTimeMs to CapturedFrame (nanoseconds).
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000380 elapsed_time = sample.render_time_ms() * rtc::kNumNanosecsPerMillisec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 time_stamp = elapsed_time;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000382 data_size = rtc::checked_cast<uint32>(length);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 data = buffer;
384}
385
386} // namespace cricket
387
388#endif // HAVE_WEBRTC_VIDEO