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 // ReleaseOnCodecThread() and InitEncodeOnCodecThread() in an attempt to |
magjed_webrtc
2015/10/12 08:22:53
I was confused the first time I read this sentence
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
109 void CheckOnCodecThread(); | 111 // restore the codec to an operable state. Necessary after all manner of |
110 | 112 // OMX-layer errors. |
111 // Release() and InitEncode() in an attempt to restore the codec to an | 113 void ResetCodecOnCodecThread(); |
112 // operable state. Necessary after all manner of OMX-layer errors. | |
113 void ResetCodec(); | |
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. Also reconfigures the | |
123 // encoder if |frame| is a texture/byte buffer and the encoder is initialized | |
magjed_webrtc
2015/10/12 08:22:53
Remove comment about textures :)
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
124 // for byte buffer/texture. Returns false if reconfiguring fails. | |
125 bool MaybeReconfigureEncoderOnCodecThread(const webrtc::VideoFrame& frame); | |
magjed_webrtc
2015/10/12 08:22:53
nit: From just reading bool MaybeReconfigureEncode
perkj_webrtc
2015/10/12 09:16:05
Acknowledged.
| |
122 int32_t EncodeOnCodecThread( | 126 int32_t EncodeOnCodecThread( |
123 const webrtc::VideoFrame& input_image, | 127 const webrtc::VideoFrame& input_image, |
124 const std::vector<webrtc::VideoFrameType>* frame_types); | 128 const std::vector<webrtc::VideoFrameType>* frame_types); |
129 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, | |
130 bool key_frame, const webrtc::VideoFrame& frame); | |
131 | |
125 int32_t RegisterEncodeCompleteCallbackOnCodecThread( | 132 int32_t RegisterEncodeCompleteCallbackOnCodecThread( |
126 webrtc::EncodedImageCallback* callback); | 133 webrtc::EncodedImageCallback* callback); |
127 int32_t ReleaseOnCodecThread(); | 134 int32_t ReleaseOnCodecThread(); |
128 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); | 135 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); |
129 | 136 |
130 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. | 137 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. |
131 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); | 138 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); |
132 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); | 139 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); |
133 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); | 140 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); |
134 jlong GetOutputBufferInfoPresentationTimestampUs( | 141 jlong GetOutputBufferInfoPresentationTimestampUs( |
135 JNIEnv* jni, jobject j_output_buffer_info); | 142 JNIEnv* jni, jobject j_output_buffer_info); |
136 | 143 |
137 // Deliver any outputs pending in the MediaCodec to our |callback_| and return | 144 // Deliver any outputs pending in the MediaCodec to our |callback_| and return |
138 // true on success. | 145 // true on success. |
139 bool DeliverPendingOutputs(JNIEnv* jni); | 146 bool DeliverPendingOutputs(JNIEnv* jni); |
140 | 147 |
141 // Search for H.264 start codes. | 148 // Search for H.264 start codes. |
142 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); | 149 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); |
143 | 150 |
144 // Type of video codec. | 151 // Type of video codec. |
145 VideoCodecType codecType_; | 152 VideoCodecType codecType_; |
146 | 153 |
147 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to | 154 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to |
148 // |codec_thread_| synchronously. | 155 // |codec_thread_| synchronously. |
149 webrtc::EncodedImageCallback* callback_; | 156 webrtc::EncodedImageCallback* callback_; |
150 | 157 |
151 // State that is constant for the lifetime of this object once the ctor | 158 // State that is constant for the lifetime of this object once the ctor |
152 // returns. | 159 // returns. |
153 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec. | 160 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec. |
161 rtc::ThreadChecker codec_thread_checker_; | |
154 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_; | 162 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_; |
155 ScopedGlobalRef<jobject> j_media_codec_video_encoder_; | 163 ScopedGlobalRef<jobject> j_media_codec_video_encoder_; |
156 jmethodID j_init_encode_method_; | 164 jmethodID j_init_encode_method_; |
165 jmethodID j_get_input_buffers_method_; | |
157 jmethodID j_dequeue_input_buffer_method_; | 166 jmethodID j_dequeue_input_buffer_method_; |
158 jmethodID j_encode_method_; | 167 jmethodID j_encode_buffer_method_; |
159 jmethodID j_release_method_; | 168 jmethodID j_release_method_; |
160 jmethodID j_set_rates_method_; | 169 jmethodID j_set_rates_method_; |
161 jmethodID j_dequeue_output_buffer_method_; | 170 jmethodID j_dequeue_output_buffer_method_; |
162 jmethodID j_release_output_buffer_method_; | 171 jmethodID j_release_output_buffer_method_; |
163 jfieldID j_color_format_field_; | 172 jfieldID j_color_format_field_; |
164 jfieldID j_info_index_field_; | 173 jfieldID j_info_index_field_; |
165 jfieldID j_info_buffer_field_; | 174 jfieldID j_info_buffer_field_; |
166 jfieldID j_info_is_key_frame_field_; | 175 jfieldID j_info_is_key_frame_field_; |
167 jfieldID j_info_presentation_timestamp_us_field_; | 176 jfieldID j_info_presentation_timestamp_us_field_; |
168 | 177 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 // H264 bitstream parser, used to extract QP from encoded bitstreams. | 216 // H264 bitstream parser, used to extract QP from encoded bitstreams. |
208 webrtc::H264BitstreamParser h264_bitstream_parser_; | 217 webrtc::H264BitstreamParser h264_bitstream_parser_; |
209 }; | 218 }; |
210 | 219 |
211 MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { | 220 MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { |
212 // Call Release() to ensure no more callbacks to us after we are deleted. | 221 // Call Release() to ensure no more callbacks to us after we are deleted. |
213 Release(); | 222 Release(); |
214 } | 223 } |
215 | 224 |
216 MediaCodecVideoEncoder::MediaCodecVideoEncoder( | 225 MediaCodecVideoEncoder::MediaCodecVideoEncoder( |
217 JNIEnv* jni, VideoCodecType codecType) : | 226 JNIEnv* jni, VideoCodecType codecType) : |
magjed_webrtc
2015/10/12 08:22:53
Revert this ws change
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
218 codecType_(codecType), | 227 codecType_(codecType), |
219 callback_(NULL), | 228 callback_(NULL), |
220 inited_(false), | 229 inited_(false), |
221 picture_id_(0), | 230 picture_id_(0), |
222 codec_thread_(new Thread()), | 231 codec_thread_(new Thread()), |
223 j_media_codec_video_encoder_class_( | 232 j_media_codec_video_encoder_class_( |
224 jni, | 233 jni, |
225 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), | 234 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), |
226 j_media_codec_video_encoder_( | 235 j_media_codec_video_encoder_( |
227 jni, | 236 jni, |
228 jni->NewObject(*j_media_codec_video_encoder_class_, | 237 jni->NewObject(*j_media_codec_video_encoder_class_, |
229 GetMethodID(jni, | 238 GetMethodID(jni, |
230 *j_media_codec_video_encoder_class_, | 239 *j_media_codec_video_encoder_class_, |
231 "<init>", | 240 "<init>", |
232 "()V"))) { | 241 "()V"))) { |
233 ScopedLocalRefFrame local_ref_frame(jni); | 242 ScopedLocalRefFrame local_ref_frame(jni); |
234 // It would be nice to avoid spinning up a new thread per MediaCodec, and | 243 // 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 | 244 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug |
236 // 2732 means that deadlocks abound. This class synchronously trampolines | 245 // 2732 means that deadlocks abound. This class synchronously trampolines |
237 // to |codec_thread_|, so if anything else can be coming to _us_ from | 246 // to |codec_thread_|, so if anything else can be coming to _us_ from |
238 // |codec_thread_|, or from any thread holding the |_sendCritSect| described | 247 // |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 | 248 // in the bug, we have a problem. For now work around that with a dedicated |
240 // thread. | 249 // thread. |
241 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); | 250 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); |
242 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; | 251 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; |
243 | 252 codec_thread_checker_.DetachFromThread(); |
244 jclass j_output_buffer_info_class = | 253 jclass j_output_buffer_info_class = |
245 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); | 254 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); |
246 j_init_encode_method_ = GetMethodID( | 255 j_init_encode_method_ = GetMethodID( |
247 jni, | 256 jni, |
248 *j_media_codec_video_encoder_class_, | 257 *j_media_codec_video_encoder_class_, |
249 "initEncode", | 258 "initEncode", |
250 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;IIII)" | 259 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;IIII)Z"); |
251 "[Ljava/nio/ByteBuffer;"); | 260 j_get_input_buffers_method_ = GetMethodID( |
261 jni, | |
262 *j_media_codec_video_encoder_class_, | |
263 "getInputBuffers", | |
264 "()[Ljava/nio/ByteBuffer;"); | |
252 j_dequeue_input_buffer_method_ = GetMethodID( | 265 j_dequeue_input_buffer_method_ = GetMethodID( |
253 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I"); | 266 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I"); |
254 j_encode_method_ = GetMethodID( | 267 j_encode_buffer_method_ = GetMethodID( |
255 jni, *j_media_codec_video_encoder_class_, "encode", "(ZIIJ)Z"); | 268 jni, *j_media_codec_video_encoder_class_, "encodeBuffer", "(ZIIJ)Z"); |
256 j_release_method_ = | 269 j_release_method_ = |
257 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V"); | 270 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V"); |
258 j_set_rates_method_ = GetMethodID( | 271 j_set_rates_method_ = GetMethodID( |
259 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z"); | 272 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z"); |
260 j_dequeue_output_buffer_method_ = GetMethodID( | 273 j_dequeue_output_buffer_method_ = GetMethodID( |
261 jni, | 274 jni, |
262 *j_media_codec_video_encoder_class_, | 275 *j_media_codec_video_encoder_class_, |
263 "dequeueOutputBuffer", | 276 "dequeueOutputBuffer", |
264 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;"); | 277 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;"); |
265 j_release_output_buffer_method_ = GetMethodID( | 278 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); | 381 quality_scaler_.ReportFramerate(frame_rate); |
369 | 382 |
370 return codec_thread_->Invoke<int32_t>( | 383 return codec_thread_->Invoke<int32_t>( |
371 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, | 384 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, |
372 this, | 385 this, |
373 new_bit_rate, | 386 new_bit_rate, |
374 frame_rate)); | 387 frame_rate)); |
375 } | 388 } |
376 | 389 |
377 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { | 390 void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { |
391 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
378 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 392 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
379 ScopedLocalRefFrame local_ref_frame(jni); | 393 ScopedLocalRefFrame local_ref_frame(jni); |
380 | 394 |
381 // We only ever send one message to |this| directly (not through a Bind()'d | 395 // We only ever send one message to |this| directly (not through a Bind()'d |
382 // functor), so expect no ID/data. | 396 // functor), so expect no ID/data. |
383 RTC_CHECK(!msg->message_id) << "Unexpected message!"; | 397 RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
384 RTC_CHECK(!msg->pdata) << "Unexpected message!"; | 398 RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
385 CheckOnCodecThread(); | |
386 if (!inited_) { | 399 if (!inited_) { |
387 return; | 400 return; |
388 } | 401 } |
389 | 402 |
390 // It would be nice to recover from a failure here if one happened, but it's | 403 // 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 | 404 // 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. | 405 // about it and let the next app-called API method reveal the borkedness. |
393 DeliverPendingOutputs(jni); | 406 DeliverPendingOutputs(jni); |
394 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 407 codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
395 } | 408 } |
396 | 409 |
397 void MediaCodecVideoEncoder::CheckOnCodecThread() { | 410 void MediaCodecVideoEncoder::ResetCodecOnCodecThread() { |
398 RTC_CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread()) | 411 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
399 << "Running on wrong thread!"; | 412 ALOGE << "ResetOnCodecThread"; |
400 } | 413 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || |
401 | 414 InitEncodeOnCodecThread(width_, height_, 0, 0) |
402 void MediaCodecVideoEncoder::ResetCodec() { | 415 != 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 | 416 // TODO(fischman): wouldn't it be nice if there was a way to gracefully |
409 // degrade to a SW encoder at this point? There isn't one AFAICT :( | 417 // degrade to a SW encoder at this point? There isn't one AFAICT :( |
410 // https://code.google.com/p/webrtc/issues/detail?id=2920 | 418 // https://code.google.com/p/webrtc/issues/detail?id=2920 |
411 } | 419 } |
412 } | 420 } |
413 | 421 |
414 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( | 422 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( |
415 int width, int height, int kbps, int fps) { | 423 int width, int height, int kbps, int fps) { |
416 CheckOnCodecThread(); | 424 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
417 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 425 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
418 ScopedLocalRefFrame local_ref_frame(jni); | 426 ScopedLocalRefFrame local_ref_frame(jni); |
419 | 427 |
420 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << | 428 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << |
421 width << " x " << height << ". Bitrate: " << kbps << | 429 width << " x " << height << ". Bitrate: " << kbps << |
422 " kbps. Fps: " << fps; | 430 " kbps. Fps: " << fps; |
423 if (kbps == 0) { | 431 if (kbps == 0) { |
424 kbps = last_set_bitrate_kbps_; | 432 kbps = last_set_bitrate_kbps_; |
425 } | 433 } |
426 if (fps == 0) { | 434 if (fps == 0) { |
(...skipping 16 matching lines...) Expand all Loading... | |
443 current_encoding_time_ms_ = 0; | 451 current_encoding_time_ms_ = 0; |
444 last_input_timestamp_ms_ = -1; | 452 last_input_timestamp_ms_ = -1; |
445 last_output_timestamp_ms_ = -1; | 453 last_output_timestamp_ms_ = -1; |
446 output_timestamp_ = 0; | 454 output_timestamp_ = 0; |
447 output_render_time_ms_ = 0; | 455 output_render_time_ms_ = 0; |
448 timestamps_.clear(); | 456 timestamps_.clear(); |
449 render_times_ms_.clear(); | 457 render_times_ms_.clear(); |
450 frame_rtc_times_ms_.clear(); | 458 frame_rtc_times_ms_.clear(); |
451 drop_next_input_frame_ = false; | 459 drop_next_input_frame_ = false; |
452 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; | 460 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; |
461 | |
453 // We enforce no extra stride/padding in the format creation step. | 462 // We enforce no extra stride/padding in the format creation step. |
454 jobject j_video_codec_enum = JavaEnumFromIndex( | 463 jobject j_video_codec_enum = JavaEnumFromIndex( |
455 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); | 464 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); |
465 bool encode_status = jni->CallBooleanMethod( | |
magjed_webrtc
2015/10/12 08:22:53
const bool
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
466 *j_media_codec_video_encoder_, j_init_encode_method_, | |
467 j_video_codec_enum, width, height, kbps, fps); | |
468 if (!encode_status) { | |
469 ALOGE << "Failed to configure encoder."; | |
470 return WEBRTC_VIDEO_CODEC_ERROR; | |
471 } | |
472 CHECK_EXCEPTION(jni); | |
473 | |
456 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( | 474 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( |
457 jni->CallObjectMethod(*j_media_codec_video_encoder_, | 475 jni->CallObjectMethod(*j_media_codec_video_encoder_, |
458 j_init_encode_method_, | 476 j_get_input_buffers_method_)); |
459 j_video_codec_enum, | |
460 width_, | |
461 height_, | |
462 kbps, | |
463 fps)); | |
464 CHECK_EXCEPTION(jni); | 477 CHECK_EXCEPTION(jni); |
465 if (IsNull(jni, input_buffers)) { | 478 if (IsNull(jni, input_buffers)) { |
466 return WEBRTC_VIDEO_CODEC_ERROR; | 479 return WEBRTC_VIDEO_CODEC_ERROR; |
467 } | 480 } |
468 | 481 |
469 inited_ = true; | |
470 switch (GetIntField(jni, *j_media_codec_video_encoder_, | 482 switch (GetIntField(jni, *j_media_codec_video_encoder_, |
471 j_color_format_field_)) { | 483 j_color_format_field_)) { |
472 case COLOR_FormatYUV420Planar: | 484 case COLOR_FormatYUV420Planar: |
magjed_webrtc
2015/10/12 08:22:53
Revert indentation change, it was correct before?
perkj_webrtc
2015/10/12 09:16:06
Done.
| |
473 encoder_fourcc_ = libyuv::FOURCC_YU12; | 485 encoder_fourcc_ = libyuv::FOURCC_YU12; |
474 break; | 486 break; |
475 case COLOR_FormatYUV420SemiPlanar: | 487 case COLOR_FormatYUV420SemiPlanar: |
476 case COLOR_QCOM_FormatYUV420SemiPlanar: | 488 case COLOR_QCOM_FormatYUV420SemiPlanar: |
477 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: | 489 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: |
478 encoder_fourcc_ = libyuv::FOURCC_NV12; | 490 encoder_fourcc_ = libyuv::FOURCC_NV12; |
479 break; | 491 break; |
480 default: | 492 default: |
481 LOG(LS_ERROR) << "Wrong color format."; | 493 LOG(LS_ERROR) << "Wrong color format."; |
482 return WEBRTC_VIDEO_CODEC_ERROR; | 494 return WEBRTC_VIDEO_CODEC_ERROR; |
483 } | 495 } |
484 size_t num_input_buffers = jni->GetArrayLength(input_buffers); | 496 size_t num_input_buffers = jni->GetArrayLength(input_buffers); |
485 RTC_CHECK(input_buffers_.empty()) | 497 RTC_CHECK(input_buffers_.empty()) |
486 << "Unexpected double InitEncode without Release"; | 498 << "Unexpected double InitEncode without Release"; |
magjed_webrtc
2015/10/12 08:22:53
ditto: Revert ws change
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
487 input_buffers_.resize(num_input_buffers); | 499 input_buffers_.resize(num_input_buffers); |
488 for (size_t i = 0; i < num_input_buffers; ++i) { | 500 for (size_t i = 0; i < num_input_buffers; ++i) { |
489 input_buffers_[i] = | 501 input_buffers_[i] = |
490 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); | 502 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); |
491 int64_t yuv_buffer_capacity = | 503 int64_t yuv_buffer_capacity = |
492 jni->GetDirectBufferCapacity(input_buffers_[i]); | 504 jni->GetDirectBufferCapacity(input_buffers_[i]); |
493 CHECK_EXCEPTION(jni); | 505 CHECK_EXCEPTION(jni); |
494 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; | 506 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; |
495 } | 507 } |
496 CHECK_EXCEPTION(jni); | 508 CHECK_EXCEPTION(jni); |
497 | 509 |
510 | |
511 inited_ = true; | |
498 codec_thread_->PostDelayed(kMediaCodecPollMs, this); | 512 codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
499 return WEBRTC_VIDEO_CODEC_OK; | 513 return WEBRTC_VIDEO_CODEC_OK; |
500 } | 514 } |
501 | 515 |
502 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( | 516 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( |
503 const webrtc::VideoFrame& frame, | 517 const webrtc::VideoFrame& frame, |
504 const std::vector<webrtc::VideoFrameType>* frame_types) { | 518 const std::vector<webrtc::VideoFrameType>* frame_types) { |
505 CheckOnCodecThread(); | 519 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
506 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 520 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
507 ScopedLocalRefFrame local_ref_frame(jni); | 521 ScopedLocalRefFrame local_ref_frame(jni); |
508 | 522 |
509 if (!inited_) { | 523 if (!inited_) { |
510 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 524 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
511 } | 525 } |
526 | |
512 frames_received_++; | 527 frames_received_++; |
513 if (!DeliverPendingOutputs(jni)) { | 528 if (!DeliverPendingOutputs(jni)) { |
514 ResetCodec(); | 529 ResetCodecOnCodecThread(); |
515 // Continue as if everything's fine. | 530 // Continue as if everything's fine. |
516 } | 531 } |
517 | 532 |
518 if (drop_next_input_frame_) { | 533 if (drop_next_input_frame_) { |
519 ALOGV("Encoder drop frame - failed callback."); | 534 ALOGD << "Encoder drop frame - failed callback."; |
520 drop_next_input_frame_ = false; | 535 drop_next_input_frame_ = false; |
521 return WEBRTC_VIDEO_CODEC_OK; | 536 return WEBRTC_VIDEO_CODEC_OK; |
522 } | 537 } |
523 | 538 |
524 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; | 539 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; |
525 // Check framerate before spatial resolution change. | 540 // Check framerate before spatial resolution change. |
526 if (scale_) | 541 if (scale_) |
527 quality_scaler_.OnEncodeFrame(frame); | 542 quality_scaler_.OnEncodeFrame(frame); |
528 | 543 |
529 const VideoFrame& input_frame = | 544 const VideoFrame& input_frame = |
530 scale_ ? quality_scaler_.GetScaledFrame(frame) : frame; | 545 scale_ ? quality_scaler_.GetScaledFrame(frame) : frame; |
531 | 546 |
532 if (input_frame.width() != width_ || input_frame.height() != height_) { | 547 if (!MaybeReconfigureEncoderOnCodecThread(frame)) { |
533 ALOGD << "Frame resolution change from " << width_ << " x " << height_ << | 548 ALOGE << "Failed to reconfigure encoder."; |
534 " to " << input_frame.width() << " x " << input_frame.height(); | 549 return WEBRTC_VIDEO_CODEC_ERROR; |
535 width_ = input_frame.width(); | |
536 height_ = input_frame.height(); | |
537 ResetCodec(); | |
538 return WEBRTC_VIDEO_CODEC_OK; | |
539 } | 550 } |
540 | 551 |
541 // Check if we accumulated too many frames in encoder input buffers | 552 // Check if we accumulated too many frames in encoder input buffers |
542 // or the encoder latency exceeds 70 ms and drop frame if so. | 553 // or the encoder latency exceeds 70 ms and drop frame if so. |
543 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) { | 554 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) { |
544 int encoder_latency_ms = last_input_timestamp_ms_ - | 555 int encoder_latency_ms = last_input_timestamp_ms_ - |
545 last_output_timestamp_ms_; | 556 last_output_timestamp_ms_; |
546 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) { | 557 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) { |
547 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms << | 558 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms << |
548 " ms. Q size: " << frames_in_queue_; | 559 " ms. Q size: " << frames_in_queue_; |
549 frames_dropped_++; | 560 frames_dropped_++; |
550 // Report dropped frame to quality_scaler_. | 561 // Report dropped frame to quality_scaler_. |
551 OnDroppedFrame(); | 562 OnDroppedFrame(); |
552 return WEBRTC_VIDEO_CODEC_OK; | 563 return WEBRTC_VIDEO_CODEC_OK; |
553 } | 564 } |
554 } | 565 } |
555 | 566 |
567 last_input_timestamp_ms_ = | |
568 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; | |
569 frames_in_queue_++; | |
570 | |
571 // Save input image timestamps for later output | |
572 timestamps_.push_back(input_frame.timestamp()); | |
573 render_times_ms_.push_back(input_frame.render_time_ms()); | |
574 frame_rtc_times_ms_.push_back(GetCurrentTimeMs()); | |
575 | |
576 bool key_frame = frame_types->front() != webrtc::kDeltaFrame; | |
magjed_webrtc
2015/10/12 08:22:53
const bool
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
577 bool encode_status = true; | |
magjed_webrtc
2015/10/12 08:22:53
const bool encode_status = EncodeByteBufferOnCodec
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
578 | |
579 encode_status = EncodeByteBufferOnCodecThread(jni, key_frame, input_frame); | |
580 | |
581 current_timestamp_us_ += 1000000 / last_set_fps_; | |
582 | |
583 if (!encode_status || !DeliverPendingOutputs(jni)) { | |
584 ALOGE << "Failed deliver pending outputs."; | |
585 ResetCodecOnCodecThread(); | |
586 return WEBRTC_VIDEO_CODEC_ERROR; | |
587 } | |
588 return WEBRTC_VIDEO_CODEC_OK; | |
589 } | |
590 | |
591 bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread( | |
592 const webrtc::VideoFrame& frame) { | |
593 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
594 | |
595 bool reconfigure_due_to_size = | |
magjed_webrtc
2015/10/12 08:22:53
const bool
perkj_webrtc
2015/10/12 09:16:05
Done.
| |
596 frame.width() != width_ || frame.height() != height_; | |
597 | |
598 if (reconfigure_due_to_size) { | |
599 ALOGD << "Reconfigure encoder due to frame resolution change from " | |
600 << width_ << " x " << height_ << " to " << frame.width() << " x " | |
601 << frame.height(); | |
602 width_ = frame.width(); | |
603 height_ = frame.height(); | |
604 } | |
605 | |
606 if (!reconfigure_due_to_size) | |
607 return true; | |
608 | |
609 ReleaseOnCodecThread(); | |
610 | |
611 return InitEncodeOnCodecThread(width_, height_, 0, 0) == | |
612 WEBRTC_VIDEO_CODEC_OK; | |
613 } | |
614 | |
615 bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni, | |
616 bool key_frame, const webrtc::VideoFrame& frame) { | |
617 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
556 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, | 618 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, |
557 j_dequeue_input_buffer_method_); | 619 j_dequeue_input_buffer_method_); |
558 CHECK_EXCEPTION(jni); | 620 CHECK_EXCEPTION(jni); |
559 if (j_input_buffer_index == -1) { | 621 if (j_input_buffer_index == -1) { |
560 // Video codec falls behind - no input buffer available. | 622 // Video codec falls behind - no input buffer available. |
561 ALOGV("Encoder drop frame - no input buffers available"); | 623 ALOGD <<"Encoder drop frame - no input buffers available"; |
562 frames_dropped_++; | 624 frames_dropped_++; |
563 // Report dropped frame to quality_scaler_. | 625 // Report dropped frame to quality_scaler_. |
564 OnDroppedFrame(); | 626 OnDroppedFrame(); |
565 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. | 627 return true; // TODO(fischman): see webrtc bug 2887. |
566 } | 628 } |
567 if (j_input_buffer_index == -2) { | 629 if (j_input_buffer_index == -2) { |
568 ResetCodec(); | 630 return false; |
569 return WEBRTC_VIDEO_CODEC_ERROR; | |
570 } | 631 } |
571 | 632 |
572 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d", | 633 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d", |
573 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_); | 634 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_); |
574 | 635 |
575 jobject j_input_buffer = input_buffers_[j_input_buffer_index]; | 636 jobject j_input_buffer = input_buffers_[j_input_buffer_index]; |
576 uint8_t* yuv_buffer = | 637 uint8_t* yuv_buffer = |
577 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); | 638 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); |
578 CHECK_EXCEPTION(jni); | 639 CHECK_EXCEPTION(jni); |
579 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; | 640 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; |
580 RTC_CHECK(!libyuv::ConvertFromI420( | 641 RTC_CHECK(!libyuv::ConvertFromI420( |
581 input_frame.buffer(webrtc::kYPlane), input_frame.stride(webrtc::kYPlane), | 642 frame.buffer(webrtc::kYPlane), frame.stride(webrtc::kYPlane), |
582 input_frame.buffer(webrtc::kUPlane), input_frame.stride(webrtc::kUPlane), | 643 frame.buffer(webrtc::kUPlane), frame.stride(webrtc::kUPlane), |
583 input_frame.buffer(webrtc::kVPlane), input_frame.stride(webrtc::kVPlane), | 644 frame.buffer(webrtc::kVPlane), frame.stride(webrtc::kVPlane), |
584 yuv_buffer, width_, width_, height_, encoder_fourcc_)) | 645 yuv_buffer, width_, width_, height_, encoder_fourcc_)) |
585 << "ConvertFromI420 failed"; | 646 << "ConvertFromI420 failed"; |
586 last_input_timestamp_ms_ = current_timestamp_us_ / 1000; | |
587 frames_in_queue_++; | |
588 | 647 |
589 // Save input image timestamps for later output | |
590 timestamps_.push_back(input_frame.timestamp()); | |
591 render_times_ms_.push_back(input_frame.render_time_ms()); | |
592 frame_rtc_times_ms_.push_back(GetCurrentTimeMs()); | |
593 | 648 |
594 bool key_frame = frame_types->front() != webrtc::kDeltaFrame; | |
595 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 649 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
596 j_encode_method_, | 650 j_encode_buffer_method_, |
597 key_frame, | 651 key_frame, |
598 j_input_buffer_index, | 652 j_input_buffer_index, |
599 yuv_size_, | 653 yuv_size_, |
600 current_timestamp_us_); | 654 current_timestamp_us_); |
601 CHECK_EXCEPTION(jni); | 655 CHECK_EXCEPTION(jni); |
602 current_timestamp_us_ += 1000000 / last_set_fps_; | 656 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 } | 657 } |
611 | 658 |
612 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( | 659 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( |
613 webrtc::EncodedImageCallback* callback) { | 660 webrtc::EncodedImageCallback* callback) { |
614 CheckOnCodecThread(); | 661 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
615 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 662 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
616 ScopedLocalRefFrame local_ref_frame(jni); | 663 ScopedLocalRefFrame local_ref_frame(jni); |
617 callback_ = callback; | 664 callback_ = callback; |
618 return WEBRTC_VIDEO_CODEC_OK; | 665 return WEBRTC_VIDEO_CODEC_OK; |
619 } | 666 } |
620 | 667 |
621 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { | 668 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { |
669 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
622 if (!inited_) { | 670 if (!inited_) { |
623 return WEBRTC_VIDEO_CODEC_OK; | 671 return WEBRTC_VIDEO_CODEC_OK; |
624 } | 672 } |
625 CheckOnCodecThread(); | |
626 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 673 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
627 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << | 674 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << |
628 frames_received_ << ". Encoded: " << frames_encoded_ << | 675 frames_received_ << ". Encoded: " << frames_encoded_ << |
629 ". Dropped: " << frames_dropped_; | 676 ". Dropped: " << frames_dropped_; |
630 ScopedLocalRefFrame local_ref_frame(jni); | 677 ScopedLocalRefFrame local_ref_frame(jni); |
631 for (size_t i = 0; i < input_buffers_.size(); ++i) | 678 for (size_t i = 0; i < input_buffers_.size(); ++i) |
632 jni->DeleteGlobalRef(input_buffers_[i]); | 679 jni->DeleteGlobalRef(input_buffers_[i]); |
633 input_buffers_.clear(); | 680 input_buffers_.clear(); |
634 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); | 681 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); |
635 CHECK_EXCEPTION(jni); | 682 CHECK_EXCEPTION(jni); |
636 rtc::MessageQueueManager::Clear(this); | 683 rtc::MessageQueueManager::Clear(this); |
637 inited_ = false; | 684 inited_ = false; |
638 ALOGD << "EncoderReleaseOnCodecThread done."; | 685 ALOGD << "EncoderReleaseOnCodecThread done."; |
639 return WEBRTC_VIDEO_CODEC_OK; | 686 return WEBRTC_VIDEO_CODEC_OK; |
640 } | 687 } |
641 | 688 |
642 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, | 689 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, |
643 uint32_t frame_rate) { | 690 uint32_t frame_rate) { |
644 CheckOnCodecThread(); | 691 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
645 if (last_set_bitrate_kbps_ == new_bit_rate && | 692 if (last_set_bitrate_kbps_ == new_bit_rate && |
646 last_set_fps_ == frame_rate) { | 693 last_set_fps_ == frame_rate) { |
647 return WEBRTC_VIDEO_CODEC_OK; | 694 return WEBRTC_VIDEO_CODEC_OK; |
648 } | 695 } |
649 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 696 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
650 ScopedLocalRefFrame local_ref_frame(jni); | 697 ScopedLocalRefFrame local_ref_frame(jni); |
651 if (new_bit_rate > 0) { | 698 if (new_bit_rate > 0) { |
652 last_set_bitrate_kbps_ = new_bit_rate; | 699 last_set_bitrate_kbps_ = new_bit_rate; |
653 } | 700 } |
654 if (frame_rate > 0) { | 701 if (frame_rate > 0) { |
655 last_set_fps_ = frame_rate; | 702 last_set_fps_ = frame_rate; |
656 } | 703 } |
657 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 704 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
658 j_set_rates_method_, | 705 j_set_rates_method_, |
659 last_set_bitrate_kbps_, | 706 last_set_bitrate_kbps_, |
660 last_set_fps_); | 707 last_set_fps_); |
661 CHECK_EXCEPTION(jni); | 708 CHECK_EXCEPTION(jni); |
662 if (!ret) { | 709 if (!ret) { |
663 ResetCodec(); | 710 ResetCodecOnCodecThread(); |
664 return WEBRTC_VIDEO_CODEC_ERROR; | 711 return WEBRTC_VIDEO_CODEC_ERROR; |
665 } | 712 } |
666 return WEBRTC_VIDEO_CODEC_OK; | 713 return WEBRTC_VIDEO_CODEC_OK; |
667 } | 714 } |
668 | 715 |
669 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( | 716 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( |
670 JNIEnv* jni, | 717 JNIEnv* jni, |
671 jobject j_output_buffer_info) { | 718 jobject j_output_buffer_info) { |
672 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); | 719 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); |
673 } | 720 } |
(...skipping 11 matching lines...) Expand all Loading... | |
685 } | 732 } |
686 | 733 |
687 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( | 734 jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( |
688 JNIEnv* jni, | 735 JNIEnv* jni, |
689 jobject j_output_buffer_info) { | 736 jobject j_output_buffer_info) { |
690 return GetLongField( | 737 return GetLongField( |
691 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); | 738 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); |
692 } | 739 } |
693 | 740 |
694 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { | 741 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { |
742 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
695 while (true) { | 743 while (true) { |
696 jobject j_output_buffer_info = jni->CallObjectMethod( | 744 jobject j_output_buffer_info = jni->CallObjectMethod( |
697 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); | 745 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); |
698 CHECK_EXCEPTION(jni); | 746 CHECK_EXCEPTION(jni); |
699 if (IsNull(jni, j_output_buffer_info)) { | 747 if (IsNull(jni, j_output_buffer_info)) { |
700 break; | 748 break; |
701 } | 749 } |
702 | 750 |
703 int output_buffer_index = | 751 int output_buffer_index = |
704 GetOutputBufferInfoIndex(jni, j_output_buffer_info); | 752 GetOutputBufferInfoIndex(jni, j_output_buffer_info); |
705 if (output_buffer_index == -1) { | 753 if (output_buffer_index == -1) { |
706 ResetCodec(); | 754 ResetCodecOnCodecThread(); |
707 return false; | 755 return false; |
708 } | 756 } |
709 | 757 |
710 // Get key and config frame flags. | 758 // Get key and config frame flags. |
711 jobject j_output_buffer = | 759 jobject j_output_buffer = |
712 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); | 760 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); |
713 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); | 761 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); |
714 | 762 |
715 // Get frame timestamps from a queue - for non config frames only. | 763 // Get frame timestamps from a queue - for non config frames only. |
716 int64_t frame_encoding_time_ms = 0; | 764 int64_t frame_encoding_time_ms = 0; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
817 } | 865 } |
818 scPosition += naluPosition; | 866 scPosition += naluPosition; |
819 scPositions[scPositionsLength++] = scPosition; | 867 scPositions[scPositionsLength++] = scPosition; |
820 scPosition += H264_SC_LENGTH; | 868 scPosition += H264_SC_LENGTH; |
821 } | 869 } |
822 if (scPositionsLength == 0) { | 870 if (scPositionsLength == 0) { |
823 ALOGE << "Start code is not found!"; | 871 ALOGE << "Start code is not found!"; |
824 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] | 872 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] |
825 << " " << image->_buffer[2] << " " << image->_buffer[3] | 873 << " " << image->_buffer[2] << " " << image->_buffer[3] |
826 << " " << image->_buffer[4] << " " << image->_buffer[5]; | 874 << " " << image->_buffer[4] << " " << image->_buffer[5]; |
827 ResetCodec(); | 875 ResetCodecOnCodecThread(); |
828 return false; | 876 return false; |
829 } | 877 } |
830 scPositions[scPositionsLength] = payload_size; | 878 scPositions[scPositionsLength] = payload_size; |
831 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); | 879 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); |
832 for (size_t i = 0; i < scPositionsLength; i++) { | 880 for (size_t i = 0; i < scPositionsLength; i++) { |
833 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; | 881 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; |
834 header.fragmentationLength[i] = | 882 header.fragmentationLength[i] = |
835 scPositions[i + 1] - header.fragmentationOffset[i]; | 883 scPositions[i + 1] - header.fragmentationOffset[i]; |
836 header.fragmentationPlType[i] = 0; | 884 header.fragmentationPlType[i] = 0; |
837 header.fragmentationTimeDiff[i] = 0; | 885 header.fragmentationTimeDiff[i] = 0; |
838 } | 886 } |
839 } | 887 } |
840 | 888 |
841 callback_status = callback_->Encoded(*image, &info, &header); | 889 callback_status = callback_->Encoded(*image, &info, &header); |
842 } | 890 } |
843 | 891 |
844 // Return output buffer back to the encoder. | 892 // Return output buffer back to the encoder. |
845 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 893 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
846 j_release_output_buffer_method_, | 894 j_release_output_buffer_method_, |
847 output_buffer_index); | 895 output_buffer_index); |
848 CHECK_EXCEPTION(jni); | 896 CHECK_EXCEPTION(jni); |
849 if (!success) { | 897 if (!success) { |
850 ResetCodec(); | 898 ResetCodecOnCodecThread(); |
851 return false; | 899 return false; |
852 } | 900 } |
853 | 901 |
854 if (callback_status > 0) { | 902 if (callback_status > 0) { |
855 drop_next_input_frame_ = true; | 903 drop_next_input_frame_ = true; |
856 // Theoretically could handle callback_status<0 here, but unclear what | 904 // Theoretically could handle callback_status<0 here, but unclear what |
857 // that would mean for us. | 905 // that would mean for us. |
858 } | 906 } |
859 } | 907 } |
860 | 908 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 } | 1001 } |
954 | 1002 |
955 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1003 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
956 webrtc::VideoEncoder* encoder) { | 1004 webrtc::VideoEncoder* encoder) { |
957 ALOGD << "Destroy video encoder."; | 1005 ALOGD << "Destroy video encoder."; |
958 delete encoder; | 1006 delete encoder; |
959 } | 1007 } |
960 | 1008 |
961 } // namespace webrtc_jni | 1009 } // namespace webrtc_jni |
962 | 1010 |
OLD | NEW |