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" | 17 #include "webrtc/modules/utility/include/process_thread.h" |
18 #include "webrtc/modules/video_capture/video_capture_factory.h" | 18 #include "webrtc/modules/video_capture/video_capture_factory.h" |
19 #include "webrtc/modules/video_processing/include/video_processing.h" | 19 #include "webrtc/modules/video_processing/include/video_processing.h" |
20 #include "webrtc/modules/video_render/video_render_defines.h" | 20 #include "webrtc/modules/video_render/video_render_defines.h" |
21 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
22 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 22 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
23 #include "webrtc/system_wrappers/include/tick_util.h" | 23 #include "webrtc/system_wrappers/include/tick_util.h" |
24 #include "webrtc/video/overuse_frame_detector.h" | 24 #include "webrtc/video/overuse_frame_detector.h" |
25 #include "webrtc/video/send_statistics_proxy.h" | 25 #include "webrtc/video/send_statistics_proxy.h" |
26 #include "webrtc/video/vie_encoder.h" | 26 #include "webrtc/video/video_send_stream.h" |
27 | 27 |
28 namespace webrtc { | 28 namespace webrtc { |
29 | 29 |
30 namespace internal { | 30 namespace internal { |
31 VideoCaptureInput::VideoCaptureInput( | 31 VideoCaptureInput::VideoCaptureInput( |
32 ProcessThread* module_process_thread, | 32 ProcessThread* module_process_thread, |
33 VideoCaptureCallback* frame_callback, | 33 VideoCaptureCallback* frame_callback, |
34 VideoRenderer* local_renderer, | 34 VideoCodingModule* vcm, |
35 VideoSendStream* video_send_stream, | |
36 const webrtc::VideoSendStream::Config& config, | |
35 SendStatisticsProxy* stats_proxy, | 37 SendStatisticsProxy* stats_proxy, |
36 CpuOveruseObserver* overuse_observer, | 38 CpuOveruseObserver* overuse_observer) |
37 EncodingTimeObserver* encoding_time_observer) | |
38 : capture_cs_(CriticalSectionWrapper::CreateCriticalSection()), | 39 : capture_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
39 module_process_thread_(module_process_thread), | 40 module_process_thread_(module_process_thread), |
40 frame_callback_(frame_callback), | 41 frame_callback_(frame_callback), |
41 local_renderer_(local_renderer), | 42 vcm_(vcm), |
43 video_send_stream_(video_send_stream), | |
44 config_(config), | |
45 encoder_registered_(false), | |
42 stats_proxy_(stats_proxy), | 46 stats_proxy_(stats_proxy), |
43 incoming_frame_cs_(CriticalSectionWrapper::CreateCriticalSection()), | |
44 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), | 47 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), |
45 capture_event_(false, false), | 48 capture_event_(false, false), |
46 stop_(0), | 49 stop_(0), |
47 last_captured_timestamp_(0), | 50 last_captured_timestamp_(0), |
48 delta_ntp_internal_ms_( | 51 delta_ntp_internal_ms_( |
49 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - | 52 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - |
50 TickTime::MillisecondTimestamp()), | 53 TickTime::MillisecondTimestamp()), |
51 overuse_detector_(new OveruseFrameDetector(Clock::GetRealTimeClock(), | 54 overuse_detector_(new OveruseFrameDetector(Clock::GetRealTimeClock(), |
52 CpuOveruseOptions(), | 55 CpuOveruseOptions(), |
53 overuse_observer, | 56 overuse_observer, |
54 stats_proxy)), | 57 stats_proxy)) { |
55 encoding_time_observer_(encoding_time_observer) { | 58 RTC_DCHECK(config_.encoder_settings.encoder != nullptr); |
59 RTC_DCHECK_GE(config_.encoder_settings.payload_type, 0); | |
60 RTC_DCHECK_LE(config_.encoder_settings.payload_type, 127); | |
56 encoder_thread_.Start(); | 61 encoder_thread_.Start(); |
57 encoder_thread_.SetPriority(rtc::kHighPriority); | 62 encoder_thread_.SetPriority(rtc::kHighPriority); |
58 module_process_thread_->RegisterModule(overuse_detector_.get()); | 63 module_process_thread_->RegisterModule(overuse_detector_.get()); |
59 } | 64 } |
60 | 65 |
61 VideoCaptureInput::~VideoCaptureInput() { | 66 VideoCaptureInput::~VideoCaptureInput() { |
62 module_process_thread_->DeRegisterModule(overuse_detector_.get()); | 67 module_process_thread_->DeRegisterModule(overuse_detector_.get()); |
63 | 68 |
64 // Stop the thread. | 69 // Stop the thread. |
65 rtc::AtomicOps::ReleaseStore(&stop_, 1); | 70 rtc::AtomicOps::ReleaseStore(&stop_, 1); |
66 capture_event_.Set(); | 71 capture_event_.Set(); |
67 encoder_thread_.Stop(); | 72 encoder_thread_.Stop(); |
68 } | 73 } |
69 | 74 |
70 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { | 75 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { |
71 // TODO(pbos): Remove local rendering, it should be handled by the client code | 76 // TODO(pbos): Remove local rendering, it should be handled by the client code |
72 // if required. | 77 // if required. |
73 if (local_renderer_) | 78 if (config_.local_renderer) |
74 local_renderer_->RenderFrame(video_frame, 0); | 79 config_.local_renderer->RenderFrame(video_frame, 0); |
75 | 80 |
76 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | 81 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
77 | 82 |
78 VideoFrame incoming_frame = video_frame; | 83 VideoFrame incoming_frame = video_frame; |
79 | 84 |
80 if (incoming_frame.ntp_time_ms() != 0) { | 85 if (incoming_frame.ntp_time_ms() != 0) { |
81 // If a NTP time stamp is set, this is the time stamp we will use. | 86 // If a NTP time stamp is set, this is the time stamp we will use. |
82 incoming_frame.set_render_time_ms(incoming_frame.ntp_time_ms() - | 87 incoming_frame.set_render_time_ms(incoming_frame.ntp_time_ms() - |
83 delta_ntp_internal_ms_); | 88 delta_ntp_internal_ms_); |
84 } else { // NTP time stamp not set. | 89 } else { // NTP time stamp not set. |
(...skipping 26 matching lines...) Expand all Loading... | |
111 overuse_detector_->FrameCaptured(captured_frame_.width(), | 116 overuse_detector_->FrameCaptured(captured_frame_.width(), |
112 captured_frame_.height(), | 117 captured_frame_.height(), |
113 captured_frame_.render_time_ms()); | 118 captured_frame_.render_time_ms()); |
114 | 119 |
115 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), | 120 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), |
116 "render_time", video_frame.render_time_ms()); | 121 "render_time", video_frame.render_time_ms()); |
117 | 122 |
118 capture_event_.Set(); | 123 capture_event_.Set(); |
119 } | 124 } |
120 | 125 |
126 void VideoCaptureInput::TriggerSetSendCodec(const VideoCodec& video_codec) { | |
127 CriticalSectionScoped cs(capture_cs_.get()); | |
128 codec_settings_ = video_codec; | |
129 replace_codec_settings_ = true; | |
130 capture_event_.Set(); | |
131 } | |
132 | |
121 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { | 133 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { |
122 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); | 134 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); |
123 } | 135 } |
124 | 136 |
125 bool VideoCaptureInput::EncoderProcess() { | 137 bool VideoCaptureInput::EncoderProcess() { |
126 static const int kThreadWaitTimeMs = 100; | 138 static const int kThreadWaitTimeMs = 100; |
127 int64_t capture_time = -1; | 139 int64_t capture_time = -1; |
128 if (capture_event_.Wait(kThreadWaitTimeMs)) { | 140 if (capture_event_.Wait(kThreadWaitTimeMs)) { |
129 if (rtc::AtomicOps::AcquireLoad(&stop_)) | 141 if (rtc::AtomicOps::AcquireLoad(&stop_)) { |
142 if (encoder_registered_) { | |
stefan-webrtc
2016/01/19 12:02:09
Maybe set this to false after the external encoder
pbos-webrtc
2016/01/19 15:27:58
We're only doing this on a shutdown, so we should
stefan-webrtc
2016/01/20 12:22:48
Right, I was mostly thinking if some other method
| |
143 vcm_->RegisterExternalEncoder(nullptr, | |
144 config_.encoder_settings.payload_type); | |
145 } | |
130 return false; | 146 return false; |
147 } | |
131 | 148 |
132 int64_t encode_start_time = -1; | 149 int64_t encode_start_time = -1; |
133 VideoFrame deliver_frame; | 150 VideoFrame deliver_frame; |
151 bool replace_codec_settings; | |
152 VideoCodec codec_settings; | |
134 { | 153 { |
135 CriticalSectionScoped cs(capture_cs_.get()); | 154 CriticalSectionScoped cs(capture_cs_.get()); |
136 if (!captured_frame_.IsZeroSize()) { | 155 replace_codec_settings = replace_codec_settings_; |
156 if (replace_codec_settings) { | |
157 codec_settings = codec_settings_; | |
158 replace_codec_settings_ = false; | |
159 } else if (encoder_registered_ && !captured_frame_.IsZeroSize()) { | |
160 // Don't pick up frames before the encoder has been configured or during | |
161 // reconfigures. This prevents picking up a frame, initializing the | |
162 // encoder (during which several frames may be dropped), encoding the | |
163 // quite old frame and then encoding new frames, leaving a very visible | |
164 // gap between the first and second frame encoded with the new settings. | |
stefan-webrtc
2016/01/19 12:02:09
I don't understand this last long sentence.
pbos-webrtc
2016/01/19 15:27:58
Hope this is better. :)
stefan-webrtc
2016/01/20 12:22:48
A lot better!
| |
137 deliver_frame = captured_frame_; | 165 deliver_frame = captured_frame_; |
138 captured_frame_.Reset(); | 166 captured_frame_.Reset(); |
139 } | 167 } |
140 } | 168 } |
169 if (replace_codec_settings) { | |
stefan-webrtc
2016/01/19 12:02:09
I don't see how this can ever be true?
pbos-webrtc
2016/01/19 15:27:58
Line 155 makes it true if TriggerSetSendCodec have
stefan-webrtc
2016/01/20 12:22:48
Ah, I misread line 158 to be "replace_codec_settin
pbos-webrtc
2016/01/20 19:04:44
Calling it new_codec_settings, they're still the n
| |
170 if (!encoder_registered_) { | |
171 RTC_CHECK_EQ(VCM_OK, vcm_->RegisterExternalEncoder( | |
172 config_.encoder_settings.encoder, | |
173 config_.encoder_settings.payload_type, | |
174 config_.encoder_settings.internal_source)); | |
175 } | |
176 // Optional only for tests that don't instantiate a full VideoSendStream. | |
stefan-webrtc
2016/01/19 12:02:09
Are you saying the following code is only executed
pbos-webrtc
2016/01/19 15:27:58
Not executed by tests. Comment updated.
| |
177 // TODO(pbos): Mock VideoSendStream. | |
stefan-webrtc
2016/01/19 12:02:09
I think this TODO should be more detailed. You sho
pbos-webrtc
2016/01/19 15:27:58
Updated TODO
stefan-webrtc
2016/01/20 12:22:48
Maybe it actually makes sense to do this now so th
pbos-webrtc
2016/01/20 19:04:45
All quality tests and otherwise will start failing
stefan-webrtc
2016/01/27 09:06:43
Ok, good, but if we think it's worth adding a todo
| |
178 if (video_send_stream_) | |
179 video_send_stream_->SetSendCodec(codec_settings); | |
stefan-webrtc
2016/01/19 12:02:09
Shouldn't this call directly into the vcm? Seems a
pbos-webrtc
2016/01/19 15:27:58
It needs to configure vie_channel, vie_channel and
stefan-webrtc
2016/01/20 12:22:48
I guess one of the vie_channel you mentioned shoul
pbos-webrtc
2016/01/20 19:04:45
I still think that VideoCaptureInput should be awa
stefan-webrtc
2016/01/27 09:06:43
Yes, that might be an option? I think I would like
| |
180 if (config_.suspend_below_min_bitrate) | |
stefan-webrtc
2016/01/19 12:02:09
Doesn't this mean that suspend below min bitrate i
pbos-webrtc
2016/01/19 15:27:58
Bad API name, it turns on the option.
stefan-webrtc
2016/01/20 12:22:48
Acknowledged.
| |
181 vcm_->SuspendBelowMinBitrate(); | |
182 encoder_registered_ = true; | |
183 // We could still have a frame to pick up here since none was picked up | |
184 // while the encoder was being registered/configured. | |
185 // Flag event again and wake up as soon as possible. | |
186 capture_event_.Set(); | |
187 return true; | |
188 } | |
141 if (!deliver_frame.IsZeroSize()) { | 189 if (!deliver_frame.IsZeroSize()) { |
142 capture_time = deliver_frame.render_time_ms(); | 190 capture_time = deliver_frame.render_time_ms(); |
143 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); | 191 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); |
144 frame_callback_->DeliverFrame(deliver_frame); | 192 frame_callback_->DeliverFrame(deliver_frame); |
145 } | 193 } |
146 // Update the overuse detector with the duration. | 194 // Update the overuse detector with the duration. |
147 if (encode_start_time != -1) { | 195 if (encode_start_time != -1) { |
148 int encode_time_ms = static_cast<int>( | 196 int encode_time_ms = static_cast<int>( |
149 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); | 197 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); |
150 stats_proxy_->OnEncodedFrame(encode_time_ms); | 198 stats_proxy_->OnEncodedFrame(encode_time_ms); |
151 if (encoding_time_observer_) { | 199 if (config_.encoding_time_observer) { |
152 encoding_time_observer_->OnReportEncodedTime( | 200 config_.encoding_time_observer->OnReportEncodedTime( |
153 deliver_frame.ntp_time_ms(), encode_time_ms); | 201 deliver_frame.ntp_time_ms(), encode_time_ms); |
154 } | 202 } |
155 } | 203 } |
156 } | 204 } |
157 // We're done! | 205 // We're done! |
158 if (capture_time != -1) { | 206 if (capture_time != -1) { |
159 overuse_detector_->FrameSent(capture_time); | 207 overuse_detector_->FrameSent(capture_time); |
160 } | 208 } |
161 return true; | 209 return true; |
162 } | 210 } |
163 | 211 |
164 } // namespace internal | 212 } // namespace internal |
165 } // namespace webrtc | 213 } // namespace webrtc |
OLD | NEW |