Permalink
Browse files

CoreAudio: keep track of the current data source.

  • Loading branch information...
1 parent 4164515 commit 7a20d754b567d6f4be00ad13caec02352826a471 rain committed Aug 23, 2015
Showing with 53 additions and 4 deletions.
  1. +4 −3 TODO
  2. +49 −1 herrie/src/audio_output_coreaudio.c
View
@@ -43,9 +43,10 @@ Documentation:
- Write a developers guide?
CoreAudio (OS X) bugs:
-- Music stops playing when the default audio device changes (e.g. when
-headphones are plugged in/out).
-Solution: keep track of default audio device changes.
+- Audio device properties cannot be set once the data source changes
+(e.g. if headphones are plugged in/out, the volume cannot be changed with
+herrie's controls anymore).
+Solution: ???
- Gaps can sometimes be heard when music is playing.
Solution: implement a better buffering scheme (maybe by using a queue with a
few buffers?).
@@ -94,6 +94,11 @@ GMutex abuflock;
* application that a buffer has been processed.
*/
GCond abufdrained;
+/**
+ * @brief The data source currently being used by the CoreAudio device
+ * (headphones or speakers).
+ */
+UInt32 adscur;
#ifdef BUILD_VOLUME
/**
* @brief Preferred audio channels used for audio playback. We use it to
@@ -135,6 +140,35 @@ audio_output_ioproc(AudioDeviceID inDevice, const AudioTimeStamp *inNow,
return (0);
}
+/**
+ * @brief Get the data source of the current CoreAudio audio device.
+ */
+static int
+audio_output_get_datasource(UInt32 *ds)
+{
+ UInt32 size;
+
+ size = sizeof *ds;
+#ifdef MAC_OS_X_VERSION_10_5
+ AudioObjectPropertyAddress address;
+
+ address = (AudioObjectPropertyAddress) {
+ kAudioDevicePropertyDataSource,
+ kAudioDevicePropertyScopeOutput,
+ kAudioObjectPropertyElementMaster
+ };
+
+ if (AudioObjectGetPropertyData(adid, &address, 0, NULL, &size,
+ ds) != 0)
+#else /* !MAC_OS_X_VERSION_10_5 */
+ if (AudioDeviceGetProperty(adid, 0, false,
+ kAudioDevicePropertyDataSource, &size, ds) != 0)
+#endif /* MAC_OS_X_VERSION_10_5 */
+ return (-1);
+
+ return (0);
+}
+
int
audio_output_open(void)
{
@@ -199,6 +233,10 @@ audio_output_open(void)
#endif /* MAC_OS_X_VERSION_10_5 */
goto error;
+ /* Store the current data source */
+ if (audio_output_get_datasource(&adscur) != 0)
+ goto error;
+
#ifdef BUILD_VOLUME
/* Store the audio channels */
size = sizeof achans;
@@ -239,7 +277,7 @@ audio_output_open(void)
int
audio_output_play(struct audio_file *fd)
{
- UInt32 len, size;
+ UInt32 len, size, adsnew;
int16_t *tmp;
/* Read data in our temporary buffer */
@@ -294,6 +332,16 @@ audio_output_play(struct audio_file *fd)
/* Atomically set the usage length */
g_atomic_int_set(&abufulen, len);
+ /* Check if the data source changed */
+ if (audio_output_get_datasource(&adsnew) != 0)
+ return (-1);
+
+ if (adscur != adsnew) {
+ /* Restart the device */
+ AudioDeviceStop(adid, aprocid);
+ adscur = adsnew;
+ }
+
/* Start processing of the data */
AudioDeviceStart(adid, aprocid);

0 comments on commit 7a20d75

Please sign in to comment.