• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Shenzhen Kaihong DID Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 		http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "codec_instance_manager.h"
17 #include <map>
18 #include <mutex>
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 std::map<uintptr_t, uintptr_t> g_codecInstances;
26 std::mutex g_codecInstanceLock;
27 
AddToCodecInstanceManager(CODEC_HANDLETYPE codecHandle,struct CodecInstance * codecInstance)28 bool AddToCodecInstanceManager(CODEC_HANDLETYPE codecHandle, struct CodecInstance *codecInstance)
29 {
30     uintptr_t handle = reinterpret_cast<uintptr_t>(codecHandle);
31     uintptr_t instance = reinterpret_cast<uintptr_t>(codecInstance);
32     std::unique_lock<std::mutex> autoLock(g_codecInstanceLock);
33     std::pair<std::map<uintptr_t, uintptr_t>::iterator, bool> ret =
34         g_codecInstances.insert(std::pair<uintptr_t, uintptr_t>(handle, instance));
35     bool result = ret.second;
36     return result;
37 }
38 
FindInCodecInstanceManager(CODEC_HANDLETYPE codecHandle)39 struct CodecInstance* FindInCodecInstanceManager(CODEC_HANDLETYPE codecHandle)
40 {
41     uintptr_t handle = reinterpret_cast<uintptr_t>(codecHandle);
42     std::unique_lock<std::mutex> autoLock(g_codecInstanceLock);
43     std::map<uintptr_t, uintptr_t>::iterator it = g_codecInstances.find(handle);
44     if (it == g_codecInstances.end()) {
45         return nullptr;
46     }
47     uintptr_t ret = it->second;
48     return reinterpret_cast<struct CodecInstance *>(ret);
49 }
50 
RemoveFromCodecInstanceManager(CODEC_HANDLETYPE codecHandle)51 void RemoveFromCodecInstanceManager(CODEC_HANDLETYPE codecHandle)
52 {
53     uintptr_t handle = reinterpret_cast<uintptr_t>(codecHandle);
54     std::unique_lock<std::mutex> autoLock(g_codecInstanceLock);
55     g_codecInstances.erase(handle);
56 }
57 
58 #ifdef __cplusplus
59 }
60 #endif
61