Fix global_constructors, exit_time_destructors in audio device pulse.

Bug: webrtc:9693
Change-Id: I05498473be8a86756d65d0b9000d626c966d4ed3
Reviewed-on: https://webrtc-review.googlesource.com/100422
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24865}
diff --git a/modules/audio_device/BUILD.gn b/modules/audio_device/BUILD.gn
index 70c9c0a..2759da4 100644
--- a/modules/audio_device/BUILD.gn
+++ b/modules/audio_device/BUILD.gn
@@ -321,10 +321,6 @@
           defines += [ "WEBRTC_USE_X11" ]
         }
         if (rtc_include_pulse_audio) {
-          configs += [
-            "../..:no_exit_time_destructors",
-            "../..:no_global_constructors",
-          ]
           sources += [
             "linux/audio_device_pulse_linux.cc",
             "linux/audio_device_pulse_linux.h",
diff --git a/modules/audio_device/linux/audio_device_alsa_linux.cc b/modules/audio_device/linux/audio_device_alsa_linux.cc
index e6cfcb8..bef4ef3 100644
--- a/modules/audio_device/linux/audio_device_alsa_linux.cc
+++ b/modules/audio_device/linux/audio_device_alsa_linux.cc
@@ -22,6 +22,13 @@
   return alsa_symbol_table;
 }
 
+// Accesses ALSA functions through our late-binding symbol table instead of
+// directly. This way we don't have to link to libasound, which means our binary
+// will work on systems that don't have it.
+#define LATE(sym)                                                            \
+  LATESYM_GET(webrtc::adm_linux_alsa::AlsaSymbolTable, GetAlsaSymbolTable(), \
+              sym)
+
 // Redefine these here to be able to do late-binding
 #undef snd_ctl_card_info_alloca
 #define snd_ctl_card_info_alloca(ptr)                  \
diff --git a/modules/audio_device/linux/audio_device_alsa_linux.h b/modules/audio_device/linux/audio_device_alsa_linux.h
index 1c78ef1..3d15565 100644
--- a/modules/audio_device/linux/audio_device_alsa_linux.h
+++ b/modules/audio_device/linux/audio_device_alsa_linux.h
@@ -28,13 +28,6 @@
 typedef webrtc::adm_linux_alsa::AlsaSymbolTable WebRTCAlsaSymbolTable;
 WebRTCAlsaSymbolTable* GetAlsaSymbolTable();
 
-// Accesses ALSA functions through our late-binding symbol table instead of
-// directly. This way we don't have to link to libasound, which means our binary
-// will work on systems that don't have it.
-#define LATE(sym)                                                            \
-  LATESYM_GET(webrtc::adm_linux_alsa::AlsaSymbolTable, GetAlsaSymbolTable(), \
-              sym)
-
 namespace webrtc {
 class EventWrapper;
 
diff --git a/modules/audio_device/linux/audio_device_pulse_linux.cc b/modules/audio_device/linux/audio_device_pulse_linux.cc
index 2f343ba..1531049 100644
--- a/modules/audio_device/linux/audio_device_pulse_linux.cc
+++ b/modules/audio_device/linux/audio_device_pulse_linux.cc
@@ -16,14 +16,18 @@
 #include "rtc_base/logging.h"
 #include "system_wrappers/include/event_wrapper.h"
 
-webrtc::adm_linux_pulse::PulseAudioSymbolTable PaSymbolTable;
+WebRTCPulseSymbolTable* GetPulseSymbolTable() {
+  static WebRTCPulseSymbolTable* pulse_symbol_table =
+      new WebRTCPulseSymbolTable();
+  return pulse_symbol_table;
+}
 
 // Accesses Pulse functions through our late-binding symbol table instead of
 // directly. This way we don't have to link to libpulse, which means our binary
 // will work on systems that don't have it.
-#define LATE(sym)                                                             \
-  LATESYM_GET(webrtc::adm_linux_pulse::PulseAudioSymbolTable, &PaSymbolTable, \
-              sym)
+#define LATE(sym)                                             \
+  LATESYM_GET(webrtc::adm_linux_pulse::PulseAudioSymbolTable, \
+              GetPulseSymbolTable(), sym)
 
 namespace webrtc {
 
@@ -1547,7 +1551,7 @@
   int retVal = 0;
 
   // Load libpulse
-  if (!PaSymbolTable.Load()) {
+  if (!GetPulseSymbolTable()->Load()) {
     // Most likely the Pulse library and sound server are not installed on
     // this system
     RTC_LOG(LS_ERROR) << "failed to load symbol table";
@@ -1658,7 +1662,7 @@
 
 int32_t AudioDeviceLinuxPulse::TerminatePulseAudio() {
   // Do nothing if the instance doesn't exist
-  // likely PaSymbolTable.Load() fails
+  // likely GetPulseSymbolTable.Load() fails
   if (!_paMainloop) {
     return 0;
   }
diff --git a/modules/audio_device/linux/audio_device_pulse_linux.h b/modules/audio_device/linux/audio_device_pulse_linux.h
index 854c7c6..1655b18 100644
--- a/modules/audio_device/linux/audio_device_pulse_linux.h
+++ b/modules/audio_device/linux/audio_device_pulse_linux.h
@@ -92,6 +92,9 @@
 // calculation
 const uint32_t WEBRTC_PA_CAPTURE_BUFFER_LATENCY_ADJUSTMENT = 0;
 
+typedef webrtc::adm_linux_pulse::PulseAudioSymbolTable WebRTCPulseSymbolTable;
+WebRTCPulseSymbolTable* GetPulseSymbolTable();
+
 namespace webrtc {
 class EventWrapper;
 
diff --git a/modules/audio_device/linux/audio_mixer_manager_alsa_linux.cc b/modules/audio_device/linux/audio_mixer_manager_alsa_linux.cc
index 7701364..122944e 100644
--- a/modules/audio_device/linux/audio_mixer_manager_alsa_linux.cc
+++ b/modules/audio_device/linux/audio_mixer_manager_alsa_linux.cc
@@ -14,6 +14,13 @@
 #include "modules/audio_device/linux/audio_mixer_manager_alsa_linux.h"
 #include "rtc_base/logging.h"
 
+// Accesses ALSA functions through our late-binding symbol table instead of
+// directly. This way we don't have to link to libasound, which means our binary
+// will work on systems that don't have it.
+#define LATE(sym)                                                            \
+  LATESYM_GET(webrtc::adm_linux_alsa::AlsaSymbolTable, GetAlsaSymbolTable(), \
+              sym)
+
 namespace webrtc {
 
 AudioMixerManagerLinuxALSA::AudioMixerManagerLinuxALSA()
diff --git a/modules/audio_device/linux/audio_mixer_manager_pulse_linux.cc b/modules/audio_device/linux/audio_mixer_manager_pulse_linux.cc
index c50d56b..1e49d62 100644
--- a/modules/audio_device/linux/audio_mixer_manager_pulse_linux.cc
+++ b/modules/audio_device/linux/audio_mixer_manager_pulse_linux.cc
@@ -10,18 +10,17 @@
 
 #include <assert.h>
 
+#include "modules/audio_device/linux/audio_device_pulse_linux.h"
 #include "modules/audio_device/linux/audio_mixer_manager_pulse_linux.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 
-extern webrtc::adm_linux_pulse::PulseAudioSymbolTable PaSymbolTable;
-
 // Accesses Pulse functions through our late-binding symbol table instead of
-// directly. This way we don't have to link to libpulse, which means our
-// binary will work on systems that don't have it.
-#define LATE(sym)                                                             \
-  LATESYM_GET(webrtc::adm_linux_pulse::PulseAudioSymbolTable, &PaSymbolTable, \
-              sym)
+// directly. This way we don't have to link to libpulse, which means our binary
+// will work on systems that don't have it.
+#define LATE(sym)                                             \
+  LATESYM_GET(webrtc::adm_linux_pulse::PulseAudioSymbolTable, \
+              GetPulseSymbolTable(), sym)
 
 namespace webrtc {