Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Audio::CodecRegistry Class Reference

#include <CodecRegistry.h>

Inheritance diagram for Audio::CodecRegistry:
Singleton< CodecRegistry >

Public Member Functions

 CodecRegistry () throw ()
 
 ~CodecRegistry ()
 
void add (Codec *codec, int priority=0) throw ()
 
void remove (Codec *codec) throw ()
 
CodecfindByName (const std::string &name) const throw (CodecNotFoundException)
 
CodecfindByFile (const std::string &path, VSFileSystem::VSFileType type=VSFileSystem::UnknownFile) const throw (CodecNotFoundException)
 
Streamopen (const std::string &path, VSFileSystem::VSFileType type=VSFileSystem::UnknownFile) const throw (Exception)
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< CodecRegistry >
static CodecRegistry * getSingleton ()
 
- Protected Member Functions inherited from Singleton< CodecRegistry >
 ~Singleton ()
 
- Static Protected Member Functions inherited from Singleton< CodecRegistry >
static void initializeSingleton ()
 
static void deinitializeSingleton ()
 
- Static Protected Attributes inherited from Singleton< CodecRegistry >
static CodecRegistry * _singletonInstance
 

Detailed Description

Codec registry class.

Remarks
Use it to manage codec implementations.
It handles codec factories and manages resolving file-to-codec associations.
See Also
Codec

Definition at line 30 of file CodecRegistry.h.

Constructor & Destructor Documentation

Audio::CodecRegistry::CodecRegistry ( )
throw (
)

Construct an empty registry

Remarks
End-users of the class shouldn't be using this. Singletons need it

Definition at line 16 of file CodecRegistry.cpp.

17  {
18  }
Audio::CodecRegistry::~CodecRegistry ( )

Definition at line 20 of file CodecRegistry.cpp.

21  {
22  nameCodec.clear();
23  universalCodecs.clear();
24  extensionCodecs.clear();
25 
26  for (CodecPriority::const_iterator it = codecPriority.begin(); it != codecPriority.end(); ++it)
27  delete it->first;
28  codecPriority.clear();
29  }

Member Function Documentation

void Audio::CodecRegistry::add ( Codec codec,
int  priority = 0 
)
throw (
)

Add a codec to the registry

Remarks
You may add a codec multiple times if you wish. The class guarantees that any further call will be a no-op. Previos priority will still apply.
Parameters
codecThe codec to be added to the registry.

Definition at line 31 of file CodecRegistry.cpp.

Referenced by Audio::CodecRegistration::CodecRegistration().

32  {
33  if (codecPriority.find(codec) == codecPriority.end()) {
34  codecPriority[codec] = priority;
35  nameCodec[codec->getName()] = codec;
36 
37  std::cout << "Registering codec " << codec->getName().c_str();
38 
39  const Codec::Extensions *ext = codec->getExtensions();
40  if (ext) {
41  for (Codec::Extensions::const_iterator it = ext->begin(); it != ext->end(); ++it) {
42  std::cout << " " << it->c_str();
43  extensionCodecs[*it].insert(codec);
44  }
45  } else {
46  std::cout << " as universal";
47  universalCodecs.insert(codec);
48  }
49 
50  std::cout << "." << std::endl;
51  } else {
52  std::cout << "Codec " << codec->getName().c_str() << " already registered" << std::endl;
53  }
54  }
Codec * Audio::CodecRegistry::findByFile ( const std::string &  path,
VSFileSystem::VSFileType  type = VSFileSystem::UnknownFile 
) const
throw (CodecNotFoundException
)

Find a codec that can handle the file.

Returns
A codec instance that can handle the specified file (ie: canHandle() returns true). It will never return null.
Remarks
If there's more than one codec that can handle the file, the one with the higher priority will be returned. Notice that it can't be assured that the codec will be able to open the file, so be prepared to handle failures further down the road.
Instead of returning null, if a codec of such characteristics cannot be found,
an CodecNotFound exception is risen.

Definition at line 102 of file CodecRegistry.cpp.

103  {
104  std::vector<Codec*> candidates;
105 
106  // NOTE: we'll assume that if they are including the specified extension, they'll return
107  // true if asked if they can handle the file with !canOpen.
108  size_t sep = path.find_last_of('.');
109  if (sep != std::string::npos && (sep+1) < path.length()) {
110  ExtensionCodecs::const_iterator eit = extensionCodecs.find(
111  path.substr(sep+1, std::string::npos));
112  if (eit != extensionCodecs.end())
113  for (CodecSet::const_iterator esit=eit->second.begin(); esit != eit->second.end(); ++esit)
114  candidates.push_back(*esit);
115  }
116 
117  for (CodecSet::const_iterator uit = universalCodecs.begin(); uit != universalCodecs.end(); ++uit)
118  if ((*uit)->canHandle(path, false, type))
119  candidates.push_back(*uit);
120 
121  // Now we'll sort candidates by priority, and return the first codec that can actually handle the file.
122  if (candidates.begin() != candidates.end()) {
123  std::sort(
124  candidates.begin(),
125  candidates.end(),
126  MappedComparator<CodecPriority,Codec*>(codecPriority));
127 
128  // Why do we need an explicit cast *to* const?
129  // See http://www.mpi-inf.mpg.de/~hitoshi/otherprojects/tips/cpp-memo.shtml
130  for (std::vector<Codec*>::const_reverse_iterator it = candidates.rbegin();
131  it != reinterpret_cast<const std::vector<Codec*> &>(candidates).rend(); ++it)
132  if ((*it)->canHandle(path, true, type))
133  return *it;
134  }
135 
136  // No candidate is able to handle this one!
137  throw CodecNotFoundException(
138  std::string("No registered codec can handle the file \"") + path + "\"");
139  }
Codec * Audio::CodecRegistry::findByName ( const std::string &  name) const
throw (CodecNotFoundException
)

Find a codec by its name

Returns
A codec instance named by the specified name, never null.
Remarks
If there's more than one codec with the same name, any one of them is returned. Not even necessarily the same each time. So avoid adding conflicting codecs.
Instead of returning null, if a codec of such characteristics cannot be found,
a CodecNotFound exception is risen.

Definition at line 71 of file CodecRegistry.cpp.

72  {
73  NameCodec::const_iterator it = nameCodec.find(name);
74  if (it != nameCodec.end())
75  return it->second; else
76  throw CodecNotFoundException(
77  std::string("No codec with name \"") + name + "\" has been registered");
78  }
Stream * Audio::CodecRegistry::open ( const std::string &  path,
VSFileSystem::VSFileType  type = VSFileSystem::UnknownFile 
) const
throw (Exception
)

Open the specified file with a suitable codec.

See Also
findByFile

Definition at line 141 of file CodecRegistry.cpp.

References Audio::Codec::open().

143  {
144  Codec *codec = findByFile(path, type);
145  return codec->open(path, type);
146  }
void Audio::CodecRegistry::remove ( Codec codec)
throw (
)

Remove a codec from the registry

Remarks
If the codec had already been removed, this is a no-op.
Parameters
codecThe codec to be removed from the registry.

Definition at line 56 of file CodecRegistry.cpp.

57  {
58  if (codecPriority.find(codec) != codecPriority.end()) {
59  codecPriority.erase(codec);
60  nameCodec.erase(codec->getName());
61  const Codec::Extensions *ext = codec->getExtensions();
62  if (ext) {
63  for (Codec::Extensions::const_iterator it = ext->begin(); it != ext->end(); ++it)
64  extensionCodecs[*it].erase(codec);
65  } else {
66  universalCodecs.erase(codec);
67  }
68  }
69  }

The documentation for this class was generated from the following files: