OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/video/video_capture_input.h" | 11 #include "webrtc/video/video_capture_input.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/base/trace_event.h" | 15 #include "webrtc/base/trace_event.h" |
16 #include "webrtc/modules/include/module_common_types.h" | 16 #include "webrtc/modules/include/module_common_types.h" |
17 #include "webrtc/modules/utility/include/process_thread.h" | |
18 #include "webrtc/modules/video_capture/video_capture_factory.h" | 17 #include "webrtc/modules/video_capture/video_capture_factory.h" |
19 #include "webrtc/modules/video_processing/include/video_processing.h" | 18 #include "webrtc/modules/video_processing/include/video_processing.h" |
20 #include "webrtc/modules/video_render/video_render_defines.h" | 19 #include "webrtc/modules/video_render/video_render_defines.h" |
21 #include "webrtc/system_wrappers/include/clock.h" | 20 #include "webrtc/system_wrappers/include/clock.h" |
22 #include "webrtc/system_wrappers/include/tick_util.h" | 21 #include "webrtc/system_wrappers/include/tick_util.h" |
23 #include "webrtc/video/overuse_frame_detector.h" | 22 #include "webrtc/video/overuse_frame_detector.h" |
24 #include "webrtc/video/send_statistics_proxy.h" | 23 #include "webrtc/video/send_statistics_proxy.h" |
25 #include "webrtc/video/vie_encoder.h" | 24 #include "webrtc/video/vie_encoder.h" |
26 | 25 |
27 namespace webrtc { | 26 namespace webrtc { |
28 | 27 |
29 namespace internal { | 28 namespace internal { |
30 VideoCaptureInput::VideoCaptureInput( | 29 VideoCaptureInput::VideoCaptureInput(VideoCaptureCallback* frame_callback, |
31 ProcessThread* module_process_thread, | 30 VideoRenderer* local_renderer, |
32 VideoCaptureCallback* frame_callback, | 31 SendStatisticsProxy* stats_proxy, |
33 VideoRenderer* local_renderer, | 32 OveruseFrameDetector* overuse_detector) |
34 SendStatisticsProxy* stats_proxy, | 33 : frame_callback_(frame_callback), |
35 CpuOveruseObserver* overuse_observer, | |
36 EncodingTimeObserver* encoding_time_observer) | |
37 : module_process_thread_(module_process_thread), | |
38 frame_callback_(frame_callback), | |
39 local_renderer_(local_renderer), | 34 local_renderer_(local_renderer), |
40 stats_proxy_(stats_proxy), | 35 stats_proxy_(stats_proxy), |
41 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), | 36 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), |
42 capture_event_(false, false), | 37 capture_event_(false, false), |
43 stop_(0), | 38 stop_(0), |
44 last_captured_timestamp_(0), | 39 last_captured_timestamp_(0), |
45 delta_ntp_internal_ms_( | 40 delta_ntp_internal_ms_( |
46 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - | 41 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - |
47 TickTime::MillisecondTimestamp()), | 42 TickTime::MillisecondTimestamp()), |
48 overuse_detector_(new OveruseFrameDetector(Clock::GetRealTimeClock(), | 43 overuse_detector_(overuse_detector) { |
49 CpuOveruseOptions(), | |
50 overuse_observer, | |
51 stats_proxy)), | |
52 encoding_time_observer_(encoding_time_observer) { | |
53 encoder_thread_.Start(); | 44 encoder_thread_.Start(); |
54 encoder_thread_.SetPriority(rtc::kHighPriority); | 45 encoder_thread_.SetPriority(rtc::kHighPriority); |
55 module_process_thread_->RegisterModule(overuse_detector_.get()); | |
56 } | 46 } |
57 | 47 |
58 VideoCaptureInput::~VideoCaptureInput() { | 48 VideoCaptureInput::~VideoCaptureInput() { |
59 module_process_thread_->DeRegisterModule(overuse_detector_.get()); | |
60 | |
61 // Stop the thread. | 49 // Stop the thread. |
62 rtc::AtomicOps::ReleaseStore(&stop_, 1); | 50 rtc::AtomicOps::ReleaseStore(&stop_, 1); |
63 capture_event_.Set(); | 51 capture_event_.Set(); |
64 encoder_thread_.Stop(); | 52 encoder_thread_.Stop(); |
65 } | 53 } |
66 | 54 |
67 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { | 55 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { |
68 // TODO(pbos): Remove local rendering, it should be handled by the client code | 56 // TODO(pbos): Remove local rendering, it should be handled by the client code |
69 // if required. | 57 // if required. |
70 if (local_renderer_) | 58 if (local_renderer_) |
(...skipping 27 matching lines...) Expand all Loading... |
98 LOG(LS_WARNING) << "Same/old NTP timestamp (" | 86 LOG(LS_WARNING) << "Same/old NTP timestamp (" |
99 << incoming_frame.ntp_time_ms() | 87 << incoming_frame.ntp_time_ms() |
100 << " <= " << last_captured_timestamp_ | 88 << " <= " << last_captured_timestamp_ |
101 << ") for incoming frame. Dropping."; | 89 << ") for incoming frame. Dropping."; |
102 return; | 90 return; |
103 } | 91 } |
104 | 92 |
105 captured_frame_.ShallowCopy(incoming_frame); | 93 captured_frame_.ShallowCopy(incoming_frame); |
106 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); | 94 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); |
107 | 95 |
108 overuse_detector_->FrameCaptured(captured_frame_.width(), | 96 overuse_detector_->FrameCaptured(captured_frame_); |
109 captured_frame_.height(), | |
110 captured_frame_.render_time_ms()); | |
111 | 97 |
112 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), | 98 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), |
113 "render_time", video_frame.render_time_ms()); | 99 "render_time", video_frame.render_time_ms()); |
114 | 100 |
115 capture_event_.Set(); | 101 capture_event_.Set(); |
116 } | 102 } |
117 | 103 |
118 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { | 104 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { |
119 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); | 105 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); |
120 } | 106 } |
121 | 107 |
122 bool VideoCaptureInput::EncoderProcess() { | 108 bool VideoCaptureInput::EncoderProcess() { |
123 static const int kThreadWaitTimeMs = 100; | 109 static const int kThreadWaitTimeMs = 100; |
124 int64_t capture_time = -1; | |
125 if (capture_event_.Wait(kThreadWaitTimeMs)) { | 110 if (capture_event_.Wait(kThreadWaitTimeMs)) { |
126 if (rtc::AtomicOps::AcquireLoad(&stop_)) | 111 if (rtc::AtomicOps::AcquireLoad(&stop_)) |
127 return false; | 112 return false; |
128 | 113 |
129 int64_t encode_start_time = -1; | |
130 VideoFrame deliver_frame; | 114 VideoFrame deliver_frame; |
131 { | 115 { |
132 rtc::CritScope lock(&crit_); | 116 rtc::CritScope lock(&crit_); |
133 if (!captured_frame_.IsZeroSize()) { | 117 if (!captured_frame_.IsZeroSize()) { |
134 deliver_frame = captured_frame_; | 118 deliver_frame = captured_frame_; |
135 captured_frame_.Reset(); | 119 captured_frame_.Reset(); |
136 } | 120 } |
137 } | 121 } |
138 if (!deliver_frame.IsZeroSize()) { | 122 if (!deliver_frame.IsZeroSize()) { |
139 capture_time = deliver_frame.render_time_ms(); | |
140 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); | |
141 frame_callback_->DeliverFrame(deliver_frame); | 123 frame_callback_->DeliverFrame(deliver_frame); |
142 } | 124 } |
143 // Update the overuse detector with the duration. | |
144 if (encode_start_time != -1) { | |
145 int encode_time_ms = static_cast<int>( | |
146 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); | |
147 stats_proxy_->OnEncodedFrame(encode_time_ms); | |
148 if (encoding_time_observer_) { | |
149 encoding_time_observer_->OnReportEncodedTime( | |
150 deliver_frame.ntp_time_ms(), encode_time_ms); | |
151 } | |
152 } | |
153 } | |
154 // We're done! | |
155 if (capture_time != -1) { | |
156 overuse_detector_->FrameSent(capture_time); | |
157 } | 125 } |
158 return true; | 126 return true; |
159 } | 127 } |
160 | 128 |
161 } // namespace internal | 129 } // namespace internal |
162 } // namespace webrtc | 130 } // namespace webrtc |
OLD | NEW |