ALSAAudioSink.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "ALSAAudioSink.h"
  2. ALSAAudioSink::ALSAAudioSink() : Task("", 0, 0, 0)
  3. {
  4. /* Open the PCM device in playback mode */
  5. if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
  6. SND_PCM_STREAM_PLAYBACK, 0) < 0)
  7. {
  8. printf("ERROR: Can't open \"%s\" PCM device. %s\n",
  9. PCM_DEVICE, snd_strerror(pcm));
  10. }
  11. /* Allocate parameters object and fill it with default values*/
  12. snd_pcm_hw_params_alloca(&params);
  13. snd_pcm_hw_params_any(pcm_handle, params);
  14. /* Set parameters */
  15. if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
  16. SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
  17. printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
  18. if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
  19. SND_PCM_FORMAT_S16_LE) < 0)
  20. printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
  21. if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, 2) < 0)
  22. printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
  23. unsigned int rate = 44100;
  24. if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
  25. printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
  26. unsigned int periodTime = 800;
  27. int dir = -1;
  28. snd_pcm_hw_params_set_period_time_near(pcm_handle, params, &periodTime, &dir);
  29. /* Write parameters */
  30. if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
  31. printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
  32. /* Resume information */
  33. printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
  34. printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
  35. unsigned int tmp;
  36. snd_pcm_hw_params_get_channels(params, &tmp);
  37. printf("channels: %i ", tmp);
  38. if (tmp == 1)
  39. printf("(mono)\n");
  40. else if (tmp == 2)
  41. printf("(stereo)\n");
  42. snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
  43. printf("period_time = %d\n", tmp);
  44. snd_pcm_hw_params_get_period_size(params, &frames, 0);
  45. this->buff_size = frames * 2 * 2 /* 2 -> sample size */;
  46. printf("required buff_size: %d\n", buff_size);
  47. this->startTask();
  48. }
  49. ALSAAudioSink::~ALSAAudioSink()
  50. {
  51. snd_pcm_drain(pcm_handle);
  52. snd_pcm_close(pcm_handle);
  53. }
  54. void ALSAAudioSink::runTask()
  55. {
  56. std::unique_ptr<std::vector<uint8_t>> dataPtr;
  57. while (true)
  58. {
  59. if (!this->ringbuffer.pop(dataPtr))
  60. {
  61. usleep(100);
  62. continue;
  63. }
  64. if (pcm = snd_pcm_writei(pcm_handle, dataPtr->data(), this->frames) == -EPIPE)
  65. {
  66. snd_pcm_prepare(pcm_handle);
  67. }
  68. else if (pcm < 0)
  69. {
  70. printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
  71. }
  72. }
  73. }
  74. void ALSAAudioSink::feedPCMFrames(const uint8_t *buffer, size_t bytes)
  75. {
  76. buff.insert(buff.end(), buffer, buffer + bytes);
  77. while (buff.size() > this->buff_size)
  78. {
  79. auto ptr = std::make_unique<std::vector<uint8_t>>(this->buff.begin(), this->buff.begin() + this->buff_size);
  80. this->buff = std::vector<uint8_t>(this->buff.begin() + this->buff_size, this->buff.end());
  81. while (!this->ringbuffer.push(ptr))
  82. {
  83. usleep(100);
  84. };
  85. }
  86. }