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/critical_section_wrapper.h" | 21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
23 #include "webrtc/system_wrappers/include/tick_util.h" | 22 #include "webrtc/system_wrappers/include/tick_util.h" |
24 #include "webrtc/video/overuse_frame_detector.h" | 23 #include "webrtc/video/overuse_frame_detector.h" |
25 #include "webrtc/video/send_statistics_proxy.h" | 24 #include "webrtc/video/send_statistics_proxy.h" |
26 #include "webrtc/video/vie_encoder.h" | 25 #include "webrtc/video/vie_encoder.h" |
27 | 26 |
28 namespace webrtc { | 27 namespace webrtc { |
29 | 28 |
30 namespace internal { | 29 namespace internal { |
31 VideoCaptureInput::VideoCaptureInput( | 30 VideoCaptureInput::VideoCaptureInput( |
32 ProcessThread* module_process_thread, | |
33 VideoCaptureCallback* frame_callback, | 31 VideoCaptureCallback* frame_callback, |
34 VideoRenderer* local_renderer, | 32 VideoRenderer* local_renderer, |
35 SendStatisticsProxy* stats_proxy, | 33 SendStatisticsProxy* stats_proxy, |
36 CpuOveruseObserver* overuse_observer, | 34 OveruseFrameDetector* overuse_detector, |
37 EncodingTimeObserver* encoding_time_observer) | 35 EncodingTimeObserver* encoding_time_observer) |
38 : capture_cs_(CriticalSectionWrapper::CreateCriticalSection()), | 36 : capture_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
39 module_process_thread_(module_process_thread), | |
40 frame_callback_(frame_callback), | 37 frame_callback_(frame_callback), |
41 local_renderer_(local_renderer), | 38 local_renderer_(local_renderer), |
42 stats_proxy_(stats_proxy), | 39 stats_proxy_(stats_proxy), |
43 incoming_frame_cs_(CriticalSectionWrapper::CreateCriticalSection()), | 40 incoming_frame_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
44 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), | 41 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), |
45 capture_event_(false, false), | 42 capture_event_(false, false), |
46 stop_(0), | 43 stop_(0), |
47 last_captured_timestamp_(0), | 44 last_captured_timestamp_(0), |
48 delta_ntp_internal_ms_( | 45 delta_ntp_internal_ms_( |
49 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - | 46 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - |
50 TickTime::MillisecondTimestamp()), | 47 TickTime::MillisecondTimestamp()), |
51 overuse_detector_(new OveruseFrameDetector(Clock::GetRealTimeClock(), | 48 overuse_detector_(overuse_detector), |
52 CpuOveruseOptions(), | |
53 overuse_observer, | |
54 stats_proxy)), | |
55 encoding_time_observer_(encoding_time_observer) { | 49 encoding_time_observer_(encoding_time_observer) { |
56 encoder_thread_.Start(); | 50 encoder_thread_.Start(); |
57 encoder_thread_.SetPriority(rtc::kHighPriority); | 51 encoder_thread_.SetPriority(rtc::kHighPriority); |
58 module_process_thread_->RegisterModule(overuse_detector_.get()); | |
59 } | 52 } |
60 | 53 |
61 VideoCaptureInput::~VideoCaptureInput() { | 54 VideoCaptureInput::~VideoCaptureInput() { |
62 module_process_thread_->DeRegisterModule(overuse_detector_.get()); | |
63 | |
64 // Stop the thread. | 55 // Stop the thread. |
65 rtc::AtomicOps::ReleaseStore(&stop_, 1); | 56 rtc::AtomicOps::ReleaseStore(&stop_, 1); |
66 capture_event_.Set(); | 57 capture_event_.Set(); |
67 encoder_thread_.Stop(); | 58 encoder_thread_.Stop(); |
68 } | 59 } |
69 | 60 |
70 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { | 61 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { |
71 // TODO(pbos): Remove local rendering, it should be handled by the client code | 62 // TODO(pbos): Remove local rendering, it should be handled by the client code |
72 // if required. | 63 // if required. |
73 if (local_renderer_) | 64 if (local_renderer_) |
(...skipping 27 matching lines...) Expand all Loading... | |
101 LOG(LS_WARNING) << "Same/old NTP timestamp (" | 92 LOG(LS_WARNING) << "Same/old NTP timestamp (" |
102 << incoming_frame.ntp_time_ms() | 93 << incoming_frame.ntp_time_ms() |
103 << " <= " << last_captured_timestamp_ | 94 << " <= " << last_captured_timestamp_ |
104 << ") for incoming frame. Dropping."; | 95 << ") for incoming frame. Dropping."; |
105 return; | 96 return; |
106 } | 97 } |
107 | 98 |
108 captured_frame_.ShallowCopy(incoming_frame); | 99 captured_frame_.ShallowCopy(incoming_frame); |
109 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); | 100 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); |
110 | 101 |
111 overuse_detector_->FrameCaptured(captured_frame_.width(), | 102 overuse_detector_->FrameCaptured(captured_frame_); |
112 captured_frame_.height(), | |
113 captured_frame_.render_time_ms()); | |
114 | 103 |
115 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), | 104 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), |
116 "render_time", video_frame.render_time_ms()); | 105 "render_time", video_frame.render_time_ms()); |
117 | 106 |
118 capture_event_.Set(); | 107 capture_event_.Set(); |
119 } | 108 } |
120 | 109 |
121 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { | 110 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { |
122 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); | 111 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); |
123 } | 112 } |
124 | 113 |
125 bool VideoCaptureInput::EncoderProcess() { | 114 bool VideoCaptureInput::EncoderProcess() { |
126 static const int kThreadWaitTimeMs = 100; | 115 static const int kThreadWaitTimeMs = 100; |
127 int64_t capture_time = -1; | |
128 if (capture_event_.Wait(kThreadWaitTimeMs)) { | 116 if (capture_event_.Wait(kThreadWaitTimeMs)) { |
129 if (rtc::AtomicOps::AcquireLoad(&stop_)) | 117 if (rtc::AtomicOps::AcquireLoad(&stop_)) |
130 return false; | 118 return false; |
131 | 119 |
132 int64_t encode_start_time = -1; | 120 int64_t encode_start_time = -1; |
133 VideoFrame deliver_frame; | 121 VideoFrame deliver_frame; |
134 { | 122 { |
135 CriticalSectionScoped cs(capture_cs_.get()); | 123 CriticalSectionScoped cs(capture_cs_.get()); |
136 if (!captured_frame_.IsZeroSize()) { | 124 if (!captured_frame_.IsZeroSize()) { |
137 deliver_frame = captured_frame_; | 125 deliver_frame = captured_frame_; |
138 captured_frame_.Reset(); | 126 captured_frame_.Reset(); |
139 } | 127 } |
140 } | 128 } |
141 if (!deliver_frame.IsZeroSize()) { | 129 if (!deliver_frame.IsZeroSize()) { |
142 capture_time = deliver_frame.render_time_ms(); | |
143 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); | 130 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); |
144 frame_callback_->DeliverFrame(deliver_frame); | 131 frame_callback_->DeliverFrame(deliver_frame); |
145 } | 132 } |
146 // Update the overuse detector with the duration. | 133 // Update the overuse detector with the duration. |
147 if (encode_start_time != -1) { | 134 if (encode_start_time != -1) { |
148 int encode_time_ms = static_cast<int>( | 135 int encode_time_ms = static_cast<int>( |
149 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); | 136 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); |
150 stats_proxy_->OnEncodedFrame(encode_time_ms); | 137 // TODO(pbos): Move encoding_time_observer into OveruseFrameDetector. |
mflodman
2016/01/21 08:00:05
I think this CL is touching a lot of files already
pbos-webrtc
2016/01/21 14:18:33
Yep, done. This has the downside that times are no
| |
151 if (encoding_time_observer_) { | 138 if (encoding_time_observer_) { |
152 encoding_time_observer_->OnReportEncodedTime( | 139 encoding_time_observer_->OnReportEncodedTime( |
153 deliver_frame.ntp_time_ms(), encode_time_ms); | 140 deliver_frame.ntp_time_ms(), encode_time_ms); |
154 } | 141 } |
155 } | 142 } |
156 } | 143 } |
157 // We're done! | |
158 if (capture_time != -1) { | |
159 overuse_detector_->FrameSent(capture_time); | |
160 } | |
161 return true; | 144 return true; |
162 } | 145 } |
163 | 146 |
164 } // namespace internal | 147 } // namespace internal |
165 } // namespace webrtc | 148 } // namespace webrtc |
OLD | NEW |