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 replacement_codec_settings_ = rtc::Optional<VideoCodec>(video_codec); |
| 129 capture_event_.Set(); |
| 130 } |
| 131 |
121 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { | 132 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { |
122 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); | 133 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); |
123 } | 134 } |
124 | 135 |
125 bool VideoCaptureInput::EncoderProcess() { | 136 bool VideoCaptureInput::EncoderProcess() { |
126 static const int kThreadWaitTimeMs = 100; | 137 static const int kThreadWaitTimeMs = 100; |
127 int64_t capture_time = -1; | 138 int64_t capture_time = -1; |
128 if (capture_event_.Wait(kThreadWaitTimeMs)) { | 139 if (capture_event_.Wait(kThreadWaitTimeMs)) { |
129 if (rtc::AtomicOps::AcquireLoad(&stop_)) | 140 if (rtc::AtomicOps::AcquireLoad(&stop_)) { |
| 141 if (encoder_registered_) { |
| 142 vcm_->RegisterExternalEncoder(nullptr, |
| 143 config_.encoder_settings.payload_type); |
| 144 } |
130 return false; | 145 return false; |
| 146 } |
131 | 147 |
132 int64_t encode_start_time = -1; | 148 int64_t encode_start_time = -1; |
133 VideoFrame deliver_frame; | 149 VideoFrame deliver_frame; |
| 150 rtc::Optional<VideoCodec> new_codec_settings; |
134 { | 151 { |
135 CriticalSectionScoped cs(capture_cs_.get()); | 152 CriticalSectionScoped cs(capture_cs_.get()); |
136 if (!captured_frame_.IsZeroSize()) { | 153 if (replacement_codec_settings_) { |
| 154 new_codec_settings = replacement_codec_settings_; |
| 155 replacement_codec_settings_ = rtc::Optional<VideoCodec>(); |
| 156 } else if (encoder_registered_ && !captured_frame_.IsZeroSize()) { |
| 157 // Only pick up frames when the encoder is registered. This prevents |
| 158 // encoding old frames from before the encoder was initialized. |
137 deliver_frame = captured_frame_; | 159 deliver_frame = captured_frame_; |
138 captured_frame_.Reset(); | 160 captured_frame_.Reset(); |
139 } | 161 } |
140 } | 162 } |
| 163 if (new_codec_settings) { |
| 164 if (!encoder_registered_) { |
| 165 RTC_CHECK_EQ(VCM_OK, vcm_->RegisterExternalEncoder( |
| 166 config_.encoder_settings.encoder, |
| 167 config_.encoder_settings.payload_type, |
| 168 config_.encoder_settings.internal_source)); |
| 169 } |
| 170 // TODO(pbos): Mock VideoSendStream and use from tests so |
| 171 // video_send_stream_ can always be non-null. |
| 172 if (video_send_stream_) |
| 173 video_send_stream_->SetSendCodec(*new_codec_settings); |
| 174 if (config_.suspend_below_min_bitrate) |
| 175 vcm_->SuspendBelowMinBitrate(); |
| 176 encoder_registered_ = true; |
| 177 // We could still have a frame to pick up here since none was picked up |
| 178 // while the encoder was being registered/configured. |
| 179 // Flag event again and wake up as soon as possible. |
| 180 capture_event_.Set(); |
| 181 return true; |
| 182 } |
141 if (!deliver_frame.IsZeroSize()) { | 183 if (!deliver_frame.IsZeroSize()) { |
142 capture_time = deliver_frame.render_time_ms(); | 184 capture_time = deliver_frame.render_time_ms(); |
143 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); | 185 encode_start_time = Clock::GetRealTimeClock()->TimeInMilliseconds(); |
144 frame_callback_->DeliverFrame(deliver_frame); | 186 frame_callback_->DeliverFrame(deliver_frame); |
145 } | 187 } |
146 // Update the overuse detector with the duration. | 188 // Update the overuse detector with the duration. |
147 if (encode_start_time != -1) { | 189 if (encode_start_time != -1) { |
148 int encode_time_ms = static_cast<int>( | 190 int encode_time_ms = static_cast<int>( |
149 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); | 191 Clock::GetRealTimeClock()->TimeInMilliseconds() - encode_start_time); |
150 stats_proxy_->OnEncodedFrame(encode_time_ms); | 192 stats_proxy_->OnEncodedFrame(encode_time_ms); |
151 if (encoding_time_observer_) { | 193 if (config_.encoding_time_observer) { |
152 encoding_time_observer_->OnReportEncodedTime( | 194 config_.encoding_time_observer->OnReportEncodedTime( |
153 deliver_frame.ntp_time_ms(), encode_time_ms); | 195 deliver_frame.ntp_time_ms(), encode_time_ms); |
154 } | 196 } |
155 } | 197 } |
156 } | 198 } |
157 // We're done! | 199 // We're done! |
158 if (capture_time != -1) { | 200 if (capture_time != -1) { |
159 overuse_detector_->FrameSent(capture_time); | 201 overuse_detector_->FrameSent(capture_time); |
160 } | 202 } |
161 return true; | 203 return true; |
162 } | 204 } |
163 | 205 |
164 } // namespace internal | 206 } // namespace internal |
165 } // namespace webrtc | 207 } // namespace webrtc |
OLD | NEW |