| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ndk_media_codec_bridge.h" | 5 #include "media/base/android/ndk_media_codec_bridge.h" |
| 6 | 6 |
| 7 #include <media/NdkMediaError.h> | 7 #include <media/NdkMediaError.h> |
| 8 #include <media/NdkMediaFormat.h> | 8 #include <media/NdkMediaFormat.h> |
| 9 | 9 |
| 10 #include <limits> |
| 11 |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "media/base/decrypt_config.h" | 13 #include "media/base/decrypt_config.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 const char kMediaFormatKeyCropLeft[] = "crop-left"; | 16 const char kMediaFormatKeyCropLeft[] = "crop-left"; |
| 15 const char kMediaFormatKeyCropRight[] = "crop-right"; | 17 const char kMediaFormatKeyCropRight[] = "crop-right"; |
| 16 const char kMediaFormatKeyCropBottom[] = "crop-bottom"; | 18 const char kMediaFormatKeyCropBottom[] = "crop-bottom"; |
| 17 const char kMediaFormatKeyCropTop[] = "crop-top"; | 19 const char kMediaFormatKeyCropTop[] = "crop-top"; |
| 18 } | 20 } |
| 19 | 21 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 int NdkMediaCodecBridge::GetOutputSamplingRate() { | 86 int NdkMediaCodecBridge::GetOutputSamplingRate() { |
| 85 AMediaFormat* format = AMediaCodec_getOutputFormat(media_codec_.get()); | 87 AMediaFormat* format = AMediaCodec_getOutputFormat(media_codec_.get()); |
| 86 int sample_rate = 0; | 88 int sample_rate = 0; |
| 87 AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, &sample_rate); | 89 AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, &sample_rate); |
| 88 DCHECK(sample_rate != 0); | 90 DCHECK(sample_rate != 0); |
| 89 return sample_rate; | 91 return sample_rate; |
| 90 } | 92 } |
| 91 | 93 |
| 92 MediaCodecStatus NdkMediaCodecBridge::QueueInputBuffer( | 94 MediaCodecStatus NdkMediaCodecBridge::QueueInputBuffer( |
| 93 int index, | 95 int index, |
| 94 const uint8* data, | 96 const uint8_t* data, |
| 95 size_t data_size, | 97 size_t data_size, |
| 96 const base::TimeDelta& presentation_time) { | 98 const base::TimeDelta& presentation_time) { |
| 97 if (data_size > base::checked_cast<size_t>(kint32max)) | 99 if (data_size > |
| 100 base::checked_cast<size_t>(std::numeric_limits<int32_t>::max())) { |
| 98 return MEDIA_CODEC_ERROR; | 101 return MEDIA_CODEC_ERROR; |
| 102 } |
| 99 if (data && !FillInputBuffer(index, data, data_size)) | 103 if (data && !FillInputBuffer(index, data, data_size)) |
| 100 return MEDIA_CODEC_ERROR; | 104 return MEDIA_CODEC_ERROR; |
| 101 | 105 |
| 102 media_status_t status = | 106 media_status_t status = |
| 103 AMediaCodec_queueInputBuffer(media_codec_.get(), index, 0, data_size, | 107 AMediaCodec_queueInputBuffer(media_codec_.get(), index, 0, data_size, |
| 104 presentation_time.InMicroseconds(), 0); | 108 presentation_time.InMicroseconds(), 0); |
| 105 return TranslateMediaCodecStatus(status); | 109 return TranslateMediaCodecStatus(status); |
| 106 } | 110 } |
| 107 | 111 |
| 108 MediaCodecStatus NdkMediaCodecBridge::QueueSecureInputBuffer( | 112 MediaCodecStatus NdkMediaCodecBridge::QueueSecureInputBuffer( |
| 109 int index, | 113 int index, |
| 110 const uint8* data, | 114 const uint8_t* data, |
| 111 size_t data_size, | 115 size_t data_size, |
| 112 const std::vector<char>& key_id, | 116 const std::vector<char>& key_id, |
| 113 const std::vector<char>& iv, | 117 const std::vector<char>& iv, |
| 114 const SubsampleEntry* subsamples, | 118 const SubsampleEntry* subsamples, |
| 115 int subsamples_size, | 119 int subsamples_size, |
| 116 const base::TimeDelta& presentation_time) { | 120 const base::TimeDelta& presentation_time) { |
| 117 if (data_size > base::checked_cast<size_t>(kint32max)) | 121 if (data_size > |
| 122 base::checked_cast<size_t>(std::numeric_limits<int32_t>::max())) { |
| 118 return MEDIA_CODEC_ERROR; | 123 return MEDIA_CODEC_ERROR; |
| 124 } |
| 119 if (key_id.size() > 16 || iv.size()) | 125 if (key_id.size() > 16 || iv.size()) |
| 120 return MEDIA_CODEC_ERROR; | 126 return MEDIA_CODEC_ERROR; |
| 121 if (data && !FillInputBuffer(index, data, data_size)) | 127 if (data && !FillInputBuffer(index, data, data_size)) |
| 122 return MEDIA_CODEC_ERROR; | 128 return MEDIA_CODEC_ERROR; |
| 123 | 129 |
| 124 int new_subsamples_size = subsamples_size == 0 ? 1 : subsamples_size; | 130 int new_subsamples_size = subsamples_size == 0 ? 1 : subsamples_size; |
| 125 std::vector<size_t> clear_data, encrypted_data; | 131 std::vector<size_t> clear_data, encrypted_data; |
| 126 if (subsamples_size == 0) { | 132 if (subsamples_size == 0) { |
| 127 DCHECK(!subsamples); | 133 DCHECK(!subsamples); |
| 128 clear_data.push_back(0); | 134 clear_data.push_back(0); |
| 129 encrypted_data.push_back(data_size); | 135 encrypted_data.push_back(data_size); |
| 130 } else { | 136 } else { |
| 131 DCHECK_GT(subsamples_size, 0); | 137 DCHECK_GT(subsamples_size, 0); |
| 132 DCHECK(subsamples); | 138 DCHECK(subsamples); |
| 133 for (int i = 0; i < subsamples_size; ++i) { | 139 for (int i = 0; i < subsamples_size; ++i) { |
| 134 DCHECK(subsamples[i].clear_bytes <= std::numeric_limits<uint16>::max()); | 140 DCHECK(subsamples[i].clear_bytes <= std::numeric_limits<uint16_t>::max()); |
| 135 if (subsamples[i].cypher_bytes > | 141 if (subsamples[i].cypher_bytes > |
| 136 static_cast<uint32>(std::numeric_limits<int32>::max())) { | 142 static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) { |
| 137 return MEDIA_CODEC_ERROR; | 143 return MEDIA_CODEC_ERROR; |
| 138 } | 144 } |
| 139 clear_data.push_back(subsamples[i].clear_bytes); | 145 clear_data.push_back(subsamples[i].clear_bytes); |
| 140 encrypted_data.push_back(subsamples[i].cypher_bytes); | 146 encrypted_data.push_back(subsamples[i].cypher_bytes); |
| 141 } | 147 } |
| 142 } | 148 } |
| 143 | 149 |
| 144 AMediaCodecCryptoInfo* crypto_info = AMediaCodecCryptoInfo_new( | 150 AMediaCodecCryptoInfo* crypto_info = AMediaCodecCryptoInfo_new( |
| 145 new_subsamples_size, | 151 new_subsamples_size, |
| 146 reinterpret_cast<uint8*>(const_cast<char*>(key_id.data())), | 152 reinterpret_cast<uint8_t*>(const_cast<char*>(key_id.data())), |
| 147 reinterpret_cast<uint8*>(const_cast<char*>(iv.data())), | 153 reinterpret_cast<uint8_t*>(const_cast<char*>(iv.data())), |
| 148 AMEDIACODECRYPTOINFO_MODE_AES_CTR, clear_data.data(), | 154 AMEDIACODECRYPTOINFO_MODE_AES_CTR, clear_data.data(), |
| 149 encrypted_data.data()); | 155 encrypted_data.data()); |
| 150 | 156 |
| 151 media_status_t status = AMediaCodec_queueSecureInputBuffer( | 157 media_status_t status = AMediaCodec_queueSecureInputBuffer( |
| 152 media_codec_.get(), index, 0, crypto_info, | 158 media_codec_.get(), index, 0, crypto_info, |
| 153 presentation_time.InMicroseconds(), 0); | 159 presentation_time.InMicroseconds(), 0); |
| 154 return TranslateMediaCodecStatus(status); | 160 return TranslateMediaCodecStatus(status); |
| 155 } | 161 } |
| 156 | 162 |
| 157 void NdkMediaCodecBridge::QueueEOS(int input_buffer_index) { | 163 void NdkMediaCodecBridge::QueueEOS(int input_buffer_index) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 return MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER; | 203 return MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER; |
| 198 else | 204 else |
| 199 return MEDIA_CODEC_ERROR; | 205 return MEDIA_CODEC_ERROR; |
| 200 } | 206 } |
| 201 | 207 |
| 202 void NdkMediaCodecBridge::ReleaseOutputBuffer(int index, bool render) { | 208 void NdkMediaCodecBridge::ReleaseOutputBuffer(int index, bool render) { |
| 203 AMediaCodec_releaseOutputBuffer(media_codec_.get(), index, render); | 209 AMediaCodec_releaseOutputBuffer(media_codec_.get(), index, render); |
| 204 } | 210 } |
| 205 | 211 |
| 206 void NdkMediaCodecBridge::GetInputBuffer(int input_buffer_index, | 212 void NdkMediaCodecBridge::GetInputBuffer(int input_buffer_index, |
| 207 uint8** data, | 213 uint8_t** data, |
| 208 size_t* capacity) { | 214 size_t* capacity) { |
| 209 *data = AMediaCodec_getInputBuffer(media_codec_.get(), input_buffer_index, | 215 *data = AMediaCodec_getInputBuffer(media_codec_.get(), input_buffer_index, |
| 210 capacity); | 216 capacity); |
| 211 } | 217 } |
| 212 | 218 |
| 213 bool NdkMediaCodecBridge::CopyFromOutputBuffer(int index, | 219 bool NdkMediaCodecBridge::CopyFromOutputBuffer(int index, |
| 214 size_t offset, | 220 size_t offset, |
| 215 void* dst, | 221 void* dst, |
| 216 int dst_size) { | 222 int dst_size) { |
| 217 size_t capacity; | 223 size_t capacity; |
| 218 uint8_t* src_data = | 224 uint8_t* src_data = |
| 219 AMediaCodec_getOutputBuffer(media_codec_.get(), index, &capacity); | 225 AMediaCodec_getOutputBuffer(media_codec_.get(), index, &capacity); |
| 220 | 226 |
| 221 if (capacity < offset || capacity - offset < static_cast<size_t>(dst_size)) | 227 if (capacity < offset || capacity - offset < static_cast<size_t>(dst_size)) |
| 222 return false; | 228 return false; |
| 223 | 229 |
| 224 memcpy(dst, src_data + offset, dst_size); | 230 memcpy(dst, src_data + offset, dst_size); |
| 225 return true; | 231 return true; |
| 226 } | 232 } |
| 227 | 233 |
| 228 } // namespace media | 234 } // namespace media |
| OLD | NEW |