OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 int frames_dropped_media_encoder_; // Number of frames dropped by encoder. | 219 int frames_dropped_media_encoder_; // Number of frames dropped by encoder. |
220 // Number of dropped frames caused by full queue. | 220 // Number of dropped frames caused by full queue. |
221 int consecutive_full_queue_frame_drops_; | 221 int consecutive_full_queue_frame_drops_; |
222 int64_t stat_start_time_ms_; // Start time for statistics. | 222 int64_t stat_start_time_ms_; // Start time for statistics. |
223 int current_frames_; // Number of frames in the current statistics interval. | 223 int current_frames_; // Number of frames in the current statistics interval. |
224 int current_bytes_; // Encoded bytes in the current statistics interval. | 224 int current_bytes_; // Encoded bytes in the current statistics interval. |
225 int current_acc_qp_; // Accumulated QP in the current statistics interval. | 225 int current_acc_qp_; // Accumulated QP in the current statistics interval. |
226 int current_encoding_time_ms_; // Overall encoding time in the current second | 226 int current_encoding_time_ms_; // Overall encoding time in the current second |
227 int64_t last_input_timestamp_ms_; // Timestamp of last received yuv frame. | 227 int64_t last_input_timestamp_ms_; // Timestamp of last received yuv frame. |
228 int64_t last_output_timestamp_ms_; // Timestamp of last encoded frame. | 228 int64_t last_output_timestamp_ms_; // Timestamp of last encoded frame. |
229 bool output_delivery_loop_running_; // Is the onMessage loop running | 229 // Is the onMessage loop running at regular polling rate. If false, it is |
230 // running at lower poll rate of kMediaCodecStatisticsIntervalMs. | |
231 bool output_delivery_loop_running_; | |
230 | 232 |
231 struct InputFrameInfo { | 233 struct InputFrameInfo { |
232 InputFrameInfo(int64_t encode_start_time, | 234 InputFrameInfo(int64_t encode_start_time, |
233 int32_t frame_timestamp, | 235 int32_t frame_timestamp, |
234 int64_t frame_render_time_ms, | 236 int64_t frame_render_time_ms, |
235 webrtc::VideoRotation rotation) | 237 webrtc::VideoRotation rotation) |
236 : encode_start_time(encode_start_time), | 238 : encode_start_time(encode_start_time), |
237 frame_timestamp(frame_timestamp), | 239 frame_timestamp(frame_timestamp), |
238 frame_render_time_ms(frame_render_time_ms), | 240 frame_render_time_ms(frame_render_time_ms), |
239 rotation(rotation) {} | 241 rotation(rotation) {} |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 j_media_codec_video_encoder_( | 302 j_media_codec_video_encoder_( |
301 jni, | 303 jni, |
302 jni->NewObject(*j_media_codec_video_encoder_class_, | 304 jni->NewObject(*j_media_codec_video_encoder_class_, |
303 GetMethodID(jni, | 305 GetMethodID(jni, |
304 *j_media_codec_video_encoder_class_, | 306 *j_media_codec_video_encoder_class_, |
305 "<init>", | 307 "<init>", |
306 "()V"))), | 308 "()V"))), |
307 inited_(false), | 309 inited_(false), |
308 use_surface_(false), | 310 use_surface_(false), |
309 picture_id_(0), | 311 picture_id_(0), |
310 output_delivery_loop_running_(false), | |
311 egl_context_(egl_context) { | 312 egl_context_(egl_context) { |
312 ScopedLocalRefFrame local_ref_frame(jni); | 313 ScopedLocalRefFrame local_ref_frame(jni); |
313 // It would be nice to avoid spinning up a new thread per MediaCodec, and | 314 // It would be nice to avoid spinning up a new thread per MediaCodec, and |
314 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug | 315 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug |
315 // 2732 means that deadlocks abound. This class synchronously trampolines | 316 // 2732 means that deadlocks abound. This class synchronously trampolines |
316 // to |codec_thread_|, so if anything else can be coming to _us_ from | 317 // to |codec_thread_|, so if anything else can be coming to _us_ from |
317 // |codec_thread_|, or from any thread holding the |_sendCritSect| described | 318 // |codec_thread_|, or from any thread holding the |_sendCritSect| described |
318 // in the bug, we have a problem. For now work around that with a dedicated | 319 // in the bug, we have a problem. For now work around that with a dedicated |
319 // thread. | 320 // thread. |
320 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); | 321 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 } | 473 } |
473 | 474 |
474 // It would be nice to recover from a failure here if one happened, but it's | 475 // It would be nice to recover from a failure here if one happened, but it's |
475 // unclear how to signal such a failure to the app, so instead we stay silent | 476 // unclear how to signal such a failure to the app, so instead we stay silent |
476 // about it and let the next app-called API method reveal the borkedness. | 477 // about it and let the next app-called API method reveal the borkedness. |
477 DeliverPendingOutputs(jni); | 478 DeliverPendingOutputs(jni); |
478 | 479 |
479 // If there aren't more frames to deliver, we can stop the loop | 480 // If there aren't more frames to deliver, we can stop the loop |
480 if (!input_frame_infos_.empty()) { | 481 if (!input_frame_infos_.empty()) { |
481 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 482 codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
483 output_delivery_loop_running_ = true; | |
482 } else { | 484 } else { |
485 codec_thread_->PostDelayed(kMediaCodecStatisticsIntervalMs, this); | |
AlexG
2016/06/10 20:16:25
I think this stopping or increasing polling interv
sakal
2016/06/13 07:13:21
Done.
| |
483 output_delivery_loop_running_ = false; | 486 output_delivery_loop_running_ = false; |
484 } | 487 } |
488 | |
489 // Call log statistics here so it's called even if no frames are being | |
490 // delivered. | |
491 LogStatistics(false); | |
485 } | 492 } |
486 | 493 |
487 bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() { | 494 bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() { |
488 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 495 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
489 ALOGE << "ResetOnCodecThread"; | 496 ALOGE << "ResetOnCodecThread"; |
490 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || | 497 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || |
491 InitEncodeOnCodecThread(width_, height_, 0, 0, false) != | 498 InitEncodeOnCodecThread(width_, height_, 0, 0, false) != |
492 WEBRTC_VIDEO_CODEC_OK) { | 499 WEBRTC_VIDEO_CODEC_OK) { |
493 // TODO(fischman): wouldn't it be nice if there was a way to gracefully | 500 // TODO(fischman): wouldn't it be nice if there was a way to gracefully |
494 // degrade to a SW encoder at this point? There isn't one AFAICT :( | 501 // degrade to a SW encoder at this point? There isn't one AFAICT :( |
(...skipping 30 matching lines...) Expand all Loading... | |
525 frames_dropped_media_encoder_ = 0; | 532 frames_dropped_media_encoder_ = 0; |
526 consecutive_full_queue_frame_drops_ = 0; | 533 consecutive_full_queue_frame_drops_ = 0; |
527 current_timestamp_us_ = 0; | 534 current_timestamp_us_ = 0; |
528 stat_start_time_ms_ = rtc::TimeMillis(); | 535 stat_start_time_ms_ = rtc::TimeMillis(); |
529 current_frames_ = 0; | 536 current_frames_ = 0; |
530 current_bytes_ = 0; | 537 current_bytes_ = 0; |
531 current_acc_qp_ = 0; | 538 current_acc_qp_ = 0; |
532 current_encoding_time_ms_ = 0; | 539 current_encoding_time_ms_ = 0; |
533 last_input_timestamp_ms_ = -1; | 540 last_input_timestamp_ms_ = -1; |
534 last_output_timestamp_ms_ = -1; | 541 last_output_timestamp_ms_ = -1; |
542 output_delivery_loop_running_ = false; | |
535 output_timestamp_ = 0; | 543 output_timestamp_ = 0; |
536 output_render_time_ms_ = 0; | 544 output_render_time_ms_ = 0; |
537 input_frame_infos_.clear(); | 545 input_frame_infos_.clear(); |
538 drop_next_input_frame_ = false; | 546 drop_next_input_frame_ = false; |
539 use_surface_ = use_surface; | 547 use_surface_ = use_surface; |
540 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; | 548 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; |
541 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); | 549 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); |
542 tl0_pic_idx_ = static_cast<uint8_t>(rand()); | 550 tl0_pic_idx_ = static_cast<uint8_t>(rand()); |
543 gof_idx_ = 0; | 551 gof_idx_ = 0; |
544 last_frame_received_ms_ = -1; | 552 last_frame_received_ms_ = -1; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
588 input_buffers_[i] = | 596 input_buffers_[i] = |
589 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); | 597 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); |
590 int64_t yuv_buffer_capacity = | 598 int64_t yuv_buffer_capacity = |
591 jni->GetDirectBufferCapacity(input_buffers_[i]); | 599 jni->GetDirectBufferCapacity(input_buffers_[i]); |
592 CHECK_EXCEPTION(jni); | 600 CHECK_EXCEPTION(jni); |
593 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; | 601 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; |
594 } | 602 } |
595 } | 603 } |
596 | 604 |
597 inited_ = true; | 605 inited_ = true; |
606 codec_thread_->PostDelayed(kMediaCodecStatisticsIntervalMs, this); | |
AlexG
2016/06/10 20:16:25
This is not needed - first 10 frames are logged, s
sakal
2016/06/13 07:13:21
Done.
| |
598 return WEBRTC_VIDEO_CODEC_OK; | 607 return WEBRTC_VIDEO_CODEC_OK; |
599 } | 608 } |
600 | 609 |
601 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( | 610 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( |
602 const webrtc::VideoFrame& frame, | 611 const webrtc::VideoFrame& frame, |
603 const std::vector<webrtc::FrameType>* frame_types) { | 612 const std::vector<webrtc::FrameType>* frame_types) { |
604 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 613 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
605 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 614 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
606 ScopedLocalRefFrame local_ref_frame(jni); | 615 ScopedLocalRefFrame local_ref_frame(jni); |
607 | 616 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 // Save input image timestamps for later output. | 742 // Save input image timestamps for later output. |
734 input_frame_infos_.emplace_back( | 743 input_frame_infos_.emplace_back( |
735 time_before_calling_encode, input_frame.timestamp(), | 744 time_before_calling_encode, input_frame.timestamp(), |
736 input_frame.render_time_ms(), input_frame.rotation()); | 745 input_frame.render_time_ms(), input_frame.rotation()); |
737 | 746 |
738 last_input_timestamp_ms_ = | 747 last_input_timestamp_ms_ = |
739 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; | 748 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; |
740 | 749 |
741 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 750 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
742 | 751 |
743 if (!output_delivery_loop_running_) { | 752 if (!output_delivery_loop_running_) { |
AlexG
2016/06/10 20:16:25
It is safer and less code to just remove this if c
sakal
2016/06/13 07:13:21
Done.
| |
753 codec_thread_->Clear(this); | |
754 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | |
744 output_delivery_loop_running_ = true; | 755 output_delivery_loop_running_ = true; |
745 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | |
746 } | 756 } |
747 | 757 |
748 if (!DeliverPendingOutputs(jni)) { | 758 if (!DeliverPendingOutputs(jni)) { |
749 ALOGE << "Failed deliver pending outputs."; | 759 ALOGE << "Failed deliver pending outputs."; |
750 ResetCodecOnCodecThread(); | 760 ResetCodecOnCodecThread(); |
751 return WEBRTC_VIDEO_CODEC_ERROR; | 761 return WEBRTC_VIDEO_CODEC_ERROR; |
752 } | 762 } |
753 return WEBRTC_VIDEO_CODEC_OK; | 763 return WEBRTC_VIDEO_CODEC_OK; |
754 } | 764 } |
755 | 765 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
919 | 929 |
920 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( | 930 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( |
921 JNIEnv* jni, | 931 JNIEnv* jni, |
922 jobject j_output_buffer_info) { | 932 jobject j_output_buffer_info) { |
923 return GetLongField( | 933 return GetLongField( |
924 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); | 934 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); |
925 } | 935 } |
926 | 936 |
927 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { | 937 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { |
928 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 938 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
939 | |
929 while (true) { | 940 while (true) { |
930 jobject j_output_buffer_info = jni->CallObjectMethod( | 941 jobject j_output_buffer_info = jni->CallObjectMethod( |
931 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); | 942 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); |
932 CHECK_EXCEPTION(jni); | 943 CHECK_EXCEPTION(jni); |
933 if (IsNull(jni, j_output_buffer_info)) { | 944 if (IsNull(jni, j_output_buffer_info)) { |
934 break; | 945 break; |
935 } | 946 } |
936 | 947 |
937 int output_buffer_index = | 948 int output_buffer_index = |
938 GetOutputBufferInfoIndex(jni, j_output_buffer_info); | 949 GetOutputBufferInfoIndex(jni, j_output_buffer_info); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1114 drop_next_input_frame_ = true; | 1125 drop_next_input_frame_ = true; |
1115 // Theoretically could handle callback_status<0 here, but unclear what | 1126 // Theoretically could handle callback_status<0 here, but unclear what |
1116 // that would mean for us. | 1127 // that would mean for us. |
1117 } | 1128 } |
1118 } | 1129 } |
1119 return true; | 1130 return true; |
1120 } | 1131 } |
1121 | 1132 |
1122 void MediaCodecVideoEncoder::LogStatistics(bool force_log) { | 1133 void MediaCodecVideoEncoder::LogStatistics(bool force_log) { |
1123 int statistic_time_ms = rtc::TimeMillis() - stat_start_time_ms_; | 1134 int statistic_time_ms = rtc::TimeMillis() - stat_start_time_ms_; |
1124 if ((statistic_time_ms >= kMediaCodecStatisticsIntervalMs || force_log) && | 1135 if ((statistic_time_ms >= kMediaCodecStatisticsIntervalMs || force_log) |
1125 current_frames_ > 0 && statistic_time_ms > 0) { | 1136 && statistic_time_ms > 0) { |
1137 // Prevent division by zero | |
1138 int current_frames_divider = current_frames_ != 0 ? current_frames_ : 1; | |
1139 | |
1126 int current_bitrate = current_bytes_ * 8 / statistic_time_ms; | 1140 int current_bitrate = current_bytes_ * 8 / statistic_time_ms; |
1127 int current_fps = | 1141 int current_fps = |
1128 (current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms; | 1142 (current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms; |
1129 ALOGD << "Encoded frames: " << frames_encoded_ << | 1143 ALOGD << "Encoded frames: " << frames_encoded_ << |
1130 ". Bitrate: " << current_bitrate << | 1144 ". Bitrate: " << current_bitrate << |
1131 ", target: " << last_set_bitrate_kbps_ << " kbps" << | 1145 ", target: " << last_set_bitrate_kbps_ << " kbps" << |
1132 ", fps: " << current_fps << | 1146 ", fps: " << current_fps << |
1133 ", encTime: " << (current_encoding_time_ms_ / current_frames_) << | 1147 ", encTime: " << (current_encoding_time_ms_ / current_frames_divider) << |
1134 ". QP: " << (current_acc_qp_ / current_frames_) << | 1148 ". QP: " << (current_acc_qp_ / current_frames_divider) << |
1135 " for last " << statistic_time_ms << " ms."; | 1149 " for last " << statistic_time_ms << " ms."; |
1136 stat_start_time_ms_ = rtc::TimeMillis(); | 1150 stat_start_time_ms_ = rtc::TimeMillis(); |
1137 current_frames_ = 0; | 1151 current_frames_ = 0; |
1138 current_bytes_ = 0; | 1152 current_bytes_ = 0; |
1139 current_acc_qp_ = 0; | 1153 current_acc_qp_ = 0; |
1140 current_encoding_time_ms_ = 0; | 1154 current_encoding_time_ms_ = 0; |
1141 } | 1155 } |
1142 } | 1156 } |
1143 | 1157 |
1144 int32_t MediaCodecVideoEncoder::NextNaluPosition( | 1158 int32_t MediaCodecVideoEncoder::NextNaluPosition( |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1275 return supported_codecs_; | 1289 return supported_codecs_; |
1276 } | 1290 } |
1277 | 1291 |
1278 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1292 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
1279 webrtc::VideoEncoder* encoder) { | 1293 webrtc::VideoEncoder* encoder) { |
1280 ALOGD << "Destroy video encoder."; | 1294 ALOGD << "Destroy video encoder."; |
1281 delete encoder; | 1295 delete encoder; |
1282 } | 1296 } |
1283 | 1297 |
1284 } // namespace webrtc_jni | 1298 } // namespace webrtc_jni |
OLD | NEW |