Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: talk/app/webrtc/java/jni/androidmediadecoder_jni.cc

Issue 1178943007: Improve Android HW decoder error handling. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Alway clear message Q in Release() Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 private: 92 private:
93 // CHECK-fail if not running on |codec_thread_|. 93 // CHECK-fail if not running on |codec_thread_|.
94 void CheckOnCodecThread(); 94 void CheckOnCodecThread();
95 95
96 int32_t InitDecodeOnCodecThread(); 96 int32_t InitDecodeOnCodecThread();
97 int32_t ReleaseOnCodecThread(); 97 int32_t ReleaseOnCodecThread();
98 int32_t DecodeOnCodecThread(const EncodedImage& inputImage); 98 int32_t DecodeOnCodecThread(const EncodedImage& inputImage);
99 // Deliver any outputs pending in the MediaCodec to our |callback_| and return 99 // Deliver any outputs pending in the MediaCodec to our |callback_| and return
100 // true on success. 100 // true on success.
101 bool DeliverPendingOutputs(JNIEnv* jni, int dequeue_timeout_us); 101 bool DeliverPendingOutputs(JNIEnv* jni, int dequeue_timeout_us);
102 int32_t ProcessHWErrorOnCodecThread();
102 103
103 // Type of video codec. 104 // Type of video codec.
104 VideoCodecType codecType_; 105 VideoCodecType codecType_;
105 106
106 bool key_frame_required_; 107 bool key_frame_required_;
107 bool inited_; 108 bool inited_;
109 bool sw_fallback_required_;
108 bool use_surface_; 110 bool use_surface_;
109 int error_count_;
110 VideoCodec codec_; 111 VideoCodec codec_;
111 VideoFrame decoded_image_; 112 VideoFrame decoded_image_;
112 NativeHandleImpl native_handle_; 113 NativeHandleImpl native_handle_;
113 DecodedImageCallback* callback_; 114 DecodedImageCallback* callback_;
114 int frames_received_; // Number of frames received by decoder. 115 int frames_received_; // Number of frames received by decoder.
115 int frames_decoded_; // Number of frames decoded by decoder. 116 int frames_decoded_; // Number of frames decoded by decoder.
116 int64_t start_time_ms_; // Start time for statistics. 117 int64_t start_time_ms_; // Start time for statistics.
117 int current_frames_; // Number of frames in the current statistics interval. 118 int current_frames_; // Number of frames in the current statistics interval.
118 int current_bytes_; // Encoded bytes in the current statistics interval. 119 int current_bytes_; // Encoded bytes in the current statistics interval.
119 int current_decoding_time_ms_; // Overall decoding time in the current second 120 int current_decoding_time_ms_; // Overall decoding time in the current second
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 std::vector<jobject> input_buffers_; 158 std::vector<jobject> input_buffers_;
158 jobject surface_texture_; 159 jobject surface_texture_;
159 jobject previous_surface_texture_; 160 jobject previous_surface_texture_;
160 }; 161 };
161 162
162 MediaCodecVideoDecoder::MediaCodecVideoDecoder( 163 MediaCodecVideoDecoder::MediaCodecVideoDecoder(
163 JNIEnv* jni, VideoCodecType codecType) : 164 JNIEnv* jni, VideoCodecType codecType) :
164 codecType_(codecType), 165 codecType_(codecType),
165 key_frame_required_(true), 166 key_frame_required_(true),
166 inited_(false), 167 inited_(false),
167 error_count_(0), 168 sw_fallback_required_(false),
168 surface_texture_(NULL), 169 surface_texture_(NULL),
169 previous_surface_texture_(NULL), 170 previous_surface_texture_(NULL),
170 codec_thread_(new Thread()), 171 codec_thread_(new Thread()),
171 j_media_codec_video_decoder_class_( 172 j_media_codec_video_decoder_class_(
172 jni, 173 jni,
173 FindClass(jni, "org/webrtc/MediaCodecVideoDecoder")), 174 FindClass(jni, "org/webrtc/MediaCodecVideoDecoder")),
174 j_media_codec_video_decoder_( 175 j_media_codec_video_decoder_(
175 jni, 176 jni,
176 jni->NewObject(*j_media_codec_video_decoder_class_, 177 jni->NewObject(*j_media_codec_video_decoder_class_,
177 GetMethodID(jni, 178 GetMethodID(jni,
178 *j_media_codec_video_decoder_class_, 179 *j_media_codec_video_decoder_class_,
179 "<init>", 180 "<init>",
180 "()V"))) { 181 "()V"))) {
181 ScopedLocalRefFrame local_ref_frame(jni); 182 ScopedLocalRefFrame local_ref_frame(jni);
182 codec_thread_->SetName("MediaCodecVideoDecoder", NULL); 183 codec_thread_->SetName("MediaCodecVideoDecoder", NULL);
183 CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoDecoder"; 184 CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoDecoder";
184 185
185 j_init_decode_method_ = GetMethodID( 186 j_init_decode_method_ = GetMethodID(
186 jni, *j_media_codec_video_decoder_class_, "initDecode", 187 jni, *j_media_codec_video_decoder_class_, "initDecode",
187 "(Lorg/webrtc/MediaCodecVideoDecoder$VideoCodecType;" 188 "(Lorg/webrtc/MediaCodecVideoDecoder$VideoCodecType;"
188 "IIZZLandroid/opengl/EGLContext;)Z"); 189 "IIZLandroid/opengl/EGLContext;)Z");
189 j_release_method_ = 190 j_release_method_ =
190 GetMethodID(jni, *j_media_codec_video_decoder_class_, "release", "()V"); 191 GetMethodID(jni, *j_media_codec_video_decoder_class_, "release", "()V");
191 j_dequeue_input_buffer_method_ = GetMethodID( 192 j_dequeue_input_buffer_method_ = GetMethodID(
192 jni, *j_media_codec_video_decoder_class_, "dequeueInputBuffer", "()I"); 193 jni, *j_media_codec_video_decoder_class_, "dequeueInputBuffer", "()I");
193 j_queue_input_buffer_method_ = GetMethodID( 194 j_queue_input_buffer_method_ = GetMethodID(
194 jni, *j_media_codec_video_decoder_class_, "queueInputBuffer", "(IIJ)Z"); 195 jni, *j_media_codec_video_decoder_class_, "queueInputBuffer", "(IIJ)Z");
195 j_dequeue_output_buffer_method_ = GetMethodID( 196 j_dequeue_output_buffer_method_ = GetMethodID(
196 jni, *j_media_codec_video_decoder_class_, "dequeueOutputBuffer", 197 jni, *j_media_codec_video_decoder_class_, "dequeueOutputBuffer",
197 "(I)Lorg/webrtc/MediaCodecVideoDecoder$DecoderOutputBufferInfo;"); 198 "(I)Lorg/webrtc/MediaCodecVideoDecoder$DecoderOutputBufferInfo;");
198 j_release_output_buffer_method_ = GetMethodID( 199 j_release_output_buffer_method_ = GetMethodID(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 if (previous_surface_texture_ != NULL) { 249 if (previous_surface_texture_ != NULL) {
249 jni->DeleteGlobalRef(previous_surface_texture_); 250 jni->DeleteGlobalRef(previous_surface_texture_);
250 } 251 }
251 if (surface_texture_ != NULL) { 252 if (surface_texture_ != NULL) {
252 jni->DeleteGlobalRef(surface_texture_); 253 jni->DeleteGlobalRef(surface_texture_);
253 } 254 }
254 } 255 }
255 256
256 int32_t MediaCodecVideoDecoder::InitDecode(const VideoCodec* inst, 257 int32_t MediaCodecVideoDecoder::InitDecode(const VideoCodec* inst,
257 int32_t numberOfCores) { 258 int32_t numberOfCores) {
259 ALOGD("InitDecode.");
258 if (inst == NULL) { 260 if (inst == NULL) {
259 ALOGE("NULL VideoCodec instance"); 261 ALOGE("NULL VideoCodec instance");
260 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; 262 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
261 } 263 }
262 // Factory should guard against other codecs being used with us. 264 // Factory should guard against other codecs being used with us.
263 CHECK(inst->codecType == codecType_) << "Unsupported codec " << 265 CHECK(inst->codecType == codecType_) << "Unsupported codec " <<
264 inst->codecType << " for " << codecType_; 266 inst->codecType << " for " << codecType_;
265 267
266 int ret_val = Release(); 268 if (sw_fallback_required_) {
267 if (ret_val < 0) { 269 ALOGE("InitDecode() - fallback to SW decoder");
268 return ret_val; 270 return WEBRTC_VIDEO_CODEC_OK;
269 } 271 }
270 // Save VideoCodec instance for later. 272 // Save VideoCodec instance for later.
271 if (&codec_ != inst) { 273 if (&codec_ != inst) {
272 codec_ = *inst; 274 codec_ = *inst;
273 } 275 }
274 codec_.maxFramerate = (codec_.maxFramerate >= 1) ? codec_.maxFramerate : 1; 276 codec_.maxFramerate = (codec_.maxFramerate >= 1) ? codec_.maxFramerate : 1;
275 277
276 // Always start with a complete key frame.
277 key_frame_required_ = true;
278 frames_received_ = 0;
279 frames_decoded_ = 0;
280
281 // Call Java init. 278 // Call Java init.
282 return codec_thread_->Invoke<int32_t>( 279 return codec_thread_->Invoke<int32_t>(
283 Bind(&MediaCodecVideoDecoder::InitDecodeOnCodecThread, this)); 280 Bind(&MediaCodecVideoDecoder::InitDecodeOnCodecThread, this));
284 } 281 }
285 282
286 int32_t MediaCodecVideoDecoder::InitDecodeOnCodecThread() { 283 int32_t MediaCodecVideoDecoder::InitDecodeOnCodecThread() {
287 CheckOnCodecThread(); 284 CheckOnCodecThread();
288 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 285 JNIEnv* jni = AttachCurrentThreadIfNeeded();
289 ScopedLocalRefFrame local_ref_frame(jni); 286 ScopedLocalRefFrame local_ref_frame(jni);
290 ALOGD("InitDecodeOnCodecThread Type: %d. %d x %d. Fps: %d. Errors: %d", 287 ALOGD("InitDecodeOnCodecThread Type: %d. %d x %d. Fps: %d.",
291 (int)codecType_, codec_.width, codec_.height, 288 (int)codecType_, codec_.width, codec_.height,
292 codec_.maxFramerate, error_count_); 289 codec_.maxFramerate);
293 bool use_sw_codec = false; 290
294 if (error_count_ > 1) { 291 // Release previous codec first if it was allocated before.
295 // If more than one critical errors happen for HW codec, switch to SW codec. 292 int ret_val = ReleaseOnCodecThread();
296 use_sw_codec = true; 293 if (ret_val < 0) {
294 ALOGE("Release failure: %d - fallback to SW codec", ret_val);
295 sw_fallback_required_ = true;
296 return WEBRTC_VIDEO_CODEC_ERROR;
297 } 297 }
298 298
299 // Always start with a complete key frame.
300 key_frame_required_ = true;
301 frames_received_ = 0;
302 frames_decoded_ = 0;
303
299 jobject j_video_codec_enum = JavaEnumFromIndex( 304 jobject j_video_codec_enum = JavaEnumFromIndex(
300 jni, "MediaCodecVideoDecoder$VideoCodecType", codecType_); 305 jni, "MediaCodecVideoDecoder$VideoCodecType", codecType_);
301 bool success = jni->CallBooleanMethod( 306 bool success = jni->CallBooleanMethod(
302 *j_media_codec_video_decoder_, 307 *j_media_codec_video_decoder_,
303 j_init_decode_method_, 308 j_init_decode_method_,
304 j_video_codec_enum, 309 j_video_codec_enum,
305 codec_.width, 310 codec_.width,
306 codec_.height, 311 codec_.height,
307 use_sw_codec,
308 use_surface_, 312 use_surface_,
309 MediaCodecVideoDecoderFactory::render_egl_context_); 313 MediaCodecVideoDecoderFactory::render_egl_context_);
310 CHECK_EXCEPTION(jni); 314 if (CheckException(jni) || !success) {
311 if (!success) { 315 ALOGE("Codec initialization error - fallback to SW codec.");
316 sw_fallback_required_ = true;
312 return WEBRTC_VIDEO_CODEC_ERROR; 317 return WEBRTC_VIDEO_CODEC_ERROR;
313 } 318 }
314 inited_ = true; 319 inited_ = true;
315 320
316 switch (codecType_) { 321 switch (codecType_) {
317 case kVideoCodecVP8: 322 case kVideoCodecVP8:
318 max_pending_frames_ = kMaxPendingFramesVp8; 323 max_pending_frames_ = kMaxPendingFramesVp8;
319 break; 324 break;
320 case kVideoCodecH264: 325 case kVideoCodecH264:
321 max_pending_frames_ = kMaxPendingFramesH264; 326 max_pending_frames_ = kMaxPendingFramesH264;
(...skipping 11 matching lines...) Expand all
333 ntp_times_ms_.clear(); 338 ntp_times_ms_.clear();
334 frame_rtc_times_ms_.clear(); 339 frame_rtc_times_ms_.clear();
335 340
336 jobjectArray input_buffers = (jobjectArray)GetObjectField( 341 jobjectArray input_buffers = (jobjectArray)GetObjectField(
337 jni, *j_media_codec_video_decoder_, j_input_buffers_field_); 342 jni, *j_media_codec_video_decoder_, j_input_buffers_field_);
338 size_t num_input_buffers = jni->GetArrayLength(input_buffers); 343 size_t num_input_buffers = jni->GetArrayLength(input_buffers);
339 input_buffers_.resize(num_input_buffers); 344 input_buffers_.resize(num_input_buffers);
340 for (size_t i = 0; i < num_input_buffers; ++i) { 345 for (size_t i = 0; i < num_input_buffers; ++i) {
341 input_buffers_[i] = 346 input_buffers_[i] =
342 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); 347 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i));
343 CHECK_EXCEPTION(jni); 348 if (CheckException(jni)) {
349 ALOGE("NewGlobalRef error - fallback to SW codec.");
350 sw_fallback_required_ = true;
351 return WEBRTC_VIDEO_CODEC_ERROR;
352 }
344 } 353 }
345 354
346 if (use_surface_) { 355 if (use_surface_) {
347 jobject surface_texture = GetObjectField( 356 jobject surface_texture = GetObjectField(
348 jni, *j_media_codec_video_decoder_, j_surface_texture_field_); 357 jni, *j_media_codec_video_decoder_, j_surface_texture_field_);
349 if (previous_surface_texture_ != NULL) { 358 if (previous_surface_texture_ != NULL) {
350 jni->DeleteGlobalRef(previous_surface_texture_); 359 jni->DeleteGlobalRef(previous_surface_texture_);
351 } 360 }
352 previous_surface_texture_ = surface_texture_; 361 previous_surface_texture_ = surface_texture_;
353 surface_texture_ = jni->NewGlobalRef(surface_texture); 362 surface_texture_ = jni->NewGlobalRef(surface_texture);
(...skipping 15 matching lines...) Expand all
369 } 378 }
370 CheckOnCodecThread(); 379 CheckOnCodecThread();
371 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 380 JNIEnv* jni = AttachCurrentThreadIfNeeded();
372 ALOGD("DecoderReleaseOnCodecThread: Frames received: %d.", frames_received_); 381 ALOGD("DecoderReleaseOnCodecThread: Frames received: %d.", frames_received_);
373 ScopedLocalRefFrame local_ref_frame(jni); 382 ScopedLocalRefFrame local_ref_frame(jni);
374 for (size_t i = 0; i < input_buffers_.size(); i++) { 383 for (size_t i = 0; i < input_buffers_.size(); i++) {
375 jni->DeleteGlobalRef(input_buffers_[i]); 384 jni->DeleteGlobalRef(input_buffers_[i]);
376 } 385 }
377 input_buffers_.clear(); 386 input_buffers_.clear();
378 jni->CallVoidMethod(*j_media_codec_video_decoder_, j_release_method_); 387 jni->CallVoidMethod(*j_media_codec_video_decoder_, j_release_method_);
379 CHECK_EXCEPTION(jni); 388 inited_ = false;
380 rtc::MessageQueueManager::Clear(this); 389 rtc::MessageQueueManager::Clear(this);
381 inited_ = false; 390 if (CheckException(jni)) {
391 ALOGE("Decoder release exception");
392 return WEBRTC_VIDEO_CODEC_ERROR;
393 }
382 return WEBRTC_VIDEO_CODEC_OK; 394 return WEBRTC_VIDEO_CODEC_OK;
383 } 395 }
384 396
385 void MediaCodecVideoDecoder::CheckOnCodecThread() { 397 void MediaCodecVideoDecoder::CheckOnCodecThread() {
386 CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread()) 398 CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread())
387 << "Running on wrong thread!"; 399 << "Running on wrong thread!";
388 } 400 }
389 401
402 int32_t MediaCodecVideoDecoder::ProcessHWErrorOnCodecThread() {
403 CheckOnCodecThread();
404 int ret_val = ReleaseOnCodecThread();
405 if (ret_val < 0) {
406 ALOGE("ProcessHWError: Release failure");
407 }
408 if (codecType_ == kVideoCodecH264) {
409 // For now there is no SW H.264 which can be used as fallback codec.
410 // So try to restart hw codec for now.
411 ret_val = InitDecodeOnCodecThread();
412 ALOGE("Reset H.264 codec done. Status: %d", ret_val);
413 if (ret_val == WEBRTC_VIDEO_CODEC_OK) {
414 // H.264 codec was succesfully reset - return regular error code.
415 return WEBRTC_VIDEO_CODEC_ERROR;
416 } else {
417 // Fail to restart H.264 codec - return error code which should stop the
418 // call.
419 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
420 }
421 } else {
422 sw_fallback_required_ = true;
423 ALOGE("Return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE");
424 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
425 }
426 }
427
390 int32_t MediaCodecVideoDecoder::Decode( 428 int32_t MediaCodecVideoDecoder::Decode(
391 const EncodedImage& inputImage, 429 const EncodedImage& inputImage,
392 bool missingFrames, 430 bool missingFrames,
393 const RTPFragmentationHeader* fragmentation, 431 const RTPFragmentationHeader* fragmentation,
394 const CodecSpecificInfo* codecSpecificInfo, 432 const CodecSpecificInfo* codecSpecificInfo,
395 int64_t renderTimeMs) { 433 int64_t renderTimeMs) {
396 if (!inited_) { 434 if (sw_fallback_required_) {
397 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 435 ALOGE("Decode() - fallback to SW codec");
436 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
398 } 437 }
399 if (callback_ == NULL) { 438 if (callback_ == NULL) {
439 ALOGE("Decode() - callback_ is NULL");
400 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 440 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
401 } 441 }
402 if (inputImage._buffer == NULL && inputImage._length > 0) { 442 if (inputImage._buffer == NULL && inputImage._length > 0) {
443 ALOGE("Decode() - inputImage is incorrect");
403 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; 444 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
404 } 445 }
446 if (!inited_) {
447 ALOGE("Decode() - decoder is not initialized");
448 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
449 }
450
405 // Check if encoded frame dimension has changed. 451 // Check if encoded frame dimension has changed.
406 if ((inputImage._encodedWidth * inputImage._encodedHeight > 0) && 452 if ((inputImage._encodedWidth * inputImage._encodedHeight > 0) &&
407 (inputImage._encodedWidth != codec_.width || 453 (inputImage._encodedWidth != codec_.width ||
408 inputImage._encodedHeight != codec_.height)) { 454 inputImage._encodedHeight != codec_.height)) {
409 codec_.width = inputImage._encodedWidth; 455 codec_.width = inputImage._encodedWidth;
410 codec_.height = inputImage._encodedHeight; 456 codec_.height = inputImage._encodedHeight;
411 InitDecode(&codec_, 1); 457 int32_t ret = InitDecode(&codec_, 1);
458 if (ret < 0) {
459 ALOGE("InitDecode failure: %d - fallback to SW codec", ret);
460 sw_fallback_required_ = true;
461 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
462 }
412 } 463 }
413 464
414 // Always start with a complete key frame. 465 // Always start with a complete key frame.
415 if (key_frame_required_) { 466 if (key_frame_required_) {
416 if (inputImage._frameType != webrtc::kKeyFrame) { 467 if (inputImage._frameType != webrtc::kKeyFrame) {
417 ALOGE("Key frame is required"); 468 ALOGE("Decode() - key frame is required");
418 return WEBRTC_VIDEO_CODEC_ERROR; 469 return WEBRTC_VIDEO_CODEC_ERROR;
419 } 470 }
420 if (!inputImage._completeFrame) { 471 if (!inputImage._completeFrame) {
421 ALOGE("Complete frame is required"); 472 ALOGE("Decode() - complete frame is required");
422 return WEBRTC_VIDEO_CODEC_ERROR; 473 return WEBRTC_VIDEO_CODEC_ERROR;
423 } 474 }
424 key_frame_required_ = false; 475 key_frame_required_ = false;
425 } 476 }
426 if (inputImage._length == 0) { 477 if (inputImage._length == 0) {
427 return WEBRTC_VIDEO_CODEC_ERROR; 478 return WEBRTC_VIDEO_CODEC_ERROR;
428 } 479 }
429 480
430 return codec_thread_->Invoke<int32_t>(Bind( 481 return codec_thread_->Invoke<int32_t>(Bind(
431 &MediaCodecVideoDecoder::DecodeOnCodecThread, this, inputImage)); 482 &MediaCodecVideoDecoder::DecodeOnCodecThread, this, inputImage));
432 } 483 }
433 484
434 int32_t MediaCodecVideoDecoder::DecodeOnCodecThread( 485 int32_t MediaCodecVideoDecoder::DecodeOnCodecThread(
435 const EncodedImage& inputImage) { 486 const EncodedImage& inputImage) {
436 CheckOnCodecThread(); 487 CheckOnCodecThread();
437 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 488 JNIEnv* jni = AttachCurrentThreadIfNeeded();
438 ScopedLocalRefFrame local_ref_frame(jni); 489 ScopedLocalRefFrame local_ref_frame(jni);
439 490
440 // Try to drain the decoder and wait until output is not too 491 // Try to drain the decoder and wait until output is not too
441 // much behind the input. 492 // much behind the input.
442 if (frames_received_ > frames_decoded_ + max_pending_frames_) { 493 if (frames_received_ > frames_decoded_ + max_pending_frames_) {
443 ALOGV("Received: %d. Decoded: %d. Wait for output...", 494 ALOGV("Received: %d. Decoded: %d. Wait for output...",
444 frames_received_, frames_decoded_); 495 frames_received_, frames_decoded_);
445 if (!DeliverPendingOutputs(jni, kMediaCodecTimeoutMs * 1000)) { 496 if (!DeliverPendingOutputs(jni, kMediaCodecTimeoutMs * 1000)) {
446 error_count_++; 497 ALOGE("DeliverPendingOutputs error");
447 Reset(); 498 return ProcessHWErrorOnCodecThread();
448 return WEBRTC_VIDEO_CODEC_ERROR;
449 } 499 }
450 if (frames_received_ > frames_decoded_ + max_pending_frames_) { 500 if (frames_received_ > frames_decoded_ + max_pending_frames_) {
451 ALOGE("Output buffer dequeue timeout"); 501 ALOGE("Output buffer dequeue timeout");
452 error_count_++; 502 return ProcessHWErrorOnCodecThread();
453 Reset();
454 return WEBRTC_VIDEO_CODEC_ERROR;
455 } 503 }
456 } 504 }
457 505
458 // Get input buffer. 506 // Get input buffer.
459 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_decoder_, 507 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_decoder_,
460 j_dequeue_input_buffer_method_); 508 j_dequeue_input_buffer_method_);
461 CHECK_EXCEPTION(jni); 509 if (CheckException(jni) || j_input_buffer_index < 0) {
462 if (j_input_buffer_index < 0) {
463 ALOGE("dequeueInputBuffer error"); 510 ALOGE("dequeueInputBuffer error");
464 error_count_++; 511 return ProcessHWErrorOnCodecThread();
465 Reset();
466 return WEBRTC_VIDEO_CODEC_ERROR;
467 } 512 }
468 513
469 // Copy encoded data to Java ByteBuffer. 514 // Copy encoded data to Java ByteBuffer.
470 jobject j_input_buffer = input_buffers_[j_input_buffer_index]; 515 jobject j_input_buffer = input_buffers_[j_input_buffer_index];
471 uint8* buffer = 516 uint8* buffer =
472 reinterpret_cast<uint8*>(jni->GetDirectBufferAddress(j_input_buffer)); 517 reinterpret_cast<uint8*>(jni->GetDirectBufferAddress(j_input_buffer));
473 CHECK(buffer) << "Indirect buffer??"; 518 CHECK(buffer) << "Indirect buffer??";
474 int64 buffer_capacity = jni->GetDirectBufferCapacity(j_input_buffer); 519 int64 buffer_capacity = jni->GetDirectBufferCapacity(j_input_buffer);
475 CHECK_EXCEPTION(jni); 520 if (CheckException(jni) || buffer_capacity < inputImage._length) {
476 if (buffer_capacity < inputImage._length) {
477 ALOGE("Input frame size %d is bigger than buffer size %d.", 521 ALOGE("Input frame size %d is bigger than buffer size %d.",
478 inputImage._length, buffer_capacity); 522 inputImage._length, buffer_capacity);
479 error_count_++; 523 return ProcessHWErrorOnCodecThread();
480 Reset();
481 return WEBRTC_VIDEO_CODEC_ERROR;
482 } 524 }
483 jlong timestamp_us = (frames_received_ * 1000000) / codec_.maxFramerate; 525 jlong timestamp_us = (frames_received_ * 1000000) / codec_.maxFramerate;
484 ALOGV("Decoder frame in # %d. Type: %d. Buffer # %d. TS: %lld. Size: %d", 526 ALOGV("Decoder frame in # %d. Type: %d. Buffer # %d. TS: %lld. Size: %d",
485 frames_received_, inputImage._frameType, j_input_buffer_index, 527 frames_received_, inputImage._frameType, j_input_buffer_index,
486 timestamp_us / 1000, inputImage._length); 528 timestamp_us / 1000, inputImage._length);
487 memcpy(buffer, inputImage._buffer, inputImage._length); 529 memcpy(buffer, inputImage._buffer, inputImage._length);
488 530
489 // Save input image timestamps for later output. 531 // Save input image timestamps for later output.
490 frames_received_++; 532 frames_received_++;
491 current_bytes_ += inputImage._length; 533 current_bytes_ += inputImage._length;
492 timestamps_.push_back(inputImage._timeStamp); 534 timestamps_.push_back(inputImage._timeStamp);
493 ntp_times_ms_.push_back(inputImage.ntp_time_ms_); 535 ntp_times_ms_.push_back(inputImage.ntp_time_ms_);
494 frame_rtc_times_ms_.push_back(GetCurrentTimeMs()); 536 frame_rtc_times_ms_.push_back(GetCurrentTimeMs());
495 537
496 // Feed input to decoder. 538 // Feed input to decoder.
497 bool success = jni->CallBooleanMethod(*j_media_codec_video_decoder_, 539 bool success = jni->CallBooleanMethod(*j_media_codec_video_decoder_,
498 j_queue_input_buffer_method_, 540 j_queue_input_buffer_method_,
499 j_input_buffer_index, 541 j_input_buffer_index,
500 inputImage._length, 542 inputImage._length,
501 timestamp_us); 543 timestamp_us);
502 CHECK_EXCEPTION(jni); 544 if (CheckException(jni) || !success) {
503 if (!success) {
504 ALOGE("queueInputBuffer error"); 545 ALOGE("queueInputBuffer error");
505 error_count_++; 546 return ProcessHWErrorOnCodecThread();
506 Reset();
507 return WEBRTC_VIDEO_CODEC_ERROR;
508 } 547 }
509 548
510 // Try to drain the decoder 549 // Try to drain the decoder
511 if (!DeliverPendingOutputs(jni, 0)) { 550 if (!DeliverPendingOutputs(jni, 0)) {
512 ALOGE("DeliverPendingOutputs error"); 551 ALOGE("DeliverPendingOutputs error");
513 error_count_++; 552 return ProcessHWErrorOnCodecThread();
514 Reset();
515 return WEBRTC_VIDEO_CODEC_ERROR;
516 } 553 }
517 554
518 return WEBRTC_VIDEO_CODEC_OK; 555 return WEBRTC_VIDEO_CODEC_OK;
519 } 556 }
520 557
521 bool MediaCodecVideoDecoder::DeliverPendingOutputs( 558 bool MediaCodecVideoDecoder::DeliverPendingOutputs(
522 JNIEnv* jni, int dequeue_timeout_us) { 559 JNIEnv* jni, int dequeue_timeout_us) {
523 if (frames_received_ <= frames_decoded_) { 560 if (frames_received_ <= frames_decoded_) {
524 // No need to query for output buffers - decoder is drained. 561 // No need to query for output buffers - decoder is drained.
525 return true; 562 return true;
526 } 563 }
527 // Get decoder output. 564 // Get decoder output.
528 jobject j_decoder_output_buffer_info = jni->CallObjectMethod( 565 jobject j_decoder_output_buffer_info = jni->CallObjectMethod(
529 *j_media_codec_video_decoder_, 566 *j_media_codec_video_decoder_,
530 j_dequeue_output_buffer_method_, 567 j_dequeue_output_buffer_method_,
531 dequeue_timeout_us); 568 dequeue_timeout_us);
532 569 if (CheckException(jni)) {
533 CHECK_EXCEPTION(jni); 570 return false;
571 }
534 if (IsNull(jni, j_decoder_output_buffer_info)) { 572 if (IsNull(jni, j_decoder_output_buffer_info)) {
535 return true; 573 return true;
536 } 574 }
537 575
538 // Extract output buffer info from Java DecoderOutputBufferInfo. 576 // Extract output buffer info from Java DecoderOutputBufferInfo.
539 int output_buffer_index = 577 int output_buffer_index =
540 GetIntField(jni, j_decoder_output_buffer_info, j_info_index_field_); 578 GetIntField(jni, j_decoder_output_buffer_info, j_info_index_field_);
541 if (output_buffer_index < 0) { 579 if (output_buffer_index < 0) {
542 ALOGE("dequeueOutputBuffer error : %d", output_buffer_index); 580 ALOGE("dequeueOutputBuffer error : %d", output_buffer_index);
543 return false; 581 return false;
544 } 582 }
545 int output_buffer_offset = 583 int output_buffer_offset =
546 GetIntField(jni, j_decoder_output_buffer_info, j_info_offset_field_); 584 GetIntField(jni, j_decoder_output_buffer_info, j_info_offset_field_);
547 int output_buffer_size = 585 int output_buffer_size =
548 GetIntField(jni, j_decoder_output_buffer_info, j_info_size_field_); 586 GetIntField(jni, j_decoder_output_buffer_info, j_info_size_field_);
549 long output_timestamps_ms = GetLongField(jni, j_decoder_output_buffer_info, 587 long output_timestamps_ms = GetLongField(jni, j_decoder_output_buffer_info,
550 j_info_presentation_timestamp_us_field_) / 1000; 588 j_info_presentation_timestamp_us_field_) / 1000;
551 589 if (CheckException(jni)) {
552 CHECK_EXCEPTION(jni); 590 return false;
591 }
553 592
554 // Get decoded video frame properties. 593 // Get decoded video frame properties.
555 int color_format = GetIntField(jni, *j_media_codec_video_decoder_, 594 int color_format = GetIntField(jni, *j_media_codec_video_decoder_,
556 j_color_format_field_); 595 j_color_format_field_);
557 int width = GetIntField(jni, *j_media_codec_video_decoder_, j_width_field_); 596 int width = GetIntField(jni, *j_media_codec_video_decoder_, j_width_field_);
558 int height = GetIntField(jni, *j_media_codec_video_decoder_, j_height_field_); 597 int height = GetIntField(jni, *j_media_codec_video_decoder_, j_height_field_);
559 int stride = GetIntField(jni, *j_media_codec_video_decoder_, j_stride_field_); 598 int stride = GetIntField(jni, *j_media_codec_video_decoder_, j_stride_field_);
560 int slice_height = GetIntField(jni, *j_media_codec_video_decoder_, 599 int slice_height = GetIntField(jni, *j_media_codec_video_decoder_,
561 j_slice_height_field_); 600 j_slice_height_field_);
562 int texture_id = GetIntField(jni, *j_media_codec_video_decoder_, 601 int texture_id = GetIntField(jni, *j_media_codec_video_decoder_,
563 j_textureID_field_); 602 j_textureID_field_);
564 603
565 // Extract data from Java ByteBuffer and create output yuv420 frame - 604 // Extract data from Java ByteBuffer and create output yuv420 frame -
566 // for non surface decoding only. 605 // for non surface decoding only.
567 if (!use_surface_) { 606 if (!use_surface_) {
568 if (output_buffer_size < width * height * 3 / 2) { 607 if (output_buffer_size < width * height * 3 / 2) {
569 ALOGE("Insufficient output buffer size: %d", output_buffer_size); 608 ALOGE("Insufficient output buffer size: %d", output_buffer_size);
570 return false; 609 return false;
571 } 610 }
572 jobjectArray output_buffers = reinterpret_cast<jobjectArray>(GetObjectField( 611 jobjectArray output_buffers = reinterpret_cast<jobjectArray>(GetObjectField(
573 jni, *j_media_codec_video_decoder_, j_output_buffers_field_)); 612 jni, *j_media_codec_video_decoder_, j_output_buffers_field_));
574 jobject output_buffer = 613 jobject output_buffer =
575 jni->GetObjectArrayElement(output_buffers, output_buffer_index); 614 jni->GetObjectArrayElement(output_buffers, output_buffer_index);
576 uint8_t* payload = reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress( 615 uint8_t* payload = reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(
577 output_buffer)); 616 output_buffer));
578 CHECK_EXCEPTION(jni); 617 if (CheckException(jni)) {
618 return false;
619 }
579 payload += output_buffer_offset; 620 payload += output_buffer_offset;
580 621
581 // Create yuv420 frame. 622 // Create yuv420 frame.
582 if (color_format == COLOR_FormatYUV420Planar) { 623 if (color_format == COLOR_FormatYUV420Planar) {
583 decoded_image_.CreateFrame( 624 decoded_image_.CreateFrame(
584 payload, 625 payload,
585 payload + (stride * slice_height), 626 payload + (stride * slice_height),
586 payload + (5 * stride * slice_height / 4), 627 payload + (5 * stride * slice_height / 4),
587 width, height, 628 width, height,
588 stride, stride / 2, stride / 2); 629 stride, stride / 2, stride / 2);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 ALOGV("Decoder frame out # %d. %d x %d. %d x %d. Color: 0x%x. TS: %ld." 661 ALOGV("Decoder frame out # %d. %d x %d. %d x %d. Color: 0x%x. TS: %ld."
621 " DecTime: %lld", frames_decoded_, width, height, stride, slice_height, 662 " DecTime: %lld", frames_decoded_, width, height, stride, slice_height,
622 color_format, output_timestamps_ms, frame_decoding_time_ms); 663 color_format, output_timestamps_ms, frame_decoding_time_ms);
623 664
624 // Return output buffer back to codec. 665 // Return output buffer back to codec.
625 bool success = jni->CallBooleanMethod( 666 bool success = jni->CallBooleanMethod(
626 *j_media_codec_video_decoder_, 667 *j_media_codec_video_decoder_,
627 j_release_output_buffer_method_, 668 j_release_output_buffer_method_,
628 output_buffer_index, 669 output_buffer_index,
629 use_surface_); 670 use_surface_);
630 CHECK_EXCEPTION(jni); 671 if (CheckException(jni) || !success) {
631 if (!success) {
632 ALOGE("releaseOutputBuffer error"); 672 ALOGE("releaseOutputBuffer error");
633 return false; 673 return false;
634 } 674 }
635 675
636 // Calculate and print decoding statistics - every 3 seconds. 676 // Calculate and print decoding statistics - every 3 seconds.
637 frames_decoded_++; 677 frames_decoded_++;
638 current_frames_++; 678 current_frames_++;
639 current_decoding_time_ms_ += frame_decoding_time_ms; 679 current_decoding_time_ms_ += frame_decoding_time_ms;
640 int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_; 680 int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_;
641 if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs && 681 if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs &&
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 if (!inited_) { 731 if (!inited_) {
692 return; 732 return;
693 } 733 }
694 // We only ever send one message to |this| directly (not through a Bind()'d 734 // We only ever send one message to |this| directly (not through a Bind()'d
695 // functor), so expect no ID/data. 735 // functor), so expect no ID/data.
696 CHECK(!msg->message_id) << "Unexpected message!"; 736 CHECK(!msg->message_id) << "Unexpected message!";
697 CHECK(!msg->pdata) << "Unexpected message!"; 737 CHECK(!msg->pdata) << "Unexpected message!";
698 CheckOnCodecThread(); 738 CheckOnCodecThread();
699 739
700 if (!DeliverPendingOutputs(jni, 0)) { 740 if (!DeliverPendingOutputs(jni, 0)) {
701 error_count_++; 741 ALOGE("OnMessage: DeliverPendingOutputs error");
702 Reset(); 742 ProcessHWErrorOnCodecThread();
743 return;
703 } 744 }
704 codec_thread_->PostDelayed(kMediaCodecPollMs, this); 745 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
705 } 746 }
706 747
707 int MediaCodecVideoDecoderFactory::SetAndroidObjects(JNIEnv* jni, 748 int MediaCodecVideoDecoderFactory::SetAndroidObjects(JNIEnv* jni,
708 jobject render_egl_context) { 749 jobject render_egl_context) {
709 ALOGD("SetAndroidObjects for surface decoding."); 750 ALOGD("SetAndroidObjects for surface decoding.");
710 if (render_egl_context_) { 751 if (render_egl_context_) {
711 jni->DeleteGlobalRef(render_egl_context_); 752 jni->DeleteGlobalRef(render_egl_context_);
712 } 753 }
713 if (IsNull(jni, render_egl_context)) { 754 if (IsNull(jni, render_egl_context)) {
714 render_egl_context_ = NULL; 755 render_egl_context_ = NULL;
715 } else { 756 } else {
716 render_egl_context_ = jni->NewGlobalRef(render_egl_context); 757 render_egl_context_ = jni->NewGlobalRef(render_egl_context);
717 CHECK_EXCEPTION(jni) << "error calling NewGlobalRef for EGL Context."; 758 if (CheckException(jni)) {
718 jclass j_egl_context_class = FindClass(jni, "android/opengl/EGLContext"); 759 ALOGE("error calling NewGlobalRef for EGL Context.");
719 if (!jni->IsInstanceOf(render_egl_context_, j_egl_context_class)) {
720 ALOGE("Wrong EGL Context.");
721 jni->DeleteGlobalRef(render_egl_context_);
722 render_egl_context_ = NULL; 760 render_egl_context_ = NULL;
761 } else {
762 jclass j_egl_context_class = FindClass(jni, "android/opengl/EGLContext");
763 if (!jni->IsInstanceOf(render_egl_context_, j_egl_context_class)) {
764 ALOGE("Wrong EGL Context.");
765 jni->DeleteGlobalRef(render_egl_context_);
766 render_egl_context_ = NULL;
767 }
723 } 768 }
724 } 769 }
725 if (render_egl_context_ == NULL) { 770 if (render_egl_context_ == NULL) {
726 ALOGD("NULL VideoDecoder EGL context - HW surface decoding is disabled."); 771 ALOGD("NULL VideoDecoder EGL context - HW surface decoding is disabled.");
727 } 772 }
728 return 0; 773 return 0;
729 } 774 }
730 775
731 MediaCodecVideoDecoderFactory::MediaCodecVideoDecoderFactory() { 776 MediaCodecVideoDecoderFactory::MediaCodecVideoDecoderFactory() {
732 JNIEnv* jni = AttachCurrentThreadIfNeeded(); 777 JNIEnv* jni = AttachCurrentThreadIfNeeded();
733 ScopedLocalRefFrame local_ref_frame(jni); 778 ScopedLocalRefFrame local_ref_frame(jni);
734 jclass j_decoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoDecoder"); 779 jclass j_decoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoDecoder");
735 supported_codec_types_.clear(); 780 supported_codec_types_.clear();
736 781
737 bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( 782 bool is_vp8_hw_supported = jni->CallStaticBooleanMethod(
738 j_decoder_class, 783 j_decoder_class,
739 GetStaticMethodID(jni, j_decoder_class, "isVp8HwSupported", "()Z")); 784 GetStaticMethodID(jni, j_decoder_class, "isVp8HwSupported", "()Z"));
740 CHECK_EXCEPTION(jni); 785 if (CheckException(jni)) {
786 is_vp8_hw_supported = false;
787 }
741 if (is_vp8_hw_supported) { 788 if (is_vp8_hw_supported) {
742 ALOGD("VP8 HW Decoder supported."); 789 ALOGD("VP8 HW Decoder supported.");
743 supported_codec_types_.push_back(kVideoCodecVP8); 790 supported_codec_types_.push_back(kVideoCodecVP8);
744 } 791 }
745 792
746 bool is_h264_hw_supported = jni->CallStaticBooleanMethod( 793 bool is_h264_hw_supported = jni->CallStaticBooleanMethod(
747 j_decoder_class, 794 j_decoder_class,
748 GetStaticMethodID(jni, j_decoder_class, "isH264HwSupported", "()Z")); 795 GetStaticMethodID(jni, j_decoder_class, "isH264HwSupported", "()Z"));
749 CHECK_EXCEPTION(jni); 796 if (CheckException(jni)) {
797 is_h264_hw_supported = false;
798 }
750 if (is_h264_hw_supported) { 799 if (is_h264_hw_supported) {
751 ALOGD("H264 HW Decoder supported."); 800 ALOGD("H264 HW Decoder supported.");
752 supported_codec_types_.push_back(kVideoCodecH264); 801 supported_codec_types_.push_back(kVideoCodecH264);
753 } 802 }
754 } 803 }
755 804
756 MediaCodecVideoDecoderFactory::~MediaCodecVideoDecoderFactory() {} 805 MediaCodecVideoDecoderFactory::~MediaCodecVideoDecoderFactory() {}
757 806
758 webrtc::VideoDecoder* MediaCodecVideoDecoderFactory::CreateVideoDecoder( 807 webrtc::VideoDecoder* MediaCodecVideoDecoderFactory::CreateVideoDecoder(
759 VideoCodecType type) { 808 VideoCodecType type) {
(...skipping 11 matching lines...) Expand all
771 return NULL; 820 return NULL;
772 } 821 }
773 822
774 void MediaCodecVideoDecoderFactory::DestroyVideoDecoder( 823 void MediaCodecVideoDecoderFactory::DestroyVideoDecoder(
775 webrtc::VideoDecoder* decoder) { 824 webrtc::VideoDecoder* decoder) {
776 delete decoder; 825 delete decoder;
777 } 826 }
778 827
779 } // namespace webrtc_jni 828 } // namespace webrtc_jni
780 829
OLDNEW
« no previous file with comments | « talk/app/webrtc/java/jni/androidmediacodeccommon.h ('k') | talk/app/webrtc/java/src/org/webrtc/MediaCodecVideoDecoder.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698