aRts audio server
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.

bufferqueue.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * BC - Status (2002-03-08): ByteBuffer, BufferQueue
  3. *
  4. * None of these classes is considered part of the public API. Do NOT use it
  5. * in your apps. These are part of the implementation of libartsflow's
  6. * AudioSubSystem, and subject to change/disappearing due to optimization
  7. * work.
  8. */
  9. #ifndef _BUFFERQUEUE_H
  10. #define _BUFFERQUEUE_H
  11. #include "thread.h"
  12. #define _DEFAULT_CHUNK_SIZE 4096
  13. #define _MAX_CHUNKS 3
  14. namespace Arts
  15. {
  16. class ByteBuffer
  17. {
  18. unsigned char* content;
  19. int _size;
  20. int _maxSize;
  21. int rp;
  22. public:
  23. ByteBuffer() {
  24. _size = rp = 0;
  25. _maxSize = _DEFAULT_CHUNK_SIZE;
  26. content = new unsigned char[_DEFAULT_CHUNK_SIZE];
  27. }
  28. ByteBuffer(const void* s, int len) {
  29. _maxSize = _DEFAULT_CHUNK_SIZE;
  30. content = new unsigned char[_DEFAULT_CHUNK_SIZE];
  31. put(s, len);
  32. }
  33. ~ByteBuffer() { delete [] content; }
  34. void put(const void* s, int len) {
  35. if ((_size = len) != 0)
  36. memcpy(content, s, len);
  37. rp = 0;
  38. }
  39. void* get() { return content+rp; }
  40. void* reset() { _size = 0; rp = 0; return content; }
  41. int push(int len) { _size -= len; rp += len; return _size; }
  42. void set(int len) { _size = len; rp = 0; }
  43. int size() const { return _size; }
  44. int maxSize() const { return _maxSize; }
  45. void setMaxSize(int size) {
  46. delete [] content;
  47. content = new unsigned char[size];
  48. _maxSize = size;
  49. }
  50. };
  51. ///////////////////////////////////////////////////////////////////////////////
  52. class BufferQueue
  53. {
  54. private:
  55. ByteBuffer bufs[_MAX_CHUNKS];
  56. int rp;
  57. int wp;
  58. Arts::Semaphore* sema_produced;
  59. Arts::Semaphore* sema_consumed;
  60. void semaReinit() {
  61. delete sema_consumed;
  62. delete sema_produced;
  63. sema_consumed = new Arts::Semaphore(0, _MAX_CHUNKS);
  64. sema_produced = new Arts::Semaphore(0, 0);
  65. }
  66. public:
  67. BufferQueue() {
  68. rp = wp = 0;
  69. sema_consumed = new Arts::Semaphore(0, _MAX_CHUNKS);
  70. sema_produced = new Arts::Semaphore(0, 0);
  71. }
  72. ~BufferQueue() {
  73. delete sema_consumed;
  74. delete sema_produced;
  75. }
  76. void write(void* data, int len);
  77. ByteBuffer* waitConsumed();
  78. void produced();
  79. ByteBuffer* waitProduced();
  80. void consumed();
  81. bool isEmpty() const { return sema_produced->getValue() == 0; }
  82. int bufferedChunks() const { return sema_produced->getValue(); }
  83. int freeChunks() const { return sema_consumed->getValue(); }
  84. int maxChunks() const { return _MAX_CHUNKS; }
  85. int chunkSize() const { return bufs[0].maxSize(); }
  86. void clear() { rp = wp = 0; semaReinit(); }
  87. void setChunkSize(int size){
  88. for (int i=0; i < maxChunks(); i++)
  89. bufs[i].setMaxSize(size);
  90. }
  91. };
  92. ///////////////////////////////////////////////////////////////////////////////
  93. inline void BufferQueue::write(void* data, int len)
  94. {
  95. sema_consumed->wait();
  96. bufs[wp].put(data, len);
  97. ++wp %= _MAX_CHUNKS;
  98. sema_produced->post();
  99. }
  100. inline ByteBuffer* BufferQueue::waitConsumed()
  101. {
  102. sema_consumed->wait();
  103. return &bufs[wp];
  104. }
  105. inline void BufferQueue::produced()
  106. {
  107. ++wp %= _MAX_CHUNKS;
  108. sema_produced->post();
  109. }
  110. inline ByteBuffer* BufferQueue::waitProduced()
  111. {
  112. sema_produced->wait();
  113. return &bufs[rp];
  114. }
  115. inline void BufferQueue::consumed()
  116. {
  117. ++rp %=_MAX_CHUNKS;
  118. sema_consumed->post();
  119. }
  120. }
  121. #endif