AudioCodecs.cpp 1.8 KB

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