fifo_threads.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <audio_utils/fifo.h>
  21. extern "C" {
  22. #include "getch.h"
  23. }
  24. struct Context {
  25. audio_utils_fifo_writer *mInputWriter;
  26. audio_utils_fifo_reader *mInputReader;
  27. audio_utils_fifo_writer *mTransferWriter;
  28. audio_utils_fifo_reader *mTransferReader;
  29. audio_utils_fifo_writer *mOutputWriter;
  30. audio_utils_fifo_reader *mOutputReader;
  31. };
  32. void *input_routine(void *arg)
  33. {
  34. Context *context = (Context *) arg;
  35. for (;;) {
  36. struct timespec timeout;
  37. timeout.tv_sec = 30;
  38. timeout.tv_nsec = 0;
  39. char buffer[4];
  40. ssize_t actual = context->mInputReader->read(buffer, sizeof(buffer), &timeout);
  41. // TODO this test is unreadable
  42. if (actual > 0) {
  43. if ((size_t) actual > sizeof(buffer)) {
  44. printf("input.read actual = %d\n", (int) actual);
  45. abort();
  46. }
  47. ssize_t actual2 = context->mTransferWriter->write(buffer, actual, &timeout);
  48. if (actual2 != actual) {
  49. printf("transfer.write(%d) = %d\n", (int) actual, (int) actual2);
  50. }
  51. //sleep(10);
  52. } else if (actual == -ETIMEDOUT) {
  53. (void) write(1, "t", 1);
  54. } else {
  55. printf("input.read actual = %d\n", (int) actual);
  56. }
  57. }
  58. return NULL;
  59. }
  60. volatile bool outputPaused = false;
  61. void *output_routine(void *arg)
  62. {
  63. Context *context = (Context *) arg;
  64. for (;;) {
  65. if (outputPaused) {
  66. sleep(1);
  67. continue;
  68. }
  69. struct timespec timeout;
  70. timeout.tv_sec = 60;
  71. timeout.tv_nsec = 0;
  72. char buffer[4];
  73. ssize_t actual = context->mTransferReader->read(buffer, sizeof(buffer), &timeout);
  74. if (actual > 0) {
  75. if ((size_t) actual > sizeof(buffer)) {
  76. printf("transfer.read actual = %d\n", (int) actual);
  77. abort();
  78. }
  79. ssize_t actual2 = context->mOutputWriter->write(buffer, actual, NULL /*timeout*/);
  80. if (actual2 != actual) {
  81. printf("output.write(%d) = %d\n", (int) actual, (int) actual2);
  82. }
  83. } else if (actual == -ETIMEDOUT) {
  84. (void) write(1, "T", 1);
  85. } else {
  86. printf("transfer.read actual = %d\n", (int) actual);
  87. }
  88. }
  89. return NULL;
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. set_conio_terminal_mode();
  94. argc = argc + 0;
  95. argv = &argv[0];
  96. char inputBuffer[16];
  97. audio_utils_fifo inputFifo(sizeof(inputBuffer) /*frameCount*/, 1 /*frameSize*/, inputBuffer,
  98. true /*throttlesWriter*/);
  99. audio_utils_fifo_writer inputWriter(inputFifo);
  100. audio_utils_fifo_reader inputReader(inputFifo, true /*throttlesWriter*/);
  101. //inputWriter.setHysteresis(sizeof(inputBuffer) * 1/4, sizeof(inputBuffer) * 3/4);
  102. char transferBuffer[64];
  103. audio_utils_fifo transferFifo(sizeof(transferBuffer) /*frameCount*/, 1 /*frameSize*/,
  104. transferBuffer, true /*throttlesWriter*/);
  105. audio_utils_fifo_writer transferWriter(transferFifo);
  106. audio_utils_fifo_reader transferReader(transferFifo, true /*throttlesWriter*/);
  107. transferReader.setHysteresis(sizeof(transferBuffer) * 3/4, sizeof(transferBuffer) * 1/4);
  108. //transferWriter.setEffective(8);
  109. char outputBuffer[64];
  110. audio_utils_fifo outputFifo(sizeof(outputBuffer) /*frameCount*/, 1 /*frameSize*/, outputBuffer,
  111. true /*throttlesWriter*/);
  112. audio_utils_fifo_writer outputWriter(outputFifo);
  113. audio_utils_fifo_reader outputReader(outputFifo, true /*readerThrottlesWriter*/);
  114. Context context;
  115. context.mInputWriter = &inputWriter;
  116. context.mInputReader = &inputReader;
  117. context.mTransferWriter = &transferWriter;
  118. context.mTransferReader = &transferReader;
  119. context.mOutputWriter = &outputWriter;
  120. context.mOutputReader = &outputReader;
  121. pthread_t input_thread;
  122. int ok = pthread_create(&input_thread, (const pthread_attr_t *) NULL, input_routine,
  123. (void *) &context);
  124. pthread_t output_thread;
  125. ok = pthread_create(&output_thread, (const pthread_attr_t *) NULL, output_routine,
  126. (void *) &context);
  127. ok = ok + 0;
  128. for (;;) {
  129. char buffer[4];
  130. ssize_t actual = outputReader.read(buffer, sizeof(buffer), NULL /*timeout*/);
  131. if (actual > 0) {
  132. printf("%.*s", (int) actual, buffer);
  133. fflush(stdout);
  134. } else if (actual != 0) {
  135. printf("outputReader.read actual = %d\n", (int) actual);
  136. }
  137. if (kbhit()) {
  138. int ch = getch();
  139. if (ch <= 0 || ch == '\003' /*control-C*/) {
  140. break;
  141. }
  142. if (ch == 'p')
  143. outputPaused = true;
  144. else if (ch == 'p')
  145. outputPaused = false;
  146. buffer[0] = ch;
  147. actual = inputWriter.write(buffer, 1, NULL /*timeout*/);
  148. if (actual != 1) {
  149. printf("inputWriter.write actual = %d\n", (int) actual);
  150. }
  151. }
  152. }
  153. reset_terminal_mode();
  154. }