| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // The file defines the symbols from NDKMediaCodec that android is using. It |
| 6 // then loads the library dynamically on first use. |
| 7 |
| 8 #include <media/NdkMediaCodec.h> |
| 9 #include <media/NdkMediaFormat.h> |
| 10 |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/native_library.h" |
| 14 |
| 15 #define LOOKUP_FUNC(func_name, return_type, args, arg_names) \ |
| 16 return_type func_name args { \ |
| 17 typedef return_type(*signature) args; \ |
| 18 static signature g_##func_name = \ |
| 19 reinterpret_cast<signature>(base::GetFunctionPointerFromNativeLibrary( \ |
| 20 LibraryHandle(), #func_name)); \ |
| 21 return g_##func_name arg_names; \ |
| 22 } |
| 23 |
| 24 // The constants used in chromium. Those symbols are defined as extern symbols |
| 25 // in the NdkMediaFormat headers. They will be initialized to their correct |
| 26 // values when the library is loaded. |
| 27 const char* AMEDIAFORMAT_KEY_HEIGHT; |
| 28 const char* AMEDIAFORMAT_KEY_SAMPLE_RATE; |
| 29 const char* AMEDIAFORMAT_KEY_WIDTH; |
| 30 |
| 31 namespace { |
| 32 |
| 33 // The name of the library to load. |
| 34 const char kMediaNDKLibraryName[] = "libmediandk.so"; |
| 35 |
| 36 // Loads the OpenSLES library, and initializes all the proxies. |
| 37 base::NativeLibrary IntializeLibraryHandle() { |
| 38 base::NativeLibrary handle = |
| 39 base::LoadNativeLibrary(base::FilePath(kMediaNDKLibraryName), NULL); |
| 40 DCHECK(handle) << "Unable to load " << kMediaNDKLibraryName; |
| 41 |
| 42 // Setup the proxy for each symbol. |
| 43 // Attach the symbol name to the proxy address. |
| 44 struct SymbolDefinition { |
| 45 const char* name; |
| 46 const char** value; |
| 47 }; |
| 48 |
| 49 // The list of defined symbols. |
| 50 const SymbolDefinition kSymbols[] = { |
| 51 {"AMEDIAFORMAT_KEY_HEIGHT", &AMEDIAFORMAT_KEY_HEIGHT}, |
| 52 {"AMEDIAFORMAT_KEY_SAMPLE_RATE", &AMEDIAFORMAT_KEY_SAMPLE_RATE}, |
| 53 {"AMEDIAFORMAT_KEY_SAMPLE_RATE", &AMEDIAFORMAT_KEY_SAMPLE_RATE}, |
| 54 }; |
| 55 |
| 56 for (size_t i = 0; i < sizeof(kSymbols) / sizeof(kSymbols[0]); ++i) { |
| 57 memcpy(kSymbols[i].value, |
| 58 base::GetFunctionPointerFromNativeLibrary(handle, kSymbols[i].name), |
| 59 sizeof(char*)); |
| 60 DCHECK(*kSymbols[i].value) << "Unable to find symbol for " |
| 61 << kSymbols[i].name; |
| 62 } |
| 63 return handle; |
| 64 } |
| 65 |
| 66 // Returns the handler to the shared library. The library itself will be lazily |
| 67 // loaded during the first call to this function. |
| 68 base::NativeLibrary LibraryHandle() { |
| 69 // The handle is lazily initialized on the first call. |
| 70 static base::NativeLibrary g_mediandk_LibraryHandle = |
| 71 IntializeLibraryHandle(); |
| 72 return g_mediandk_LibraryHandle; |
| 73 } |
| 74 } |
| 75 |
| 76 LOOKUP_FUNC(AMediaCodec_createDecoderByType, |
| 77 AMediaCodec*, |
| 78 (const char* mime_type), |
| 79 (mime_type)); |
| 80 LOOKUP_FUNC(AMediaCodec_createEncoderByType, |
| 81 AMediaCodec*, |
| 82 (const char* mime_type), |
| 83 (mime_type)); |
| 84 LOOKUP_FUNC(AMediaCodec_delete, media_status_t, (AMediaCodec * codec), (codec)); |
| 85 LOOKUP_FUNC(AMediaCodec_configure, |
| 86 media_status_t, |
| 87 (AMediaCodec * codec, |
| 88 const AMediaFormat* format, |
| 89 ANativeWindow* surface, |
| 90 AMediaCrypto* crypto, |
| 91 uint32_t flags), |
| 92 (codec, format, surface, crypto, flags)); |
| 93 LOOKUP_FUNC(AMediaCodec_start, media_status_t, (AMediaCodec * codec), (codec)); |
| 94 LOOKUP_FUNC(AMediaCodec_stop, media_status_t, (AMediaCodec * codec), (codec)); |
| 95 LOOKUP_FUNC(AMediaCodec_flush, media_status_t, (AMediaCodec * codec), (codec)); |
| 96 LOOKUP_FUNC(AMediaCodec_getInputBuffer, |
| 97 uint8_t*, |
| 98 (AMediaCodec * codec, size_t idx, size_t* out_size), |
| 99 (codec, idx, out_size)); |
| 100 LOOKUP_FUNC(AMediaCodec_getOutputBuffer, |
| 101 uint8_t*, |
| 102 (AMediaCodec * codec, size_t idx, size_t* out_size), |
| 103 (codec, idx, out_size)); |
| 104 LOOKUP_FUNC(AMediaCodec_dequeueInputBuffer, |
| 105 ssize_t, |
| 106 (AMediaCodec * codec, int64_t timeoutUs), |
| 107 (codec, timeoutUs)); |
| 108 LOOKUP_FUNC(AMediaCodec_queueInputBuffer, |
| 109 media_status_t, |
| 110 (AMediaCodec * codec, |
| 111 size_t idx, |
| 112 off_t offset, |
| 113 size_t size, |
| 114 uint64_t time, |
| 115 uint32_t flags), |
| 116 (codec, idx, offset, size, time, flags)); |
| 117 LOOKUP_FUNC(AMediaCodec_queueSecureInputBuffer, |
| 118 media_status_t, |
| 119 (AMediaCodec * codec, |
| 120 size_t idx, |
| 121 off_t offset, |
| 122 AMediaCodecCryptoInfo* cryto, |
| 123 uint64_t time, |
| 124 uint32_t flags), |
| 125 (codec, idx, offset, cryto, time, flags)); |
| 126 LOOKUP_FUNC(AMediaCodec_dequeueInputBuffer, |
| 127 ssize_t, |
| 128 (AMediaCodec * codec, |
| 129 AMediaCodecBufferInfo* info, |
| 130 int64_t timeoutUs), |
| 131 (codec, info, timeoutUs)); |
| 132 LOOKUP_FUNC(AMediaCodec_dequeueOutputBuffer, |
| 133 ssize_t, |
| 134 (AMediaCodec * codec, |
| 135 AMediaCodecBufferInfo* info, |
| 136 int64_t timeoutUs), |
| 137 (codec, info, timeoutUs)); |
| 138 LOOKUP_FUNC(AMediaCodec_getOutputFormat, |
| 139 AMediaFormat*, |
| 140 (AMediaCodec * codec), |
| 141 (codec)); |
| 142 LOOKUP_FUNC(AMediaCodec_releaseOutputBuffer, |
| 143 media_status_t, |
| 144 (AMediaCodec * codec, size_t idx, bool render), |
| 145 (codec, idx, render)); |
| 146 LOOKUP_FUNC(AMediaCodecCryptoInfo_new, |
| 147 AMediaCodecCryptoInfo*, |
| 148 (int num_subsamples, |
| 149 uint8_t key[16], |
| 150 uint8_t iv[16], |
| 151 cryptoinfo_mode_t mode, |
| 152 size_t* clear_bytes, |
| 153 size_t* encrypted_bytes), |
| 154 (num_subsamples, key, iv, mode, clear_bytes, encrypted_bytes)); |
| 155 LOOKUP_FUNC(AMediaCodecCryptoInfo_delete, |
| 156 media_status_t, |
| 157 (AMediaCodecCryptoInfo * info), |
| 158 (info)); |
| 159 LOOKUP_FUNC(AMediaFormat_getInt32, |
| 160 bool, |
| 161 (AMediaFormat * format, const char* name, int32_t* out), |
| 162 (format, name, out)); |
| OLD | NEW |