Bug 212406 - Use one audio unit for all MediaStreamTracks of a given process
Summary: Use one audio unit for all MediaStreamTracks of a given process
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebRTC (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: youenn fablet
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-05-27 05:27 PDT by youenn fablet
Modified: 2020-06-08 07:02 PDT (History)
10 users (show)

See Also:


Attachments
Patch (39.01 KB, patch)
2020-05-27 06:10 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (40.07 KB, patch)
2020-05-27 06:20 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (39.90 KB, patch)
2020-05-28 02:17 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (39.91 KB, patch)
2020-05-29 05:45 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch for landing (40.45 KB, patch)
2020-06-08 05:02 PDT, youenn fablet
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description youenn fablet 2020-05-27 05:27:05 PDT
Use one audio unit for all tracks of a given process
Comment 1 youenn fablet 2020-05-27 06:10:48 PDT
Created attachment 400325 [details]
Patch
Comment 2 youenn fablet 2020-05-27 06:20:28 PDT
Created attachment 400327 [details]
Patch
Comment 3 Eric Carlson 2020-05-27 10:13:33 PDT
Comment on attachment 400327 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=400327&action=review

> Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp:-117
> -    if (source.streamDescription() != streamDescription())
> -        return kAudio_ParamError;
> -
> -    if (frameCount > source.sampleCount())
> -        frameCount = source.sampleCount();
> -
> -    if (frameCount > m_sampleCapacity)
> -        return kAudio_ParamError;

Maybe add ASSERTS for these?

> Source/WebCore/platform/mediastream/mac/AudioMediaStreamTrackRendererUnit.cpp:113
> +    if (m_isStarted)
> +        AudioOutputUnitStop(m_remoteIOUnit);
> +
> +    AudioComponentInstanceDispose(m_remoteIOUnit);
> +    m_remoteIOUnit = nullptr;

Should you set m_isStarted to false?

> Source/WebCore/platform/mediastream/mac/AudioMediaStreamTrackRendererUnit.cpp:203
> +    // Mix all sources.

Nit: this comment belongs with the next block.
Comment 4 youenn fablet 2020-05-27 13:32:51 PDT
(In reply to Eric Carlson from comment #3)
> Comment on attachment 400327 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=400327&action=review
> 
> > Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp:-117
> > -    if (source.streamDescription() != streamDescription())
> > -        return kAudio_ParamError;
> > -
> > -    if (frameCount > source.sampleCount())
> > -        frameCount = source.sampleCount();
> > -
> > -    if (frameCount > m_sampleCapacity)
> > -        return kAudio_ParamError;
> 
> Maybe add ASSERTS for these?

Will do.

> > Source/WebCore/platform/mediastream/mac/AudioMediaStreamTrackRendererUnit.cpp:113
> > +    if (m_isStarted)
> > +        AudioOutputUnitStop(m_remoteIOUnit);
> > +
> > +    AudioComponentInstanceDispose(m_remoteIOUnit);
> > +    m_remoteIOUnit = nullptr;
> 
> Should you set m_isStarted to false?

Right, will fix it.

> > Source/WebCore/platform/mediastream/mac/AudioMediaStreamTrackRendererUnit.cpp:203
> > +    // Mix all sources.
> 
> Nit: this comment belongs with the next block.

OK
Comment 5 youenn fablet 2020-05-28 02:17:37 PDT
Created attachment 400438 [details]
Patch
Comment 6 youenn fablet 2020-05-29 05:06:32 PDT
I tested using the core audio shared unit on MacOS in WebProcess and used it to play the track samples and this works. I haven't tried yet on iOS.
Comment 7 youenn fablet 2020-05-29 05:45:16 PDT
Created attachment 400575 [details]
Patch
Comment 8 youenn fablet 2020-06-05 07:56:45 PDT
ping review
Comment 9 Eric Carlson 2020-06-05 08:43:31 PDT
Comment on attachment 400575 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=400575&action=review

> Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp:-108
> +static void mixBuffers(WebAudioBufferList& destinationBuffer, const AudioBufferList& sourceBuffer, AudioStreamDescription::PCMFormat format, size_t frameCount)
>  {
> -    ASSERT(source.streamDescription() == streamDescription());

ASSERT(frameCount <= sourceBuffer.sampleCount()) ?

> Source/WebCore/platform/audio/mac/AudioSampleDataSource.mm:294
> +        if (m_volume < .95)

Not new to this patch but the magic number should be a named constant, especially since it is used in more than one place now.
Comment 10 Jer Noble 2020-06-05 09:37:15 PDT
Comment on attachment 400575 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=400575&action=review

The AudioMediaStreamTrackRendererUnit class here duplicates code in AudioDestinationCocoa. Could we refactor that class and use it here instead? The AudioDestinationCocoa class handles cases where the output buffer size changes, for example.  It also clamps output to [-1, 1] to avoid clipping.

> Source/WebCore/platform/mediastream/mac/AudioMediaStreamTrackRendererUnit.cpp:202
> +        m_renderSources = copyToVector(m_sources);

This will malloc on the audio thread, which is a blocking operation, and could cause audible glitches.
Comment 11 youenn fablet 2020-06-08 04:45:30 PDT
> > Source/WebCore/platform/audio/mac/AudioSampleBufferList.cpp:-108
> > +static void mixBuffers(WebAudioBufferList& destinationBuffer, const AudioBufferList& sourceBuffer, AudioStreamDescription::PCMFormat format, size_t frameCount)
> >  {
> > -    ASSERT(source.streamDescription() == streamDescription());
> 
> ASSERT(frameCount <= sourceBuffer.sampleCount()) ?

OK

> > Source/WebCore/platform/audio/mac/AudioSampleDataSource.mm:294
> > +        if (m_volume < .95)
> 
> Not new to this patch but the magic number should be a named constant,
> especially since it is used in more than one place now.

I added a constexpr.

> The AudioMediaStreamTrackRendererUnit class here duplicates code in
> AudioDestinationCocoa. Could we refactor that class and use it here instead?
> The AudioDestinationCocoa class handles cases where the output buffer size
> changes, for example.  It also clamps output to [-1, 1] to avoid clipping.

Yes, I saw the duplication. I'll refactor code as a follow-up.

> > Source/WebCore/platform/mediastream/mac/AudioMediaStreamTrackRendererUnit.cpp:202
> > +        m_renderSources = copyToVector(m_sources);
> 
> This will malloc on the audio thread, which is a blocking operation, and
> could cause audible glitches.

True, I will add another Vector as a member variable.
Comment 12 youenn fablet 2020-06-08 05:02:07 PDT
Created attachment 401325 [details]
Patch for landing
Comment 13 EWS 2020-06-08 07:01:02 PDT
Committed r262710: <https://trac.webkit.org/changeset/262710>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 401325 [details].
Comment 14 Radar WebKit Bug Importer 2020-06-08 07:02:18 PDT
<rdar://problem/64117578>