2 // Copyright 2011 Google Inc.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
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.
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.
26 // Implementation of class WebRtcVideoCapturer.
28 #include "talk/media/webrtc/webrtcvideocapturer.h"
34 #ifdef HAVE_WEBRTC_VIDEO
35 #include "talk/base/criticalsection.h"
36 #include "talk/base/logging.h"
37 #include "talk/base/thread.h"
38 #include "talk/base/timeutils.h"
39 #include "talk/media/webrtc/webrtcvideoframe.h"
41 #include "talk/base/win32.h" // Need this to #include the impl files.
42 #include "webrtc/modules/video_capture/include/video_capture_factory.h"
46 struct kVideoFourCCEntry {
48 webrtc::RawVideoType webrtc_type;
51 // This indicates our format preferences and defines a mapping between
52 // webrtc::RawVideoType (from video_capture_defines.h) to our FOURCCs.
53 static kVideoFourCCEntry kSupportedFourCCs[] = {
54 { FOURCC_I420, webrtc::kVideoI420 }, // 12 bpp, no conversion.
55 { FOURCC_YV12, webrtc::kVideoYV12 }, // 12 bpp, no conversion.
56 { FOURCC_NV12, webrtc::kVideoNV12 }, // 12 bpp, fast conversion.
57 { FOURCC_NV21, webrtc::kVideoNV21 }, // 12 bpp, fast conversion.
58 { FOURCC_YUY2, webrtc::kVideoYUY2 }, // 16 bpp, fast conversion.
59 { FOURCC_UYVY, webrtc::kVideoUYVY }, // 16 bpp, fast conversion.
60 { FOURCC_MJPG, webrtc::kVideoMJPEG }, // compressed, slow conversion.
61 { FOURCC_ARGB, webrtc::kVideoARGB }, // 32 bpp, slow conversion.
62 { FOURCC_24BG, webrtc::kVideoRGB24 }, // 24 bpp, slow conversion.
65 class WebRtcVcmFactory : public WebRtcVcmFactoryInterface {
67 virtual webrtc::VideoCaptureModule* Create(int id, const char* device) {
68 return webrtc::VideoCaptureFactory::Create(id, device);
70 virtual webrtc::VideoCaptureModule::DeviceInfo* CreateDeviceInfo(int id) {
71 return webrtc::VideoCaptureFactory::CreateDeviceInfo(id);
73 virtual void DestroyDeviceInfo(webrtc::VideoCaptureModule::DeviceInfo* info) {
78 static bool CapabilityToFormat(const webrtc::VideoCaptureCapability& cap,
79 VideoFormat* format) {
81 for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) {
82 if (kSupportedFourCCs[i].webrtc_type == cap.rawType) {
83 fourcc = kSupportedFourCCs[i].fourcc;
91 format->fourcc = fourcc;
92 format->width = cap.width;
93 format->height = cap.height;
94 format->interval = VideoFormat::FpsToInterval(cap.maxFPS);
98 static bool FormatToCapability(const VideoFormat& format,
99 webrtc::VideoCaptureCapability* cap) {
100 webrtc::RawVideoType webrtc_type = webrtc::kVideoUnknown;
101 for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) {
102 if (kSupportedFourCCs[i].fourcc == format.fourcc) {
103 webrtc_type = kSupportedFourCCs[i].webrtc_type;
107 if (webrtc_type == webrtc::kVideoUnknown) {
111 cap->width = format.width;
112 cap->height = format.height;
113 cap->maxFPS = VideoFormat::IntervalToFps(format.interval);
114 cap->expectedCaptureDelay = 0;
115 cap->rawType = webrtc_type;
116 cap->codecType = webrtc::kVideoCodecUnknown;
117 cap->interlaced = false;
121 ///////////////////////////////////////////////////////////////////////////
122 // Implementation of class WebRtcVideoCapturer
123 ///////////////////////////////////////////////////////////////////////////
125 WebRtcVideoCapturer::WebRtcVideoCapturer()
126 : factory_(new WebRtcVcmFactory),
128 captured_frames_(0) {
131 WebRtcVideoCapturer::WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory)
134 captured_frames_(0) {
137 WebRtcVideoCapturer::~WebRtcVideoCapturer() {
143 bool WebRtcVideoCapturer::Init(const Device& device) {
145 LOG(LS_ERROR) << "The capturer is already initialized";
149 webrtc::VideoCaptureModule::DeviceInfo* info = factory_->CreateDeviceInfo(0);
154 // Find the desired camera, by name.
155 // In the future, comparing IDs will be more robust.
156 // TODO(juberti): Figure what's needed to allow this.
157 int num_cams = info->NumberOfDevices();
158 char vcm_id[256] = "";
160 for (int index = 0; index < num_cams; ++index) {
162 if (info->GetDeviceName(index, vcm_name, ARRAY_SIZE(vcm_name),
163 vcm_id, ARRAY_SIZE(vcm_id)) != -1) {
164 if (device.name == reinterpret_cast<char*>(vcm_name)) {
171 LOG(LS_WARNING) << "Failed to find capturer for id: " << device.id;
172 factory_->DestroyDeviceInfo(info);
176 // Enumerate the supported formats.
177 // TODO(juberti): Find out why this starts/stops the camera...
178 std::vector<VideoFormat> supported;
179 int32_t num_caps = info->NumberOfCapabilities(vcm_id);
180 for (int32_t i = 0; i < num_caps; ++i) {
181 webrtc::VideoCaptureCapability cap;
182 if (info->GetCapability(vcm_id, i, cap) != -1) {
184 if (CapabilityToFormat(cap, &format)) {
185 supported.push_back(format);
187 LOG(LS_WARNING) << "Ignoring unsupported WebRTC capture format "
192 factory_->DestroyDeviceInfo(info);
193 if (supported.empty()) {
194 LOG(LS_ERROR) << "Failed to find usable formats for id: " << device.id;
198 module_ = factory_->Create(0, vcm_id);
200 LOG(LS_ERROR) << "Failed to create capturer for id: " << device.id;
204 // It is safe to change member attributes now.
207 SetSupportedFormats(supported);
211 bool WebRtcVideoCapturer::Init(webrtc::VideoCaptureModule* module) {
213 LOG(LS_ERROR) << "The capturer is already initialized";
217 LOG(LS_ERROR) << "Invalid VCM supplied";
220 // TODO(juberti): Set id and formats.
221 (module_ = module)->AddRef();
225 bool WebRtcVideoCapturer::GetBestCaptureFormat(const VideoFormat& desired,
226 VideoFormat* best_format) {
231 if (!VideoCapturer::GetBestCaptureFormat(desired, best_format)) {
232 // We maybe using a manually injected VCM which doesn't support enum.
233 // Use the desired format as the best format.
234 best_format->width = desired.width;
235 best_format->height = desired.height;
236 best_format->fourcc = FOURCC_I420;
237 best_format->interval = desired.interval;
238 LOG(LS_INFO) << "Failed to find best capture format,"
239 << " fall back to the requested format "
240 << best_format->ToString();
245 CaptureState WebRtcVideoCapturer::Start(const VideoFormat& capture_format) {
247 LOG(LS_ERROR) << "The capturer has not been initialized";
251 talk_base::CritScope cs(&critical_section_stopping_);
252 // TODO(hellner): weird to return failure when it is in fact actually running.
254 LOG(LS_ERROR) << "The capturer is already running";
258 SetCaptureFormat(&capture_format);
260 webrtc::VideoCaptureCapability cap;
261 if (!FormatToCapability(capture_format, &cap)) {
262 LOG(LS_ERROR) << "Invalid capture format specified";
266 std::string camera_id(GetId());
267 uint32 start = talk_base::Time();
268 #if defined(USE_WEBRTC_DEV_BRANCH)
269 module_->RegisterCaptureDataCallback(*this);
270 if (module_->StartCapture(cap) != 0) {
272 if (module_->RegisterCaptureDataCallback(*this) != 0 ||
273 module_->StartCapture(cap) != 0) {
275 LOG(LS_ERROR) << "Camera '" << camera_id << "' failed to start";
279 LOG(LS_INFO) << "Camera '" << camera_id << "' started with format "
280 << capture_format.ToString() << ", elapsed time "
281 << talk_base::TimeSince(start) << " ms";
283 captured_frames_ = 0;
284 SetCaptureState(CS_RUNNING);
288 // Critical section blocks Stop from shutting down during callbacks from capture
289 // thread to OnIncomingCapturedFrame. Note that the crit is try-locked in
290 // OnFrameCaptured, as the lock ordering between this and the system component
291 // controlling the camera is reversed: system frame -> OnIncomingCapturedFrame;
292 // Stop -> system stop camera).
293 void WebRtcVideoCapturer::Stop() {
294 talk_base::CritScope cs(&critical_section_stopping_);
296 talk_base::Thread::Current()->Clear(this);
297 module_->StopCapture();
298 module_->DeRegisterCaptureDataCallback();
300 // TODO(juberti): Determine if the VCM exposes any drop stats we can use.
301 double drop_ratio = 0.0;
302 std::string camera_id(GetId());
303 LOG(LS_INFO) << "Camera '" << camera_id << "' stopped after capturing "
304 << captured_frames_ << " frames and dropping "
305 << drop_ratio << "%";
307 SetCaptureFormat(NULL);
310 bool WebRtcVideoCapturer::IsRunning() {
311 return (module_ != NULL && module_->CaptureStarted());
314 bool WebRtcVideoCapturer::GetPreferredFourccs(
315 std::vector<uint32>* fourccs) {
321 for (size_t i = 0; i < ARRAY_SIZE(kSupportedFourCCs); ++i) {
322 fourccs->push_back(kSupportedFourCCs[i].fourcc);
327 void WebRtcVideoCapturer::OnIncomingCapturedFrame(const int32_t id,
328 webrtc::I420VideoFrame& sample) {
329 // This would be a normal CritScope, except that it's possible that:
330 // (1) whatever system component producing this frame has taken a lock, and
331 // (2) Stop() probably calls back into that system component, which may take
332 // the same lock. Due to the reversed order, we have to try-lock in order to
333 // avoid a potential deadlock. Besides, if we can't enter because we're
334 // stopping, we may as well drop the frame.
335 talk_base::TryCritScope cs(&critical_section_stopping_);
336 if (!cs.locked() || !IsRunning()) {
337 // Capturer has been stopped or is in the process of stopping.
342 // Log the size and pixel aspect ratio of the first captured frame.
343 if (1 == captured_frames_) {
344 LOG(LS_INFO) << "Captured frame size "
345 << sample.width() << "x" << sample.height()
346 << ". Expected format " << GetCaptureFormat()->ToString();
349 // Signal down stream components on captured frame.
350 // The CapturedFrame class doesn't support planes. We have to ExtractBuffer
351 // to one block for it.
352 int length = webrtc::CalcBufferSize(webrtc::kI420,
353 sample.width(), sample.height());
354 capture_buffer_.resize(length);
355 // TODO(ronghuawu): Refactor the WebRtcCapturedFrame to avoid memory copy.
356 webrtc::ExtractBuffer(sample, length, &capture_buffer_[0]);
357 WebRtcCapturedFrame frame(sample, &capture_buffer_[0], length);
358 SignalFrameCaptured(this, &frame);
361 void WebRtcVideoCapturer::OnCaptureDelayChanged(const int32_t id,
362 const int32_t delay) {
363 LOG(LS_INFO) << "Capture delay changed to " << delay << " ms";
366 // WebRtcCapturedFrame
367 WebRtcCapturedFrame::WebRtcCapturedFrame(const webrtc::I420VideoFrame& sample,
370 width = sample.width();
371 height = sample.height();
372 fourcc = FOURCC_I420;
373 // TODO(hellner): Support pixel aspect ratio (for OSX).
376 // Convert units from VideoFrame RenderTimeMs to CapturedFrame (nanoseconds).
377 elapsed_time = sample.render_time_ms() * talk_base::kNumNanosecsPerMillisec;
378 time_stamp = elapsed_time;
383 } // namespace cricket
385 #endif // HAVE_WEBRTC_VIDEO