2 * Copyright (C) 2010-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "AESinkPULSE.h"
23 #include "utils/log.h"
25 #include "guilib/LocalizeStrings.h"
29 static const char *ContextStateToString(pa_context_state s)
33 case PA_CONTEXT_UNCONNECTED:
35 case PA_CONTEXT_CONNECTING:
37 case PA_CONTEXT_AUTHORIZING:
39 case PA_CONTEXT_SETTING_NAME:
40 return "setting name";
41 case PA_CONTEXT_READY:
43 case PA_CONTEXT_FAILED:
45 case PA_CONTEXT_TERMINATED:
52 static const char *StreamStateToString(pa_stream_state s)
56 case PA_STREAM_UNCONNECTED:
58 case PA_STREAM_CREATING:
62 case PA_STREAM_FAILED:
64 case PA_STREAM_TERMINATED:
71 static pa_sample_format AEFormatToPulseFormat(AEDataFormat format)
75 case AE_FMT_U8 : return PA_SAMPLE_U8;
76 case AE_FMT_S16LE : return PA_SAMPLE_S16LE;
77 case AE_FMT_S16BE : return PA_SAMPLE_S16BE;
78 case AE_FMT_S16NE : return PA_SAMPLE_S16NE;
79 case AE_FMT_S24LE3: return PA_SAMPLE_S24LE;
80 case AE_FMT_S24BE3: return PA_SAMPLE_S24BE;
81 case AE_FMT_S24NE3: return PA_SAMPLE_S24NE;
82 case AE_FMT_S24LE4: return PA_SAMPLE_S24_32LE;
83 case AE_FMT_S24BE4: return PA_SAMPLE_S24_32BE;
84 case AE_FMT_S24NE4: return PA_SAMPLE_S24_32NE;
85 case AE_FMT_S32BE : return PA_SAMPLE_S32BE;
86 case AE_FMT_S32LE : return PA_SAMPLE_S32LE;
87 case AE_FMT_S32NE : return PA_SAMPLE_S32NE;
88 case AE_FMT_FLOAT : return PA_SAMPLE_FLOAT32;
93 return PA_SAMPLE_S16NE;
96 return PA_SAMPLE_INVALID;
100 static pa_encoding AEFormatToPulseEncoding(AEDataFormat format)
104 case AE_FMT_AC3 : return PA_ENCODING_AC3_IEC61937;
105 case AE_FMT_DTS : return PA_ENCODING_DTS_IEC61937;
106 case AE_FMT_EAC3 : return PA_ENCODING_EAC3_IEC61937;
109 return PA_ENCODING_PCM;
113 static AEDataFormat defaultDataFormats[] = {
130 static unsigned int defaultSampleRates[] = {
147 /* Static callback functions */
149 static void ContextStateCallback(pa_context *c, void *userdata)
151 pa_threaded_mainloop *m = (pa_threaded_mainloop *)userdata;
152 switch (pa_context_get_state(c))
154 case PA_CONTEXT_READY:
155 case PA_CONTEXT_TERMINATED:
156 case PA_CONTEXT_UNCONNECTED:
157 case PA_CONTEXT_CONNECTING:
158 case PA_CONTEXT_AUTHORIZING:
159 case PA_CONTEXT_SETTING_NAME:
160 case PA_CONTEXT_FAILED:
161 pa_threaded_mainloop_signal(m, 0);
166 static void StreamStateCallback(pa_stream *s, void *userdata)
168 pa_threaded_mainloop *m = (pa_threaded_mainloop *)userdata;
169 switch (pa_stream_get_state(s))
171 case PA_STREAM_UNCONNECTED:
172 case PA_STREAM_CREATING:
173 case PA_STREAM_READY:
174 case PA_STREAM_FAILED:
175 case PA_STREAM_TERMINATED:
176 pa_threaded_mainloop_signal(m, 0);
181 static void StreamRequestCallback(pa_stream *s, size_t length, void *userdata)
183 pa_threaded_mainloop *m = (pa_threaded_mainloop *)userdata;
184 pa_threaded_mainloop_signal(m, 0);
187 static void StreamLatencyUpdateCallback(pa_stream *s, void *userdata)
189 pa_threaded_mainloop *m = (pa_threaded_mainloop *)userdata;
190 pa_threaded_mainloop_signal(m, 0);
193 static void SinkChangedCallback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata)
195 CAESinkPULSE* p = (CAESinkPULSE*) userdata;
199 CSingleLock lock(p->m_sec);
200 if (p->IsInitialized())
202 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW)
204 CLog::Log(LOGDEBUG, "Sink appeared");
205 CAEFactory::DeviceChange();
207 else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE)
209 CLog::Log(LOGDEBUG, "Sink removed");
210 CAEFactory::DeviceChange();
212 else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
214 CLog::Log(LOGDEBUG, "Sink changed");
215 //CAEFactory::DeviceChange();
220 struct SinkInfoStruct
222 AEDeviceInfoList *list;
225 pa_threaded_mainloop *mainloop;
235 static void SinkInfoCallback(pa_context *c, const pa_sink_info *i, int eol, void *userdata)
237 SinkInfoStruct *sinkStruct = (SinkInfoStruct *)userdata;
240 if (i->flags && (i->flags & PA_SINK_HARDWARE))
241 sinkStruct->isHWDevice = true;
243 sinkStruct->device_found = true;
245 pa_threaded_mainloop_signal(sinkStruct->mainloop, 0);
248 static AEChannel PAChannelToAEChannel(pa_channel_position_t channel)
250 AEChannel ae_channel;
253 case PA_CHANNEL_POSITION_FRONT_LEFT: ae_channel = AE_CH_FL; break;
254 case PA_CHANNEL_POSITION_FRONT_RIGHT: ae_channel = AE_CH_FR; break;
255 case PA_CHANNEL_POSITION_FRONT_CENTER: ae_channel = AE_CH_FC; break;
256 case PA_CHANNEL_POSITION_LFE: ae_channel = AE_CH_LFE; break;
257 case PA_CHANNEL_POSITION_REAR_LEFT: ae_channel = AE_CH_BL; break;
258 case PA_CHANNEL_POSITION_REAR_RIGHT: ae_channel = AE_CH_BR; break;
259 case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER: ae_channel = AE_CH_FLOC; break;
260 case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER: ae_channel = AE_CH_FROC; break;
261 case PA_CHANNEL_POSITION_REAR_CENTER: ae_channel = AE_CH_BC; break;
262 case PA_CHANNEL_POSITION_SIDE_LEFT: ae_channel = AE_CH_SL; break;
263 case PA_CHANNEL_POSITION_SIDE_RIGHT: ae_channel = AE_CH_SR; break;
264 case PA_CHANNEL_POSITION_TOP_FRONT_LEFT: ae_channel = AE_CH_TFL; break;
265 case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT: ae_channel = AE_CH_TFR; break;
266 case PA_CHANNEL_POSITION_TOP_FRONT_CENTER: ae_channel = AE_CH_TFC; break;
267 case PA_CHANNEL_POSITION_TOP_CENTER: ae_channel = AE_CH_TC; break;
268 case PA_CHANNEL_POSITION_TOP_REAR_LEFT: ae_channel = AE_CH_TBL; break;
269 case PA_CHANNEL_POSITION_TOP_REAR_RIGHT: ae_channel = AE_CH_TBR; break;
270 case PA_CHANNEL_POSITION_TOP_REAR_CENTER: ae_channel = AE_CH_TBC; break;
271 default: ae_channel = AE_CH_NULL; break;
276 static pa_channel_position_t AEChannelToPAChannel(AEChannel ae_channel)
278 pa_channel_position_t pa_channel;
281 case AE_CH_FL: pa_channel = PA_CHANNEL_POSITION_FRONT_LEFT; break;
282 case AE_CH_FR: pa_channel = PA_CHANNEL_POSITION_FRONT_RIGHT; break;
283 case AE_CH_FC: pa_channel = PA_CHANNEL_POSITION_FRONT_CENTER; break;
284 case AE_CH_LFE: pa_channel = PA_CHANNEL_POSITION_LFE; break;
285 case AE_CH_BL: pa_channel = PA_CHANNEL_POSITION_REAR_LEFT; break;
286 case AE_CH_BR: pa_channel = PA_CHANNEL_POSITION_REAR_RIGHT; break;
287 case AE_CH_FLOC: pa_channel = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; break;
288 case AE_CH_FROC: pa_channel = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; break;
289 case AE_CH_BC: pa_channel = PA_CHANNEL_POSITION_REAR_CENTER; break;
290 case AE_CH_SL: pa_channel = PA_CHANNEL_POSITION_SIDE_LEFT; break;
291 case AE_CH_SR: pa_channel = PA_CHANNEL_POSITION_SIDE_RIGHT; break;
292 case AE_CH_TFL: pa_channel = PA_CHANNEL_POSITION_TOP_FRONT_LEFT; break;
293 case AE_CH_TFR: pa_channel = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT; break;
294 case AE_CH_TFC: pa_channel = PA_CHANNEL_POSITION_TOP_FRONT_CENTER; break;
295 case AE_CH_TC: pa_channel = PA_CHANNEL_POSITION_TOP_CENTER; break;
296 case AE_CH_TBL: pa_channel = PA_CHANNEL_POSITION_TOP_REAR_LEFT; break;
297 case AE_CH_TBR: pa_channel = PA_CHANNEL_POSITION_TOP_REAR_RIGHT; break;
298 case AE_CH_TBC: pa_channel = PA_CHANNEL_POSITION_TOP_REAR_CENTER; break;
299 default: pa_channel = PA_CHANNEL_POSITION_INVALID; break;
304 static pa_channel_map AEChannelMapToPAChannel(CAEChannelInfo info)
307 pa_channel_map_init(&map);
308 pa_channel_position_t pos;
309 for (unsigned int i = 0; i < info.Count(); ++i)
311 pos = AEChannelToPAChannel(info[i]);
312 if(pos != PA_CHANNEL_POSITION_INVALID)
314 // remember channel name and increase channel count
315 map.map[map.channels++] = pos;
321 static CAEChannelInfo PAChannelToAEChannelMap(pa_channel_map channels)
326 for (unsigned int i=0; i<channels.channels; i++)
328 ch = PAChannelToAEChannel(channels.map[i]);
335 static void SinkInfoRequestCallback(pa_context *c, const pa_sink_info *i, int eol, void *userdata)
338 SinkInfoStruct *sinkStruct = (SinkInfoStruct *)userdata;
340 if(sinkStruct && sinkStruct->list->empty())
342 //add a default device first
343 CAEDeviceInfo defaultDevice;
344 defaultDevice.m_deviceName = std::string("Default");
345 defaultDevice.m_displayName = std::string("Default");
346 defaultDevice.m_displayNameExtra = std::string("Default Output Device (PULSEAUDIO)");
347 defaultDevice.m_dataFormats.insert(defaultDevice.m_dataFormats.end(), defaultDataFormats, defaultDataFormats + sizeof(defaultDataFormats) / sizeof(defaultDataFormats[0]));
348 defaultDevice.m_channels = CAEChannelInfo(AE_CH_LAYOUT_2_0);
349 defaultDevice.m_sampleRates.assign(defaultSampleRates, defaultSampleRates + sizeof(defaultSampleRates) / sizeof(defaultSampleRates[0]));
350 defaultDevice.m_deviceType = AE_DEVTYPE_PCM;
351 sinkStruct->list->push_back(defaultDevice);
356 CAEDeviceInfo device;
358 device.m_deviceName = string(i->name);
359 device.m_displayName = string(i->description);
360 if (i->active_port && i->active_port->description)
361 device.m_displayNameExtra = std::string((i->active_port->description)).append(" (PULSEAUDIO)");
363 device.m_displayNameExtra = std::string((i->description)).append(" (PULSEAUDIO)");
364 unsigned int device_type = AE_DEVTYPE_PCM; //0
366 device.m_channels = PAChannelToAEChannelMap(i->channel_map);
368 // Don't add devices that would not have a channel map
369 if(device.m_channels.Count() == 0)
372 device.m_sampleRates.assign(defaultSampleRates, defaultSampleRates + sizeof(defaultSampleRates) / sizeof(defaultSampleRates[0]));
374 for (unsigned int j = 0; j < i->n_formats; j++)
376 switch(i->formats[j]->encoding)
378 case PA_ENCODING_AC3_IEC61937:
379 device.m_dataFormats.push_back(AE_FMT_AC3);
380 device_type = AE_DEVTYPE_IEC958;
382 case PA_ENCODING_DTS_IEC61937:
383 device.m_dataFormats.push_back(AE_FMT_DTS);
384 device_type = AE_DEVTYPE_IEC958;
386 case PA_ENCODING_EAC3_IEC61937:
387 device.m_dataFormats.push_back(AE_FMT_EAC3);
388 device_type = AE_DEVTYPE_IEC958;
390 case PA_ENCODING_PCM:
391 device.m_dataFormats.insert(device.m_dataFormats.end(), defaultDataFormats, defaultDataFormats + sizeof(defaultDataFormats) / sizeof(defaultDataFormats[0]));
397 // passthrough is only working when device has Stereo channel config
398 if (device_type > AE_DEVTYPE_PCM && device.m_channels.Count() == 2)
399 device.m_deviceType = AE_DEVTYPE_IEC958;
401 device.m_deviceType = AE_DEVTYPE_PCM;
404 CLog::Log(LOGDEBUG, "PulseAudio: Found %s with devicestring %s", device.m_displayName.c_str(), device.m_deviceName.c_str());
405 sinkStruct->list->push_back(device);
409 CLog::Log(LOGDEBUG, "PulseAudio: Skipped %s with devicestring %s", device.m_displayName.c_str(), device.m_deviceName.c_str());
412 pa_threaded_mainloop_signal(sinkStruct->mainloop, 0);
415 /* PulseAudio class memberfunctions*/
418 CAESinkPULSE::CAESinkPULSE()
420 m_IsAllocated = false;
421 m_passthrough = false;
423 m_BytesPerSecond = 0;
430 CAESinkPULSE::~CAESinkPULSE()
435 bool CAESinkPULSE::Initialize(AEAudioFormat &format, std::string &device)
438 CSingleLock lock(m_sec);
439 m_IsAllocated = false;
441 m_passthrough = false;
442 m_BytesPerSecond = 0;
448 if (!SetupContext(NULL, &m_Context, &m_MainLoop))
450 CLog::Log(LOGNOTICE, "PulseAudio might not be running. Context was not created.");
455 pa_threaded_mainloop_lock(m_MainLoop);
458 // Register Callback for Sink changes
459 CSingleLock lock(m_sec);
460 pa_context_set_subscribe_callback(m_Context, SinkChangedCallback, this);
461 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK;
462 pa_operation *op = pa_context_subscribe(m_Context, mask, NULL, this);
464 pa_operation_unref(op);
467 struct pa_channel_map map;
468 pa_channel_map_init(&map);
470 m_passthrough = AE_IS_RAW(format.m_dataFormat);
475 format.m_channelLayout = AE_CH_LAYOUT_2_0;
479 map = AEChannelMapToPAChannel(format.m_channelLayout);
480 // if count has changed we need to fit the AE Map
481 if(map.channels != format.m_channelLayout.Count())
482 format.m_channelLayout = PAChannelToAEChannelMap(map);
484 m_Channels = format.m_channelLayout.Count();
486 pa_cvolume_reset(&m_Volume, m_Channels);
488 pa_format_info *info[1];
489 info[0] = pa_format_info_new();
490 info[0]->encoding = AEFormatToPulseEncoding(format.m_dataFormat);
493 pa_format_info_set_sample_format(info[0], AEFormatToPulseFormat(format.m_dataFormat));
494 pa_format_info_set_channel_map(info[0], &map);
496 pa_format_info_set_channels(info[0], m_Channels);
498 // PA requires m_encodedRate in order to do EAC3
499 unsigned int samplerate;
502 if (format.m_encodedRate == 0)
504 CLog::Log(LOGNOTICE, "PulseAudio: Passthrough in use but m_encodedRate is not set - fallback to m_sampleRate");
505 samplerate = format.m_sampleRate;
508 samplerate = format.m_encodedRate;
511 samplerate = format.m_sampleRate;
513 pa_format_info_set_rate(info[0], samplerate);
515 if (!pa_format_info_valid(info[0]))
517 CLog::Log(LOGERROR, "PulseAudio: Invalid format info");
518 pa_format_info_free(info[0]);
519 pa_threaded_mainloop_unlock(m_MainLoop);
525 #if PA_CHECK_VERSION(2,0,0)
526 pa_format_info_to_sample_spec(info[0], &spec, NULL);
528 spec.rate = (AEFormatToPulseEncoding(format.m_dataFormat) == PA_ENCODING_EAC3_IEC61937) ? 4 * samplerate : samplerate;
529 spec.format = AEFormatToPulseFormat(format.m_dataFormat);
530 spec.channels = m_Channels;
532 if (!pa_sample_spec_valid(&spec))
534 CLog::Log(LOGERROR, "PulseAudio: Invalid sample spec");
535 pa_format_info_free(info[0]);
536 pa_threaded_mainloop_unlock(m_MainLoop);
541 m_BytesPerSecond = pa_bytes_per_second(&spec);
542 unsigned int frameSize = pa_frame_size(&spec);
544 m_Stream = pa_stream_new_extended(m_Context, "audio stream", info, 1, NULL);
545 pa_format_info_free(info[0]);
547 if (m_Stream == NULL)
549 CLog::Log(LOGERROR, "PulseAudio: Could not create a stream");
550 pa_threaded_mainloop_unlock(m_MainLoop);
555 pa_stream_set_state_callback(m_Stream, StreamStateCallback, m_MainLoop);
556 pa_stream_set_write_callback(m_Stream, StreamRequestCallback, m_MainLoop);
557 pa_stream_set_latency_update_callback(m_Stream, StreamLatencyUpdateCallback, m_MainLoop);
559 bool isDefaultDevice = (device == "Default");
561 pa_buffer_attr buffer_attr;
562 SinkInfoStruct sinkStruct;
563 sinkStruct.mainloop = m_MainLoop;
564 sinkStruct.isHWDevice = false;
565 sinkStruct.device_found = true; // needed to get default device opened
567 if (!isDefaultDevice)
569 // we need to check if the device we want to open really exists
570 // default device is handled in a special manner
571 sinkStruct.device_found = false; // if sink is valid it will be set true in pa_context_get_sink_info_by_name
572 WaitForOperation(pa_context_get_sink_info_by_name(m_Context, device.c_str(),SinkInfoCallback, &sinkStruct), m_MainLoop, "Get Sink Info");
575 if(!sinkStruct.device_found) // ActiveAE will open us again with a valid device name
577 CLog::Log(LOGERROR, "PulseAudio: Sink %s not found", device.c_str());
578 pa_threaded_mainloop_unlock(m_MainLoop);
584 // 50ms min packet size
585 if(sinkStruct.isHWDevice || isDefaultDevice)
587 unsigned int latency = m_BytesPerSecond / 5;
588 unsigned int process_time = latency / 4;
589 memset(&buffer_attr, 0, sizeof(buffer_attr));
590 buffer_attr.tlength = (uint32_t) latency;
591 buffer_attr.minreq = (uint32_t) process_time;
592 buffer_attr.maxlength = (uint32_t) -1;
593 buffer_attr.prebuf = (uint32_t) -1;
594 buffer_attr.fragsize = (uint32_t) latency;
597 if (pa_stream_connect_playback(m_Stream, isDefaultDevice ? NULL : device.c_str(), sinkStruct.isHWDevice ? &buffer_attr : NULL, ((pa_stream_flags)(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY)), m_passthrough ? NULL : &m_Volume, NULL) < 0)
599 CLog::Log(LOGERROR, "PulseAudio: Failed to connect stream to output");
600 pa_threaded_mainloop_unlock(m_MainLoop);
605 /* Wait until the stream is ready */
608 pa_threaded_mainloop_wait(m_MainLoop);
609 CLog::Log(LOGDEBUG, "PulseAudio: Stream %s", StreamStateToString(pa_stream_get_state(m_Stream)));
611 while (pa_stream_get_state(m_Stream) != PA_STREAM_READY && pa_stream_get_state(m_Stream) != PA_STREAM_FAILED);
613 if (pa_stream_get_state(m_Stream) == PA_STREAM_FAILED)
615 CLog::Log(LOGERROR, "PulseAudio: Waited for the stream but it failed");
616 pa_threaded_mainloop_unlock(m_MainLoop);
621 const pa_buffer_attr *a;
623 if (!(a = pa_stream_get_buffer_attr(m_Stream)))
625 CLog::Log(LOGERROR, "PulseAudio: %s", pa_strerror(pa_context_errno(m_Context)));
626 pa_threaded_mainloop_unlock(m_MainLoop);
632 unsigned int packetSize = a->minreq;
633 m_BufferSize = a->tlength;
635 format.m_frames = packetSize / frameSize;
638 pa_threaded_mainloop_unlock(m_MainLoop);
640 format.m_frameSize = frameSize;
641 format.m_frameSamples = format.m_frames * format.m_channelLayout.Count();
643 format.m_dataFormat = m_passthrough ? AE_FMT_S16NE : format.m_dataFormat;
647 CSingleLock lock(m_sec);
648 m_IsAllocated = true;
654 void CAESinkPULSE::Deinitialize()
656 CSingleLock lock(m_sec);
657 m_IsAllocated = false;
658 m_passthrough = false;
664 pa_threaded_mainloop_stop(m_MainLoop);
668 pa_stream_disconnect(m_Stream);
669 pa_stream_unref(m_Stream);
675 pa_context_disconnect(m_Context);
676 pa_context_unref(m_Context);
682 pa_threaded_mainloop_free(m_MainLoop);
687 double CAESinkPULSE::GetDelay()
693 pa_usec_t latency = (pa_usec_t) -1;
694 pa_threaded_mainloop_lock(m_MainLoop);
695 if ((error = pa_stream_get_latency(m_Stream, &latency, NULL)) < 0)
697 if (error == -PA_ERR_NODATA)
699 WaitForOperation(pa_stream_update_timing_info(m_Stream, NULL,NULL), m_MainLoop, "Update Timing Information");
700 if ((error = pa_stream_get_latency(m_Stream, &latency, NULL)) < 0)
702 CLog::Log(LOGDEBUG, "GetDelay - Failed to get Latency %d", error);
707 latency = (pa_usec_t) 0;
709 pa_threaded_mainloop_unlock(m_MainLoop);
710 return latency / 1000000.0;
713 double CAESinkPULSE::GetCacheTotal()
715 return (float)m_BufferSize / (float)m_BytesPerSecond;
718 unsigned int CAESinkPULSE::AddPackets(uint8_t *data, unsigned int frames, bool hasAudio, bool blocking)
723 pa_threaded_mainloop_lock(m_MainLoop);
725 unsigned int available = frames * m_format.m_frameSize;
726 unsigned int length = 0;
727 // revisit me after Gotham - should use a callback for the write function
728 while ((length = pa_stream_writable_size(m_Stream)) == 0)
729 pa_threaded_mainloop_wait(m_MainLoop);
731 length = std::min((unsigned int)length, available);
733 int error = pa_stream_write(m_Stream, data, length, NULL, 0, PA_SEEK_RELATIVE);
734 pa_threaded_mainloop_unlock(m_MainLoop);
738 CLog::Log(LOGERROR, "CPulseAudioDirectSound::AddPackets - pa_stream_write failed\n");
742 return (unsigned int)(length / m_format.m_frameSize);
745 void CAESinkPULSE::Drain()
750 pa_threaded_mainloop_lock(m_MainLoop);
751 WaitForOperation(pa_stream_drain(m_Stream, NULL, NULL), m_MainLoop, "Drain");
752 pa_threaded_mainloop_unlock(m_MainLoop);
755 void CAESinkPULSE::SetVolume(float volume)
757 if (m_IsAllocated && !m_passthrough)
759 pa_threaded_mainloop_lock(m_MainLoop);
760 pa_volume_t pavolume = pa_sw_volume_from_linear(volume);
762 pa_cvolume_mute(&m_Volume, m_Channels);
764 pa_cvolume_set(&m_Volume, m_Channels, pavolume);
765 pa_operation *op = pa_context_set_sink_input_volume(m_Context, pa_stream_get_index(m_Stream), &m_Volume, NULL, NULL);
767 CLog::Log(LOGERROR, "PulseAudio: Failed to set volume");
769 pa_operation_unref(op);
771 pa_threaded_mainloop_unlock(m_MainLoop);
775 void CAESinkPULSE::EnumerateDevicesEx(AEDeviceInfoList &list, bool force)
778 pa_threaded_mainloop *mainloop;
780 if (!SetupContext(NULL, &context, &mainloop))
782 CLog::Log(LOGNOTICE, "PulseAudio might not be running. Context was not created.");
786 pa_threaded_mainloop_lock(mainloop);
788 SinkInfoStruct sinkStruct;
789 sinkStruct.mainloop = mainloop;
790 sinkStruct.list = &list;
791 WaitForOperation(pa_context_get_sink_info_list(context, SinkInfoRequestCallback, &sinkStruct), mainloop, "EnumerateAudioSinks");
793 pa_threaded_mainloop_unlock(mainloop);
796 pa_threaded_mainloop_stop(mainloop);
800 pa_context_disconnect(context);
801 pa_context_unref(context);
807 pa_threaded_mainloop_free(mainloop);
812 bool CAESinkPULSE::IsInitialized()
814 CSingleLock lock(m_sec);
815 return m_IsAllocated;
818 bool CAESinkPULSE::Pause(bool pause)
820 pa_threaded_mainloop_lock(m_MainLoop);
822 if (!WaitForOperation(pa_stream_cork(m_Stream, pause ? 1 : 0, NULL, NULL), m_MainLoop, pause ? "Pause" : "Resume"))
825 pa_threaded_mainloop_unlock(m_MainLoop);
830 inline bool CAESinkPULSE::WaitForOperation(pa_operation *op, pa_threaded_mainloop *mainloop, const char *LogEntry = "")
837 while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
838 pa_threaded_mainloop_wait(mainloop);
840 if (pa_operation_get_state(op) != PA_OPERATION_DONE)
842 CLog::Log(LOGERROR, "PulseAudio: %s Operation failed", LogEntry);
846 pa_operation_unref(op);
850 bool CAESinkPULSE::SetupContext(const char *host, pa_context **context, pa_threaded_mainloop **mainloop)
852 if ((*mainloop = pa_threaded_mainloop_new()) == NULL)
854 CLog::Log(LOGERROR, "PulseAudio: Failed to allocate main loop");
858 if (((*context) = pa_context_new(pa_threaded_mainloop_get_api(*mainloop), "XBMC")) == NULL)
860 CLog::Log(LOGERROR, "PulseAudio: Failed to allocate context");
864 pa_context_set_state_callback(*context, ContextStateCallback, *mainloop);
866 if (pa_context_connect(*context, host, (pa_context_flags_t)0, NULL) < 0)
868 CLog::Log(LOGERROR, "PulseAudio: Failed to connect context");
871 pa_threaded_mainloop_lock(*mainloop);
873 if (pa_threaded_mainloop_start(*mainloop) < 0)
875 CLog::Log(LOGERROR, "PulseAudio: Failed to start MainLoop");
876 pa_threaded_mainloop_unlock(*mainloop);
880 /* Wait until the context is ready */
883 pa_threaded_mainloop_wait(*mainloop);
884 CLog::Log(LOGDEBUG, "PulseAudio: Context %s", ContextStateToString(pa_context_get_state(*context)));
886 while (pa_context_get_state(*context) != PA_CONTEXT_READY && pa_context_get_state(*context) != PA_CONTEXT_FAILED);
888 if (pa_context_get_state(*context) == PA_CONTEXT_FAILED)
890 CLog::Log(LOGERROR, "PulseAudio: Waited for the Context but it failed");
891 pa_threaded_mainloop_unlock(*mainloop);
895 pa_threaded_mainloop_unlock(*mainloop);