You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.0KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef AUDIOSRC_H_
  11. #define AUDIOSRC_H_
  12. #include <memory>
  13. #include <string>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include "SDL2/SDL.h"
  18. #ifdef __cplusplus
  19. }
  20. #endif
  21. #if defined(_WIN32)
  22. #pragma comment(lib,"SDL2.lib")
  23. #endif //defined(_WIN32)
  24. #include "Network/Buffer.h"
  25. #include "SDLAudioDevice.h"
  26. class AudioSRCDelegate {
  27. public:
  28. virtual ~AudioSRCDelegate() {};
  29. virtual SDL_AudioFormat getPCMFormat() = 0;
  30. virtual int getPCMSampleRate() = 0;
  31. virtual int getPCMChannel() = 0;
  32. virtual int getPCMData(char *buf, int size) = 0;
  33. };
  34. //该类实现pcm的重采样
  35. class AudioSRC {
  36. public:
  37. using Ptr = std::shared_ptr<AudioSRC>;
  38. AudioSRC(AudioSRCDelegate *);
  39. virtual ~AudioSRC();
  40. void setEnableMix(bool flag);
  41. void setOutputAudioConfig(const SDL_AudioSpec &cfg);
  42. int getPCMData(char *buf, int size);
  43. private:
  44. bool _enabled = true;
  45. int _buf_size = 0;
  46. std::shared_ptr<char> _buf;
  47. AudioSRCDelegate *_delegate = nullptr;
  48. toolkit::BufferLikeString _target_buf;
  49. SDL_AudioCVT _audio_cvt;
  50. };
  51. class AudioPlayer : public AudioSRC, private AudioSRCDelegate{
  52. public:
  53. AudioPlayer();
  54. ~AudioPlayer() override;
  55. void setup(int sample_rate, int channel, SDL_AudioFormat format);
  56. void playPCM(const char *data, size_t size);
  57. private:
  58. SDL_AudioFormat getPCMFormat() override;
  59. int getPCMSampleRate() override;
  60. int getPCMChannel() override;
  61. int getPCMData(char *buf, int size) override;
  62. private:
  63. int _sample_rate, _channel;
  64. SDL_AudioFormat _format;
  65. std::mutex _mtx;
  66. toolkit::BufferLikeString _buffer;
  67. SDLAudioDevice::Ptr _device;
  68. };
  69. #endif /* AUDIOSRC_H_ */