| 1 | /*** |
| 2 | * This file was part of PulseAudio, the license has been upgraded to GPL v2 |
| 3 | * or later as per the LGPL grant this was originally distributed under. |
| 4 | * |
| 5 | * Copyright 2004-2006 Lennart Poettering |
| 6 | * |
| 7 | * MythTV is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/wait.h> |
| 24 | |
| 25 | #include <signal.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | #include <unistd.h> |
| 29 | #include <assert.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <limits.h> |
| 33 | #include <getopt.h> |
| 34 | #include <locale.h> |
| 35 | |
| 36 | #ifdef __linux__ |
| 37 | #include <sys/prctl.h> |
| 38 | #endif |
| 39 | |
| 40 | #include <pulse/pulseaudio.h> |
| 41 | |
| 42 | #include <QThread> |
| 43 | #include <QMutex> |
| 44 | #include <QWaitCondition> |
| 45 | #include "mythverbose.h" |
| 46 | #include "util.h" // for IsPulseAudioRunning() |
| 47 | #include "exitcodes.h" |
| 48 | |
| 49 | #define BUFSIZE 1024 |
| 50 | |
| 51 | #define LOC QString("AudioPulseUtil: ") |
| 52 | #define LOC_WARN QString("AudioPulseUtil, Warning: ") |
| 53 | #define LOC_ERR QString("AudioPulseUtil, Error: ") |
| 54 | #define pa_assert(X) assert(X) |
| 55 | #define pa_assert_se(X) do { bool i = (bool) X; assert(i); } while (0) |
| 56 | #define _(X) X |
| 57 | |
| 58 | static pa_context *context = NULL; |
| 59 | static pa_mainloop_api *mainloop_api = NULL; |
| 60 | static int dead = 1; |
| 61 | static QMutex pa_lock; |
| 62 | static QWaitCondition pa_wait; |
| 63 | enum pa_values { |
| 64 | kPA_undefined = -1, |
| 65 | kPA_suspended = +0, |
| 66 | kPA_not_suspended_remote_server = +1, |
| 67 | kPA_not_suspended_error = +2, |
| 68 | kPA_not_suspended_success = +3, |
| 69 | }; |
| 70 | static int pa_value = -1; |
| 71 | |
| 72 | static void set_pa_value(int new_value) |
| 73 | { |
| 74 | QMutexLocker ml(&pa_lock); |
| 75 | pa_value = new_value; |
| 76 | pa_wait.wakeAll(); |
| 77 | } |
| 78 | |
| 79 | static void quit(int ret) { |
| 80 | pa_assert(mainloop_api); |
| 81 | mainloop_api->quit(mainloop_api, ret); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static void context_drain_complete(pa_context *c, void *userdata) { |
| 86 | pa_context_disconnect(c); |
| 87 | } |
| 88 | |
| 89 | static void drain(void) { |
| 90 | pa_operation *o; |
| 91 | |
| 92 | if (!(o = pa_context_drain(context, context_drain_complete, NULL))) |
| 93 | pa_context_disconnect(context); |
| 94 | else |
| 95 | pa_operation_unref(o); |
| 96 | } |
| 97 | |
| 98 | static void suspend_complete(pa_context *c, int success, void *userdata) |
| 99 | { |
| 100 | if (!success) |
| 101 | { |
| 102 | VERBOSE(VB_IMPORTANT, LOC_ERR + QString("Failure to suspend: %1") |
| 103 | .arg(pa_strerror(pa_context_errno(c)))); |
| 104 | |
| 105 | set_pa_value(kPA_not_suspended_error); |
| 106 | |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | VERBOSE(VB_GENERAL, LOC + "Suspend Success"); |
| 111 | |
| 112 | set_pa_value(kPA_suspended); |
| 113 | } |
| 114 | |
| 115 | static void resume_complete(pa_context *c, int success, void *userdata) |
| 116 | { |
| 117 | static int n = 0; |
| 118 | |
| 119 | n++; |
| 120 | |
| 121 | if (!success) |
| 122 | { |
| 123 | VERBOSE(VB_IMPORTANT, LOC_ERR + QString("Failure to resume: %1") |
| 124 | .arg(pa_strerror(pa_context_errno(c)))); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (n >= 2) |
| 129 | drain(); /* drain and quit */ |
| 130 | |
| 131 | VERBOSE(VB_GENERAL, LOC + "Resume Success"); |
| 132 | |
| 133 | set_pa_value(kPA_not_suspended_success); |
| 134 | } |
| 135 | |
| 136 | static void context_state_callback(pa_context *c, void *userdata) |
| 137 | { |
| 138 | pa_assert(c); |
| 139 | |
| 140 | switch (pa_context_get_state(c)) |
| 141 | { |
| 142 | case PA_CONTEXT_CONNECTING: |
| 143 | case PA_CONTEXT_AUTHORIZING: |
| 144 | case PA_CONTEXT_SETTING_NAME: |
| 145 | break; |
| 146 | |
| 147 | case PA_CONTEXT_READY: |
| 148 | if (pa_context_is_local(c)) |
| 149 | { |
| 150 | pa_operation_unref( |
| 151 | pa_context_suspend_sink_by_index( |
| 152 | c, PA_INVALID_INDEX, 1, suspend_complete, NULL)); |
| 153 | pa_operation_unref( |
| 154 | pa_context_suspend_source_by_index( |
| 155 | c, PA_INVALID_INDEX, 1, suspend_complete, NULL)); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | VERBOSE(VB_IMPORTANT, LOC_ERR + |
| 160 | "Sound server is not local, can not suspend."); |
| 161 | |
| 162 | set_pa_value(kPA_not_suspended_remote_server); |
| 163 | } |
| 164 | |
| 165 | break; |
| 166 | |
| 167 | case PA_CONTEXT_TERMINATED: |
| 168 | quit(0); |
| 169 | break; |
| 170 | |
| 171 | case PA_CONTEXT_FAILED: |
| 172 | default: |
| 173 | VERBOSE(VB_IMPORTANT, LOC_WARN + |
| 174 | "Can not connect to sound server, can not suspend." + |
| 175 | QString("\n\t\t\t%1") |
| 176 | .arg(pa_strerror(pa_context_errno(c)))); |
| 177 | |
| 178 | set_pa_value(kPA_not_suspended_error); |
| 179 | |
| 180 | pa_context_unref(context); |
| 181 | context = NULL; |
| 182 | |
| 183 | //if (child_pid == (pid_t) -1) |
| 184 | // /* not started yet, then we do it now */ |
| 185 | // start_child(); |
| 186 | //else if (dead) |
| 187 | // /* already started, and dead, so let's quit */ |
| 188 | // quit(1); |
| 189 | |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) { |
| 195 | |
| 196 | return;//**// |
| 197 | |
| 198 | int status = 0; |
| 199 | pid_t p; |
| 200 | |
| 201 | p = waitpid(-1, &status, WNOHANG); |
| 202 | |
| 203 | //if (p != child_pid) |
| 204 | // return; |
| 205 | |
| 206 | dead = 1; |
| 207 | |
| 208 | if (WIFEXITED(status)) |
| 209 | { |
| 210 | //child_ret = WEXITSTATUS(status); |
| 211 | } |
| 212 | else if (WIFSIGNALED(status)) { |
| 213 | fprintf(stderr, _("WARNING: Child process terminated by signal %u\n"), WTERMSIG(status)); |
| 214 | //child_ret = 1; |
| 215 | } |
| 216 | |
| 217 | if (context) { |
| 218 | if (pa_context_is_local(context)) { |
| 219 | /* A context is around, so let's resume */ |
| 220 | pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL)); |
| 221 | pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL)); |
| 222 | } else |
| 223 | drain(); |
| 224 | } else |
| 225 | /* Hmm, no context here, so let's terminate right away */ |
| 226 | quit(0); |
| 227 | } |
| 228 | |
| 229 | void suspend_pulseaudio_internal(void) { |
| 230 | pa_mainloop* m = NULL; |
| 231 | int ret = 1; |
| 232 | char *server = NULL; |
| 233 | const char *bn = "mythtv"; |
| 234 | |
| 235 | //setlocale(LC_ALL, ""); |
| 236 | //bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR); |
| 237 | |
| 238 | if (!(m = pa_mainloop_new())) { |
| 239 | fprintf(stderr, _("pa_mainloop_new() failed.\n")); |
| 240 | goto quit; |
| 241 | } |
| 242 | |
| 243 | mainloop_api = pa_mainloop_get_api(m); |
| 244 | if (!mainloop_api) |
| 245 | goto quit; |
| 246 | |
| 247 | if (pa_signal_init(mainloop_api) != 0) |
| 248 | goto quit; |
| 249 | |
| 250 | pa_signal_new(SIGCHLD, sigchld_callback, NULL); |
| 251 | |
| 252 | if (!(context = pa_context_new(mainloop_api, bn))) { |
| 253 | fprintf(stderr, _("pa_context_new() failed.\n")); |
| 254 | goto quit; |
| 255 | } |
| 256 | |
| 257 | pa_context_set_state_callback(context, context_state_callback, NULL); |
| 258 | pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL); |
| 259 | |
| 260 | if (pa_mainloop_run(m, &ret) < 0) { |
| 261 | fprintf(stderr, _("pa_mainloop_run() failed.\n")); |
| 262 | goto quit; |
| 263 | } |
| 264 | |
| 265 | quit: |
| 266 | if (context) |
| 267 | pa_context_unref(context); |
| 268 | |
| 269 | if (m) { |
| 270 | pa_signal_done(); |
| 271 | pa_mainloop_free(m); |
| 272 | } |
| 273 | |
| 274 | pa_xfree(server); |
| 275 | } |
| 276 | |
| 277 | class PAThread : public QThread |
| 278 | { |
| 279 | public: |
| 280 | void run(void) |
| 281 | { |
| 282 | VERBOSE(VB_IMPORTANT, LOC + "Here - begin"); |
| 283 | suspend_pulseaudio_internal(); |
| 284 | VERBOSE(VB_IMPORTANT, LOC + "Here - end"); |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | /// \returns true if successful |
| 289 | bool suspend_pulseaudio(void) |
| 290 | { |
| 291 | QThread *t = new PAThread(); |
| 292 | t->start(); |
| 293 | |
| 294 | QMutexLocker ml(&pa_lock); |
| 295 | while (pa_value < 0) |
| 296 | pa_wait.wait(&pa_lock); |
| 297 | |
| 298 | return kPA_suspended == pa_value; |
| 299 | } |
| 300 | |
| 301 | int handle_pulseaudio(void) |
| 302 | { |
| 303 | #ifndef USING_PULSE |
| 304 | if (IsPulseAudioRunning()) |
| 305 | { |
| 306 | VERBOSE(VB_IMPORTANT, "ERROR: ***Pulse Audio is running!!!!***"); |
| 307 | VERBOSE(VB_IMPORTANT, "ERROR: But MythTV has not been compiled " |
| 308 | "with Pulse Audio disabling support. EXITING!"); |
| 309 | return GENERIC_EXIT_NOT_OK; |
| 310 | } |
| 311 | #else |
| 312 | if (getenv("EXPERIMENTALLY_ALLOW_PULSE_AUDIO")) |
| 313 | { |
| 314 | VERBOSE(VB_IMPORTANT, "WARNING: "); |
| 315 | VERBOSE(VB_IMPORTANT, "WARNING: ***Pulse Audio is running!!!!***"); |
| 316 | VERBOSE(VB_IMPORTANT, "WARNING: "); |
| 317 | VERBOSE(VB_IMPORTANT, "WARNING: You have told MythTV to ignore it."); |
| 318 | VERBOSE(VB_IMPORTANT, "WARNING: "); |
| 319 | } |
| 320 | else if (IsPulseAudioRunning() && !suspend_pulseaudio()) |
| 321 | { |
| 322 | VERBOSE(VB_IMPORTANT, "ERROR: ***Pulse Audio is running!!!!***"); |
| 323 | VERBOSE(VB_IMPORTANT, |
| 324 | "ERROR: But MythTV was not able to suspend it. EXITING!"); |
| 325 | |
| 326 | return GENERIC_EXIT_NOT_OK; |
| 327 | } |
| 328 | #endif |
| 329 | |
| 330 | return GENERIC_EXIT_OK; |
| 331 | } |