AudioPlayerProvider.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include "audio/android/IAudioPlayer.h"
  23. #include "audio/android/OpenSLHelper.h"
  24. #include "audio/android/PcmData.h"
  25. #include <unordered_map>
  26. #include <memory>
  27. #include <condition_variable>
  28. namespace cocos2d { namespace experimental {
  29. // Manage PcmAudioPlayer& UrlAudioPlayer
  30. class PcmAudioPlayer;
  31. class PcmAudioService;
  32. class UrlAudioPlayer;
  33. class AudioMixerController;
  34. class ICallerThreadUtils;
  35. class AssetFd;
  36. class ThreadPool;
  37. class AudioPlayerProvider
  38. {
  39. public:
  40. AudioPlayerProvider(SLEngineItf engineItf, SLObjectItf outputMixObject, int deviceSampleRate,
  41. int bufferSizeInFrames, const FdGetterCallback &fdGetterCallback,
  42. ICallerThreadUtils* callerThreadUtils);
  43. virtual ~AudioPlayerProvider();
  44. IAudioPlayer *getAudioPlayer(const std::string &audioFilePath);
  45. typedef std::function<void(bool/* succeed */, PcmData /* data */)> PreloadCallback;
  46. void preloadEffect(const std::string &audioFilePath, const PreloadCallback& cb);
  47. void clearPcmCache(const std::string &audioFilePath);
  48. void clearAllPcmCaches();
  49. void pause();
  50. void resume();
  51. private:
  52. struct AudioFileInfo
  53. {
  54. std::string url;
  55. std::shared_ptr<AssetFd> assetFd;
  56. off_t start;
  57. off_t length;
  58. AudioFileInfo()
  59. : assetFd(nullptr), start(0), length(0)
  60. { };
  61. inline bool isValid() const
  62. {
  63. return !url.empty() && length > 0;
  64. }
  65. };
  66. PcmAudioPlayer *obtainPcmAudioPlayer(const std::string &url, const PcmData &pcmData);
  67. UrlAudioPlayer *createUrlAudioPlayer(const AudioFileInfo &info);
  68. void preloadEffect(const AudioFileInfo &info, const PreloadCallback& cb, bool isPreloadInPlay2d);
  69. AudioFileInfo getFileInfo(const std::string &audioFilePath);
  70. bool isSmallFile(const AudioFileInfo &info);
  71. private:
  72. SLEngineItf _engineItf;
  73. SLObjectItf _outputMixObject;
  74. int _deviceSampleRate;
  75. int _bufferSizeInFrames;
  76. FdGetterCallback _fdGetterCallback;
  77. ICallerThreadUtils* _callerThreadUtils;
  78. std::unordered_map<std::string, PcmData> _pcmCache;
  79. std::mutex _pcmCacheMutex;
  80. struct PreloadCallbackParam
  81. {
  82. PreloadCallback callback;
  83. bool isPreloadInPlay2d;
  84. };
  85. std::unordered_map<std::string, std::vector<PreloadCallbackParam>> _preloadCallbackMap;
  86. std::mutex _preloadCallbackMutex;
  87. std::mutex _preloadWaitMutex;
  88. std::condition_variable _preloadWaitCond;
  89. PcmAudioService* _pcmAudioService;
  90. AudioMixerController *_mixController;
  91. ThreadPool* _threadPool;
  92. };
  93. }} // namespace cocos2d { namespace experimental {