HeadphonesDetector.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "HeadphonesDetector.h"
  2. #include "WallpaperEngine/Logging/Log.h"
  3. #include <cstring>
  4. using namespace WallpaperEngine::Audio::Drivers::Detectors;
  5. namespace {
  6. constexpr pa_usec_t STARTUP_TIMEOUT_USEC = 2 * PA_USEC_PER_SEC;
  7. bool isHeadphonesFormFactor (const char* value) {
  8. return value != nullptr && (std::strcmp (value, "headphone") == 0 || std::strcmp (value, "headset") == 0);
  9. }
  10. /** WASAPI's Headphones/Headset form factors on a stereo device, in PulseAudio terms */
  11. bool isHeadphones (const pa_sink_info& info) {
  12. if (info.sample_spec.channels != 2) {
  13. return false;
  14. }
  15. if (isHeadphonesFormFactor (pa_proplist_gets (info.proplist, PA_PROP_DEVICE_FORM_FACTOR))
  16. || isHeadphonesFormFactor (pa_proplist_gets (info.proplist, "device.form-factor"))) {
  17. return true;
  18. }
  19. if (info.active_port == nullptr) {
  20. return false;
  21. }
  22. #if PA_CHECK_VERSION(14, 0, 0)
  23. if (info.active_port->type == PA_DEVICE_PORT_TYPE_HEADPHONES
  24. || info.active_port->type == PA_DEVICE_PORT_TYPE_HEADSET) {
  25. return true;
  26. }
  27. #endif
  28. // what OpenAL Soft's own PulseAudio backend checks
  29. return std::strcmp (info.active_port->name, "analog-output-headphones") == 0;
  30. }
  31. }
  32. HeadphonesDetector::HeadphonesDetector () {
  33. this->m_mainloop = pa_threaded_mainloop_new ();
  34. this->connect (PA_CONTEXT_NOFLAGS);
  35. if (pa_threaded_mainloop_start (this->m_mainloop) < 0) {
  36. sLog.error ("Cannot start the sound server connection, headphones detection is disabled");
  37. return;
  38. }
  39. // the first sounds start right after this, so wait for the first answer, but not on a server that never gives one
  40. pa_threaded_mainloop_lock (this->m_mainloop);
  41. pa_mainloop_api* api = pa_threaded_mainloop_get_api (this->m_mainloop);
  42. timeval deadline {};
  43. pa_timeval_add (pa_gettimeofday (&deadline), STARTUP_TIMEOUT_USEC);
  44. pa_time_event* timeout = api->time_new (api, &deadline, &HeadphonesDetector::startupTimeout, this);
  45. while (!this->m_startupExpired) {
  46. const pa_context_state_t state = pa_context_get_state (this->m_context);
  47. if (!PA_CONTEXT_IS_GOOD (state) || (state == PA_CONTEXT_READY && this->m_pending == 0)) {
  48. break;
  49. }
  50. pa_threaded_mainloop_wait (this->m_mainloop);
  51. }
  52. api->time_free (timeout);
  53. pa_threaded_mainloop_unlock (this->m_mainloop);
  54. if (this->m_startupExpired) {
  55. sLog.debug ("The sound server didn't answer in time, headphones detection goes on in the background");
  56. }
  57. }
  58. HeadphonesDetector::~HeadphonesDetector () {
  59. if (this->m_mainloop != nullptr) {
  60. pa_threaded_mainloop_stop (this->m_mainloop);
  61. }
  62. if (this->m_reconnect != nullptr) {
  63. pa_threaded_mainloop_get_api (this->m_mainloop)->time_free (this->m_reconnect);
  64. }
  65. if (this->m_context != nullptr) {
  66. pa_context_disconnect (this->m_context);
  67. pa_context_unref (this->m_context);
  68. }
  69. if (this->m_mainloop != nullptr) {
  70. pa_threaded_mainloop_free (this->m_mainloop);
  71. }
  72. }
  73. void HeadphonesDetector::connect (pa_context_flags_t flags) {
  74. if (this->m_context != nullptr) {
  75. pa_context_set_state_callback (this->m_context, nullptr, nullptr);
  76. pa_context_set_subscribe_callback (this->m_context, nullptr, nullptr);
  77. pa_context_disconnect (this->m_context);
  78. pa_context_unref (this->m_context);
  79. }
  80. this->m_pending = 0;
  81. this->m_wasReady = false;
  82. this->m_context = pa_context_new (pa_threaded_mainloop_get_api (this->m_mainloop), "wallpaperengine-output");
  83. pa_context_set_state_callback (this->m_context, &HeadphonesDetector::contextState, this);
  84. if (pa_context_connect (this->m_context, nullptr, flags, nullptr) < 0) {
  85. // the state callback has seen the failure already when the mainloop runs, before that it's the caller's
  86. sLog.debug ("Cannot connect to the sound server: ", pa_strerror (pa_context_errno (this->m_context)));
  87. }
  88. }
  89. void HeadphonesDetector::scheduleReconnect (pa_usec_t delay) {
  90. pa_mainloop_api* api = pa_threaded_mainloop_get_api (this->m_mainloop);
  91. timeval when {};
  92. pa_timeval_add (pa_gettimeofday (&when), delay);
  93. // the context can't be replaced from inside its own state callback
  94. if (this->m_reconnect != nullptr) {
  95. api->time_restart (this->m_reconnect, &when);
  96. } else {
  97. this->m_reconnect = api->time_new (api, &when, &HeadphonesDetector::reconnectTimer, this);
  98. }
  99. }
  100. void HeadphonesDetector::startupTimeout (pa_mainloop_api*, pa_time_event*, const timeval*, void* userdata) {
  101. auto* detector = static_cast<HeadphonesDetector*> (userdata);
  102. detector->m_startupExpired = true;
  103. pa_threaded_mainloop_signal (detector->m_mainloop, 0);
  104. }
  105. void HeadphonesDetector::reconnectTimer (pa_mainloop_api* api, pa_time_event* event, const timeval*, void* userdata) {
  106. auto* detector = static_cast<HeadphonesDetector*> (userdata);
  107. api->time_free (event);
  108. detector->m_reconnect = nullptr;
  109. detector->connect (static_cast<pa_context_flags_t> (PA_CONTEXT_NOFAIL));
  110. }
  111. void HeadphonesDetector::contextState (pa_context* context, void* userdata) {
  112. auto* detector = static_cast<HeadphonesDetector*> (userdata);
  113. switch (pa_context_get_state (context)) {
  114. case PA_CONTEXT_READY:
  115. {
  116. pa_context_set_subscribe_callback (context, &HeadphonesDetector::subscription, detector);
  117. if (pa_operation* operation = pa_context_subscribe (
  118. context,
  119. static_cast<pa_subscription_mask_t> (PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER),
  120. nullptr, nullptr
  121. )) {
  122. pa_operation_unref (operation);
  123. }
  124. detector->m_wasReady = true;
  125. detector->query ();
  126. break;
  127. }
  128. case PA_CONTEXT_FAILED:
  129. case PA_CONTEXT_TERMINATED:
  130. detector->m_headphones.store (false, std::memory_order_relaxed);
  131. pa_threaded_mainloop_signal (detector->m_mainloop, 0);
  132. // a server that went away: reconnect at once, NOFAIL then waits for it on D-Bus. A connection that never
  133. // got anywhere had no way to wait (no D-Bus, PULSE_SERVER), try again later
  134. detector->scheduleReconnect (detector->m_wasReady ? 0 : 2 * PA_USEC_PER_SEC);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. void HeadphonesDetector::subscription (pa_context*, pa_subscription_event_type_t, uint32_t, void* userdata) {
  141. static_cast<HeadphonesDetector*> (userdata)->query ();
  142. }
  143. void HeadphonesDetector::query () {
  144. if (pa_operation* operation = pa_context_get_server_info (this->m_context, &HeadphonesDetector::serverInfo, this)) {
  145. this->m_pending++;
  146. pa_operation_unref (operation);
  147. }
  148. }
  149. void HeadphonesDetector::serverInfo (pa_context* context, const pa_server_info* info, void* userdata) {
  150. auto* detector = static_cast<HeadphonesDetector*> (userdata);
  151. if (info != nullptr && info->default_sink_name != nullptr) {
  152. if (pa_operation* operation = pa_context_get_sink_info_by_name (
  153. context, info->default_sink_name, &HeadphonesDetector::sinkInfo, detector
  154. )) {
  155. detector->m_pending++;
  156. pa_operation_unref (operation);
  157. }
  158. } else {
  159. detector->m_headphones.store (false, std::memory_order_relaxed);
  160. }
  161. detector->m_pending--;
  162. pa_threaded_mainloop_signal (detector->m_mainloop, 0);
  163. }
  164. void HeadphonesDetector::sinkInfo (pa_context*, const pa_sink_info* info, int eol, void* userdata) {
  165. auto* detector = static_cast<HeadphonesDetector*> (userdata);
  166. if (eol != 0) {
  167. detector->m_pending--;
  168. pa_threaded_mainloop_signal (detector->m_mainloop, 0);
  169. return;
  170. }
  171. if (info == nullptr) {
  172. return;
  173. }
  174. const bool headphones = isHeadphones (*info);
  175. const std::string report = std::string (info->name) + (headphones ? " (headphones)" : "");
  176. detector->m_headphones.store (headphones, std::memory_order_relaxed);
  177. if (report != detector->m_lastReport) {
  178. detector->m_lastReport = report;
  179. sLog.debug ("Default audio output: ", report);
  180. }
  181. }