OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/video/video_capture_input.h" |
| 12 |
| 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/base/trace_event.h" |
| 16 #include "webrtc/modules/include/module_common_types.h" |
| 17 #include "webrtc/modules/video_capture/video_capture_factory.h" |
| 18 #include "webrtc/modules/video_processing/include/video_processing.h" |
| 19 #include "webrtc/video/overuse_frame_detector.h" |
| 20 #include "webrtc/video/send_statistics_proxy.h" |
| 21 #include "webrtc/video/vie_encoder.h" |
| 22 |
| 23 namespace webrtc { |
| 24 |
| 25 namespace internal { |
| 26 VideoCaptureInput::VideoCaptureInput( |
| 27 rtc::Event* capture_event, |
| 28 rtc::VideoSinkInterface<VideoFrame>* local_renderer, |
| 29 SendStatisticsProxy* stats_proxy, |
| 30 OveruseFrameDetector* overuse_detector) |
| 31 : local_renderer_(local_renderer), |
| 32 stats_proxy_(stats_proxy), |
| 33 capture_event_(capture_event), |
| 34 // TODO(danilchap): Pass clock from outside to ensure it is same clock |
| 35 // rtcp module use to calculate offset since last frame captured |
| 36 // to estimate rtp timestamp for SenderReport. |
| 37 clock_(Clock::GetRealTimeClock()), |
| 38 last_captured_timestamp_(0), |
| 39 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
| 40 clock_->TimeInMilliseconds()), |
| 41 overuse_detector_(overuse_detector) {} |
| 42 |
| 43 VideoCaptureInput::~VideoCaptureInput() { |
| 44 } |
| 45 |
| 46 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { |
| 47 // TODO(pbos): Remove local rendering, it should be handled by the client code |
| 48 // if required. |
| 49 if (local_renderer_) |
| 50 local_renderer_->OnFrame(video_frame); |
| 51 |
| 52 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
| 53 |
| 54 VideoFrame incoming_frame = video_frame; |
| 55 |
| 56 // Local time in webrtc time base. |
| 57 int64_t current_time = clock_->TimeInMilliseconds(); |
| 58 incoming_frame.set_render_time_ms(current_time); |
| 59 |
| 60 // Capture time may come from clock with an offset and drift from clock_. |
| 61 int64_t capture_ntp_time_ms; |
| 62 if (video_frame.ntp_time_ms() != 0) { |
| 63 capture_ntp_time_ms = video_frame.ntp_time_ms(); |
| 64 } else if (video_frame.render_time_ms() != 0) { |
| 65 capture_ntp_time_ms = video_frame.render_time_ms() + delta_ntp_internal_ms_; |
| 66 } else { |
| 67 capture_ntp_time_ms = current_time + delta_ntp_internal_ms_; |
| 68 } |
| 69 incoming_frame.set_ntp_time_ms(capture_ntp_time_ms); |
| 70 |
| 71 // Convert NTP time, in ms, to RTP timestamp. |
| 72 const int kMsToRtpTimestamp = 90; |
| 73 incoming_frame.set_timestamp( |
| 74 kMsToRtpTimestamp * static_cast<uint32_t>(incoming_frame.ntp_time_ms())); |
| 75 |
| 76 rtc::CritScope lock(&crit_); |
| 77 if (incoming_frame.ntp_time_ms() <= last_captured_timestamp_) { |
| 78 // We don't allow the same capture time for two frames, drop this one. |
| 79 LOG(LS_WARNING) << "Same/old NTP timestamp (" |
| 80 << incoming_frame.ntp_time_ms() |
| 81 << " <= " << last_captured_timestamp_ |
| 82 << ") for incoming frame. Dropping."; |
| 83 return; |
| 84 } |
| 85 |
| 86 captured_frame_.reset(new VideoFrame); |
| 87 captured_frame_->ShallowCopy(incoming_frame); |
| 88 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); |
| 89 |
| 90 overuse_detector_->FrameCaptured(*captured_frame_); |
| 91 |
| 92 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), |
| 93 "render_time", video_frame.render_time_ms()); |
| 94 |
| 95 capture_event_->Set(); |
| 96 } |
| 97 |
| 98 bool VideoCaptureInput::GetVideoFrame(VideoFrame* video_frame) { |
| 99 rtc::CritScope lock(&crit_); |
| 100 if (!captured_frame_) |
| 101 return false; |
| 102 |
| 103 *video_frame = *captured_frame_; |
| 104 captured_frame_.reset(); |
| 105 return true; |
| 106 } |
| 107 |
| 108 } // namespace internal |
| 109 } // namespace webrtc |
OLD | NEW |