Bug 76417 - Only create AudioBus with required number of channels for AudioNodeOutput
Summary: Only create AudioBus with required number of channels for AudioNodeOutput
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Audio (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Raymond
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-16 19:00 PST by Raymond
Modified: 2012-01-19 16:42 PST (History)
4 users (show)

See Also:


Attachments
Patch (5.88 KB, patch)
2012-01-16 20:08 PST, Raymond
no flags Details | Formatted Diff | Diff
Patch (5.96 KB, patch)
2012-01-17 17:50 PST, Raymond
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Raymond 2012-01-16 19:00:11 PST
Only create AudioBus with required number of channels for AudioNodeOutput
Comment 1 Raymond 2012-01-16 20:08:18 PST
Created attachment 122705 [details]
Patch
Comment 2 Raymond 2012-01-16 20:10:12 PST
Hi

this patch is aim to create AudioBus on demand for AudioNodeOutput to :

1. Save memory
2. Help to support multi-channels for WebAudio later more easily.

My goal is also apply this to AudioNodeInput, While since AudioNodeInput's case is slightly complicated than AudioNodeOutput due to the similar change might involve changes in AudioNode's checkNumberOfChannelsForInput and thus its subclass, So I would prefer to do it in a separate patch when this patch is accepted.

One thing I am not quite sure is that it seems to me the original design is to pre-allocate all memories upon node creation, is that aim to help the render thread run more smoothly? However, since this memory allocation work is done at the beginning or end of a rendering quantum, and also in some audio processor class they also do memory allocations upon channels change, so I think this might not be a big deal, and after all, channels number rarely changes.
Comment 3 Chris Rogers 2012-01-17 14:13:22 PST
Raymond, thanks for the patch.

As it stands right now the re-allocation of AudioBus is not thread-safe with the audio thread.  For example, a mono bus might be deleted and a new stereo bus created, but meanwhile the audio thread is using the mono bus.

Two solutions I can see:

1) Waste even more memory and allocate storage for up to six channels.  This doesn't seem like a great solution

2) Make it thread-safe using a similar approach to what we now do for connections (see pre and post-render tasks in AudioContext)
Comment 4 Raymond 2012-01-17 17:21:14 PST
(In reply to comment #3)
> Raymond, thanks for the patch.
> 
> As it stands right now the re-allocation of AudioBus is not thread-safe with the audio thread.  For example, a mono bus might be deleted and a new stereo bus created, but meanwhile the audio thread is using the mono bus.
> 
> Two solutions I can see:
> 
> 1) Waste even more memory and allocate storage for up to six channels.  This doesn't seem like a great solution
> 
> 2) Make it thread-safe using a similar approach to what we now do for connections (see pre and post-render tasks in AudioContext)

Hi Roger

thanks for the review. I did pay attention to the thread-safe issue, while maybe I miss some code path? But as far as I can see at present:

AudioNodeOutput::updateInternalBus is called by AudioNodeOutput::updateNumberOfChannels which is called by two function : AudioNodeOutput::updateRenderingState and AudioNodeOutput::setNumberOfChannels.

In both case, AudioNodeOutput::updateNumberOfChannels will only be invoked  at the very start or end of a rendering quantum, which is the same case as the connections handle dirty input/output node.

so is there any other case I miss here?
Comment 5 Chris Rogers 2012-01-17 17:33:10 PST
(In reply to comment #4)
> (In reply to comment #3)
> > Raymond, thanks for the patch.
> > 
> > As it stands right now the re-allocation of AudioBus is not thread-safe with the audio thread.  For example, a mono bus might be deleted and a new stereo bus created, but meanwhile the audio thread is using the mono bus.
> > 
> > Two solutions I can see:
> > 
> > 1) Waste even more memory and allocate storage for up to six channels.  This doesn't seem like a great solution
> > 
> > 2) Make it thread-safe using a similar approach to what we now do for connections (see pre and post-render tasks in AudioContext)
> 
> Hi Roger
> 
> thanks for the review. I did pay attention to the thread-safe issue, while maybe I miss some code path? But as far as I can see at present:
> 
> AudioNodeOutput::updateInternalBus is called by AudioNodeOutput::updateNumberOfChannels which is called by two function : AudioNodeOutput::updateRenderingState and AudioNodeOutput::setNumberOfChannels.
> 
> In both case, AudioNodeOutput::updateNumberOfChannels will only be invoked  at the very start or end of a rendering quantum, which is the same case as the connections handle dirty input/output node.
> 
> so is there any other case I miss here?

Sorry, you're right.  I even see we have an ASSERT in updateNumberOfChannels()...

This looks fine to me with one minor comment fix (but I'm not a reviewer).

by the way, no worries, but my name is "Chris" :)
Comment 6 Chris Rogers 2012-01-17 17:34:22 PST
Comment on attachment 122705 [details]
Patch

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

> Source/WebCore/webaudio/AudioNodeOutput.h:113
> +    // m_internalBus must only be changed in the audio thread (or constructor).

It would be good to be exact here about where in the audio thread it's safe:

"in the audio thread" -> "in the audio thread with the context's graph lock"
Comment 7 Raymond 2012-01-17 17:41:53 PST
(In reply to comment #6)
> (From update of attachment 122705 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=122705&action=review
> 
> > Source/WebCore/webaudio/AudioNodeOutput.h:113
> > +    // m_internalBus must only be changed in the audio thread (or constructor).
> 
> It would be good to be exact here about where in the audio thread it's safe:
> 
> "in the audio thread" -> "in the audio thread with the context's graph lock"

Oh, right, exactly. I will update the patch.
Thanks Chris ;)
Comment 8 Raymond 2012-01-17 17:50:52 PST
Created attachment 122852 [details]
Patch
Comment 9 Chris Rogers 2012-01-18 14:26:35 PST
Thanks - looks fine to me!
Comment 10 Kenneth Russell 2012-01-18 17:55:10 PST
Comment on attachment 122852 [details]
Patch

rs=me based on crogers' review.
Comment 11 WebKit Review Bot 2012-01-18 19:23:11 PST
Comment on attachment 122852 [details]
Patch

Clearing flags on attachment: 122852

Committed r105373: <http://trac.webkit.org/changeset/105373>
Comment 12 WebKit Review Bot 2012-01-18 19:23:15 PST
All reviewed patches have been landed.  Closing bug.
Comment 13 Roland Steiner 2012-01-18 23:55:39 PST
In line 50: ASSERT(numberOfChannels >= 0 && numberOfChannels <= MaxNumberOfChannels);

numberOfChannels is 'unsigned', so it's always >= 0 (this causes compile issues when warnings are treated as errors)
Comment 14 Raymond 2012-01-19 00:00:18 PST
(In reply to comment #13)
> In line 50: ASSERT(numberOfChannels >= 0 && numberOfChannels <= MaxNumberOfChannels);
> 
> numberOfChannels is 'unsigned', so it's always >= 0 (this causes compile issues when warnings are treated as errors)

oops... will try to fix this issue together with other patch later.
Comment 15 Roland Steiner 2012-01-19 00:31:58 PST
I took the liberty and removed that part of the clause in r105390.
Comment 16 Raymond 2012-01-19 16:42:17 PST
(In reply to comment #15)
> I took the liberty and removed that part of the clause in r105390.

Thanks a lot!