Ray Essick | 1831f7b | 2021-03-15 16:10:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "ManageShapingCodecs" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <mutex> |
| 22 | #include <string> |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <media/NdkMediaFormat.h> |
| 26 | #include <media/formatshaper/CodecProperties.h> |
| 27 | |
| 28 | namespace android { |
| 29 | namespace mediaformatshaper { |
| 30 | |
| 31 | // manage the list of codec information. |
| 32 | // |
| 33 | // XXX: the mutex here is too heavy; rework that. |
| 34 | // |
| 35 | |
| 36 | static std::mutex sCodecMutex; |
| 37 | static std::map<std::string, CodecProperties*> sCodecTraits; |
| 38 | |
| 39 | CodecProperties *findCodec(const char *codecName, const char *mediaType) { |
| 40 | CodecProperties *codec = nullptr; |
| 41 | |
| 42 | // synthesize a name from both codecName + mediaType |
| 43 | // some codecs support multiple media types and may have different capabilities |
| 44 | // for each media type |
| 45 | // |
| 46 | std::string codecKey = codecName; |
| 47 | codecKey += "-"; |
| 48 | codecKey += mediaType; |
| 49 | |
| 50 | std::lock_guard _l(sCodecMutex); |
| 51 | |
| 52 | auto it = sCodecTraits.find(codecKey); |
| 53 | if (it != sCodecTraits.end()) { |
| 54 | codec = it->second; |
| 55 | } |
| 56 | |
| 57 | return codec; |
| 58 | } |
| 59 | |
| 60 | CodecProperties *registerCodec(CodecProperties *codec, const char *codecName, |
| 61 | const char *mediaType) { |
| 62 | |
| 63 | CodecProperties *registeredCodec = nullptr; |
| 64 | |
| 65 | if (codec->isRegistered()) { |
| 66 | return nullptr; |
| 67 | } |
| 68 | |
| 69 | // synthesize a name from both codecName + mediaType |
| 70 | // some codecs support multiple media types and may have different capabilities |
| 71 | // for each media type |
| 72 | // |
| 73 | std::string codecKey = codecName; |
| 74 | codecKey += "-"; |
| 75 | codecKey += mediaType; |
| 76 | |
| 77 | std::lock_guard _l(sCodecMutex); |
| 78 | |
| 79 | auto it = sCodecTraits.find(codecKey); |
| 80 | if (it != sCodecTraits.end()) { |
| 81 | registeredCodec = it->second; |
| 82 | } |
| 83 | |
| 84 | if (registeredCodec == nullptr) { |
| 85 | // register the one that was passed to us |
| 86 | ALOGV("Creating entry for codec %s, mediaType %s, key %s", codecName, mediaType, |
| 87 | codecKey.c_str()); |
| 88 | sCodecTraits.insert({codecKey, codec}); |
| 89 | registeredCodec = codec; |
| 90 | codec->setRegistered(true); |
| 91 | } else { |
| 92 | // one has already been registered, use that |
| 93 | // and discard the candidate |
| 94 | delete codec; |
| 95 | codec = nullptr; |
| 96 | } |
| 97 | |
| 98 | return registeredCodec; |
| 99 | } |
| 100 | |
| 101 | } // namespace mediaformatshaper |
| 102 | } // namespace android |
| 103 | |