| 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 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; | 406 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; |
| 407 scale_ = false; | 407 scale_ = false; |
| 408 } | 408 } |
| 409 QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); | 409 QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); |
| 410 init_width = res.width; | 410 init_width = res.width; |
| 411 init_height = res.height; | 411 init_height = res.height; |
| 412 ALOGD << "Scaled resolution: " << init_width << " x " << init_height; | 412 ALOGD << "Scaled resolution: " << init_width << " x " << init_height; |
| 413 } | 413 } |
| 414 | 414 |
| 415 return codec_thread_->Invoke<int32_t>( | 415 return codec_thread_->Invoke<int32_t>( |
| 416 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, | 416 RTC_FROM_HERE, |
| 417 this, | 417 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, |
| 418 init_width, | 418 init_height, codec_settings->startBitrate, |
| 419 init_height, | 419 codec_settings->maxFramerate, false /* use_surface */)); |
| 420 codec_settings->startBitrate, | |
| 421 codec_settings->maxFramerate, | |
| 422 false /* use_surface */)); | |
| 423 } | 420 } |
| 424 | 421 |
| 425 int32_t MediaCodecVideoEncoder::Encode( | 422 int32_t MediaCodecVideoEncoder::Encode( |
| 426 const webrtc::VideoFrame& frame, | 423 const webrtc::VideoFrame& frame, |
| 427 const webrtc::CodecSpecificInfo* /* codec_specific_info */, | 424 const webrtc::CodecSpecificInfo* /* codec_specific_info */, |
| 428 const std::vector<webrtc::FrameType>* frame_types) { | 425 const std::vector<webrtc::FrameType>* frame_types) { |
| 429 return codec_thread_->Invoke<int32_t>(Bind( | 426 return codec_thread_->Invoke<int32_t>( |
| 430 &MediaCodecVideoEncoder::EncodeOnCodecThread, this, frame, frame_types)); | 427 RTC_FROM_HERE, Bind(&MediaCodecVideoEncoder::EncodeOnCodecThread, this, |
| 428 frame, frame_types)); |
| 431 } | 429 } |
| 432 | 430 |
| 433 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallback( | 431 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallback( |
| 434 webrtc::EncodedImageCallback* callback) { | 432 webrtc::EncodedImageCallback* callback) { |
| 435 return codec_thread_->Invoke<int32_t>( | 433 return codec_thread_->Invoke<int32_t>( |
| 434 RTC_FROM_HERE, |
| 436 Bind(&MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread, | 435 Bind(&MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread, |
| 437 this, | 436 this, callback)); |
| 438 callback)); | |
| 439 } | 437 } |
| 440 | 438 |
| 441 int32_t MediaCodecVideoEncoder::Release() { | 439 int32_t MediaCodecVideoEncoder::Release() { |
| 442 ALOGD << "EncoderRelease request"; | 440 ALOGD << "EncoderRelease request"; |
| 443 return codec_thread_->Invoke<int32_t>( | 441 return codec_thread_->Invoke<int32_t>( |
| 444 Bind(&MediaCodecVideoEncoder::ReleaseOnCodecThread, this)); | 442 RTC_FROM_HERE, Bind(&MediaCodecVideoEncoder::ReleaseOnCodecThread, this)); |
| 445 } | 443 } |
| 446 | 444 |
| 447 int32_t MediaCodecVideoEncoder::SetChannelParameters(uint32_t /* packet_loss */, | 445 int32_t MediaCodecVideoEncoder::SetChannelParameters(uint32_t /* packet_loss */, |
| 448 int64_t /* rtt */) { | 446 int64_t /* rtt */) { |
| 449 return WEBRTC_VIDEO_CODEC_OK; | 447 return WEBRTC_VIDEO_CODEC_OK; |
| 450 } | 448 } |
| 451 | 449 |
| 452 int32_t MediaCodecVideoEncoder::SetRates(uint32_t new_bit_rate, | 450 int32_t MediaCodecVideoEncoder::SetRates(uint32_t new_bit_rate, |
| 453 uint32_t frame_rate) { | 451 uint32_t frame_rate) { |
| 454 return codec_thread_->Invoke<int32_t>( | 452 return codec_thread_->Invoke<int32_t>( |
| 455 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, | 453 RTC_FROM_HERE, Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, this, |
| 456 this, | 454 new_bit_rate, frame_rate)); |
| 457 new_bit_rate, | |
| 458 frame_rate)); | |
| 459 } | 455 } |
| 460 | 456 |
| 461 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { | 457 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { |
| 462 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 458 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 463 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 459 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 464 ScopedLocalRefFrame local_ref_frame(jni); | 460 ScopedLocalRefFrame local_ref_frame(jni); |
| 465 | 461 |
| 466 // We only ever send one message to |this| directly (not through a Bind()'d | 462 // We only ever send one message to |this| directly (not through a Bind()'d |
| 467 // functor), so expect no ID/data. | 463 // functor), so expect no ID/data. |
| 468 RTC_CHECK(!msg->message_id) << "Unexpected message!"; | 464 RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
| 469 RTC_CHECK(!msg->pdata) << "Unexpected message!"; | 465 RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
| 470 if (!inited_) { | 466 if (!inited_) { |
| 471 return; | 467 return; |
| 472 } | 468 } |
| 473 | 469 |
| 474 // It would be nice to recover from a failure here if one happened, but it's | 470 // 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 | 471 // 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. | 472 // about it and let the next app-called API method reveal the borkedness. |
| 477 DeliverPendingOutputs(jni); | 473 DeliverPendingOutputs(jni); |
| 478 | 474 |
| 479 // If there aren't more frames to deliver, we can stop the loop | 475 // If there aren't more frames to deliver, we can stop the loop |
| 480 if (!input_frame_infos_.empty()) { | 476 if (!input_frame_infos_.empty()) { |
| 481 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 477 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); |
| 482 } else { | 478 } else { |
| 483 output_delivery_loop_running_ = false; | 479 output_delivery_loop_running_ = false; |
| 484 } | 480 } |
| 485 } | 481 } |
| 486 | 482 |
| 487 bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() { | 483 bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() { |
| 488 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 484 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 489 ALOGE << "ResetOnCodecThread"; | 485 ALOGE << "ResetOnCodecThread"; |
| 490 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || | 486 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || |
| 491 InitEncodeOnCodecThread(width_, height_, 0, 0, false) != | 487 InitEncodeOnCodecThread(width_, height_, 0, 0, false) != |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 time_before_calling_encode, input_frame.timestamp(), | 731 time_before_calling_encode, input_frame.timestamp(), |
| 736 input_frame.render_time_ms(), input_frame.rotation()); | 732 input_frame.render_time_ms(), input_frame.rotation()); |
| 737 | 733 |
| 738 last_input_timestamp_ms_ = | 734 last_input_timestamp_ms_ = |
| 739 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; | 735 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; |
| 740 | 736 |
| 741 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 737 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 742 | 738 |
| 743 if (!output_delivery_loop_running_) { | 739 if (!output_delivery_loop_running_) { |
| 744 output_delivery_loop_running_ = true; | 740 output_delivery_loop_running_ = true; |
| 745 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 741 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); |
| 746 } | 742 } |
| 747 | 743 |
| 748 if (!DeliverPendingOutputs(jni)) { | 744 if (!DeliverPendingOutputs(jni)) { |
| 749 ALOGE << "Failed deliver pending outputs."; | 745 ALOGE << "Failed deliver pending outputs."; |
| 750 ResetCodecOnCodecThread(); | 746 ResetCodecOnCodecThread(); |
| 751 return WEBRTC_VIDEO_CODEC_ERROR; | 747 return WEBRTC_VIDEO_CODEC_ERROR; |
| 752 } | 748 } |
| 753 return WEBRTC_VIDEO_CODEC_OK; | 749 return WEBRTC_VIDEO_CODEC_OK; |
| 754 } | 750 } |
| 755 | 751 |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1171 return (int32_t)(head - buffer); | 1167 return (int32_t)(head - buffer); |
| 1172 } | 1168 } |
| 1173 return -1; | 1169 return -1; |
| 1174 } | 1170 } |
| 1175 | 1171 |
| 1176 void MediaCodecVideoEncoder::OnDroppedFrame() { | 1172 void MediaCodecVideoEncoder::OnDroppedFrame() { |
| 1177 // Methods running on the codec thread should call OnDroppedFrameOnCodecThread | 1173 // Methods running on the codec thread should call OnDroppedFrameOnCodecThread |
| 1178 // directly. | 1174 // directly. |
| 1179 RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); | 1175 RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); |
| 1180 codec_thread_->Invoke<void>( | 1176 codec_thread_->Invoke<void>( |
| 1177 RTC_FROM_HERE, |
| 1181 Bind(&MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread, this)); | 1178 Bind(&MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread, this)); |
| 1182 } | 1179 } |
| 1183 | 1180 |
| 1184 void MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread() { | 1181 void MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread() { |
| 1185 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 1182 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 1186 // Report dropped frame to quality_scaler_. | 1183 // Report dropped frame to quality_scaler_. |
| 1187 if (scale_) | 1184 if (scale_) |
| 1188 quality_scaler_.ReportDroppedFrame(); | 1185 quality_scaler_.ReportDroppedFrame(); |
| 1189 } | 1186 } |
| 1190 | 1187 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1275 return supported_codecs_; | 1272 return supported_codecs_; |
| 1276 } | 1273 } |
| 1277 | 1274 |
| 1278 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1275 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
| 1279 webrtc::VideoEncoder* encoder) { | 1276 webrtc::VideoEncoder* encoder) { |
| 1280 ALOGD << "Destroy video encoder."; | 1277 ALOGD << "Destroy video encoder."; |
| 1281 delete encoder; | 1278 delete encoder; |
| 1282 } | 1279 } |
| 1283 | 1280 |
| 1284 } // namespace webrtc_jni | 1281 } // namespace webrtc_jni |
| OLD | NEW |