1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 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 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 #ifndef SDP_MEDIAPARSER_REGISTRY_H 19 #define SDP_MEDIAPARSER_REGISTRY_H 20 #include "oscl_mem.h" 21 #include "oscl_map.h" 22 #include "oscl_string_containers.h" 23 #include "oscl_base_alloc.h" 24 #include "oscl_string.h" 25 #include "oscl_str_ptr_len.h" 26 27 //use an appropriate registry 28 #include "oscl_error.h" 29 // TLS registry 30 #define SDPMEDIAPARSER_REGISTRY OsclTLSRegistryEx 31 #define SDPMEDIAPARSER_REGISTRY_ID OSCL_TLS_ID_SDPMEDIAPARSER 32 #define SDPMEDIAPARSER_REGISTRY_WRAPPER OsclTLSEx 33 34 class SDPBaseMediaInfoParser; 35 36 class SDPMediaParserFactory : public OsclDestructDealloc 37 { 38 public: 39 virtual ~SDPMediaParserFactory() {} 40 virtual SDPBaseMediaInfoParser* createSDPMediaParserInstance() = 0; 41 virtual void destruct_and_dealloc(OsclAny* ptr) = 0; 42 }; 43 44 typedef OSCL_HeapString<OsclMemAllocator> string_key_type; 45 46 template<class Alloc> struct SDPRegMimeStringCompare 47 { 48 bool operator()(const string_key_type& x, const string_key_type& y) const 49 { 50 if ((oscl_CIstrcmp(x.get_str(), y.get_str())) < 0) 51 { 52 return true; 53 } 54 else 55 { 56 return false; 57 } 58 } 59 }; 60 61 class SDPMediaParserRegistry 62 { 63 public: 64 65 /** 66 * SDPMediaParserRegistry needs to be initialized once per thread. 67 * This creates the SDPMediaParserRegistry singleton that is used 68 * throughout the duration of the thread. 69 * 70 * @exception leaves if out of memory 71 */ 72 static void Init() 73 { 74 /* 75 * If the registry does not exist, create one 76 */ 77 if (SDPMEDIAPARSER_REGISTRY::getInstance(SDPMEDIAPARSER_REGISTRY_ID) == NULL) 78 { 79 Oscl_TAlloc<SDPMediaParserRegistry, OsclMemAllocator> talloc; 80 SDPMediaParserRegistry *sdpMediaParserReg = OSCL_ALLOC_NEW(talloc, SDPMediaParserRegistry, ()); 81 SDPMEDIAPARSER_REGISTRY::registerInstance(sdpMediaParserReg, SDPMEDIAPARSER_REGISTRY_ID); 82 } 83 } 84 85 86 /** 87 * Frees the SDPMediaParserRegistry singleton used by the current 88 * thread. This must be called before thread exit. 89 * 90 * @return 91 */ 92 static void Cleanup() 93 { 94 SDPMediaParserRegistry *sdpMediaParserReg = 95 OSCL_STATIC_CAST(SDPMediaParserRegistry*, SDPMEDIAPARSER_REGISTRY::getInstance(SDPMEDIAPARSER_REGISTRY_ID)); 96 97 Oscl_TAlloc<SDPMediaParserRegistry, OsclMemAllocator> talloc; 98 OSCL_ALLOC_DELETE(sdpMediaParserReg, talloc, SDPMediaParserRegistry); 99 100 SDPMEDIAPARSER_REGISTRY::registerInstance(NULL, SDPMEDIAPARSER_REGISTRY_ID); 101 } 102 103 virtual ~SDPMediaParserRegistry() {}; 104 105 /** 106 * Get the sdp media parser registry. There is only one 107 * registry instance per thread. 108 */ 109 static SDPMediaParserRegistry* GetSDPMediaParserRegistry() 110 { 111 SDPMEDIAPARSER_REGISTRY_WRAPPER< SDPMediaParserRegistry, SDPMEDIAPARSER_REGISTRY_ID > sdpMediaParserRegSng; 112 return &(*sdpMediaParserRegSng); 113 } 114 115 bool addMediaParserFactoryToRegistry(StrPtrLen mimeType, 116 SDPMediaParserFactory* mediaParserFactory) 117 { 118 string_key_type mimeString(OSCL_CONST_CAST(const char*, mimeType.c_str())); 119 _SDPMediaParserRegistry.insert(value_type(mimeString, mediaParserFactory)); 120 return true; 121 }; 122 123 SDPMediaParserFactory* 124 lookupSDPMediaParserFactory(OsclMemoryFragment memFrag) 125 { 126 string_key_type mimeString; 127 mimeString.set((const char*)(memFrag.ptr), memFrag.len); 128 Oscl_Map<string_key_type, SDPMediaParserFactory*, OsclMemAllocator, string_key_compare_class>::iterator it; 129 it = _SDPMediaParserRegistry.find(mimeString); 130 131 if (it != _SDPMediaParserRegistry.end()) 132 { 133 return (((*it).second)); 134 } 135 return NULL; 136 } 137 138 private: 139 SDPMediaParserRegistry() {}; 140 141 typedef SDPRegMimeStringCompare<OsclMemAllocator> string_key_compare_class; 142 typedef Oscl_Map<string_key_type, SDPMediaParserFactory*, OsclMemAllocator, string_key_compare_class>::value_type value_type; 143 144 Oscl_Map<string_key_type, SDPMediaParserFactory*, OsclMemAllocator, string_key_compare_class> _SDPMediaParserRegistry; 145 }; 146 #endif //SDP_REGISTRAR_H 147