| 1 | /* |
| 2 | * Copyright (C) 2008 Alan Calvert |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU General Public License |
| 5 | * as published by the Free Software Foundation; either version 2 |
| 6 | * of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "audiooutputpulse.h" |
| 20 | |
| 21 | #define LOC QString("PulseAudio: ") |
| 22 | #define LOC_ERR QString("PulseAudio Error: ") |
| 23 | |
| 24 | AudioOutputPulseAudio::AudioOutputPulseAudio(const AudioSettings &settings): |
| 25 | AudioOutputBase(settings), pcontext(NULL), pstream(NULL), mainloop(NULL), |
| 26 | sample_rate(settings.samplerate) |
| 27 | { |
| 28 | for (unsigned int i = 0; i < PA_CHANNELS_MAX; ++i) |
| 29 | channel_map.map[i] = PA_CHANNEL_POSITION_INVALID; |
| 30 | volume_control.channels = 0; |
| 31 | for (unsigned int i = 0; i < PA_CHANNELS_MAX; ++i) |
| 32 | volume_control.values[i] = PA_VOLUME_MUTED; |
| 33 | ChooseHost(); |
| 34 | Reconfigure(settings); |
| 35 | } |
| 36 | |
| 37 | AudioOutputPulseAudio::~AudioOutputPulseAudio() |
| 38 | { |
| 39 | KillAudio(); |
| 40 | } |
| 41 | |
| 42 | bool AudioOutputPulseAudio::OpenDevice() |
| 43 | { |
| 44 | QString fn_log_tag = "OpenDevice, "; |
| 45 | if (audio_channels > MAX_CHANNELS ) |
| 46 | { |
| 47 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 48 | + QString("audio channel limit %1, but %2 requested") |
| 49 | .arg(MAX_CHANNELS).arg(audio_channels)); |
| 50 | return false; |
| 51 | } |
| 52 | sample_spec.rate = sample_rate; |
| 53 | sample_spec.channels = volume_control.channels = audio_channels; |
| 54 | switch (audio_bits) |
| 55 | { |
| 56 | case 8: |
| 57 | sample_spec.format = PA_SAMPLE_U8; |
| 58 | break; |
| 59 | case 16: |
| 60 | sample_spec.format = PA_SAMPLE_S16NE; |
| 61 | break; |
| 62 | case 32: |
| 63 | sample_spec.format = PA_SAMPLE_FLOAT32RE; |
| 64 | break; |
| 65 | default: |
| 66 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 67 | + QString("unsupported %1 bit sample format") |
| 68 | .arg(audio_bits)); |
| 69 | return false; |
| 70 | } |
| 71 | if (!pa_sample_spec_valid(&sample_spec)) |
| 72 | { |
| 73 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 74 | + QString("invalid sample spec")); |
| 75 | return false; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | char spec[PA_SAMPLE_SPEC_SNPRINT_MAX]; |
| 80 | pa_sample_spec_snprint(spec, sizeof(spec), &sample_spec); |
| 81 | VERBOSE(VB_AUDIO, LOC + fn_log_tag |
| 82 | + QString("using sample spec %1").arg(spec)); |
| 83 | } |
| 84 | if (!MapChannels()) |
| 85 | return false; |
| 86 | else |
| 87 | { |
| 88 | if (!pa_channel_map_valid(&channel_map)) |
| 89 | { |
| 90 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 91 | + QString("channel map invalid")); |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | mainloop = pa_threaded_mainloop_new(); |
| 96 | if (!mainloop) |
| 97 | { |
| 98 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 99 | + QString("failed to get new threaded mainloop")); |
| 100 | return false; |
| 101 | } |
| 102 | pa_threaded_mainloop_start(mainloop); |
| 103 | pa_threaded_mainloop_lock(mainloop); |
| 104 | if (!ContextConnect()) |
| 105 | { |
| 106 | pa_threaded_mainloop_unlock(mainloop); |
| 107 | pa_threaded_mainloop_stop(mainloop); |
| 108 | return false; |
| 109 | } |
| 110 | if (!ConnectPlaybackStream()) |
| 111 | { |
| 112 | pa_threaded_mainloop_unlock(mainloop); |
| 113 | pa_threaded_mainloop_stop(mainloop); |
| 114 | return false; |
| 115 | } |
| 116 | pa_threaded_mainloop_unlock(mainloop); |
| 117 | pa_usec_t usec; |
| 118 | int neg; |
| 119 | pa_stream_get_latency(pstream, &usec, &neg); |
| 120 | VERBOSE(VB_AUDIO, LOC + fn_log_tag |
| 121 | + QString("total stream latency: %1%2 usecs") |
| 122 | .arg((neg == 1) ? "-" : "") |
| 123 | .arg(usec)); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | void AudioOutputPulseAudio::CloseDevice() |
| 128 | { |
| 129 | if (mainloop) |
| 130 | pa_threaded_mainloop_lock(mainloop); |
| 131 | if (pstream) |
| 132 | { |
| 133 | FlushStream("CloseDevice"); |
| 134 | pa_stream_disconnect(pstream); |
| 135 | pa_stream_unref(pstream); |
| 136 | pstream = NULL; |
| 137 | } |
| 138 | if (pcontext) |
| 139 | { |
| 140 | pa_context_drain(pcontext, NULL, NULL); |
| 141 | pa_context_disconnect(pcontext); |
| 142 | pcontext = NULL; |
| 143 | } |
| 144 | |
| 145 | if (mainloop) |
| 146 | { |
| 147 | pa_threaded_mainloop_unlock(mainloop); |
| 148 | pa_threaded_mainloop_stop(mainloop); |
| 149 | mainloop = NULL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void AudioOutputPulseAudio::WriteAudio(unsigned char* aubuf, int size) |
| 154 | { |
| 155 | if (audio_actually_paused) |
| 156 | return; |
| 157 | QString fn_log_tag = "WriteAudio, "; |
| 158 | pa_stream_state_t sstate = pa_stream_get_state(pstream); |
| 159 | if (PA_STREAM_IS_GOOD(sstate)) |
| 160 | { |
| 161 | int retries = 0; |
| 162 | int write_status; |
| 163 | size_t write; |
| 164 | size_t writable; |
| 165 | size_t to_write = size; |
| 166 | unsigned char* buf_ptr = aubuf; |
| 167 | pa_context_state_t cstate; |
| 168 | |
| 169 | pa_threaded_mainloop_lock(mainloop); |
| 170 | while (to_write > 0 && retries < 3) |
| 171 | { |
| 172 | write_status = 0; |
| 173 | writable = pa_stream_writable_size(pstream); |
| 174 | if (writable > 0) |
| 175 | { |
| 176 | write = min(to_write, writable); |
| 177 | write_status = pa_stream_write(pstream, buf_ptr, write, |
| 178 | NULL, 0, PA_SEEK_RELATIVE); |
| 179 | if (!write_status) |
| 180 | { |
| 181 | buf_ptr += write; |
| 182 | to_write -= write; |
| 183 | } |
| 184 | else |
| 185 | break; |
| 186 | } |
| 187 | else if (writable < 0) |
| 188 | break; |
| 189 | else // writable == 0 |
| 190 | pa_threaded_mainloop_wait(mainloop); |
| 191 | ++retries; |
| 192 | } |
| 193 | pa_threaded_mainloop_unlock(mainloop); |
| 194 | if (to_write > 0) |
| 195 | { |
| 196 | if (writable < 0) |
| 197 | { |
| 198 | cstate = pa_context_get_state(pcontext); |
| 199 | sstate = pa_stream_get_state(pstream); |
| 200 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 201 | + QString("stream unfit for writing (writable < 0), " |
| 202 | "context state: %1, stream state: %2") |
| 203 | .arg(cstate,0,16).arg(sstate,0,16)); |
| 204 | } |
| 205 | if (write_status != 0) |
| 206 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 207 | + QString("stream write failed: %1") |
| 208 | .arg((write_status == PA_ERR_BADSTATE) |
| 209 | ? "PA_ERR_BADSTATE" |
| 210 | : "PA_ERR_INVALID")); |
| 211 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 212 | + QString("short write, %1 of %2, tries %3") |
| 213 | .arg(size - to_write).arg(size).arg(retries)); |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 218 | + QString("stream state not good: %1").arg(sstate,0,16)); |
| 219 | } |
| 220 | |
| 221 | int AudioOutputPulseAudio::GetBufferedOnSoundcard(void) const |
| 222 | { |
| 223 | pa_threaded_mainloop_lock(mainloop); |
| 224 | int writable = pa_stream_writable_size(pstream); |
| 225 | pa_threaded_mainloop_unlock(mainloop); |
| 226 | return soundcard_buffer_size - writable; |
| 227 | } |
| 228 | |
| 229 | int AudioOutputPulseAudio::GetSpaceOnSoundcard(void) const |
| 230 | { |
| 231 | pa_threaded_mainloop_lock(mainloop); |
| 232 | int writable = pa_stream_writable_size(pstream); |
| 233 | pa_threaded_mainloop_unlock(mainloop); |
| 234 | return writable; |
| 235 | } |
| 236 | |
| 237 | int AudioOutputPulseAudio::GetVolumeChannel(int channel) const |
| 238 | { |
| 239 | return (float)volume_control.values[channel] |
| 240 | / (float)PA_VOLUME_NORM * 100.0f; |
| 241 | } |
| 242 | |
| 243 | void AudioOutputPulseAudio::SetVolumeChannel(int channel, int volume) |
| 244 | { |
| 245 | QString fn_log_tag = "SetVolumeChannel, "; |
| 246 | if (channel < 0 || channel > MAX_CHANNELS || volume < 0) |
| 247 | { |
| 248 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 249 | + QString("bad volume params, channel %1, volume %2") |
| 250 | .arg(channel).arg(volume)); |
| 251 | return; |
| 252 | } |
| 253 | volume_control.values[channel] = |
| 254 | (float)volume / 100.0f * (float)PA_VOLUME_NORM; |
| 255 | volume = min(100, volume); |
| 256 | volume = max(0, volume); |
| 257 | uint32_t sink_index = pa_stream_get_device_index(pstream); |
| 258 | pa_threaded_mainloop_lock(mainloop); |
| 259 | pa_operation* op = |
| 260 | pa_context_set_sink_volume_by_index(pcontext, sink_index, |
| 261 | &volume_control, |
| 262 | OpCompletionCallback, this); |
| 263 | pa_threaded_mainloop_unlock(mainloop); |
| 264 | if (op) |
| 265 | pa_operation_unref(op); |
| 266 | else |
| 267 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 268 | + QString("set sink volume operation failed, sink: %1, " |
| 269 | "error: %2 ") |
| 270 | .arg(sink_index) |
| 271 | .arg(pa_strerror(pa_context_errno(pcontext)))); |
| 272 | } |
| 273 | |
| 274 | void AudioOutputPulseAudio::Pause(bool paused) |
| 275 | { |
| 276 | pa_operation* op; |
| 277 | if (paused && !audio_actually_paused) |
| 278 | { |
| 279 | FlushStream("Pause"); |
| 280 | pa_threaded_mainloop_lock(mainloop); |
| 281 | op = pa_stream_cork(pstream, 1, NULL, this); |
| 282 | pa_threaded_mainloop_unlock(mainloop); |
| 283 | if (op) |
| 284 | { |
| 285 | pa_operation_unref(op); |
| 286 | audio_actually_paused = true; |
| 287 | VERBOSE(VB_AUDIO+VB_EXTRA, LOC + "Pause, audio actually paused"); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | VERBOSE(VB_IMPORTANT, LOC_ERR + "Pause, cork stream failed"); |
| 292 | audio_actually_paused = false; |
| 293 | } |
| 294 | pauseaudio = true; |
| 295 | } |
| 296 | else if(!paused && audio_actually_paused) |
| 297 | { |
| 298 | pa_threaded_mainloop_lock(mainloop); |
| 299 | op = pa_stream_cork(pstream, 0, NULL, this); |
| 300 | pa_threaded_mainloop_unlock(mainloop); |
| 301 | if (op) |
| 302 | { |
| 303 | pa_operation_unref(op); |
| 304 | VERBOSE(VB_AUDIO+VB_EXTRA, LOC + "Pause, audio actually unpaused"); |
| 305 | } |
| 306 | else |
| 307 | VERBOSE(VB_IMPORTANT, LOC_ERR + "Pause, un-cork stream failed"); |
| 308 | FlushStream("(un)Pause"); |
| 309 | pauseaudio = false; |
| 310 | audio_actually_paused = false; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void AudioOutputPulseAudio::Reset(void) |
| 315 | { |
| 316 | AudioOutputBase::Reset(); |
| 317 | FlushStream("Reset"); |
| 318 | } |
| 319 | |
| 320 | void AudioOutputPulseAudio::Drain(void) |
| 321 | { |
| 322 | AudioOutputBase::Drain(); |
| 323 | pa_threaded_mainloop_lock(mainloop); |
| 324 | pa_operation* op = pa_stream_drain(pstream, NULL, this); |
| 325 | pa_threaded_mainloop_unlock(mainloop); |
| 326 | if (op) |
| 327 | pa_operation_unref(op); |
| 328 | else |
| 329 | VERBOSE(VB_IMPORTANT, LOC_ERR + "Drain, stream drain failed "); |
| 330 | } |
| 331 | |
| 332 | bool AudioOutputPulseAudio::MapChannels(void) |
| 333 | { |
| 334 | QString fn_log_tag = "MapChannels, "; |
| 335 | channel_map.channels = audio_channels; |
| 336 | for (int ch = 0; ch < channel_map.channels; ++ch) |
| 337 | switch (ch) |
| 338 | { |
| 339 | case 0: |
| 340 | channel_map.map[0] = |
| 341 | (audio_channels < 2) ? PA_CHANNEL_POSITION_MONO |
| 342 | : PA_CHANNEL_POSITION_FRONT_LEFT; |
| 343 | break; |
| 344 | case 1: |
| 345 | channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT; |
| 346 | break; |
| 347 | case 2: |
| 348 | channel_map.map[2] = PA_CHANNEL_POSITION_REAR_LEFT; |
| 349 | break; |
| 350 | case 3: |
| 351 | channel_map.map[3] = PA_CHANNEL_POSITION_REAR_RIGHT; |
| 352 | break; |
| 353 | case 4: |
| 354 | channel_map.map[4] = PA_CHANNEL_POSITION_FRONT_CENTER; |
| 355 | break; |
| 356 | case 5: |
| 357 | channel_map.map[5] = PA_CHANNEL_POSITION_LFE; |
| 358 | break; |
| 359 | default: |
| 360 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 361 | + QString("invalid channel map count: %1 channels") |
| 362 | .arg(audio_channels)); |
| 363 | return false; |
| 364 | } |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | bool AudioOutputPulseAudio::ContextConnect(void) |
| 369 | { |
| 370 | QString fn_log_tag = "ContextConnect, "; |
| 371 | if (pcontext) |
| 372 | { |
| 373 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 374 | + QString("context appears to exist, but shouldn't (yet)")); |
| 375 | pa_context_unref(pcontext); |
| 376 | pcontext = NULL; |
| 377 | return false; |
| 378 | } |
| 379 | pcontext = pa_context_new(pa_threaded_mainloop_get_api(mainloop), "MythTV"); |
| 380 | if (!pcontext) |
| 381 | { |
| 382 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 383 | + QString("failed to acquire new context")); |
| 384 | return false; |
| 385 | } |
| 386 | pa_context_set_state_callback(pcontext, ContextStateCallback, this); |
| 387 | |
| 388 | char* pulse_host = ChooseHost(); |
| 389 | int chk = pa_context_connect(pcontext, pulse_host, (pa_context_flags_t)0, NULL); |
| 390 | if (pulse_host) |
| 391 | delete(pulse_host); |
| 392 | if (chk < 0) |
| 393 | { |
| 394 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 395 | + QString("context connect failed: %1") |
| 396 | .arg(pa_strerror(pa_context_errno(pcontext)))); |
| 397 | return false; |
| 398 | } |
| 399 | bool connected = false; |
| 400 | pa_context_state_t state = pa_context_get_state(pcontext); |
| 401 | for (; !connected; state = pa_context_get_state(pcontext)) |
| 402 | { |
| 403 | switch(state) |
| 404 | { |
| 405 | case PA_CONTEXT_READY: |
| 406 | VERBOSE(VB_AUDIO+VB_EXTRA, LOC + fn_log_tag |
| 407 | + QString("context connection ready, move on")); |
| 408 | connected = true; |
| 409 | continue; |
| 410 | |
| 411 | case PA_CONTEXT_FAILED: |
| 412 | case PA_CONTEXT_TERMINATED: |
| 413 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 414 | + QString("context connection failed or was " |
| 415 | "terminated: %1") |
| 416 | .arg(pa_strerror(pa_context_errno(pcontext)))); |
| 417 | return false; |
| 418 | |
| 419 | default: |
| 420 | VERBOSE(VB_AUDIO+VB_EXTRA, LOC + fn_log_tag |
| 421 | + QString("waiting for context connection ready")); |
| 422 | pa_threaded_mainloop_wait(mainloop); |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | pa_operation* op = |
| 427 | pa_context_get_server_info(pcontext, ServerInfoCallback, this); |
| 428 | if (op) |
| 429 | pa_operation_unref(op); |
| 430 | else |
| 431 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 432 | + QString("failed to get PulseAudio server info")); |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | char* AudioOutputPulseAudio::ChooseHost(void) |
| 437 | { |
| 438 | QString fn_log_tag = "ChooseHost, "; |
| 439 | char* pulse_host = NULL; |
| 440 | QByteArray host = QByteArray(audio_main_device.toAscii()); |
| 441 | host.remove(0, host.indexOf(":") + 1); |
| 442 | if (!(host.isEmpty() || host.isNull() || host == "default")) |
| 443 | if ((pulse_host = new char[host.size()])) |
| 444 | strcpy(pulse_host, host.constData()); |
| 445 | else |
| 446 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 447 | + QString("allocation of pulse host '%1' char[%2] failed") |
| 448 | .arg(host.constData()).arg(host.size() + 1)); |
| 449 | if (!pulse_host && host != "default") |
| 450 | { |
| 451 | char* env_pulse_host = getenv("PULSE_SERVER"); |
| 452 | if (env_pulse_host && strlen(env_pulse_host) > 0) |
| 453 | { |
| 454 | int host_len = strlen(env_pulse_host) + 1; |
| 455 | if ((pulse_host = new char[host_len])) |
| 456 | strcpy(pulse_host, env_pulse_host); |
| 457 | else |
| 458 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 459 | + QString("allocation of pulse host '%1' char[%2] failed") |
| 460 | .arg(env_pulse_host).arg(host_len)); |
| 461 | } |
| 462 | } |
| 463 | VERBOSE(VB_AUDIO, LOC + fn_log_tag |
| 464 | + QString("chosen PulseAudio server: %1") |
| 465 | .arg((pulse_host != NULL) ? pulse_host : "default")); |
| 466 | return pulse_host; |
| 467 | } |
| 468 | |
| 469 | bool AudioOutputPulseAudio::ConnectPlaybackStream(void) |
| 470 | { |
| 471 | QString fn_log_tag = "ConnectPlaybackStream, "; |
| 472 | pstream = pa_stream_new(pcontext, "MythTV playback", &sample_spec, |
| 473 | &channel_map); |
| 474 | if (!pstream) |
| 475 | { |
| 476 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 477 | + QString("failed to create new playback stream")); |
| 478 | return false; |
| 479 | } |
| 480 | pa_stream_set_state_callback(pstream, StreamStateCallback, this); |
| 481 | pa_stream_set_overflow_callback(pstream, BufferFlowCallback, (char*)"over"); |
| 482 | pa_stream_set_underflow_callback(pstream, BufferFlowCallback, |
| 483 | (char*)"under"); |
| 484 | if (set_initial_vol) |
| 485 | { |
| 486 | int volume = gContext->GetNumSetting("MasterMixerVolume", 80); |
| 487 | pa_cvolume_set(&volume_control, audio_channels, |
| 488 | (float)volume * (float)PA_VOLUME_NORM / 100.0f); |
| 489 | } |
| 490 | else |
| 491 | pa_cvolume_reset(&volume_control, audio_channels); |
| 492 | |
| 493 | // set myth sizes and pa buffer metrics |
| 494 | fragment_size = (float)sample_rate * 0.020f // 20msec |
| 495 | * (float)(audio_bits / 8 * audio_channels); |
| 496 | soundcard_buffer_size = 16 * fragment_size; |
| 497 | buffer_settings.maxlength = soundcard_buffer_size; |
| 498 | buffer_settings.tlength = fragment_size * 4; |
| 499 | buffer_settings.prebuf = (uint32_t)-1; |
| 500 | buffer_settings.minreq = (uint32_t)-1; |
| 501 | VERBOSE(VB_AUDIO, LOC + fn_log_tag |
| 502 | + QString("fragment size %1, soundcard buffer size %2") |
| 503 | .arg(fragment_size).arg(soundcard_buffer_size)); |
| 504 | |
| 505 | int flags = PA_STREAM_INTERPOLATE_TIMING |
| 506 | | PA_STREAM_AUTO_TIMING_UPDATE |
| 507 | | PA_STREAM_NO_REMAP_CHANNELS |
| 508 | | PA_STREAM_NO_REMIX_CHANNELS; |
| 509 | pa_stream_connect_playback(pstream, NULL, &buffer_settings, |
| 510 | (pa_stream_flags_t)flags, &volume_control, NULL); |
| 511 | pa_context_state_t cstate; |
| 512 | pa_stream_state_t sstate; |
| 513 | bool connected = false, failed = false; |
| 514 | while (!(connected || failed)) |
| 515 | { |
| 516 | switch (cstate = pa_context_get_state(pcontext)) |
| 517 | { |
| 518 | case PA_CONTEXT_FAILED: |
| 519 | case PA_CONTEXT_TERMINATED: |
| 520 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 521 | + QString("context is stuffed, %1") |
| 522 | .arg(pa_strerror(pa_context_errno( |
| 523 | pcontext)))); |
| 524 | failed = true; |
| 525 | break; |
| 526 | default: |
| 527 | switch (sstate = pa_stream_get_state(pstream)) |
| 528 | { |
| 529 | case PA_STREAM_READY: |
| 530 | connected = true; |
| 531 | break; |
| 532 | case PA_STREAM_FAILED: |
| 533 | case PA_STREAM_TERMINATED: |
| 534 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 535 | + QString("stream failed or was terminated, " |
| 536 | "context state %1, stream state %2") |
| 537 | .arg(cstate).arg(sstate)); |
| 538 | failed = true; |
| 539 | break; |
| 540 | default: |
| 541 | pa_threaded_mainloop_wait(mainloop); |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | return (connected && !failed); |
| 547 | } |
| 548 | |
| 549 | void AudioOutputPulseAudio::FlushStream(const char* caller) |
| 550 | { |
| 551 | QString fn_log_tag = QString("FlushStream (%1), ").arg(caller); |
| 552 | pa_threaded_mainloop_lock(mainloop); |
| 553 | pa_operation* op = pa_stream_flush(pstream, NULL, this); |
| 554 | pa_threaded_mainloop_unlock(mainloop); |
| 555 | if (op) |
| 556 | pa_operation_unref(op); |
| 557 | else |
| 558 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 559 | + "stream flush operation failed "); |
| 560 | } |
| 561 | |
| 562 | void AudioOutputPulseAudio::ContextStateCallback(pa_context* c, void* arg) |
| 563 | { |
| 564 | QString fn_log_tag = "_ContextStateCallback, "; |
| 565 | AudioOutputPulseAudio* audoutP = static_cast<AudioOutputPulseAudio*>(arg); |
| 566 | switch (pa_context_get_state(c)) |
| 567 | { |
| 568 | case PA_CONTEXT_READY: |
| 569 | pa_threaded_mainloop_signal(audoutP->mainloop, 0); |
| 570 | break; |
| 571 | case PA_CONTEXT_TERMINATED: |
| 572 | case PA_CONTEXT_FAILED: |
| 573 | pa_threaded_mainloop_signal(audoutP->mainloop, 0); |
| 574 | break; |
| 575 | case PA_CONTEXT_CONNECTING: |
| 576 | case PA_CONTEXT_UNCONNECTED: |
| 577 | case PA_CONTEXT_AUTHORIZING: |
| 578 | case PA_CONTEXT_SETTING_NAME: |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void AudioOutputPulseAudio::StreamStateCallback(pa_stream* s, void* arg) |
| 584 | { |
| 585 | QString fn_log_tag = "StreamStateCallback, "; |
| 586 | AudioOutputPulseAudio* audoutP = static_cast<AudioOutputPulseAudio*>(arg); |
| 587 | switch (pa_stream_get_state(s)) |
| 588 | { |
| 589 | case PA_STREAM_READY: |
| 590 | case PA_STREAM_TERMINATED: |
| 591 | case PA_STREAM_FAILED: |
| 592 | pa_threaded_mainloop_signal(audoutP->mainloop, 0); |
| 593 | break; |
| 594 | case PA_STREAM_UNCONNECTED: |
| 595 | case PA_STREAM_CREATING: |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void AudioOutputPulseAudio::BufferFlowCallback(pa_stream* s, void* tag) |
| 601 | { |
| 602 | VERBOSE(VB_IMPORTANT, LOC_ERR + QString("stream buffer %1flow") |
| 603 | .arg((char*)tag)); |
| 604 | } |
| 605 | |
| 606 | void AudioOutputPulseAudio::OpCompletionCallback(pa_context* c, int ok, |
| 607 | void* arg) |
| 608 | { |
| 609 | QString fn_log_tag = "OpCompletionCallback, "; |
| 610 | AudioOutputPulseAudio* audoutP = static_cast<AudioOutputPulseAudio*>(arg); |
| 611 | if (!ok) |
| 612 | VERBOSE(VB_IMPORTANT, LOC_ERR + fn_log_tag |
| 613 | + QString("bummer, an operation failed: %1") |
| 614 | .arg(pa_strerror(pa_context_errno(c)))); |
| 615 | pa_threaded_mainloop_signal(audoutP->mainloop, 0); |
| 616 | } |
| 617 | |
| 618 | void AudioOutputPulseAudio::ServerInfoCallback(pa_context* context, |
| 619 | const pa_server_info* inf, |
| 620 | void* arg) |
| 621 | { |
| 622 | QString fn_log_tag = "ServerInfoCallback, "; |
| 623 | VERBOSE(VB_AUDIO, LOC + fn_log_tag |
| 624 | + QString("PulseAudio server info - host name: %1, server version: " |
| 625 | "%2, server name: %3, default sink: %4") |
| 626 | .arg(inf->host_name).arg(inf->server_version) |
| 627 | .arg(inf->server_name).arg(inf->default_sink_name)); |
| 628 | } |
| 629 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |