Skip to content

Not a good practice to access AudioQueueBuffer.AudioData by Marshal.ReadIntPtr #6410

@zjli-2019

Description

@zjli-2019

I am working on iOS audio. After struggling with IntPtr-s, which might be a AudioQueueBuffer*,
or pointing to raw samples, I finally managed to make things work.

But, I find below code seems not a good practice.

https://github.com/xamarin/xamarin-macios/blob/e45c3ba794e25c4ecb1c076678b733746ca184e4/src/AudioToolbox/AudioQueue.cs#L499

Steps to Reproduce

Check the source code of AudioQueue.FillAudioData(...).

public static void FillAudioData (IntPtr audioQueueBuffer, int offset, IntPtr source, int sourceOffset, nint size)
{
    // Here, location of field AudioData is almost *hard-coded*
    IntPtr target = Marshal.ReadIntPtr (audioQueueBuffer, IntPtr.Size);
    unsafe {
        byte *targetp = (byte *) target;
        byte *sourcep = (byte *) source;
        Runtime.memcpy (targetp + offset, sourcep + sourceOffset, size);
    }
}

Expected Behavior

  1. Use AudioQueueBuffer.AudioData;
  2. Use AudioQueueBuffer.CopyToAudioData(IntPtr source, int size).

Actual Behavior

The implementation seems not a good one.

Environment

N/A

Build Logs

N/A

Example Project (If Possible)

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThe issue or pull request is an enhancementiOSIssues affecting iOSmacOSIssues affecting macOS

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions