PcmBufferProvider.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /****************************************************************************
  2. Copyright (c) 2016-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #define LOG_TAG "PcmBufferProvider"
  21. #include "audio/android/cutils/log.h"
  22. #include "audio/android/PcmBufferProvider.h"
  23. //#define VERY_VERY_VERBOSE_LOGGING
  24. #ifdef VERY_VERY_VERBOSE_LOGGING
  25. #define ALOGVV ALOGV
  26. #else
  27. #define ALOGVV(a...) do { } while (0)
  28. #endif
  29. namespace cocos2d { namespace experimental {
  30. PcmBufferProvider::PcmBufferProvider()
  31. : _addr(nullptr)
  32. , _numFrames(0)
  33. , _frameSize(0)
  34. , _nextFrame(0)
  35. , _unrel(0)
  36. {
  37. }
  38. bool PcmBufferProvider::init(const void *addr, size_t frames, size_t frameSize)
  39. {
  40. _addr = addr;
  41. _numFrames = frames;
  42. _frameSize = frameSize;
  43. _nextFrame = 0;
  44. _unrel = 0;
  45. return true;
  46. }
  47. status_t PcmBufferProvider::getNextBuffer(Buffer *buffer,
  48. int64_t pts/* = kInvalidPTS*/) {
  49. (void) pts; // suppress warning
  50. size_t requestedFrames = buffer->frameCount;
  51. if (requestedFrames > _numFrames - _nextFrame) {
  52. buffer->frameCount = _numFrames - _nextFrame;
  53. }
  54. ALOGVV("getNextBuffer() requested %zu frames out of %zu frames available,"
  55. " and returned %zu frames",
  56. requestedFrames, (size_t) (_numFrames - _nextFrame), buffer->frameCount);
  57. _unrel = buffer->frameCount;
  58. if (buffer->frameCount > 0) {
  59. buffer->raw = (char *) _addr + _frameSize * _nextFrame;
  60. return NO_ERROR;
  61. } else {
  62. buffer->raw = NULL;
  63. return NOT_ENOUGH_DATA;
  64. }
  65. }
  66. void PcmBufferProvider::releaseBuffer(Buffer *buffer) {
  67. if (buffer->frameCount > _unrel) {
  68. ALOGVV("ERROR releaseBuffer() released %zu frames but only %zu available "
  69. "to release", buffer->frameCount, _unrel);
  70. _nextFrame += _unrel;
  71. _unrel = 0;
  72. } else {
  73. ALOGVV("releaseBuffer() released %zu frames out of %zu frames available "
  74. "to release", buffer->frameCount, _unrel);
  75. _nextFrame += buffer->frameCount;
  76. _unrel -= buffer->frameCount;
  77. }
  78. buffer->frameCount = 0;
  79. buffer->raw = NULL;
  80. }
  81. void PcmBufferProvider::reset() {
  82. _nextFrame = 0;
  83. }
  84. }} // namespace cocos2d { namespace experimental {