| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/android/media_codec_bridge.h" | 5 #include "media/base/android/media_codec_bridge.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 | 8 |
| 8 #include "base/android/build_info.h" | 9 #include "base/android/build_info.h" |
| 9 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/numerics/safe_conversions.h" | 16 #include "base/numerics/safe_conversions.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 env, j_media_codec_.obj(), input_buffer_index)); | 486 env, j_media_codec_.obj(), input_buffer_index)); |
| 486 *data = static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())); | 487 *data = static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())); |
| 487 *capacity = base::checked_cast<size_t>( | 488 *capacity = base::checked_cast<size_t>( |
| 488 env->GetDirectBufferCapacity(j_buffer.obj())); | 489 env->GetDirectBufferCapacity(j_buffer.obj())); |
| 489 } | 490 } |
| 490 | 491 |
| 491 bool MediaCodecBridge::CopyFromOutputBuffer(int index, | 492 bool MediaCodecBridge::CopyFromOutputBuffer(int index, |
| 492 size_t offset, | 493 size_t offset, |
| 493 void* dst, | 494 void* dst, |
| 494 int dst_size) { | 495 int dst_size) { |
| 495 JNIEnv* env = AttachCurrentThread(); | 496 void* src_data = nullptr; |
| 496 ScopedJavaLocalRef<jobject> j_buffer( | 497 int src_capacity = GetOutputBufferAddress(index, offset, &src_data); |
| 497 Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index)); | |
| 498 void* src_data = | |
| 499 reinterpret_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())) + | |
| 500 offset; | |
| 501 int src_capacity = env->GetDirectBufferCapacity(j_buffer.obj()) - offset; | |
| 502 if (src_capacity < dst_size) | 498 if (src_capacity < dst_size) |
| 503 return false; | 499 return false; |
| 504 memcpy(dst, src_data, dst_size); | 500 memcpy(dst, src_data, dst_size); |
| 505 return true; | 501 return true; |
| 506 } | 502 } |
| 507 | 503 |
| 504 int MediaCodecBridge::GetOutputBufferAddress(int index, |
| 505 size_t offset, |
| 506 void** addr) { |
| 507 JNIEnv* env = AttachCurrentThread(); |
| 508 ScopedJavaLocalRef<jobject> j_buffer( |
| 509 Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index)); |
| 510 *addr = reinterpret_cast<uint8*>( |
| 511 env->GetDirectBufferAddress(j_buffer.obj())) + offset; |
| 512 return env->GetDirectBufferCapacity(j_buffer.obj()) - offset; |
| 513 } |
| 514 |
| 508 bool MediaCodecBridge::FillInputBuffer(int index, | 515 bool MediaCodecBridge::FillInputBuffer(int index, |
| 509 const uint8* data, | 516 const uint8* data, |
| 510 size_t size) { | 517 size_t size) { |
| 511 uint8* dst = NULL; | 518 uint8* dst = NULL; |
| 512 size_t capacity = 0; | 519 size_t capacity = 0; |
| 513 GetInputBuffer(index, &dst, &capacity); | 520 GetInputBuffer(index, &dst, &capacity); |
| 514 CHECK(dst); | 521 CHECK(dst); |
| 515 | 522 |
| 516 if (size > capacity) { | 523 if (size > capacity) { |
| 517 LOG(ERROR) << "Input buffer size " << size | 524 LOG(ERROR) << "Input buffer size " << size |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 break; | 697 break; |
| 691 } | 698 } |
| 692 default: | 699 default: |
| 693 LOG(ERROR) << "Invalid header encountered for codec: " | 700 LOG(ERROR) << "Invalid header encountered for codec: " |
| 694 << AudioCodecToAndroidMimeType(codec); | 701 << AudioCodecToAndroidMimeType(codec); |
| 695 return false; | 702 return false; |
| 696 } | 703 } |
| 697 return true; | 704 return true; |
| 698 } | 705 } |
| 699 | 706 |
| 700 int64 AudioCodecBridge::PlayOutputBuffer(int index, size_t size) { | 707 int64 AudioCodecBridge::PlayOutputBuffer( |
| 708 int index, size_t size, size_t offset) { |
| 701 DCHECK_LE(0, index); | 709 DCHECK_LE(0, index); |
| 702 int numBytes = base::checked_cast<int>(size); | 710 int numBytes = base::checked_cast<int>(size); |
| 711 |
| 712 void* buffer = nullptr; |
| 713 int capacity = GetOutputBufferAddress(index, offset, &buffer); |
| 714 numBytes = std::min(capacity, numBytes); |
| 715 CHECK_GE(numBytes, 0); |
| 716 |
| 703 JNIEnv* env = AttachCurrentThread(); | 717 JNIEnv* env = AttachCurrentThread(); |
| 704 ScopedJavaLocalRef<jobject> buf = | 718 ScopedJavaLocalRef<jbyteArray> byte_array = base::android::ToJavaByteArray( |
| 705 Java_MediaCodecBridge_getOutputBuffer(env, media_codec(), index); | 719 env, static_cast<uint8*>(buffer), numBytes); |
| 706 uint8* buffer = static_cast<uint8*>(env->GetDirectBufferAddress(buf.obj())); | |
| 707 | |
| 708 ScopedJavaLocalRef<jbyteArray> byte_array = | |
| 709 base::android::ToJavaByteArray(env, buffer, numBytes); | |
| 710 return Java_MediaCodecBridge_playOutputBuffer( | 720 return Java_MediaCodecBridge_playOutputBuffer( |
| 711 env, media_codec(), byte_array.obj()); | 721 env, media_codec(), byte_array.obj()); |
| 712 } | 722 } |
| 713 | 723 |
| 714 void AudioCodecBridge::SetVolume(double volume) { | 724 void AudioCodecBridge::SetVolume(double volume) { |
| 715 JNIEnv* env = AttachCurrentThread(); | 725 JNIEnv* env = AttachCurrentThread(); |
| 716 Java_MediaCodecBridge_setVolume(env, media_codec(), volume); | 726 Java_MediaCodecBridge_setVolume(env, media_codec(), volume); |
| 717 } | 727 } |
| 718 | 728 |
| 719 // static | 729 // static |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 JNIEnv* env = AttachCurrentThread(); | 851 JNIEnv* env = AttachCurrentThread(); |
| 842 return Java_MediaCodecBridge_isAdaptivePlaybackSupported( | 852 return Java_MediaCodecBridge_isAdaptivePlaybackSupported( |
| 843 env, media_codec(), width, height); | 853 env, media_codec(), width, height); |
| 844 } | 854 } |
| 845 | 855 |
| 846 bool MediaCodecBridge::RegisterMediaCodecBridge(JNIEnv* env) { | 856 bool MediaCodecBridge::RegisterMediaCodecBridge(JNIEnv* env) { |
| 847 return RegisterNativesImpl(env); | 857 return RegisterNativesImpl(env); |
| 848 } | 858 } |
| 849 | 859 |
| 850 } // namespace media | 860 } // namespace media |
| OLD | NEW |