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 
27 #include "CodecProperties.h"
28 
29 namespace android {
30 namespace mediaformatshaper {
31 
32 // manage the list of codec information.
33 //
34 // XXX: the mutex here is too heavy; rework that.
35 //
36 
37 static std::mutex sCodecMutex;
38 static std::map<std::string, CodecProperties*> sCodecTraits;
39 
findCodec(const char * codecName,const char * mediaType)40 CodecProperties *findCodec(const char *codecName, const char *mediaType) {
41     CodecProperties *codec = nullptr;
42 
43     // synthesize a name from both codecName + mediaType
44     // some codecs support multiple media types and may have different capabilities
45     // for each media type
46     //
47     std::string codecKey = codecName;
48     codecKey += "-";
49     codecKey += mediaType;
50 
51     std::lock_guard  _l(sCodecMutex);
52 
53     auto it = sCodecTraits.find(codecKey);
54     if (it != sCodecTraits.end()) {
55         codec = it->second;
56     }
57 
58     return codec;
59 }
60 
registerCodec(CodecProperties * codec,const char * codecName,const char * mediaType)61 CodecProperties *registerCodec(CodecProperties *codec, const char *codecName,
62                                const char *mediaType) {
63 
64     CodecProperties *registeredCodec = nullptr;
65 
66     if (codec->isRegistered()) {
67         return nullptr;
68     }
69 
70     // synthesize a name from both codecName + mediaType
71     // some codecs support multiple media types and may have different capabilities
72     // for each media type
73     //
74     std::string codecKey = codecName;
75     codecKey += "-";
76     codecKey += mediaType;
77 
78     std::lock_guard  _l(sCodecMutex);
79 
80     auto it = sCodecTraits.find(codecKey);
81     if (it != sCodecTraits.end()) {
82         registeredCodec = it->second;
83     }
84 
85     if (registeredCodec == nullptr) {
86         // register the one that was passed to us
87         ALOGV("Creating entry for codec %s, mediaType %s, key %s", codecName, mediaType,
88               codecKey.c_str());
89         sCodecTraits.insert({codecKey, codec});
90         registeredCodec = codec;
91         codec->setRegistered(true);
92     } else {
93         // one has already been registered, use that
94         // and discard the candidate
95         delete codec;
96         codec = nullptr;
97     }
98 
99     return registeredCodec;
100 }
101 
102 }  // namespace mediaformatshaper
103 }  // namespace android
104 
105