UnityAudioEncoderFactory.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "pch.h"
  2. #include <api/audio_codecs/L16/audio_encoder_L16.h>
  3. #include <api/audio_codecs/audio_encoder_factory_template.h>
  4. #include <api/audio_codecs/g711/audio_encoder_g711.h>
  5. #include <api/audio_codecs/g722/audio_encoder_g722.h>
  6. #include <api/audio_codecs/ilbc/audio_encoder_ilbc.h>
  7. #include <api/audio_codecs/isac/audio_encoder_isac.h>
  8. #include <api/audio_codecs/opus/audio_encoder_multi_channel_opus.h>
  9. #include <api/audio_codecs/opus/audio_encoder_opus.h>
  10. #include "UnityAudioEncoderFactory.h"
  11. using namespace ::webrtc;
  12. namespace unity
  13. {
  14. namespace webrtc
  15. {
  16. template<typename T>
  17. struct StereoSupportEncoder
  18. {
  19. using Config = typename T::Config;
  20. static absl::optional<Config> SdpToConfig(const SdpAudioFormat& audio_format)
  21. {
  22. return T::SdpToConfig(audio_format);
  23. }
  24. static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs)
  25. {
  26. std::vector<AudioCodecSpec> new_specs;
  27. T::AppendSupportedEncoders(&new_specs);
  28. RTC_DCHECK_EQ(new_specs.size(), 1);
  29. auto spec = new_specs[0];
  30. if (spec.format.num_channels == 2)
  31. {
  32. if (spec.format.parameters.find("stereo") == spec.format.parameters.end())
  33. spec.format.parameters.emplace("stereo", "1");
  34. if (spec.format.parameters.find("sprop-stereo") == spec.format.parameters.end())
  35. spec.format.parameters.emplace("sprop-stereo", "1");
  36. spec.info.num_channels = 2;
  37. }
  38. specs->push_back(spec);
  39. }
  40. static AudioCodecInfo QueryAudioEncoder(const Config& config) { return T::QueryAudioEncoder(config); }
  41. static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
  42. const Config& config, int payload_type, absl::optional<AudioCodecPairId> codec_pair_id = absl::nullopt)
  43. {
  44. return T::MakeAudioEncoder(config, payload_type, codec_pair_id);
  45. }
  46. };
  47. rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory()
  48. {
  49. return ::webrtc::CreateAudioEncoderFactory<
  50. StereoSupportEncoder<AudioEncoderOpus>,
  51. AudioEncoderMultiChannelOpus,
  52. AudioEncoderIlbc,
  53. AudioEncoderIsac,
  54. AudioEncoderG722,
  55. AudioEncoderG711,
  56. AudioEncoderL16>();
  57. }
  58. } // end namespace webrtc
  59. } // end namespace unity