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 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 FrameWriter* decoded_frame_writer) | 128 FrameWriter* decoded_frame_writer) |
129 : encoder_(encoder), | 129 : encoder_(encoder), |
130 decoder_(decoder), | 130 decoder_(decoder), |
131 bitrate_allocator_(CreateBitrateAllocator(config)), | 131 bitrate_allocator_(CreateBitrateAllocator(config)), |
132 encode_callback_(new VideoProcessorEncodeCompleteCallback(this)), | 132 encode_callback_(new VideoProcessorEncodeCompleteCallback(this)), |
133 decode_callback_(new VideoProcessorDecodeCompleteCallback(this)), | 133 decode_callback_(new VideoProcessorDecodeCompleteCallback(this)), |
134 packet_manipulator_(packet_manipulator), | 134 packet_manipulator_(packet_manipulator), |
135 config_(config), | 135 config_(config), |
136 analysis_frame_reader_(analysis_frame_reader), | 136 analysis_frame_reader_(analysis_frame_reader), |
137 analysis_frame_writer_(analysis_frame_writer), | 137 analysis_frame_writer_(analysis_frame_writer), |
138 num_frames_(analysis_frame_reader->NumberOfFrames()), | |
139 source_frame_writer_(source_frame_writer), | 138 source_frame_writer_(source_frame_writer), |
140 encoded_frame_writer_(encoded_frame_writer), | 139 encoded_frame_writer_(encoded_frame_writer), |
141 decoded_frame_writer_(decoded_frame_writer), | 140 decoded_frame_writer_(decoded_frame_writer), |
142 bit_rate_factor_(config.codec_settings->maxFramerate * 0.001 * 8), | |
143 initialized_(false), | 141 initialized_(false), |
144 last_encoded_frame_num_(-1), | 142 last_encoded_frame_num_(-1), |
145 last_decoded_frame_num_(-1), | 143 last_decoded_frame_num_(-1), |
146 first_key_frame_has_been_excluded_(false), | 144 first_key_frame_has_been_excluded_(false), |
147 last_decoded_frame_buffer_(0, analysis_frame_reader->FrameLength()), | 145 last_decoded_frame_buffer_(0, analysis_frame_reader->FrameLength()), |
148 stats_(stats), | 146 stats_(stats), |
149 num_dropped_frames_(0), | 147 num_dropped_frames_(0), |
150 num_spatial_resizes_(0) { | 148 num_spatial_resizes_(0) { |
151 RTC_DCHECK(encoder); | 149 RTC_DCHECK(encoder); |
152 RTC_DCHECK(decoder); | 150 RTC_DCHECK(decoder); |
153 RTC_DCHECK(packet_manipulator); | 151 RTC_DCHECK(packet_manipulator); |
154 RTC_DCHECK(analysis_frame_reader); | 152 RTC_DCHECK(analysis_frame_reader); |
155 RTC_DCHECK(analysis_frame_writer); | 153 RTC_DCHECK(analysis_frame_writer); |
156 RTC_DCHECK(stats); | 154 RTC_DCHECK(stats); |
157 | 155 frame_infos_.reserve(analysis_frame_reader->NumberOfFrames()); |
158 frame_infos_.reserve(num_frames_); | |
159 } | 156 } |
160 | 157 |
161 bool VideoProcessorImpl::Init() { | 158 void VideoProcessorImpl::Init() { |
162 RTC_DCHECK(!initialized_) | 159 RTC_DCHECK(!initialized_) << "VideoProcessor already initialized."; |
163 << "This VideoProcessor has already been initialized."; | 160 initialized_ = true; |
164 | 161 |
165 // Setup required callbacks for the encoder/decoder. | 162 // Setup required callbacks for the encoder/decoder. |
166 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(encode_callback_.get()), | 163 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(encode_callback_.get()), |
167 WEBRTC_VIDEO_CODEC_OK) | 164 WEBRTC_VIDEO_CODEC_OK) |
168 << "Failed to register encode complete callback"; | 165 << "Failed to register encode complete callback"; |
169 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(decode_callback_.get()), | 166 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(decode_callback_.get()), |
170 WEBRTC_VIDEO_CODEC_OK) | 167 WEBRTC_VIDEO_CODEC_OK) |
171 << "Failed to register decode complete callback"; | 168 << "Failed to register decode complete callback"; |
172 | 169 |
173 // Initialize the encoder and decoder. | 170 // Initialize the encoder and decoder. |
174 uint32_t num_cores = | 171 uint32_t num_cores = |
175 config_.use_single_core ? 1 : CpuInfo::DetectNumberOfCores(); | 172 config_.use_single_core ? 1 : CpuInfo::DetectNumberOfCores(); |
176 RTC_CHECK_EQ( | 173 RTC_CHECK_EQ( |
177 encoder_->InitEncode(config_.codec_settings, num_cores, | 174 encoder_->InitEncode(config_.codec_settings, num_cores, |
178 config_.networking_config.max_payload_size_in_bytes), | 175 config_.networking_config.max_payload_size_in_bytes), |
179 WEBRTC_VIDEO_CODEC_OK) | 176 WEBRTC_VIDEO_CODEC_OK) |
180 << "Failed to initialize VideoEncoder"; | 177 << "Failed to initialize VideoEncoder"; |
181 | 178 |
182 RTC_CHECK_EQ(decoder_->InitDecode(config_.codec_settings, num_cores), | 179 RTC_CHECK_EQ(decoder_->InitDecode(config_.codec_settings, num_cores), |
183 WEBRTC_VIDEO_CODEC_OK) | 180 WEBRTC_VIDEO_CODEC_OK) |
184 << "Failed to initialize VideoDecoder"; | 181 << "Failed to initialize VideoDecoder"; |
185 | 182 |
186 if (config_.verbose) { | 183 if (config_.verbose) { |
187 printf("Video Processor:\n"); | 184 printf("Video Processor:\n"); |
188 printf(" #CPU cores used : %d\n", num_cores); | 185 printf(" #CPU cores used : %d\n", num_cores); |
189 printf(" Total # of frames: %d\n", num_frames_); | 186 printf(" Total # of frames: %d\n", |
| 187 analysis_frame_reader_->NumberOfFrames()); |
190 printf(" Codec settings:\n"); | 188 printf(" Codec settings:\n"); |
191 printf(" Encoder implementation name: %s\n", | 189 printf(" Encoder implementation name: %s\n", |
192 encoder_->ImplementationName()); | 190 encoder_->ImplementationName()); |
193 printf(" Decoder implementation name: %s\n", | 191 printf(" Decoder implementation name: %s\n", |
194 decoder_->ImplementationName()); | 192 decoder_->ImplementationName()); |
195 if (strcmp(encoder_->ImplementationName(), | 193 if (strcmp(encoder_->ImplementationName(), |
196 decoder_->ImplementationName()) == 0) { | 194 decoder_->ImplementationName()) == 0) { |
197 printf(" Codec implementation name: %s_%s\n", | 195 printf(" Codec implementation name: %s_%s\n", |
198 CodecTypeToPayloadName(config_.codec_settings->codecType) | 196 CodecTypeToPayloadName(config_.codec_settings->codecType) |
199 .value_or("Unknown"), | 197 .value_or("Unknown"), |
200 encoder_->ImplementationName()); | 198 encoder_->ImplementationName()); |
201 } | 199 } |
202 PrintCodecSettings(config_.codec_settings); | 200 PrintCodecSettings(config_.codec_settings); |
203 } | 201 } |
204 | |
205 initialized_ = true; | |
206 | |
207 return true; | |
208 } | 202 } |
209 | 203 |
210 VideoProcessorImpl::~VideoProcessorImpl() { | 204 VideoProcessorImpl::~VideoProcessorImpl() { |
211 encoder_->RegisterEncodeCompleteCallback(nullptr); | 205 encoder_->RegisterEncodeCompleteCallback(nullptr); |
212 decoder_->RegisterDecodeCompleteCallback(nullptr); | 206 decoder_->RegisterDecodeCompleteCallback(nullptr); |
213 } | 207 } |
214 | 208 |
215 void VideoProcessorImpl::SetRates(int bit_rate, int frame_rate) { | 209 void VideoProcessorImpl::SetRates(int bit_rate, int frame_rate) { |
216 int set_rates_result = encoder_->SetRateAllocation( | 210 int set_rates_result = encoder_->SetRateAllocation( |
217 bitrate_allocator_->GetAllocation(bit_rate * 1000, frame_rate), | 211 bitrate_allocator_->GetAllocation(bit_rate * 1000, frame_rate), |
(...skipping 29 matching lines...) Expand all Loading... |
247 } | 241 } |
248 | 242 |
249 int VideoProcessorImpl::NumberSpatialResizes() { | 243 int VideoProcessorImpl::NumberSpatialResizes() { |
250 return num_spatial_resizes_; | 244 return num_spatial_resizes_; |
251 } | 245 } |
252 | 246 |
253 bool VideoProcessorImpl::ProcessFrame(int frame_number) { | 247 bool VideoProcessorImpl::ProcessFrame(int frame_number) { |
254 RTC_DCHECK_GE(frame_number, 0); | 248 RTC_DCHECK_GE(frame_number, 0); |
255 RTC_DCHECK_LE(frame_number, frame_infos_.size()) | 249 RTC_DCHECK_LE(frame_number, frame_infos_.size()) |
256 << "Must process frames without gaps."; | 250 << "Must process frames without gaps."; |
257 RTC_DCHECK(initialized_) << "Attempting to use uninitialized VideoProcessor"; | 251 RTC_DCHECK(initialized_) << "VideoProcessor not initialized."; |
258 | 252 |
259 rtc::scoped_refptr<I420BufferInterface> buffer( | 253 rtc::scoped_refptr<I420BufferInterface> buffer( |
260 analysis_frame_reader_->ReadFrame()); | 254 analysis_frame_reader_->ReadFrame()); |
261 | 255 |
262 if (!buffer) { | 256 if (!buffer) { |
263 // Last frame has been reached. | 257 // Last frame has been reached. |
264 return false; | 258 return false; |
265 } | 259 } |
266 | 260 |
267 if (source_frame_writer_) { | 261 if (source_frame_writer_) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 &frame_info->qp_bitstream); | 360 &frame_info->qp_bitstream); |
367 } | 361 } |
368 FrameStatistic* frame_stat = &stats_->stats_[frame_number]; | 362 FrameStatistic* frame_stat = &stats_->stats_[frame_number]; |
369 frame_stat->encode_time_in_us = | 363 frame_stat->encode_time_in_us = |
370 GetElapsedTimeMicroseconds(frame_info->encode_start_ns, encode_stop_ns); | 364 GetElapsedTimeMicroseconds(frame_info->encode_start_ns, encode_stop_ns); |
371 frame_stat->encoding_successful = true; | 365 frame_stat->encoding_successful = true; |
372 frame_stat->encoded_frame_length_in_bytes = encoded_image._length; | 366 frame_stat->encoded_frame_length_in_bytes = encoded_image._length; |
373 frame_stat->frame_number = frame_number; | 367 frame_stat->frame_number = frame_number; |
374 frame_stat->frame_type = encoded_image._frameType; | 368 frame_stat->frame_type = encoded_image._frameType; |
375 frame_stat->qp = encoded_image.qp_; | 369 frame_stat->qp = encoded_image.qp_; |
376 frame_stat->bit_rate_in_kbps = encoded_image._length * bit_rate_factor_; | 370 frame_stat->bit_rate_in_kbps = static_cast<int>( |
| 371 encoded_image._length * config_.codec_settings->maxFramerate * 8 / 1000); |
377 frame_stat->total_packets = | 372 frame_stat->total_packets = |
378 encoded_image._length / config_.networking_config.packet_size_in_bytes + | 373 encoded_image._length / config_.networking_config.packet_size_in_bytes + |
379 1; | 374 1; |
380 | 375 |
381 // Simulate packet loss. | 376 // Simulate packet loss. |
382 bool exclude_this_frame = false; | 377 bool exclude_this_frame = false; |
383 if (encoded_image._frameType == kVideoFrameKey) { | 378 if (encoded_image._frameType == kVideoFrameKey) { |
384 // Only keyframes can be excluded. | 379 // Only keyframes can be excluded. |
385 switch (config_.exclude_frame_types) { | 380 switch (config_.exclude_frame_types) { |
386 case kExcludeOnlyFirstKeyFrame: | 381 case kExcludeOnlyFirstKeyFrame: |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 if (decoded_frame_writer_) { | 499 if (decoded_frame_writer_) { |
505 RTC_DCHECK_EQ(extracted_length, decoded_frame_writer_->FrameLength()); | 500 RTC_DCHECK_EQ(extracted_length, decoded_frame_writer_->FrameLength()); |
506 RTC_CHECK(decoded_frame_writer_->WriteFrame(extracted_buffer.data())); | 501 RTC_CHECK(decoded_frame_writer_->WriteFrame(extracted_buffer.data())); |
507 } | 502 } |
508 | 503 |
509 last_decoded_frame_buffer_ = std::move(extracted_buffer); | 504 last_decoded_frame_buffer_ = std::move(extracted_buffer); |
510 } | 505 } |
511 | 506 |
512 } // namespace test | 507 } // namespace test |
513 } // namespace webrtc | 508 } // namespace webrtc |
OLD | NEW |