OLD | NEW |
---|---|
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 15 matching lines...) Expand all Loading... | |
26 * | 26 * |
27 */ | 27 */ |
28 | 28 |
29 #include "talk/app/webrtc/java/jni/androidmediaencoder_jni.h" | 29 #include "talk/app/webrtc/java/jni/androidmediaencoder_jni.h" |
30 #include "talk/app/webrtc/java/jni/classreferenceholder.h" | 30 #include "talk/app/webrtc/java/jni/classreferenceholder.h" |
31 #include "talk/app/webrtc/java/jni/androidmediacodeccommon.h" | 31 #include "talk/app/webrtc/java/jni/androidmediacodeccommon.h" |
32 #include "webrtc/base/bind.h" | 32 #include "webrtc/base/bind.h" |
33 #include "webrtc/base/checks.h" | 33 #include "webrtc/base/checks.h" |
34 #include "webrtc/base/logging.h" | 34 #include "webrtc/base/logging.h" |
35 #include "webrtc/base/thread.h" | 35 #include "webrtc/base/thread.h" |
36 #include "webrtc/base/thread_checker.h" | |
36 #include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h" | 37 #include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h" |
37 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" | 38 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" |
38 #include "webrtc/modules/video_coding/utility/include/quality_scaler.h" | 39 #include "webrtc/modules/video_coding/utility/include/quality_scaler.h" |
39 #include "webrtc/modules/video_coding/utility/include/vp8_header_parser.h" | 40 #include "webrtc/modules/video_coding/utility/include/vp8_header_parser.h" |
40 #include "webrtc/system_wrappers/interface/field_trial.h" | 41 #include "webrtc/system_wrappers/interface/field_trial.h" |
41 #include "webrtc/system_wrappers/interface/logcat_trace_context.h" | 42 #include "webrtc/system_wrappers/interface/logcat_trace_context.h" |
42 #include "third_party/libyuv/include/libyuv/convert.h" | 43 #include "third_party/libyuv/include/libyuv/convert.h" |
43 #include "third_party/libyuv/include/libyuv/convert_from.h" | 44 #include "third_party/libyuv/include/libyuv/convert_from.h" |
44 #include "third_party/libyuv/include/libyuv/video_common.h" | 45 #include "third_party/libyuv/include/libyuv/video_common.h" |
45 | 46 |
(...skipping 26 matching lines...) Expand all Loading... | |
72 // MediaCodecVideoEncoder is a webrtc::VideoEncoder implementation that uses | 73 // MediaCodecVideoEncoder is a webrtc::VideoEncoder implementation that uses |
73 // Android's MediaCodec SDK API behind the scenes to implement (hopefully) | 74 // Android's MediaCodec SDK API behind the scenes to implement (hopefully) |
74 // HW-backed video encode. This C++ class is implemented as a very thin shim, | 75 // HW-backed video encode. This C++ class is implemented as a very thin shim, |
75 // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder. | 76 // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder. |
76 // MediaCodecVideoEncoder is created, operated, and destroyed on a single | 77 // MediaCodecVideoEncoder is created, operated, and destroyed on a single |
77 // thread, currently the libjingle Worker thread. | 78 // thread, currently the libjingle Worker thread. |
78 class MediaCodecVideoEncoder : public webrtc::VideoEncoder, | 79 class MediaCodecVideoEncoder : public webrtc::VideoEncoder, |
79 public rtc::MessageHandler { | 80 public rtc::MessageHandler { |
80 public: | 81 public: |
81 virtual ~MediaCodecVideoEncoder(); | 82 virtual ~MediaCodecVideoEncoder(); |
82 explicit MediaCodecVideoEncoder(JNIEnv* jni, VideoCodecType codecType); | 83 MediaCodecVideoEncoder(JNIEnv* jni, |
84 VideoCodecType codecType); | |
83 | 85 |
84 // webrtc::VideoEncoder implementation. Everything trampolines to | 86 // webrtc::VideoEncoder implementation. Everything trampolines to |
85 // |codec_thread_| for execution. | 87 // |codec_thread_| for execution. |
86 int32_t InitEncode(const webrtc::VideoCodec* codec_settings, | 88 int32_t InitEncode(const webrtc::VideoCodec* codec_settings, |
87 int32_t /* number_of_cores */, | 89 int32_t /* number_of_cores */, |
88 size_t /* max_payload_size */) override; | 90 size_t /* max_payload_size */) override; |
89 int32_t Encode( | 91 int32_t Encode( |
90 const webrtc::VideoFrame& input_image, | 92 const webrtc::VideoFrame& input_image, |
91 const webrtc::CodecSpecificInfo* /* codec_specific_info */, | 93 const webrtc::CodecSpecificInfo* /* codec_specific_info */, |
92 const std::vector<webrtc::VideoFrameType>* frame_types) override; | 94 const std::vector<webrtc::VideoFrameType>* frame_types) override; |
93 int32_t RegisterEncodeCompleteCallback( | 95 int32_t RegisterEncodeCompleteCallback( |
94 webrtc::EncodedImageCallback* callback) override; | 96 webrtc::EncodedImageCallback* callback) override; |
95 int32_t Release() override; | 97 int32_t Release() override; |
96 int32_t SetChannelParameters(uint32_t /* packet_loss */, | 98 int32_t SetChannelParameters(uint32_t /* packet_loss */, |
97 int64_t /* rtt */) override; | 99 int64_t /* rtt */) override; |
98 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override; | 100 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override; |
99 | 101 |
100 // rtc::MessageHandler implementation. | 102 // rtc::MessageHandler implementation. |
101 void OnMessage(rtc::Message* msg) override; | 103 void OnMessage(rtc::Message* msg) override; |
102 | 104 |
103 void OnDroppedFrame() override; | 105 void OnDroppedFrame() override; |
104 | 106 |
105 int GetTargetFramerate() override; | 107 int GetTargetFramerate() override; |
106 | 108 |
107 private: | 109 private: |
108 // CHECK-fail if not running on |codec_thread_|. | 110 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and |
109 void CheckOnCodecThread(); | 111 // InitEncodeOnCodecThread() in an attempt to restore the codec to an |
110 | |
111 // Release() and InitEncode() in an attempt to restore the codec to an | |
112 // operable state. Necessary after all manner of OMX-layer errors. | 112 // operable state. Necessary after all manner of OMX-layer errors. |
113 void ResetCodec(); | 113 void ResetCodecOnCodecThread(); |
114 | 114 |
115 // Implementation of webrtc::VideoEncoder methods above, all running on the | 115 // Implementation of webrtc::VideoEncoder methods above, all running on the |
116 // codec thread exclusively. | 116 // codec thread exclusively. |
117 // | 117 // |
118 // If width==0 then this is assumed to be a re-initialization and the | 118 // If width==0 then this is assumed to be a re-initialization and the |
119 // previously-current values are reused instead of the passed parameters | 119 // previously-current values are reused instead of the passed parameters |
120 // (makes it easier to reason about thread-safety). | 120 // (makes it easier to reason about thread-safety). |
121 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps); | 121 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps); |
122 // Reconfigure to match |frame| in width, height. Returns false if | |
123 // reconfiguring fails. | |
124 bool MaybeReconfigureEncoderOnCodecThread(const webrtc::VideoFrame& frame); | |
122 int32_t EncodeOnCodecThread( | 125 int32_t EncodeOnCodecThread( |
123 const webrtc::VideoFrame& input_image, | 126 const webrtc::VideoFrame& input_image, |
124 const std::vector<webrtc::VideoFrameType>* frame_types); | 127 const std::vector<webrtc::VideoFrameType>* frame_types); |
128 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, | |
129 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); | |
130 | |
125 int32_t RegisterEncodeCompleteCallbackOnCodecThread( | 131 int32_t RegisterEncodeCompleteCallbackOnCodecThread( |
126 webrtc::EncodedImageCallback* callback); | 132 webrtc::EncodedImageCallback* callback); |
127 int32_t ReleaseOnCodecThread(); | 133 int32_t ReleaseOnCodecThread(); |
128 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); | 134 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); |
129 | 135 |
130 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. | 136 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. |
131 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); | 137 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); |
132 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); | 138 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); |
133 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); | 139 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); |
134 jlong GetOutputBufferInfoPresentationTimestampUs( | 140 jlong GetOutputBufferInfoPresentationTimestampUs( |
135 JNIEnv* jni, jobject j_output_buffer_info); | 141 JNIEnv* jni, jobject j_output_buffer_info); |
136 | 142 |
137 // Deliver any outputs pending in the MediaCodec to our |callback_| and return | 143 // Deliver any outputs pending in the MediaCodec to our |callback_| and return |
138 // true on success. | 144 // true on success. |
139 bool DeliverPendingOutputs(JNIEnv* jni); | 145 bool DeliverPendingOutputs(JNIEnv* jni); |
140 | 146 |
141 // Search for H.264 start codes. | 147 // Search for H.264 start codes. |
142 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); | 148 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); |
143 | 149 |
144 // Type of video codec. | 150 // Type of video codec. |
145 VideoCodecType codecType_; | 151 VideoCodecType codecType_; |
146 | 152 |
147 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to | 153 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to |
148 // |codec_thread_| synchronously. | 154 // |codec_thread_| synchronously. |
149 webrtc::EncodedImageCallback* callback_; | 155 webrtc::EncodedImageCallback* callback_; |
150 | 156 |
151 // State that is constant for the lifetime of this object once the ctor | 157 // State that is constant for the lifetime of this object once the ctor |
152 // returns. | 158 // returns. |
153 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec. | 159 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec. |
160 rtc::ThreadChecker codec_thread_checker_; | |
154 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_; | 161 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_; |
155 ScopedGlobalRef<jobject> j_media_codec_video_encoder_; | 162 ScopedGlobalRef<jobject> j_media_codec_video_encoder_; |
156 jmethodID j_init_encode_method_; | 163 jmethodID j_init_encode_method_; |
164 jmethodID j_get_input_buffers_method_; | |
157 jmethodID j_dequeue_input_buffer_method_; | 165 jmethodID j_dequeue_input_buffer_method_; |
158 jmethodID j_encode_method_; | 166 jmethodID j_encode_buffer_method_; |
159 jmethodID j_release_method_; | 167 jmethodID j_release_method_; |
160 jmethodID j_set_rates_method_; | 168 jmethodID j_set_rates_method_; |
161 jmethodID j_dequeue_output_buffer_method_; | 169 jmethodID j_dequeue_output_buffer_method_; |
162 jmethodID j_release_output_buffer_method_; | 170 jmethodID j_release_output_buffer_method_; |
163 jfieldID j_color_format_field_; | 171 jfieldID j_color_format_field_; |
164 jfieldID j_info_index_field_; | 172 jfieldID j_info_index_field_; |
165 jfieldID j_info_buffer_field_; | 173 jfieldID j_info_buffer_field_; |
166 jfieldID j_info_is_key_frame_field_; | 174 jfieldID j_info_is_key_frame_field_; |
167 jfieldID j_info_presentation_timestamp_us_field_; | 175 jfieldID j_info_presentation_timestamp_us_field_; |
168 | 176 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 ScopedLocalRefFrame local_ref_frame(jni); | 241 ScopedLocalRefFrame local_ref_frame(jni); |
234 // It would be nice to avoid spinning up a new thread per MediaCodec, and | 242 // It would be nice to avoid spinning up a new thread per MediaCodec, and |
235 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug | 243 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug |
236 // 2732 means that deadlocks abound. This class synchronously trampolines | 244 // 2732 means that deadlocks abound. This class synchronously trampolines |
237 // to |codec_thread_|, so if anything else can be coming to _us_ from | 245 // to |codec_thread_|, so if anything else can be coming to _us_ from |
238 // |codec_thread_|, or from any thread holding the |_sendCritSect| described | 246 // |codec_thread_|, or from any thread holding the |_sendCritSect| described |
239 // in the bug, we have a problem. For now work around that with a dedicated | 247 // in the bug, we have a problem. For now work around that with a dedicated |
240 // thread. | 248 // thread. |
241 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); | 249 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); |
242 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; | 250 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; |
243 | 251 codec_thread_checker_.DetachFromThread(); |
244 jclass j_output_buffer_info_class = | 252 jclass j_output_buffer_info_class = |
245 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); | 253 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); |
246 j_init_encode_method_ = GetMethodID( | 254 j_init_encode_method_ = GetMethodID( |
247 jni, | 255 jni, |
248 *j_media_codec_video_encoder_class_, | 256 *j_media_codec_video_encoder_class_, |
249 "initEncode", | 257 "initEncode", |
250 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;IIII)" | 258 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;IIII)Z"); |
251 "[Ljava/nio/ByteBuffer;"); | 259 j_get_input_buffers_method_ = GetMethodID( |
260 jni, | |
261 *j_media_codec_video_encoder_class_, | |
262 "getInputBuffers", | |
263 "()[Ljava/nio/ByteBuffer;"); | |
252 j_dequeue_input_buffer_method_ = GetMethodID( | 264 j_dequeue_input_buffer_method_ = GetMethodID( |
253 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I"); | 265 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I"); |
254 j_encode_method_ = GetMethodID( | 266 j_encode_buffer_method_ = GetMethodID( |
255 jni, *j_media_codec_video_encoder_class_, "encode", "(ZIIJ)Z"); | 267 jni, *j_media_codec_video_encoder_class_, "encodeBuffer", "(ZIIJ)Z"); |
256 j_release_method_ = | 268 j_release_method_ = |
257 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V"); | 269 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V"); |
258 j_set_rates_method_ = GetMethodID( | 270 j_set_rates_method_ = GetMethodID( |
259 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z"); | 271 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z"); |
260 j_dequeue_output_buffer_method_ = GetMethodID( | 272 j_dequeue_output_buffer_method_ = GetMethodID( |
261 jni, | 273 jni, |
262 *j_media_codec_video_encoder_class_, | 274 *j_media_codec_video_encoder_class_, |
263 "dequeueOutputBuffer", | 275 "dequeueOutputBuffer", |
264 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;"); | 276 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;"); |
265 j_release_output_buffer_method_ = GetMethodID( | 277 j_release_output_buffer_method_ = GetMethodID( |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 quality_scaler_.ReportFramerate(frame_rate); | 380 quality_scaler_.ReportFramerate(frame_rate); |
369 | 381 |
370 return codec_thread_->Invoke<int32_t>( | 382 return codec_thread_->Invoke<int32_t>( |
371 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, | 383 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, |
372 this, | 384 this, |
373 new_bit_rate, | 385 new_bit_rate, |
374 frame_rate)); | 386 frame_rate)); |
375 } | 387 } |
376 | 388 |
377 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { | 389 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { |
390 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
AlexG
2015/10/19 23:38:44
RTC_DCHECK -> RTC_CHECK. We probably should fail o
perkj_webrtc
2015/10/21 04:36:28
The thread_checker doesnt do anything in release,
| |
378 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 391 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
379 ScopedLocalRefFrame local_ref_frame(jni); | 392 ScopedLocalRefFrame local_ref_frame(jni); |
380 | 393 |
381 // We only ever send one message to |this| directly (not through a Bind()'d | 394 // We only ever send one message to |this| directly (not through a Bind()'d |
382 // functor), so expect no ID/data. | 395 // functor), so expect no ID/data. |
383 RTC_CHECK(!msg->message_id) << "Unexpected message!"; | 396 RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
384 RTC_CHECK(!msg->pdata) << "Unexpected message!"; | 397 RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
385 CheckOnCodecThread(); | |
386 if (!inited_) { | 398 if (!inited_) { |
387 return; | 399 return; |
388 } | 400 } |
389 | 401 |
390 // It would be nice to recover from a failure here if one happened, but it's | 402 // It would be nice to recover from a failure here if one happened, but it's |
391 // unclear how to signal such a failure to the app, so instead we stay silent | 403 // unclear how to signal such a failure to the app, so instead we stay silent |
392 // about it and let the next app-called API method reveal the borkedness. | 404 // about it and let the next app-called API method reveal the borkedness. |
393 DeliverPendingOutputs(jni); | 405 DeliverPendingOutputs(jni); |
394 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 406 codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
395 } | 407 } |
396 | 408 |
397 void MediaCodecVideoEncoder::CheckOnCodecThread() { | 409 void MediaCodecVideoEncoder::ResetCodecOnCodecThread() { |
398 RTC_CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread()) | 410 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
AlexG
2015/10/19 23:38:45
ditto: RTC_DCHECK -> RTC_CHECK and below
perkj_webrtc
2015/10/21 04:36:28
dito
| |
399 << "Running on wrong thread!"; | 411 ALOGE << "ResetOnCodecThread"; |
400 } | 412 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || |
401 | 413 InitEncodeOnCodecThread(width_, height_, 0, 0) |
402 void MediaCodecVideoEncoder::ResetCodec() { | 414 != WEBRTC_VIDEO_CODEC_OK) { |
403 ALOGE << "ResetCodec"; | |
404 if (Release() != WEBRTC_VIDEO_CODEC_OK || | |
405 codec_thread_->Invoke<int32_t>(Bind( | |
406 &MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, | |
407 width_, height_, 0, 0)) != WEBRTC_VIDEO_CODEC_OK) { | |
408 // TODO(fischman): wouldn't it be nice if there was a way to gracefully | 415 // TODO(fischman): wouldn't it be nice if there was a way to gracefully |
AlexG
2015/10/19 23:38:44
May be it's a good time now to make Reset to retur
perkj_webrtc
2015/10/21 04:36:28
Ok. But the whole thing is a bit weird. It seems l
| |
409 // degrade to a SW encoder at this point? There isn't one AFAICT :( | 416 // degrade to a SW encoder at this point? There isn't one AFAICT :( |
410 // https://code.google.com/p/webrtc/issues/detail?id=2920 | 417 // https://code.google.com/p/webrtc/issues/detail?id=2920 |
411 } | 418 } |
412 } | 419 } |
413 | 420 |
414 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( | 421 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( |
415 int width, int height, int kbps, int fps) { | 422 int width, int height, int kbps, int fps) { |
416 CheckOnCodecThread(); | 423 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
417 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 424 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
418 ScopedLocalRefFrame local_ref_frame(jni); | 425 ScopedLocalRefFrame local_ref_frame(jni); |
419 | 426 |
420 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << | 427 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << |
421 width << " x " << height << ". Bitrate: " << kbps << | 428 width << " x " << height << ". Bitrate: " << kbps << |
422 " kbps. Fps: " << fps; | 429 " kbps. Fps: " << fps; |
423 if (kbps == 0) { | 430 if (kbps == 0) { |
424 kbps = last_set_bitrate_kbps_; | 431 kbps = last_set_bitrate_kbps_; |
425 } | 432 } |
426 if (fps == 0) { | 433 if (fps == 0) { |
(...skipping 16 matching lines...) Expand all Loading... | |
443 current_encoding_time_ms_ = 0; | 450 current_encoding_time_ms_ = 0; |
444 last_input_timestamp_ms_ = -1; | 451 last_input_timestamp_ms_ = -1; |
445 last_output_timestamp_ms_ = -1; | 452 last_output_timestamp_ms_ = -1; |
446 output_timestamp_ = 0; | 453 output_timestamp_ = 0; |
447 output_render_time_ms_ = 0; | 454 output_render_time_ms_ = 0; |
448 timestamps_.clear(); | 455 timestamps_.clear(); |
449 render_times_ms_.clear(); | 456 render_times_ms_.clear(); |
450 frame_rtc_times_ms_.clear(); | 457 frame_rtc_times_ms_.clear(); |
451 drop_next_input_frame_ = false; | 458 drop_next_input_frame_ = false; |
452 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; | 459 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; |
460 | |
453 // We enforce no extra stride/padding in the format creation step. | 461 // We enforce no extra stride/padding in the format creation step. |
454 jobject j_video_codec_enum = JavaEnumFromIndex( | 462 jobject j_video_codec_enum = JavaEnumFromIndex( |
455 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); | 463 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); |
464 const bool encode_status = jni->CallBooleanMethod( | |
465 *j_media_codec_video_encoder_, j_init_encode_method_, | |
466 j_video_codec_enum, width, height, kbps, fps); | |
467 if (!encode_status) { | |
468 ALOGE << "Failed to configure encoder."; | |
469 return WEBRTC_VIDEO_CODEC_ERROR; | |
470 } | |
471 CHECK_EXCEPTION(jni); | |
472 | |
456 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( | 473 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( |
457 jni->CallObjectMethod(*j_media_codec_video_encoder_, | 474 jni->CallObjectMethod(*j_media_codec_video_encoder_, |
458 j_init_encode_method_, | 475 j_get_input_buffers_method_)); |
459 j_video_codec_enum, | |
460 width_, | |
461 height_, | |
462 kbps, | |
463 fps)); | |
464 CHECK_EXCEPTION(jni); | 476 CHECK_EXCEPTION(jni); |
465 if (IsNull(jni, input_buffers)) { | 477 if (IsNull(jni, input_buffers)) { |
466 return WEBRTC_VIDEO_CODEC_ERROR; | 478 return WEBRTC_VIDEO_CODEC_ERROR; |
467 } | 479 } |
468 | 480 |
469 inited_ = true; | |
470 switch (GetIntField(jni, *j_media_codec_video_encoder_, | 481 switch (GetIntField(jni, *j_media_codec_video_encoder_, |
471 j_color_format_field_)) { | 482 j_color_format_field_)) { |
472 case COLOR_FormatYUV420Planar: | 483 case COLOR_FormatYUV420Planar: |
473 encoder_fourcc_ = libyuv::FOURCC_YU12; | 484 encoder_fourcc_ = libyuv::FOURCC_YU12; |
474 break; | 485 break; |
475 case COLOR_FormatYUV420SemiPlanar: | 486 case COLOR_FormatYUV420SemiPlanar: |
476 case COLOR_QCOM_FormatYUV420SemiPlanar: | 487 case COLOR_QCOM_FormatYUV420SemiPlanar: |
477 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: | 488 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: |
478 encoder_fourcc_ = libyuv::FOURCC_NV12; | 489 encoder_fourcc_ = libyuv::FOURCC_NV12; |
479 break; | 490 break; |
480 default: | 491 default: |
481 LOG(LS_ERROR) << "Wrong color format."; | 492 LOG(LS_ERROR) << "Wrong color format."; |
482 return WEBRTC_VIDEO_CODEC_ERROR; | 493 return WEBRTC_VIDEO_CODEC_ERROR; |
483 } | 494 } |
484 size_t num_input_buffers = jni->GetArrayLength(input_buffers); | 495 size_t num_input_buffers = jni->GetArrayLength(input_buffers); |
485 RTC_CHECK(input_buffers_.empty()) | 496 RTC_CHECK(input_buffers_.empty()) |
486 << "Unexpected double InitEncode without Release"; | 497 << "Unexpected double InitEncode without Release"; |
487 input_buffers_.resize(num_input_buffers); | 498 input_buffers_.resize(num_input_buffers); |
488 for (size_t i = 0; i < num_input_buffers; ++i) { | 499 for (size_t i = 0; i < num_input_buffers; ++i) { |
489 input_buffers_[i] = | 500 input_buffers_[i] = |
490 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); | 501 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); |
491 int64_t yuv_buffer_capacity = | 502 int64_t yuv_buffer_capacity = |
492 jni->GetDirectBufferCapacity(input_buffers_[i]); | 503 jni->GetDirectBufferCapacity(input_buffers_[i]); |
493 CHECK_EXCEPTION(jni); | 504 CHECK_EXCEPTION(jni); |
494 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; | 505 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; |
495 } | 506 } |
496 CHECK_EXCEPTION(jni); | 507 CHECK_EXCEPTION(jni); |
497 | 508 |
509 | |
510 inited_ = true; | |
498 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 511 codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
499 return WEBRTC_VIDEO_CODEC_OK; | 512 return WEBRTC_VIDEO_CODEC_OK; |
500 } | 513 } |
501 | 514 |
502 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( | 515 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( |
503 const webrtc::VideoFrame& frame, | 516 const webrtc::VideoFrame& frame, |
504 const std::vector<webrtc::VideoFrameType>* frame_types) { | 517 const std::vector<webrtc::VideoFrameType>* frame_types) { |
505 CheckOnCodecThread(); | 518 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
506 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 519 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
507 ScopedLocalRefFrame local_ref_frame(jni); | 520 ScopedLocalRefFrame local_ref_frame(jni); |
508 | 521 |
509 if (!inited_) { | 522 if (!inited_) { |
510 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 523 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
511 } | 524 } |
525 | |
512 frames_received_++; | 526 frames_received_++; |
513 if (!DeliverPendingOutputs(jni)) { | 527 if (!DeliverPendingOutputs(jni)) { |
514 ResetCodec(); | 528 ResetCodecOnCodecThread(); |
515 // Continue as if everything's fine. | 529 // Continue as if everything's fine. |
516 } | 530 } |
517 | 531 |
518 if (drop_next_input_frame_) { | 532 if (drop_next_input_frame_) { |
519 ALOGV("Encoder drop frame - failed callback."); | 533 ALOGD << "Encoder drop frame - failed callback."; |
AlexG
2015/10/19 23:38:44
ALOGW?
perkj_webrtc
2015/10/21 04:36:28
Done.
| |
520 drop_next_input_frame_ = false; | 534 drop_next_input_frame_ = false; |
521 return WEBRTC_VIDEO_CODEC_OK; | 535 return WEBRTC_VIDEO_CODEC_OK; |
522 } | 536 } |
523 | 537 |
524 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; | 538 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; |
525 // Check framerate before spatial resolution change. | 539 // Check framerate before spatial resolution change. |
526 if (scale_) | 540 if (scale_) |
527 quality_scaler_.OnEncodeFrame(frame); | 541 quality_scaler_.OnEncodeFrame(frame); |
528 | 542 |
529 const VideoFrame& input_frame = | 543 const VideoFrame& input_frame = |
530 scale_ ? quality_scaler_.GetScaledFrame(frame) : frame; | 544 scale_ ? quality_scaler_.GetScaledFrame(frame) : frame; |
531 | 545 |
532 if (input_frame.width() != width_ || input_frame.height() != height_) { | 546 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { |
533 ALOGD << "Frame resolution change from " << width_ << " x " << height_ << | 547 ALOGE << "Failed to reconfigure encoder."; |
534 " to " << input_frame.width() << " x " << input_frame.height(); | 548 return WEBRTC_VIDEO_CODEC_ERROR; |
535 width_ = input_frame.width(); | |
536 height_ = input_frame.height(); | |
537 ResetCodec(); | |
538 return WEBRTC_VIDEO_CODEC_OK; | |
539 } | 549 } |
540 | 550 |
541 // Check if we accumulated too many frames in encoder input buffers | 551 // Check if we accumulated too many frames in encoder input buffers |
542 // or the encoder latency exceeds 70 ms and drop frame if so. | 552 // or the encoder latency exceeds 70 ms and drop frame if so. |
543 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) { | 553 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) { |
544 int encoder_latency_ms = last_input_timestamp_ms_ - | 554 int encoder_latency_ms = last_input_timestamp_ms_ - |
545 last_output_timestamp_ms_; | 555 last_output_timestamp_ms_; |
546 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) { | 556 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) { |
547 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms << | 557 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms << |
548 " ms. Q size: " << frames_in_queue_; | 558 " ms. Q size: " << frames_in_queue_; |
549 frames_dropped_++; | 559 frames_dropped_++; |
550 // Report dropped frame to quality_scaler_. | 560 // Report dropped frame to quality_scaler_. |
551 OnDroppedFrame(); | 561 OnDroppedFrame(); |
552 return WEBRTC_VIDEO_CODEC_OK; | 562 return WEBRTC_VIDEO_CODEC_OK; |
553 } | 563 } |
554 } | 564 } |
555 | 565 |
556 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, | 566 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, |
557 j_dequeue_input_buffer_method_); | 567 j_dequeue_input_buffer_method_); |
558 CHECK_EXCEPTION(jni); | 568 CHECK_EXCEPTION(jni); |
559 if (j_input_buffer_index == -1) { | 569 if (j_input_buffer_index == -1) { |
560 // Video codec falls behind - no input buffer available. | 570 // Video codec falls behind - no input buffer available. |
561 ALOGV("Encoder drop frame - no input buffers available"); | 571 ALOGD <<"Encoder drop frame - no input buffers available"; |
AlexG
2015/10/19 23:38:44
nit: whitespace after "<<" + ALOGD -> ALOGW?
perkj_webrtc
2015/10/21 04:36:28
Done.
| |
562 frames_dropped_++; | 572 frames_dropped_++; |
563 // Report dropped frame to quality_scaler_. | 573 // Report dropped frame to quality_scaler_. |
564 OnDroppedFrame(); | 574 OnDroppedFrame(); |
565 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. | 575 return true; // TODO(fischman): see webrtc bug 2887. |
AlexG
2015/10/19 23:38:44
Why true? This function should return WEBRTC_VIDEO
perkj_webrtc
2015/10/21 04:36:28
darn. Yes...
Is there an easy way to trigger this
| |
566 } | 576 } |
567 if (j_input_buffer_index == -2) { | 577 if (j_input_buffer_index == -2) { |
AlexG
2015/10/19 23:38:44
ResetCodecOnCodecThread() ?
perkj_webrtc
2015/10/21 04:36:28
Done.
| |
568 ResetCodec(); | 578 return false; |
AlexG
2015/10/19 23:38:44
return WEBRTC_VIDEO_CODEC_ERROR?
perkj_webrtc
2015/10/21 04:36:28
Done.
| |
569 return WEBRTC_VIDEO_CODEC_ERROR; | |
570 } | 579 } |
571 | 580 |
572 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d", | 581 last_input_timestamp_ms_ = |
573 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_); | 582 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; |
574 | |
575 jobject j_input_buffer = input_buffers_[j_input_buffer_index]; | |
576 uint8_t* yuv_buffer = | |
577 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); | |
578 CHECK_EXCEPTION(jni); | |
579 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; | |
580 RTC_CHECK(!libyuv::ConvertFromI420( | |
581 input_frame.buffer(webrtc::kYPlane), input_frame.stride(webrtc::kYPlane), | |
582 input_frame.buffer(webrtc::kUPlane), input_frame.stride(webrtc::kUPlane), | |
583 input_frame.buffer(webrtc::kVPlane), input_frame.stride(webrtc::kVPlane), | |
584 yuv_buffer, width_, width_, height_, encoder_fourcc_)) | |
585 << "ConvertFromI420 failed"; | |
586 last_input_timestamp_ms_ = current_timestamp_us_ / 1000; | |
587 frames_in_queue_++; | 583 frames_in_queue_++; |
588 | 584 |
589 // Save input image timestamps for later output | 585 // Save input image timestamps for later output |
590 timestamps_.push_back(input_frame.timestamp()); | 586 timestamps_.push_back(input_frame.timestamp()); |
591 render_times_ms_.push_back(input_frame.render_time_ms()); | 587 render_times_ms_.push_back(input_frame.render_time_ms()); |
592 frame_rtc_times_ms_.push_back(GetCurrentTimeMs()); | 588 frame_rtc_times_ms_.push_back(GetCurrentTimeMs()); |
593 | 589 |
594 bool key_frame = frame_types->front() != webrtc::kDeltaFrame; | 590 const bool key_frame = frame_types->front() != webrtc::kDeltaFrame; |
591 const bool encode_status = | |
592 EncodeByteBufferOnCodecThread(jni, key_frame, input_frame, | |
593 j_input_buffer_index); | |
594 | |
595 current_timestamp_us_ += 1000000 / last_set_fps_; | |
596 | |
597 if (!encode_status || !DeliverPendingOutputs(jni)) { | |
598 ALOGE << "Failed deliver pending outputs."; | |
599 ResetCodecOnCodecThread(); | |
600 return WEBRTC_VIDEO_CODEC_ERROR; | |
601 } | |
602 return WEBRTC_VIDEO_CODEC_OK; | |
603 } | |
604 | |
605 bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread( | |
606 const webrtc::VideoFrame& frame) { | |
607 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
608 | |
609 const bool reconfigure_due_to_size = | |
610 frame.width() != width_ || frame.height() != height_; | |
611 | |
612 if (reconfigure_due_to_size) { | |
613 ALOGD << "Reconfigure encoder due to frame resolution change from " | |
614 << width_ << " x " << height_ << " to " << frame.width() << " x " | |
615 << frame.height(); | |
616 width_ = frame.width(); | |
617 height_ = frame.height(); | |
618 } | |
619 | |
620 if (!reconfigure_due_to_size) | |
621 return true; | |
622 | |
623 ReleaseOnCodecThread(); | |
624 | |
625 return InitEncodeOnCodecThread(width_, height_, 0, 0) == | |
626 WEBRTC_VIDEO_CODEC_OK; | |
627 } | |
628 | |
629 bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni, | |
630 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index) { | |
631 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
632 | |
633 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d", | |
634 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_); | |
635 | |
636 jobject j_input_buffer = input_buffers_[input_buffer_index]; | |
637 uint8_t* yuv_buffer = | |
638 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); | |
639 CHECK_EXCEPTION(jni); | |
640 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; | |
641 RTC_CHECK(!libyuv::ConvertFromI420( | |
642 frame.buffer(webrtc::kYPlane), frame.stride(webrtc::kYPlane), | |
643 frame.buffer(webrtc::kUPlane), frame.stride(webrtc::kUPlane), | |
644 frame.buffer(webrtc::kVPlane), frame.stride(webrtc::kVPlane), | |
645 yuv_buffer, width_, width_, height_, encoder_fourcc_)) | |
646 << "ConvertFromI420 failed"; | |
647 | |
595 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 648 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
596 j_encode_method_, | 649 j_encode_buffer_method_, |
597 key_frame, | 650 key_frame, |
598 j_input_buffer_index, | 651 input_buffer_index, |
599 yuv_size_, | 652 yuv_size_, |
600 current_timestamp_us_); | 653 current_timestamp_us_); |
601 CHECK_EXCEPTION(jni); | 654 CHECK_EXCEPTION(jni); |
602 current_timestamp_us_ += 1000000 / last_set_fps_; | 655 return encode_status; |
603 | |
604 if (!encode_status || !DeliverPendingOutputs(jni)) { | |
605 ResetCodec(); | |
606 return WEBRTC_VIDEO_CODEC_ERROR; | |
607 } | |
608 | |
609 return WEBRTC_VIDEO_CODEC_OK; | |
610 } | 656 } |
611 | 657 |
612 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( | 658 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( |
613 webrtc::EncodedImageCallback* callback) { | 659 webrtc::EncodedImageCallback* callback) { |
614 CheckOnCodecThread(); | 660 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
615 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 661 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
616 ScopedLocalRefFrame local_ref_frame(jni); | 662 ScopedLocalRefFrame local_ref_frame(jni); |
617 callback_ = callback; | 663 callback_ = callback; |
618 return WEBRTC_VIDEO_CODEC_OK; | 664 return WEBRTC_VIDEO_CODEC_OK; |
619 } | 665 } |
620 | 666 |
621 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { | 667 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { |
668 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
622 if (!inited_) { | 669 if (!inited_) { |
623 return WEBRTC_VIDEO_CODEC_OK; | 670 return WEBRTC_VIDEO_CODEC_OK; |
624 } | 671 } |
625 CheckOnCodecThread(); | |
626 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 672 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
627 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << | 673 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << |
628 frames_received_ << ". Encoded: " << frames_encoded_ << | 674 frames_received_ << ". Encoded: " << frames_encoded_ << |
629 ". Dropped: " << frames_dropped_; | 675 ". Dropped: " << frames_dropped_; |
630 ScopedLocalRefFrame local_ref_frame(jni); | 676 ScopedLocalRefFrame local_ref_frame(jni); |
631 for (size_t i = 0; i < input_buffers_.size(); ++i) | 677 for (size_t i = 0; i < input_buffers_.size(); ++i) |
632 jni->DeleteGlobalRef(input_buffers_[i]); | 678 jni->DeleteGlobalRef(input_buffers_[i]); |
633 input_buffers_.clear(); | 679 input_buffers_.clear(); |
634 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); | 680 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); |
635 CHECK_EXCEPTION(jni); | 681 CHECK_EXCEPTION(jni); |
636 rtc::MessageQueueManager::Clear(this); | 682 rtc::MessageQueueManager::Clear(this); |
637 inited_ = false; | 683 inited_ = false; |
638 ALOGD << "EncoderReleaseOnCodecThread done."; | 684 ALOGD << "EncoderReleaseOnCodecThread done."; |
639 return WEBRTC_VIDEO_CODEC_OK; | 685 return WEBRTC_VIDEO_CODEC_OK; |
640 } | 686 } |
641 | 687 |
642 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, | 688 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, |
643 uint32_t frame_rate) { | 689 uint32_t frame_rate) { |
644 CheckOnCodecThread(); | 690 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
645 if (last_set_bitrate_kbps_ == new_bit_rate && | 691 if (last_set_bitrate_kbps_ == new_bit_rate && |
646 last_set_fps_ == frame_rate) { | 692 last_set_fps_ == frame_rate) { |
647 return WEBRTC_VIDEO_CODEC_OK; | 693 return WEBRTC_VIDEO_CODEC_OK; |
648 } | 694 } |
649 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 695 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
650 ScopedLocalRefFrame local_ref_frame(jni); | 696 ScopedLocalRefFrame local_ref_frame(jni); |
651 if (new_bit_rate > 0) { | 697 if (new_bit_rate > 0) { |
652 last_set_bitrate_kbps_ = new_bit_rate; | 698 last_set_bitrate_kbps_ = new_bit_rate; |
653 } | 699 } |
654 if (frame_rate > 0) { | 700 if (frame_rate > 0) { |
655 last_set_fps_ = frame_rate; | 701 last_set_fps_ = frame_rate; |
656 } | 702 } |
657 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 703 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
658 j_set_rates_method_, | 704 j_set_rates_method_, |
659 last_set_bitrate_kbps_, | 705 last_set_bitrate_kbps_, |
660 last_set_fps_); | 706 last_set_fps_); |
661 CHECK_EXCEPTION(jni); | 707 CHECK_EXCEPTION(jni); |
662 if (!ret) { | 708 if (!ret) { |
663 ResetCodec(); | 709 ResetCodecOnCodecThread(); |
664 return WEBRTC_VIDEO_CODEC_ERROR; | 710 return WEBRTC_VIDEO_CODEC_ERROR; |
665 } | 711 } |
666 return WEBRTC_VIDEO_CODEC_OK; | 712 return WEBRTC_VIDEO_CODEC_OK; |
667 } | 713 } |
668 | 714 |
669 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( | 715 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( |
670 JNIEnv* jni, | 716 JNIEnv* jni, |
671 jobject j_output_buffer_info) { | 717 jobject j_output_buffer_info) { |
672 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); | 718 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); |
673 } | 719 } |
(...skipping 11 matching lines...) Expand all Loading... | |
685 } | 731 } |
686 | 732 |
687 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( | 733 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( |
688 JNIEnv* jni, | 734 JNIEnv* jni, |
689 jobject j_output_buffer_info) { | 735 jobject j_output_buffer_info) { |
690 return GetLongField( | 736 return GetLongField( |
691 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); | 737 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); |
692 } | 738 } |
693 | 739 |
694 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { | 740 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { |
741 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
695 while (true) { | 742 while (true) { |
696 jobject j_output_buffer_info = jni->CallObjectMethod( | 743 jobject j_output_buffer_info = jni->CallObjectMethod( |
697 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); | 744 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); |
698 CHECK_EXCEPTION(jni); | 745 CHECK_EXCEPTION(jni); |
699 if (IsNull(jni, j_output_buffer_info)) { | 746 if (IsNull(jni, j_output_buffer_info)) { |
700 break; | 747 break; |
701 } | 748 } |
702 | 749 |
703 int output_buffer_index = | 750 int output_buffer_index = |
704 GetOutputBufferInfoIndex(jni, j_output_buffer_info); | 751 GetOutputBufferInfoIndex(jni, j_output_buffer_info); |
705 if (output_buffer_index == -1) { | 752 if (output_buffer_index == -1) { |
706 ResetCodec(); | 753 ResetCodecOnCodecThread(); |
707 return false; | 754 return false; |
708 } | 755 } |
709 | 756 |
710 // Get key and config frame flags. | 757 // Get key and config frame flags. |
711 jobject j_output_buffer = | 758 jobject j_output_buffer = |
712 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); | 759 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); |
713 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); | 760 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); |
714 | 761 |
715 // Get frame timestamps from a queue - for non config frames only. | 762 // Get frame timestamps from a queue - for non config frames only. |
716 int64_t frame_encoding_time_ms = 0; | 763 int64_t frame_encoding_time_ms = 0; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
817 } | 864 } |
818 scPosition += naluPosition; | 865 scPosition += naluPosition; |
819 scPositions[scPositionsLength++] = scPosition; | 866 scPositions[scPositionsLength++] = scPosition; |
820 scPosition += H264_SC_LENGTH; | 867 scPosition += H264_SC_LENGTH; |
821 } | 868 } |
822 if (scPositionsLength == 0) { | 869 if (scPositionsLength == 0) { |
823 ALOGE << "Start code is not found!"; | 870 ALOGE << "Start code is not found!"; |
824 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] | 871 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] |
825 << " " << image->_buffer[2] << " " << image->_buffer[3] | 872 << " " << image->_buffer[2] << " " << image->_buffer[3] |
826 << " " << image->_buffer[4] << " " << image->_buffer[5]; | 873 << " " << image->_buffer[4] << " " << image->_buffer[5]; |
827 ResetCodec(); | 874 ResetCodecOnCodecThread(); |
828 return false; | 875 return false; |
829 } | 876 } |
830 scPositions[scPositionsLength] = payload_size; | 877 scPositions[scPositionsLength] = payload_size; |
831 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); | 878 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); |
832 for (size_t i = 0; i < scPositionsLength; i++) { | 879 for (size_t i = 0; i < scPositionsLength; i++) { |
833 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; | 880 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; |
834 header.fragmentationLength[i] = | 881 header.fragmentationLength[i] = |
835 scPositions[i + 1] - header.fragmentationOffset[i]; | 882 scPositions[i + 1] - header.fragmentationOffset[i]; |
836 header.fragmentationPlType[i] = 0; | 883 header.fragmentationPlType[i] = 0; |
837 header.fragmentationTimeDiff[i] = 0; | 884 header.fragmentationTimeDiff[i] = 0; |
838 } | 885 } |
839 } | 886 } |
840 | 887 |
841 callback_status = callback_->Encoded(*image, &info, &header); | 888 callback_status = callback_->Encoded(*image, &info, &header); |
842 } | 889 } |
843 | 890 |
844 // Return output buffer back to the encoder. | 891 // Return output buffer back to the encoder. |
845 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 892 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
846 j_release_output_buffer_method_, | 893 j_release_output_buffer_method_, |
847 output_buffer_index); | 894 output_buffer_index); |
848 CHECK_EXCEPTION(jni); | 895 CHECK_EXCEPTION(jni); |
849 if (!success) { | 896 if (!success) { |
850 ResetCodec(); | 897 ResetCodecOnCodecThread(); |
851 return false; | 898 return false; |
852 } | 899 } |
853 | 900 |
854 if (callback_status > 0) { | 901 if (callback_status > 0) { |
855 drop_next_input_frame_ = true; | 902 drop_next_input_frame_ = true; |
856 // Theoretically could handle callback_status<0 here, but unclear what | 903 // Theoretically could handle callback_status<0 here, but unclear what |
857 // that would mean for us. | 904 // that would mean for us. |
858 } | 905 } |
859 } | 906 } |
860 | 907 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 } | 1000 } |
954 | 1001 |
955 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1002 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
956 webrtc::VideoEncoder* encoder) { | 1003 webrtc::VideoEncoder* encoder) { |
957 ALOGD << "Destroy video encoder."; | 1004 ALOGD << "Destroy video encoder."; |
958 delete encoder; | 1005 delete encoder; |
959 } | 1006 } |
960 | 1007 |
961 } // namespace webrtc_jni | 1008 } // namespace webrtc_jni |
962 | 1009 |
OLD | NEW |