2
0

AudioCodecs.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "AudioCodecs.h"
  2. #include <map> // for map, operator!=, map<>::iterator, map<>:...
  3. #include <type_traits> // for remove_extent_t
  4. #include "AudioContainer.h" // for AudioContainer
  5. namespace bell {
  6. class BaseCodec;
  7. } // namespace bell
  8. using namespace bell;
  9. #ifdef BELL_CODEC_AAC
  10. #include "AACDecoder.h" // for AACDecoder
  11. static std::shared_ptr<AACDecoder> codecAac;
  12. #endif
  13. #ifdef BELL_CODEC_MP3
  14. #include "MP3Decoder.h" // for MP3Decoder
  15. static std::shared_ptr<MP3Decoder> codecMp3;
  16. #endif
  17. #ifdef BELL_CODEC_VORBIS
  18. #include "VorbisDecoder.h" // for VorbisDecoder
  19. static std::shared_ptr<VorbisDecoder> codecVorbis;
  20. #endif
  21. #ifdef BELL_CODEC_OPUS
  22. #include "OPUSDecoder.h" // for OPUSDecoder
  23. static std::shared_ptr<OPUSDecoder> codecOpus;
  24. #endif
  25. std::map<AudioCodec, std::shared_ptr<BaseCodec>> customCodecs;
  26. std::shared_ptr<BaseCodec> AudioCodecs::getCodec(AudioCodec type) {
  27. if (customCodecs.find(type) != customCodecs.end())
  28. return customCodecs[type];
  29. switch (type) {
  30. #ifdef BELL_CODEC_AAC
  31. case AudioCodec::AAC:
  32. if (codecAac)
  33. return codecAac;
  34. codecAac = std::make_shared<AACDecoder>();
  35. return codecAac;
  36. #endif
  37. #ifdef BELL_CODEC_MP3
  38. case AudioCodec::MP3:
  39. if (codecMp3)
  40. return codecMp3;
  41. codecMp3 = std::make_shared<MP3Decoder>();
  42. return codecMp3;
  43. #endif
  44. #ifdef BELL_CODEC_VORBIS
  45. case AudioCodec::VORBIS:
  46. if (codecVorbis)
  47. return codecVorbis;
  48. codecVorbis = std::make_shared<VorbisDecoder>();
  49. return codecVorbis;
  50. #endif
  51. #ifdef BELL_CODEC_OPUS
  52. case AudioCodec::OPUS:
  53. if (codecOpus)
  54. return codecOpus;
  55. codecOpus = std::make_shared<OPUSDecoder>();
  56. return codecOpus;
  57. #endif
  58. default:
  59. return nullptr;
  60. }
  61. }
  62. std::shared_ptr<BaseCodec> AudioCodecs::getCodec(AudioContainer* container) {
  63. auto codec = getCodec(container->getCodec());
  64. if (codec != nullptr) {
  65. codec->setup(container);
  66. }
  67. return codec;
  68. }
  69. void AudioCodecs::addCodec(AudioCodec type,
  70. const std::shared_ptr<BaseCodec>& codec) {
  71. customCodecs[type] = codec;
  72. }