Skip to contentSkip to conversation

coreaudio fixes

PATCH Superseded Akihiko Odaki 2 participants 10 messages Jan 15, 2025 – Jan 16, 2025

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover coreaudio fixes

This series contains two fixes for coreaudio. See each one for details.

---
Akihiko Odaki (2):
coreaudio: Commit the result of init in the end
coreaudio: Initialize the buffer for device change

 audio/audio_int.h |  2 ++
 audio/audio.c     | 24 ++++++++++++++++++------
 audio/coreaudio.m | 50 ++++++++++++++++++++++++++++----------------------
 3 files changed, 48 insertions(+), 28 deletions(-)

---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20250109-coreaudio-c984607e1d8c

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v3 1/2 coreaudio: Commit the result of init in the end

init_out_device may only commit some part of the result and leave the state inconsistent when it encounters an error. Commit the result in the end of the function so that it commits the result iff it sees no error.

With this change, handle_voice_change can rely on core->outputDeviceID to know whether the output device is initialized after calling init_out_device.

---
 audio/coreaudio.m | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)
audio/coreaudio.m +27 -22
index cadd729d5053..b9e1a952ed37 100644
@@ -355,7 +355,10 @@ static OSStatus audioDeviceIOProc(
 static OSStatus init_out_device(coreaudioVoiceOut *core)
 {
     OSStatus status;
+    AudioDeviceID deviceID;
     AudioValueRange frameRange;
+    UInt32 audioDevicePropertyBufferFrameSize;
+    AudioDeviceIOProcID ioprocid;
 
     AudioStreamBasicDescription streamBasicDescription = {
         .mBitsPerChannel = core->hw.info.bits,
@@ -368,20 +371,19 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
         .mSampleRate = core->hw.info.freq
     };
 
-    status = coreaudio_get_voice(&core->outputDeviceID);
+    status = coreaudio_get_voice(&deviceID);
     if (status != kAudioHardwareNoError) {
         coreaudio_playback_logerr (status,
                                    "Could not get default output Device\n");
         return status;
     }
-    if (core->outputDeviceID == kAudioDeviceUnknown) {
+    if (deviceID == kAudioDeviceUnknown) {
         dolog ("Could not initialize playback - Unknown Audiodevice\n");
         return status;
     }
 
     /* get minimum and maximum buffer frame sizes */
-    status = coreaudio_get_framesizerange(core->outputDeviceID,
-                                          &frameRange);
+    status = coreaudio_get_framesizerange(deviceID, &frameRange);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
@@ -392,31 +394,31 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
     }
 
     if (frameRange.mMinimum > core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
         dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
     } else if (frameRange.mMaximum < core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
         dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
     } else {
-        core->audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
+        audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
     }
 
     /* set Buffer Frame Size */
-    status = coreaudio_set_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_set_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
     if (status != kAudioHardwareNoError) {
         coreaudio_playback_logerr (status,
                                     "Could not set device buffer frame size %" PRIu32 "\n",
-                                    (uint32_t)core->audioDevicePropertyBufferFrameSize);
+                                    (uint32_t)audioDevicePropertyBufferFrameSize);
         return status;
     }
 
     /* get Buffer Frame Size */
-    status = coreaudio_get_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_get_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
@@ -425,11 +427,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
                                     "Could not get device buffer frame size\n");
         return status;
     }
-    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
 
     /* set Samplerate */
-    status = coreaudio_set_streamformat(core->outputDeviceID,
-                                        &streamBasicDescription);
+    status = coreaudio_set_streamformat(deviceID, &streamBasicDescription);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
@@ -437,7 +437,6 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
         coreaudio_playback_logerr (status,
                                    "Could not set samplerate %lf\n",
                                    streamBasicDescription.mSampleRate);
-        core->outputDeviceID = kAudioDeviceUnknown;
         return status;
     }
 
@@ -451,20 +450,24 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
      * Therefore, the specified callback must be designed to avoid a deadlock
      * with the callers of AudioObjectGetPropertyData.
      */
-    core->ioprocid = NULL;
-    status = AudioDeviceCreateIOProcID(core->outputDeviceID,
+    ioprocid = NULL;
+    status = AudioDeviceCreateIOProcID(deviceID,
                                        audioDeviceIOProc,
                                        &core->hw,
-                                       &core->ioprocid);
+                                       &ioprocid);
     if (status == kAudioHardwareBadDeviceError) {
         return 0;
     }
-    if (status != kAudioHardwareNoError || core->ioprocid == NULL) {
+    if (status != kAudioHardwareNoError || ioprocid == NULL) {
         coreaudio_playback_logerr (status, "Could not set IOProc\n");
-        core->outputDeviceID = kAudioDeviceUnknown;
         return status;
     }
 
+    core->outputDeviceID = deviceID;
+    core->audioDevicePropertyBufferFrameSize = audioDevicePropertyBufferFrameSize;
+    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
+    core->ioprocid = ioprocid;
+
     return 0;
 }
 
@@ -548,7 +551,9 @@ static OSStatus handle_voice_change(
         fini_out_device(core);
     }
 
-    if (!init_out_device(core)) {
+    init_out_device(core);
+
+    if (core->outputDeviceID) {
         update_device_playback_state(core);
     }
 
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH v3 1/2] coreaudio: Commit the result of init in the end

Quoted 3 lines · CET Akihiko Odaki

init_out_device may only commit some part of the result and leave the state inconsistent when it encounters an error. Commit the result in the end of the function so that it commits the result iff it sees no

Typo "if".

Quoted 16 lines

error.

With this change, handle_voice_change can rely on core->outputDeviceID to know whether the output device is initialized after calling init_out_device.

---
 audio/coreaudio.m | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)
audio/coreaudio.m +0 -0
index cadd729d5053..b9e1a952ed37 100644

audio/coreaudio.m static OSStatus audioDeviceIOProc( · line 358

Quoted 4 lines
audio/coreaudio.m +1 -0
@@ -355,7 +355,10 @@ static OSStatus audioDeviceIOProc(
 static OSStatus init_out_device(coreaudioVoiceOut *core)
 {
     OSStatus status;
+    AudioDeviceID deviceID;

I would probably preserve the name 'outputDeviceID' to make it more clear that it's for output.

Quoted 49 lines
audio/coreaudio.m +8 -9
     AudioValueRange frameRange;
+    UInt32 audioDevicePropertyBufferFrameSize;
+    AudioDeviceIOProcID ioprocid;
 
     AudioStreamBasicDescription streamBasicDescription = {
         .mBitsPerChannel = core->hw.info.bits,
@@ -368,20 +371,19 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
         .mSampleRate = core->hw.info.freq
     };
 
-    status = coreaudio_get_voice(&core->outputDeviceID);
+    status = coreaudio_get_voice(&deviceID);
     if (status != kAudioHardwareNoError) {
         coreaudio_playback_logerr (status,
                                    "Could not get default output Device\n");
         return status;
     }
-    if (core->outputDeviceID == kAudioDeviceUnknown) {
+    if (deviceID == kAudioDeviceUnknown) {
         dolog ("Could not initialize playback - Unknown Audiodevice\n");
         return status;
     }
 
     /* get minimum and maximum buffer frame sizes */
-    status = coreaudio_get_framesizerange(core->outputDeviceID,
-                                          &frameRange);
+    status = coreaudio_get_framesizerange(deviceID, &frameRange);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
@@ -392,31 +394,31 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
     }
 
     if (frameRange.mMinimum > core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
         dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
     } else if (frameRange.mMaximum < core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
         dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
     } else {
-        core->audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
+        audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
     }
 
     /* set Buffer Frame Size */
-    status = coreaudio_set_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 415

Quoted 10 lines
audio/coreaudio.m +3 -1
+    status = coreaudio_set_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
     if (status != kAudioHardwareNoError) {
         coreaudio_playback_logerr (status,
                                     "Could not set device buffer frame size %" PRIu32 "\n",
-                                    (uint32_t)core->audioDevicePropertyBufferFrameSize);
+                                    (uint32_t)audioDevicePropertyBufferFrameSize);

'audioDevicePropertyBufferFrameSize' is declared as UInt32, so I guess the cast can be dropped.

Quoted 12 lines
audio/coreaudio.m +2 -2
         return status;
     }
 
     /* get Buffer Frame Size */
-    status = coreaudio_get_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_get_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 428

Quoted 4 lines
audio/coreaudio.m +0 -1
@@ -425,11 +427,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
                                     "Could not get device buffer frame size\n");
         return status;
     }
-    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;

Are you sure this should be deferred to the end of the function?

Quoted 59 lines
audio/coreaudio.m +13 -9
     /* set Samplerate */
-    status = coreaudio_set_streamformat(core->outputDeviceID,
-                                        &streamBasicDescription);
+    status = coreaudio_set_streamformat(deviceID, &streamBasicDescription);
     if (status == kAudioHardwareBadObjectError) {
         return 0;
     }
@@ -437,7 +437,6 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
         coreaudio_playback_logerr (status,
                                    "Could not set samplerate %lf\n",
                                    streamBasicDescription.mSampleRate);
-        core->outputDeviceID = kAudioDeviceUnknown;
         return status;
     }
 
@@ -451,20 +450,24 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
      * Therefore, the specified callback must be designed to avoid a deadlock
      * with the callers of AudioObjectGetPropertyData.
      */
-    core->ioprocid = NULL;
-    status = AudioDeviceCreateIOProcID(core->outputDeviceID,
+    ioprocid = NULL;
+    status = AudioDeviceCreateIOProcID(deviceID,
                                        audioDeviceIOProc,
                                        &core->hw,
-                                       &core->ioprocid);
+                                       &ioprocid);
     if (status == kAudioHardwareBadDeviceError) {
         return 0;
     }
-    if (status != kAudioHardwareNoError || core->ioprocid == NULL) {
+    if (status != kAudioHardwareNoError || ioprocid == NULL) {
         coreaudio_playback_logerr (status, "Could not set IOProc\n");
-        core->outputDeviceID = kAudioDeviceUnknown;
         return status;
     }
 
+    core->outputDeviceID = deviceID;
+    core->audioDevicePropertyBufferFrameSize = audioDevicePropertyBufferFrameSize;
+    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
+    core->ioprocid = ioprocid;
+
     return 0;
 }
 
@@ -548,7 +551,9 @@ static OSStatus handle_voice_change(
         fini_out_device(core);
     }
 
-    if (!init_out_device(core)) {
+    init_out_device(core);
+
+    if (core->outputDeviceID) {
         update_device_playback_state(core);
     }
 
#3 lore raw

Re: [PATCH v3 1/2] coreaudio: Commit the result of init in the end

Quoted 6 lines · Christian Schoenebeck
Quoted 3 lines · CET Akihiko Odaki

init_out_device may only commit some part of the result and leave the state inconsistent when it encounters an error. Commit the result in the end of the function so that it commits the result iff it sees no

Typo "if".

I meant if and only if.

Quoted 12 lines
Quoted 12 lines

error.

With this change, handle_voice_change can rely on core->outputDeviceID to know whether the output device is initialized after calling init_out_device.

---
  audio/coreaudio.m | 49 +++++++++++++++++++++++++++----------------------
  1 file changed, 27 insertions(+), 22 deletions(-)
Quoted 12 lines

audio/coreaudio.m static OSStatus audioDeviceIOProc( · line 358

Quoted 9 lines
audio/coreaudio.m +1 -0
index cadd729d5053..b9e1a952ed37 100644
@@ -355,7 +355,10 @@ static OSStatus audioDeviceIOProc(
  static OSStatus init_out_device(coreaudioVoiceOut *core)
  {
      OSStatus status;
+    AudioDeviceID deviceID;

I would probably preserve the name 'outputDeviceID' to make it more clear that it's for output.

I omitted output because this function is for the output device; every variable in this function is for output and prefixing them with output makes the code verbose.

Quoted 47 lines
Quoted 47 lines
audio/coreaudio.m +8 -7
      AudioValueRange frameRange;
+    UInt32 audioDevicePropertyBufferFrameSize;
+    AudioDeviceIOProcID ioprocid;
 
      AudioStreamBasicDescription streamBasicDescription = {
          .mBitsPerChannel = core->hw.info.bits,
@@ -368,20 +371,19 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
          .mSampleRate = core->hw.info.freq
      };
 
-    status = coreaudio_get_voice(&core->outputDeviceID);
+    status = coreaudio_get_voice(&deviceID);
      if (status != kAudioHardwareNoError) {
          coreaudio_playback_logerr (status,
                                     "Could not get default output Device\n");
          return status;
      }
-    if (core->outputDeviceID == kAudioDeviceUnknown) {
+    if (deviceID == kAudioDeviceUnknown) {
          dolog ("Could not initialize playback - Unknown Audiodevice\n");
          return status;
      }
 
      /* get minimum and maximum buffer frame sizes */
-    status = coreaudio_get_framesizerange(core->outputDeviceID,
-                                          &frameRange);
+    status = coreaudio_get_framesizerange(deviceID, &frameRange);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
@@ -392,31 +394,31 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
      }
 
      if (frameRange.mMinimum > core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
          dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
      } else if (frameRange.mMaximum < core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
          dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
      } else {
-        core->audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
+        audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
      }
 
Quoted 16 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 415

Quoted 13 lines
audio/coreaudio.m +3 -3
      /* set Buffer Frame Size */
-    status = coreaudio_set_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_set_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
      if (status != kAudioHardwareNoError) {
          coreaudio_playback_logerr (status,
                                      "Could not set device buffer frame size %" PRIu32 "\n",
-                                    (uint32_t)core->audioDevicePropertyBufferFrameSize);
+                                    (uint32_t)audioDevicePropertyBufferFrameSize);

'audioDevicePropertyBufferFrameSize' is declared as UInt32, so I guess the cast can be dropped.

It had a cast even though core->audioDevicePropertyBufferFrameSize is also UInt32. I suspect there are some cases where uint32_t and UInt32 are defined as different types and the compiler complains for "wrong" print format.

Quoted 4 lines
Quoted 4 lines
audio/coreaudio.m +0 -0
          return status;
      }
 
Quoted 15 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 428

Quoted 13 lines
audio/coreaudio.m +2 -3
      /* get Buffer Frame Size */
-    status = coreaudio_get_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_get_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
@@ -425,11 +427,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
                                      "Could not get device buffer frame size\n");
          return status;
      }
-    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;

Are you sure this should be deferred to the end of the function?

Yes. Setting core->hw.samples only makes sense after AudioDeviceCreateIOProcID() succeeds, which starts generating samples according to the set value.

Quoted 62 lines
Quoted 60 lines
audio/coreaudio.m +13 -9
      /* set Samplerate */
-    status = coreaudio_set_streamformat(core->outputDeviceID,
-                                        &streamBasicDescription);
+    status = coreaudio_set_streamformat(deviceID, &streamBasicDescription);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
@@ -437,7 +437,6 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
          coreaudio_playback_logerr (status,
                                     "Could not set samplerate %lf\n",
                                     streamBasicDescription.mSampleRate);
-        core->outputDeviceID = kAudioDeviceUnknown;
          return status;
      }
 
@@ -451,20 +450,24 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
       * Therefore, the specified callback must be designed to avoid a deadlock
       * with the callers of AudioObjectGetPropertyData.
       */
-    core->ioprocid = NULL;
-    status = AudioDeviceCreateIOProcID(core->outputDeviceID,
+    ioprocid = NULL;
+    status = AudioDeviceCreateIOProcID(deviceID,
                                         audioDeviceIOProc,
                                         &core->hw,
-                                       &core->ioprocid);
+                                       &ioprocid);
      if (status == kAudioHardwareBadDeviceError) {
          return 0;
      }
-    if (status != kAudioHardwareNoError || core->ioprocid == NULL) {
+    if (status != kAudioHardwareNoError || ioprocid == NULL) {
          coreaudio_playback_logerr (status, "Could not set IOProc\n");
-        core->outputDeviceID = kAudioDeviceUnknown;
          return status;
      }
 
+    core->outputDeviceID = deviceID;
+    core->audioDevicePropertyBufferFrameSize = audioDevicePropertyBufferFrameSize;
+    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
+    core->ioprocid = ioprocid;
+
      return 0;
  }
 
@@ -548,7 +551,9 @@ static OSStatus handle_voice_change(
          fini_out_device(core);
      }
 
-    if (!init_out_device(core)) {
+    init_out_device(core);
+
+    if (core->outputDeviceID) {
          update_device_playback_state(core);
      }
 
#4 lore raw

Re: [PATCH v3 1/2] coreaudio: Commit the result of init in the end

Quoted 35 lines · CET Akihiko Odaki
Quoted 6 lines · Christian Schoenebeck
Quoted 3 lines · CET Akihiko Odaki

init_out_device may only commit some part of the result and leave the state inconsistent when it encounters an error. Commit the result in the end of the function so that it commits the result iff it sees no

Typo "if".

I meant if and only if.

Quoted 12 lines
Quoted 12 lines

error.

With this change, handle_voice_change can rely on core->outputDeviceID to know whether the output device is initialized after calling init_out_device.

---
  audio/coreaudio.m | 49 +++++++++++++++++++++++++++----------------------
  1 file changed, 27 insertions(+), 22 deletions(-)
Quoted 12 lines

audio/coreaudio.m static OSStatus audioDeviceIOProc( · line 358

Quoted 9 lines
audio/coreaudio.m +1 -0
index cadd729d5053..b9e1a952ed37 100644
@@ -355,7 +355,10 @@ static OSStatus audioDeviceIOProc(
  static OSStatus init_out_device(coreaudioVoiceOut *core)
  {
      OSStatus status;
+    AudioDeviceID deviceID;

I would probably preserve the name 'outputDeviceID' to make it more clear that it's for output.

Quoted 3 lines

I omitted output because this function is for the output device; every variable in this function is for output and prefixing them with output makes the code verbose.

Disagree. When you review audio driver code there are many 'devices', so it is helpful to see the context straight away. Especially on large functions like this one.

Quoted 64 lines
Quoted 47 lines
Quoted 47 lines
audio/coreaudio.m +8 -7
      AudioValueRange frameRange;
+    UInt32 audioDevicePropertyBufferFrameSize;
+    AudioDeviceIOProcID ioprocid;
 
      AudioStreamBasicDescription streamBasicDescription = {
          .mBitsPerChannel = core->hw.info.bits,
@@ -368,20 +371,19 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
          .mSampleRate = core->hw.info.freq
      };
 
-    status = coreaudio_get_voice(&core->outputDeviceID);
+    status = coreaudio_get_voice(&deviceID);
      if (status != kAudioHardwareNoError) {
          coreaudio_playback_logerr (status,
                                     "Could not get default output Device\n");
          return status;
      }
-    if (core->outputDeviceID == kAudioDeviceUnknown) {
+    if (deviceID == kAudioDeviceUnknown) {
          dolog ("Could not initialize playback - Unknown Audiodevice\n");
          return status;
      }
 
      /* get minimum and maximum buffer frame sizes */
-    status = coreaudio_get_framesizerange(core->outputDeviceID,
-                                          &frameRange);
+    status = coreaudio_get_framesizerange(deviceID, &frameRange);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
@@ -392,31 +394,31 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
      }
 
      if (frameRange.mMinimum > core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
          dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
      } else if (frameRange.mMaximum < core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
          dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
      } else {
-        core->audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
+        audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
      }
 
Quoted 16 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 415

Quoted 13 lines
audio/coreaudio.m +3 -3
      /* set Buffer Frame Size */
-    status = coreaudio_set_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_set_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
      if (status != kAudioHardwareNoError) {
          coreaudio_playback_logerr (status,
                                      "Could not set device buffer frame size %" PRIu32 "\n",
-                                    (uint32_t)core->audioDevicePropertyBufferFrameSize);
+                                    (uint32_t)audioDevicePropertyBufferFrameSize);

'audioDevicePropertyBufferFrameSize' is declared as UInt32, so I guess the cast can be dropped.

Quoted 4 lines

It had a cast even though core->audioDevicePropertyBufferFrameSize is also UInt32. I suspect there are some cases where uint32_t and UInt32 are defined as different types and the compiler complains for "wrong" print format.

This was introduced by cbc36cb05. While changing the format specifier made sense, the cast was unnecessary, but ... never mind.

Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
audio/coreaudio.m +0 -0
          return status;
      }
 
Quoted 15 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 428

Quoted 13 lines
audio/coreaudio.m +2 -3
      /* get Buffer Frame Size */
-    status = coreaudio_get_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_get_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
      if (status == kAudioHardwareBadObjectError) {
          return 0;
      }
@@ -425,11 +427,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
                                      "Could not get device buffer frame size\n");
          return status;
      }
-    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;

Are you sure this should be deferred to the end of the function?

Quoted 3 lines

Yes. Setting core->hw.samples only makes sense after AudioDeviceCreateIOProcID() succeeds, which starts generating samples according to the set value.

OK, I just reviewed the pathes in this function that return zero, and realized that these were added by you (3ba6e3f6), however I don't see any comment on why you have treated them as returning as non-error cases there.

In general I highly recommend to be more verbose on your changes. For instance for this patch here it would have helped to see a comment which error path exactly you encountered. Because it makes it more easy for other people to understand what you are trying to fix exactly.

/Christian

#5 lore raw

Re: [PATCH v3 1/2] coreaudio: Commit the result of init in the end

Quoted 36 lines · Christian Schoenebeck
Quoted 35 lines · CET Akihiko Odaki
Quoted 6 lines · Christian Schoenebeck
Quoted 3 lines · CET Akihiko Odaki

init_out_device may only commit some part of the result and leave the state inconsistent when it encounters an error. Commit the result in the end of the function so that it commits the result iff it sees no

Typo "if".

I meant if and only if.

Quoted 24 lines
Quoted 17 lines

error.

With this change, handle_voice_change can rely on core->outputDeviceID to know whether the output device is initialized after calling init_out_device.

---
   audio/coreaudio.m | 49 +++++++++++++++++++++++++++----------------------
   1 file changed, 27 insertions(+), 22 deletions(-)
audio/coreaudio.m +0 -0
index cadd729d5053..b9e1a952ed37 100644

audio/coreaudio.m static OSStatus audioDeviceIOProc( · line 358

Quoted 4 lines
audio/coreaudio.m +1 -0
@@ -355,7 +355,10 @@ static OSStatus audioDeviceIOProc(
   static OSStatus init_out_device(coreaudioVoiceOut *core)
   {
       OSStatus status;
+    AudioDeviceID deviceID;

I would probably preserve the name 'outputDeviceID' to make it more clear that it's for output.

Quoted 7 lines
Quoted 3 lines

I omitted output because this function is for the output device; every variable in this function is for output and prefixing them with output makes the code verbose.

Disagree. When you review audio driver code there are many 'devices', so it is helpful to see the context straight away. Especially on large functions like this one.

It is not special to the device ID. Other variables like frameRange, audioDevicePropertyBufferFrameSize, and ioprocid are bound to this output device. In theory we could have similar corresponding variables for the input device so they should be prefixed with "output" if it matters. Prefixing only this variable is inconsistent.

Quoted 93 lines
Quoted 65 lines
Quoted 48 lines
Quoted 48 lines
audio/coreaudio.m +8 -7
       AudioValueRange frameRange;
+    UInt32 audioDevicePropertyBufferFrameSize;
+    AudioDeviceIOProcID ioprocid;
 
       AudioStreamBasicDescription streamBasicDescription = {
           .mBitsPerChannel = core->hw.info.bits,
@@ -368,20 +371,19 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
           .mSampleRate = core->hw.info.freq
       };
 
-    status = coreaudio_get_voice(&core->outputDeviceID);
+    status = coreaudio_get_voice(&deviceID);
       if (status != kAudioHardwareNoError) {
           coreaudio_playback_logerr (status,
                                      "Could not get default output Device\n");
           return status;
       }
-    if (core->outputDeviceID == kAudioDeviceUnknown) {
+    if (deviceID == kAudioDeviceUnknown) {
           dolog ("Could not initialize playback - Unknown Audiodevice\n");
           return status;
       }
 
       /* get minimum and maximum buffer frame sizes */
-    status = coreaudio_get_framesizerange(core->outputDeviceID,
-                                          &frameRange);
+    status = coreaudio_get_framesizerange(deviceID, &frameRange);
       if (status == kAudioHardwareBadObjectError) {
           return 0;
       }
@@ -392,31 +394,31 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
       }
 
       if (frameRange.mMinimum > core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
           dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
       } else if (frameRange.mMaximum < core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
+        audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
           dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
       } else {
-        core->audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
+        audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
       }
 
Quoted 16 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 415

Quoted 13 lines
audio/coreaudio.m +3 -3
       /* set Buffer Frame Size */
-    status = coreaudio_set_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_set_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
       if (status == kAudioHardwareBadObjectError) {
           return 0;
       }
       if (status != kAudioHardwareNoError) {
           coreaudio_playback_logerr (status,
                                       "Could not set device buffer frame size %" PRIu32 "\n",
-                                    (uint32_t)core->audioDevicePropertyBufferFrameSize);
+                                    (uint32_t)audioDevicePropertyBufferFrameSize);

'audioDevicePropertyBufferFrameSize' is declared as UInt32, so I guess the cast can be dropped.

Quoted 4 lines

It had a cast even though core->audioDevicePropertyBufferFrameSize is also UInt32. I suspect there are some cases where uint32_t and UInt32 are defined as different types and the compiler complains for "wrong" print format.

This was introduced by cbc36cb05. While changing the format specifier made sense, the cast was unnecessary, but ... never mind.

Quoted 20 lines
Quoted 19 lines
Quoted 13 lines
audio/coreaudio.m +2 -2
           return status;
       }
 
       /* get Buffer Frame Size */
-    status = coreaudio_get_framesize(core->outputDeviceID,
-                                     &core->audioDevicePropertyBufferFrameSize);
+    status = coreaudio_get_framesize(deviceID,
+                                     &audioDevicePropertyBufferFrameSize);
       if (status == kAudioHardwareBadObjectError) {
           return 0;
       }

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 428

Quoted 4 lines
audio/coreaudio.m +0 -1
@@ -425,11 +427,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
                                       "Could not get device buffer frame size\n");
           return status;
       }
-    core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;

Are you sure this should be deferred to the end of the function?

Quoted 7 lines
Quoted 3 lines

Yes. Setting core->hw.samples only makes sense after AudioDeviceCreateIOProcID() succeeds, which starts generating samples according to the set value.

OK, I just reviewed the pathes in this function that return zero, and realized that these were added by you (3ba6e3f6), however I don't see any comment on why you have treated them as returning as non-error cases there.

kAudioHardwareBadObjectError and kAudioHardwareBadDeviceError imply the device was unplugged. They are not fatal as eventually we will get another active device with the handle_voice_change callback.

Regards,
Akihiko Odaki

Quoted 9 lines

In general I highly recommend to be more verbose on your changes. For instance for this patch here it would have helped to see a comment which error path exactly you encountered. Because it makes it more easy for other people to understand what you are trying to fix exactly.

/Christian

#6 lore raw

Re: [PATCH v3 1/2] coreaudio: Commit the result of init in the end

Quoted 15 lines · CET Akihiko Odaki
Quoted 14 lines · Christian Schoenebeck
Quoted 13 lines · CET Akihiko Odaki
Quoted 6 lines · Christian Schoenebeck
Quoted 3 lines · CET Akihiko Odaki

init_out_device may only commit some part of the result and leave the state inconsistent when it encounters an error. Commit the result in the end of the function so that it commits the result iff it sees no

Typo "if".

I meant if and only if.

Quoted 3 lines
Quoted 3 lines

error.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

With this change, handle_voice_change can rely on core->outputDeviceID to know whether the output device is initialized after calling init_out_device.

---
   audio/coreaudio.m | 49 ++++++++++++++++++++++++++
+----------------------
> >>>>    1 file changed, 27 insertions(+), 22 deletions(-)
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

audio/coreaudio.m static OSStatus audioDeviceIOProc( · line 358

Quoted 10 lines
audio/coreaudio.m +1 -0
index cadd729d5053..b9e1a952ed37 100644
@@ -355,7 +355,10 @@ static OSStatus audioDeviceIOProc(
   static OSStatus init_out_device(coreaudioVoiceOut *core)
   {
       OSStatus status;
+    AudioDeviceID deviceID;

I would probably preserve the name 'outputDeviceID' to make it more

clear that

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

it's for output.

I omitted output because this function is for the output device; every variable in this function is for output and prefixing them with output makes the code verbose.

Disagree. When you review audio driver code there are many 'devices', so

it is

Quoted 1 line
Quoted 1 line

helpful to see the context straight away. Especially on large functions

like

Quoted 10 lines
Quoted 1 line

this one.

It is not special to the device ID. Other variables like frameRange, audioDevicePropertyBufferFrameSize, and ioprocid are bound to this output device. In theory we could have similar corresponding variables for the input device so they should be prefixed with "output" if it matters. Prefixing only this variable is inconsistent.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut

Quoted 7 lines
audio/coreaudio.m +2 -0
       AudioValueRange frameRange;
+    UInt32 audioDevicePropertyBufferFrameSize;
+    AudioDeviceIOProcID ioprocid;
 
       AudioStreamBasicDescription streamBasicDescription = {
           .mBitsPerChannel = core->hw.info.bits,
@@ -368,20 +371,19 @@ static OSStatus init_out_device(coreaudioVoiceOut 

*core)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 8 lines
audio/coreaudio.m +1 -1
           .mSampleRate = core->hw.info.freq
       };
 
-    status = coreaudio_get_voice(&core->outputDeviceID);
+    status = coreaudio_get_voice(&deviceID);
       if (status != kAudioHardwareNoError) {
           coreaudio_playback_logerr (status,
                                      "Could not get default output 

Device\n");

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 5 lines
audio/coreaudio.m +1 -1
           return status;
       }
-    if (core->outputDeviceID == kAudioDeviceUnknown) {
+    if (deviceID == kAudioDeviceUnknown) {
           dolog ("Could not initialize playback - Unknown 

Audiodevice\n");

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut

Quoted 11 lines
audio/coreaudio.m +1 -2
           return status;
       }
 
       /* get minimum and maximum buffer frame sizes */
-    status = coreaudio_get_framesizerange(core->outputDeviceID,
-                                          &frameRange);
+    status = coreaudio_get_framesizerange(deviceID, &frameRange);
       if (status == kAudioHardwareBadObjectError) {
           return 0;
       }
@@ -392,31 +394,31 @@ static OSStatus init_out_device(coreaudioVoiceOut 

*core)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 4 lines
audio/coreaudio.m +0 -1
       }
 
       if (frameRange.mMinimum > core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) 

frameRange.mMinimum;

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 397

Quoted 1 line
audio/coreaudio.m +1 -0
+        audioDevicePropertyBufferFrameSize = (UInt32) 

frameRange.mMinimum;

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 398

Quoted 1 line
audio/coreaudio.m +0 -0
           dolog ("warning: Upsizing Buffer Frames to %f\n", 

frameRange.mMinimum);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 398

Quoted 2 lines
audio/coreaudio.m +0 -1
       } else if (frameRange.mMaximum < core->frameSizeSetting) {
-        core->audioDevicePropertyBufferFrameSize = (UInt32) 

frameRange.mMaximum;

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 400

Quoted 1 line
audio/coreaudio.m +1 -0
+        audioDevicePropertyBufferFrameSize = (UInt32) 

frameRange.mMaximum;

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 401

Quoted 1 line
audio/coreaudio.m +0 -0
           dolog ("warning: Downsizing Buffer Frames to %f\n", 

frameRange.mMaximum);

Quoted 12 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 401

Quoted 2 lines
audio/coreaudio.m +0 -1
       } else {
-        core->audioDevicePropertyBufferFrameSize = core-

frameSizeSetting;

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 6 lines
audio/coreaudio.m +1 -2
+        audioDevicePropertyBufferFrameSize = core->frameSizeSetting;
       }
 
       /* set Buffer Frame Size */
-    status = coreaudio_set_framesize(core->outputDeviceID,
-                                     &core-

audioDevicePropertyBufferFrameSize);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
audio/coreaudio.m +2 -0
+    status = coreaudio_set_framesize(deviceID,
+                                     

&audioDevicePropertyBufferFrameSize);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 6 lines
audio/coreaudio.m +0 -0
       if (status == kAudioHardwareBadObjectError) {
           return 0;
       }
       if (status != kAudioHardwareNoError) {
           coreaudio_playback_logerr (status,
                                       "Could not set device buffer 

frame size %" PRIu32 "\n",

Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 413

Quoted 1 line
audio/coreaudio.m +0 -1
-                                    (uint32_t)core-

audioDevicePropertyBufferFrameSize);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
audio/coreaudio.m +1 -0
+                                    

(uint32_t)audioDevicePropertyBufferFrameSize);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

'audioDevicePropertyBufferFrameSize' is declared as UInt32, so I guess

the

Quoted 11 lines
Quoted 11 lines
Quoted 6 lines
Quoted 1 line

cast can be dropped.

It had a cast even though core->audioDevicePropertyBufferFrameSize is also UInt32. I suspect there are some cases where uint32_t and UInt32 are defined as different types and the compiler complains for "wrong" print format.

This was introduced by cbc36cb05. While changing the format specifier made sense, the cast was unnecessary, but ... never mind.

Quoted 1 line
Quoted 1 line
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 6 lines
audio/coreaudio.m +0 -2
           return status;
       }
 
       /* get Buffer Frame Size */
-    status = coreaudio_get_framesize(core->outputDeviceID,
-                                     &core-

audioDevicePropertyBufferFrameSize);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
audio/coreaudio.m +2 -0
+    status = coreaudio_get_framesize(deviceID,
+                                     

&audioDevicePropertyBufferFrameSize);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut

Quoted 4 lines
audio/coreaudio.m +0 -0
       if (status == kAudioHardwareBadObjectError) {
           return 0;
       }
@@ -425,11 +427,9 @@ static OSStatus init_out_device(coreaudioVoiceOut 

*core)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut · line 427

Quoted 1 line
audio/coreaudio.m +0 -0
                                       "Could not get device buffer 

frame size\n");

Quoted 12 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core)

Quoted 3 lines
audio/coreaudio.m +0 -1
           return status;
       }
-    core->hw.samples = core->bufferCount * core-

audioDevicePropertyBufferFrameSize;

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Are you sure this should be deferred to the end of the function?

Yes. Setting core->hw.samples only makes sense after AudioDeviceCreateIOProcID() succeeds, which starts generating samples according to the set value.

OK, I just reviewed the pathes in this function that return zero, and

realized

Quoted 1 line
Quoted 1 line

that these were added by you (3ba6e3f6), however I don't see any comment

on

Quoted 5 lines
Quoted 1 line

why you have treated them as returning as non-error cases there.

kAudioHardwareBadObjectError and kAudioHardwareBadDeviceError imply the device was unplugged. They are not fatal as eventually we will get another active device with the handle_voice_change callback.

It would "imply" a device been unplugged if either two of the constants were documented by Apple exactly that way, which is not the case.

What happened instead is that you noticed that this error was returned by the CoreAudio API after you unplugged an audio device. That's called "observed".

And that's exactly my point why I think you should be more verbose on your changes in general, so that other people can also understand your changes, without having to ask you. They are definitely not clear enough for other readers.

Quoted 1 line
Quoted 1 line

In general I highly recommend to be more verbose on your changes. For

instance

Quoted 10 lines
Quoted 7 lines

for this patch here it would have helped to see a comment which error path exactly you encountered. Because it makes it more easy for other people to understand what you are trying to fix exactly.

/Christian

#7 lore raw

PATCH v3 2/2 coreaudio: Initialize the buffer for device change

In reply to Akihiko Odaki

Reallocate buffers when the active device change as the required buffer size may differ.

---
 audio/audio_int.h |  2 ++
 audio/audio.c     | 24 ++++++++++++++++++------
 audio/coreaudio.m |  1 +
 3 files changed, 21 insertions(+), 6 deletions(-)
audio/audio_int.h +2 -0
index 2d079d00a259..9ba4a144d571 100644
@@ -187,9 +187,11 @@ struct audio_pcm_ops {
     void   (*volume_in)(HWVoiceIn *hw, Volume *vol);
 };
 
+void audio_generic_initialize_buffer_in(HWVoiceIn *hw);
 void audio_generic_run_buffer_in(HWVoiceIn *hw);
 void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
 void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
+void audio_generic_initialize_buffer_out(HWVoiceOut *hw);
 void audio_generic_run_buffer_out(HWVoiceOut *hw);
 size_t audio_generic_buffer_get_free(HWVoiceOut *hw);
 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
audio/audio.c +18 -6
index 87b4e9b6f2f3..17c6bbd0ae9e 100644
@@ -1407,12 +1407,18 @@ void audio_run(AudioState *s, const char *msg)
 #endif
 }
 
+void audio_generic_initialize_buffer_in(HWVoiceIn *hw)
+{
+    g_free(hw->buf_emul);
+    hw->size_emul = hw->samples * hw->info.bytes_per_frame;
+    hw->buf_emul = g_malloc(hw->size_emul);
+    hw->pos_emul = hw->pending_emul = 0;
+}
+
 void audio_generic_run_buffer_in(HWVoiceIn *hw)
 {
     if (unlikely(!hw->buf_emul)) {
-        hw->size_emul = hw->samples * hw->info.bytes_per_frame;
-        hw->buf_emul = g_malloc(hw->size_emul);
-        hw->pos_emul = hw->pending_emul = 0;
+        audio_generic_initialize_buffer_in(hw);
     }
 
     while (hw->pending_emul < hw->size_emul) {
@@ -1446,6 +1452,14 @@ void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
     hw->pending_emul -= size;
 }
 
+void audio_generic_initialize_buffer_out(HWVoiceOut *hw)
+{
+    g_free(hw->buf_emul);
+    hw->size_emul = hw->samples * hw->info.bytes_per_frame;
+    hw->buf_emul = g_malloc(hw->size_emul);
+    hw->pos_emul = hw->pending_emul = 0;
+}
+
 size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
 {
     if (hw->buf_emul) {
@@ -1477,9 +1491,7 @@ void audio_generic_run_buffer_out(HWVoiceOut *hw)
 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
 {
     if (unlikely(!hw->buf_emul)) {
-        hw->size_emul = hw->samples * hw->info.bytes_per_frame;
-        hw->buf_emul = g_malloc(hw->size_emul);
-        hw->pos_emul = hw->pending_emul = 0;
+        audio_generic_initialize_buffer_out(hw);
     }
 
     *size = MIN(hw->size_emul - hw->pending_emul,
audio/coreaudio.m +1 -0
index b9e1a952ed37..72a6df0f75ee 100644
@@ -466,6 +466,7 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
     core->outputDeviceID = deviceID;
     core->audioDevicePropertyBufferFrameSize = audioDevicePropertyBufferFrameSize;
     core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
+    audio_generic_initialize_buffer_out(&core->hw);
     core->ioprocid = ioprocid;
 
     return 0;
Signature · 1 line
2.47.1
#8 lore raw

Re: [PATCH v3 2/2] coreaudio: Initialize the buffer for device change

Quoted 32 lines · CET Akihiko Odaki

Reallocate buffers when the active device change as the required buffer size may differ.

---
 audio/audio_int.h |  2 ++
 audio/audio.c     | 24 ++++++++++++++++++------
 audio/coreaudio.m |  1 +
 3 files changed, 21 insertions(+), 6 deletions(-)
audio/audio_int.h +2 -0
index 2d079d00a259..9ba4a144d571 100644
@@ -187,9 +187,11 @@ struct audio_pcm_ops {
     void   (*volume_in)(HWVoiceIn *hw, Volume *vol);
 };
 
+void audio_generic_initialize_buffer_in(HWVoiceIn *hw);
 void audio_generic_run_buffer_in(HWVoiceIn *hw);
 void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
 void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
+void audio_generic_initialize_buffer_out(HWVoiceOut *hw);
 void audio_generic_run_buffer_out(HWVoiceOut *hw);
 size_t audio_generic_buffer_get_free(HWVoiceOut *hw);
 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
audio/audio.c +0 -0
index 87b4e9b6f2f3..17c6bbd0ae9e 100644
@@ -1407,12 +1407,18 @@ void audio_run(AudioState *s, const char *msg)
 #endif

audio/audio.c void audio_run(AudioState *s, const char *msg) · line 1417

Quoted 10 lines
audio/audio.c +8 -0
 }
 
+void audio_generic_initialize_buffer_in(HWVoiceIn *hw)
+{
+    g_free(hw->buf_emul);
+    hw->size_emul = hw->samples * hw->info.bytes_per_frame;
+    hw->buf_emul = g_malloc(hw->size_emul);
+    hw->pos_emul = hw->pending_emul = 0;
+}
+

Better something like "reinit" in the name maybe?

Quoted 41 lines
audio/audio.c +10 -6
 void audio_generic_run_buffer_in(HWVoiceIn *hw)
 {
     if (unlikely(!hw->buf_emul)) {
-        hw->size_emul = hw->samples * hw->info.bytes_per_frame;
-        hw->buf_emul = g_malloc(hw->size_emul);
-        hw->pos_emul = hw->pending_emul = 0;
+        audio_generic_initialize_buffer_in(hw);
     }
 
     while (hw->pending_emul < hw->size_emul) {
@@ -1446,6 +1452,14 @@ void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
     hw->pending_emul -= size;
 }
 
+void audio_generic_initialize_buffer_out(HWVoiceOut *hw)
+{
+    g_free(hw->buf_emul);
+    hw->size_emul = hw->samples * hw->info.bytes_per_frame;
+    hw->buf_emul = g_malloc(hw->size_emul);
+    hw->pos_emul = hw->pending_emul = 0;
+}
+
 size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
 {
     if (hw->buf_emul) {
@@ -1477,9 +1491,7 @@ void audio_generic_run_buffer_out(HWVoiceOut *hw)
 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
 {
     if (unlikely(!hw->buf_emul)) {
-        hw->size_emul = hw->samples * hw->info.bytes_per_frame;
-        hw->buf_emul = g_malloc(hw->size_emul);
-        hw->pos_emul = hw->pending_emul = 0;
+        audio_generic_initialize_buffer_out(hw);
     }
 
     *size = MIN(hw->size_emul - hw->pending_emul,
audio/coreaudio.m +0 -0
index b9e1a952ed37..72a6df0f75ee 100644

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 470

Quoted 5 lines
audio/coreaudio.m +1 -0
@@ -466,6 +466,7 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
     core->outputDeviceID = deviceID;
     core->audioDevicePropertyBufferFrameSize = audioDevicePropertyBufferFrameSize;
     core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
+    audio_generic_initialize_buffer_out(&core->hw);
     core->ioprocid = ioprocid;

I would have probably separated this change into a separate patch, as changes above were more or less just refactoring, whereas this one changes behaviour.

And like my comment in the previous patch, I wonder whether that call comes too late. Keep in mind there are e.g. audio devices where you can't change certain parameters. So not every error here is a fatal error.

Quoted 2 lines
audio/coreaudio.m +0 -0
     return 0;
#9 lore raw

Re: [PATCH v3 2/2] coreaudio: Initialize the buffer for device change

Quoted 81 lines · Christian Schoenebeck
Quoted 32 lines · CET Akihiko Odaki

Reallocate buffers when the active device change as the required buffer size may differ.

---
  audio/audio_int.h |  2 ++
  audio/audio.c     | 24 ++++++++++++++++++------
  audio/coreaudio.m |  1 +
  3 files changed, 21 insertions(+), 6 deletions(-)
audio/audio_int.h +2 -0
index 2d079d00a259..9ba4a144d571 100644
@@ -187,9 +187,11 @@ struct audio_pcm_ops {
      void   (*volume_in)(HWVoiceIn *hw, Volume *vol);
  };
 
+void audio_generic_initialize_buffer_in(HWVoiceIn *hw);
  void audio_generic_run_buffer_in(HWVoiceIn *hw);
  void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
  void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
+void audio_generic_initialize_buffer_out(HWVoiceOut *hw);
  void audio_generic_run_buffer_out(HWVoiceOut *hw);
  size_t audio_generic_buffer_get_free(HWVoiceOut *hw);
  void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
audio/audio.c +0 -0
index 87b4e9b6f2f3..17c6bbd0ae9e 100644
@@ -1407,12 +1407,18 @@ void audio_run(AudioState *s, const char *msg)
  #endif

audio/audio.c void audio_run(AudioState *s, const char *msg) · line 1417

Quoted 10 lines
audio/audio.c +8 -0
  }
 
+void audio_generic_initialize_buffer_in(HWVoiceIn *hw)
+{
+    g_free(hw->buf_emul);
+    hw->size_emul = hw->samples * hw->info.bytes_per_frame;
+    hw->buf_emul = g_malloc(hw->size_emul);
+    hw->pos_emul = hw->pending_emul = 0;
+}
+

Better something like "reinit" in the name maybe?

Quoted 35 lines
audio/audio.c +10 -6
  void audio_generic_run_buffer_in(HWVoiceIn *hw)
  {
      if (unlikely(!hw->buf_emul)) {
-        hw->size_emul = hw->samples * hw->info.bytes_per_frame;
-        hw->buf_emul = g_malloc(hw->size_emul);
-        hw->pos_emul = hw->pending_emul = 0;
+        audio_generic_initialize_buffer_in(hw);
      }
 
      while (hw->pending_emul < hw->size_emul) {
@@ -1446,6 +1452,14 @@ void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
      hw->pending_emul -= size;
  }
 
+void audio_generic_initialize_buffer_out(HWVoiceOut *hw)
+{
+    g_free(hw->buf_emul);
+    hw->size_emul = hw->samples * hw->info.bytes_per_frame;
+    hw->buf_emul = g_malloc(hw->size_emul);
+    hw->pos_emul = hw->pending_emul = 0;
+}
+
  size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
  {
      if (hw->buf_emul) {
@@ -1477,9 +1491,7 @@ void audio_generic_run_buffer_out(HWVoiceOut *hw)
  void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
  {
      if (unlikely(!hw->buf_emul)) {
-        hw->size_emul = hw->samples * hw->info.bytes_per_frame;
-        hw->buf_emul = g_malloc(hw->size_emul);
-        hw->pos_emul = hw->pending_emul = 0;
+        audio_generic_initialize_buffer_out(hw);
      }
 
Quoted 14 lines

audio/coreaudio.m static OSStatus init_out_device(coreaudioVoiceOut *core) · line 470

Quoted 11 lines
audio/audio.c +0 -0
      *size = MIN(hw->size_emul - hw->pending_emul,
audio/coreaudio.m +1 -0
index b9e1a952ed37..72a6df0f75ee 100644
@@ -466,6 +466,7 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
      core->outputDeviceID = deviceID;
      core->audioDevicePropertyBufferFrameSize = audioDevicePropertyBufferFrameSize;
      core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize;
+    audio_generic_initialize_buffer_out(&core->hw);
      core->ioprocid = ioprocid;

I would have probably separated this change into a separate patch, as changes above were more or less just refactoring, whereas this one changes behaviour.

That makes sense. I will do so if I have to respin this series.

Quoted 4 lines

And like my comment in the previous patch, I wonder whether that call comes too late. Keep in mind there are e.g. audio devices where you can't change certain parameters. So not every error here is a fatal error.

A function called late, AudioDeviceCreateIOProcID() is essential to make the output device functional so any error which triggers an early return before that is fatal.