AudioOutputPulse.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // libtgvoip is free and unencumbered public domain software.
  3. // For more information, see http://unlicense.org or the UNLICENSE file
  4. // you should have received with this source code distribution.
  5. //
  6. #include <assert.h>
  7. #include <dlfcn.h>
  8. #include <unistd.h>
  9. #include "AudioOutputPulse.h"
  10. #include "../../logging.h"
  11. #include "../../VoIPController.h"
  12. #include "AudioPulse.h"
  13. #include "PulseFunctions.h"
  14. #if !defined(__GLIBC__)
  15. #include <libgen.h>
  16. #endif
  17. #define BUFFER_SIZE 960
  18. #define CHECK_ERROR(res, msg) if(res!=0){LOGE(msg " failed: %s", pa_strerror(res)); failed=true; return;}
  19. using namespace tgvoip;
  20. using namespace tgvoip::audio;
  21. AudioOutputPulse::AudioOutputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID){
  22. isPlaying=false;
  23. isConnected=false;
  24. didStart=false;
  25. isLocked=false;
  26. this->mainloop=mainloop;
  27. this->context=context;
  28. stream=NULL;
  29. remainingDataSize=0;
  30. pa_threaded_mainloop_lock(mainloop);
  31. stream=CreateAndInitStream();
  32. pa_threaded_mainloop_unlock(mainloop);
  33. SetCurrentDevice(devID);
  34. }
  35. AudioOutputPulse::~AudioOutputPulse(){
  36. if(stream){
  37. pa_stream_disconnect(stream);
  38. pa_stream_unref(stream);
  39. }
  40. }
  41. pa_stream* AudioOutputPulse::CreateAndInitStream(){
  42. pa_sample_spec sampleSpec{
  43. .format=PA_SAMPLE_S16LE,
  44. .rate=48000,
  45. .channels=1
  46. };
  47. pa_proplist* proplist=pa_proplist_new();
  48. pa_proplist_sets(proplist, PA_PROP_FILTER_APPLY, ""); // according to PA sources, this disables any possible filters
  49. pa_stream* stream=pa_stream_new_with_proplist(context, "libtgvoip playback", &sampleSpec, NULL, proplist);
  50. pa_proplist_free(proplist);
  51. if(!stream){
  52. LOGE("Error initializing PulseAudio (pa_stream_new)");
  53. failed=true;
  54. return NULL;
  55. }
  56. pa_stream_set_state_callback(stream, AudioOutputPulse::StreamStateCallback, this);
  57. pa_stream_set_write_callback(stream, AudioOutputPulse::StreamWriteCallback, this);
  58. return stream;
  59. }
  60. void AudioOutputPulse::Start(){
  61. if(failed || isPlaying)
  62. return;
  63. isPlaying=true;
  64. pa_threaded_mainloop_lock(mainloop);
  65. pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
  66. pa_threaded_mainloop_unlock(mainloop);
  67. }
  68. void AudioOutputPulse::Stop(){
  69. if(!isPlaying)
  70. return;
  71. isPlaying=false;
  72. pa_threaded_mainloop_lock(mainloop);
  73. pa_operation_unref(pa_stream_cork(stream, 1, NULL, NULL));
  74. pa_threaded_mainloop_unlock(mainloop);
  75. }
  76. bool AudioOutputPulse::IsPlaying(){
  77. return isPlaying;
  78. }
  79. void AudioOutputPulse::SetCurrentDevice(std::string devID){
  80. pa_threaded_mainloop_lock(mainloop);
  81. currentDevice=devID;
  82. if(isPlaying && isConnected){
  83. pa_stream_disconnect(stream);
  84. pa_stream_unref(stream);
  85. isConnected=false;
  86. stream=CreateAndInitStream();
  87. }
  88. pa_buffer_attr bufferAttr={
  89. .maxlength=(uint32_t)-1,
  90. .tlength=960*2,
  91. .prebuf=(uint32_t)-1,
  92. .minreq=(uint32_t)-1,
  93. .fragsize=(uint32_t)-1
  94. };
  95. int streamFlags=PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY;
  96. int err=pa_stream_connect_playback(stream, devID=="default" ? NULL : devID.c_str(), &bufferAttr, (pa_stream_flags_t)streamFlags, NULL, NULL);
  97. if(err!=0 && devID!="default"){
  98. SetCurrentDevice("default");
  99. return;
  100. }
  101. CHECK_ERROR(err, "pa_stream_connect_playback");
  102. while(true){
  103. pa_stream_state_t streamState=pa_stream_get_state(stream);
  104. if(!PA_STREAM_IS_GOOD(streamState)){
  105. LOGE("Error connecting to audio device '%s'", devID.c_str());
  106. failed=true;
  107. return;
  108. }
  109. if(streamState==PA_STREAM_READY)
  110. break;
  111. pa_threaded_mainloop_wait(mainloop);
  112. }
  113. isConnected=true;
  114. if(isPlaying){
  115. pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
  116. }
  117. pa_threaded_mainloop_unlock(mainloop);
  118. }
  119. bool AudioOutputPulse::EnumerateDevices(std::vector<AudioOutputDevice>& devs){
  120. return AudioPulse::DoOneOperation([&](pa_context* ctx){
  121. return pa_context_get_sink_info_list(ctx, [](pa_context* ctx, const pa_sink_info* info, int eol, void* userdata){
  122. if(eol>0)
  123. return;
  124. std::vector<AudioOutputDevice>* devs=(std::vector<AudioOutputDevice>*)userdata;
  125. AudioOutputDevice dev;
  126. dev.id=std::string(info->name);
  127. dev.displayName=std::string(info->description);
  128. devs->push_back(dev);
  129. }, &devs);
  130. });
  131. }
  132. void AudioOutputPulse::StreamStateCallback(pa_stream *s, void* arg) {
  133. AudioOutputPulse* self=(AudioOutputPulse*) arg;
  134. pa_threaded_mainloop_signal(self->mainloop, 0);
  135. }
  136. void AudioOutputPulse::StreamWriteCallback(pa_stream *stream, size_t requestedBytes, void *userdata){
  137. ((AudioOutputPulse*)userdata)->StreamWriteCallback(stream, requestedBytes);
  138. }
  139. void AudioOutputPulse::StreamWriteCallback(pa_stream *stream, size_t requestedBytes) {
  140. //assert(requestedBytes<=sizeof(remainingData));
  141. if(requestedBytes>sizeof(remainingData)){
  142. requestedBytes=960*2; // force buffer size to 20ms. This probably wrecks the jitter buffer, but still better than crashing
  143. }
  144. pa_usec_t latency;
  145. if(pa_stream_get_latency(stream, &latency, NULL)==0){
  146. estimatedDelay=(int32_t)(latency/100);
  147. }
  148. while(requestedBytes>remainingDataSize){
  149. if(isPlaying){
  150. InvokeCallback(remainingData+remainingDataSize, 960*2);
  151. remainingDataSize+=960*2;
  152. }else{
  153. memset(remainingData+remainingDataSize, 0, requestedBytes-remainingDataSize);
  154. remainingDataSize=requestedBytes;
  155. }
  156. }
  157. int err=pa_stream_write(stream, remainingData, requestedBytes, NULL, 0, PA_SEEK_RELATIVE);
  158. CHECK_ERROR(err, "pa_stream_write");
  159. remainingDataSize-=requestedBytes;
  160. if(remainingDataSize>0)
  161. memmove(remainingData, remainingData+requestedBytes, remainingDataSize);
  162. }