Home | History | Annotate | Download | only in include
      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 PAYLOAD_PARSER_REGISTRY_H_INCLUDED
     19 #define PAYLOAD_PARSER_REGISTRY_H_INCLUDED
     20 
     21 
     22 #include "oscl_mem.h"
     23 #include "oscl_map.h"
     24 #include "oscl_string_containers.h"
     25 #include "oscl_base_alloc.h"
     26 #include "oscl_string.h"
     27 #include "oscl_str_ptr_len.h"
     28 
     29 #include "payload_parser_factory.h"
     30 
     31 //use the TLS registry, or the singleton registry if no TLS.
     32 //Note: singleton registry only works in single-threaded scenarios, since this
     33 //implementation assumes a per-thread registry.
     34 #include "oscl_error.h"
     35 #define PAYLOADPARSER_REGISTRY OsclTLSRegistryEx
     36 #define PAYLOADPARSER_REGISTRY_ID OSCL_TLS_ID_PAYLOADPARSER
     37 #define PAYLOADPARSER_REGISTRY_WRAPPER OsclTLSEx
     38 
     39 
     40 typedef OSCL_HeapString<OsclMemAllocator> string_key_type;
     41 
     42 class PayloadParserRegistry
     43 {
     44     public:
     45 
     46         /**
     47          * PayloadParserRegistry needs to be initialized once per thread.
     48          * This creates the PayloadParserRegistry singleton that is used
     49          * throughout the duration of the thread.
     50          *
     51          * @exception leaves if out of memory
     52          */
     53         static void Init()
     54         {
     55             /*
     56              * If the registry does not exist, create one
     57              */
     58             if (PAYLOADPARSER_REGISTRY::getInstance(PAYLOADPARSER_REGISTRY_ID) == NULL)
     59             {
     60                 Oscl_TAlloc<PayloadParserRegistry, OsclMemAllocator> talloc;
     61                 PayloadParserRegistry *rtpPayloadParserReg =
     62                     OSCL_ALLOC_NEW(talloc, PayloadParserRegistry, ());
     63                 PAYLOADPARSER_REGISTRY::registerInstance(rtpPayloadParserReg,
     64                         PAYLOADPARSER_REGISTRY_ID);
     65             }
     66         }
     67 
     68 
     69         /**am
     70          * Frees the PayloadParserRegistry singleton used by the current
     71          * thread. This must be called before thread exit.
     72          *
     73          * @return
     74          */
     75         static void Cleanup()
     76         {
     77             PayloadParserRegistry *rtpPayloadParserReg =
     78                 OSCL_STATIC_CAST(PayloadParserRegistry*,
     79                                  PAYLOADPARSER_REGISTRY::getInstance(PAYLOADPARSER_REGISTRY_ID));
     80 
     81             Oscl_TAlloc<PayloadParserRegistry, OsclMemAllocator> talloc;
     82             OSCL_ALLOC_DELETE(rtpPayloadParserReg, talloc, PayloadParserRegistry);
     83 
     84             PAYLOADPARSER_REGISTRY::registerInstance(NULL, PAYLOADPARSER_REGISTRY_ID);
     85         }
     86 
     87         virtual ~PayloadParserRegistry() {};
     88 
     89         /**
     90          * Get the payload parser registry.  There is only one
     91          * registry instance per thread.
     92          */
     93         static PayloadParserRegistry* GetPayloadParserRegistry()
     94         {
     95             PAYLOADPARSER_REGISTRY_WRAPPER < PayloadParserRegistry,
     96             PAYLOADPARSER_REGISTRY_ID > payloadParserRegSng;
     97             return &(*payloadParserRegSng);
     98         }
     99 
    100         bool addPayloadParserFactoryToRegistry(StrPtrLen mimeType,
    101                                                IPayloadParserFactory* factory)
    102         {
    103             string_key_type mimeString(OSCL_CONST_CAST(const char*, mimeType.c_str()));
    104             iRegistry.insert(value_type(mimeString, factory));
    105             return true;
    106         };
    107 
    108         IPayloadParserFactory*
    109         lookupPayloadParserFactory(OsclMemoryFragment memFrag)
    110         {
    111             string_key_type mimeString;
    112             mimeString.set((char*)(memFrag.ptr), memFrag.len);
    113             Oscl_Map < string_key_type, IPayloadParserFactory*,
    114             OsclMemAllocator, MimeStringCompare >::iterator it;
    115             it = iRegistry.find(mimeString);
    116 
    117             /* This check is necessary for the ADS1.2 compiler*/
    118             if (!(it == iRegistry.end()))
    119             {
    120                 return (((*it).second));
    121             }
    122             return NULL;
    123         }
    124 
    125         struct MimeStringCompare
    126         {
    127             bool operator()(const string_key_type& x, const string_key_type& y) const
    128             {
    129                 if ((oscl_CIstrcmp(x.get_str(), y.get_str())) < 0)
    130                 {
    131                     return true;
    132                 }
    133                 else
    134                 {
    135                     return false;
    136                 }
    137             }
    138         };
    139 
    140     private:
    141         PayloadParserRegistry() {};
    142 
    143         typedef Oscl_Map < string_key_type, IPayloadParserFactory*, OsclMemAllocator,
    144         MimeStringCompare >::value_type value_type;
    145 
    146         Oscl_Map < string_key_type, IPayloadParserFactory*,
    147         OsclMemAllocator, MimeStringCompare > iRegistry;
    148 };
    149 
    150 #endif  // PAYLOAD_PARSER_REGISTRY_H_INCLUDED
    151 
    152