Changeset 259632 in webkit


Ignore:
Timestamp:
Apr 7, 2020 4:14:01 AM (4 years ago)
Author:
youenn@apple.com
Message:

Remove unnecessary memory allocation from RealtimeIncomingAudioSourceCocoa::OnData
https://bugs.webkit.org/show_bug.cgi?id=209969

Reviewed by Eric Carlson.

Instead of allocating a new buffer for every audio chunk and copy the audio chunk,
Create a WebAudioBufferList once (without any buffer allocation) and set the audio buffer pointer
given by libwebrtc as the WebAudioBufferList buffer pointer.
We do not take care of muted state anymore since this is done by consumers anyway.
Covered by existing tests.

  • platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp:

(WebCore::RealtimeIncomingAudioSourceCocoa::OnData):

  • platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.h:
Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r259631 r259632  
     12020-04-07  Youenn Fablet  <youenn@apple.com>
     2
     3        Remove unnecessary memory allocation from RealtimeIncomingAudioSourceCocoa::OnData
     4        https://bugs.webkit.org/show_bug.cgi?id=209969
     5
     6        Reviewed by Eric Carlson.
     7
     8        Instead of allocating a new buffer for every audio chunk and copy the audio chunk,
     9        Create a WebAudioBufferList once (without any buffer allocation) and set the audio buffer pointer
     10        given by libwebrtc as the WebAudioBufferList buffer pointer.
     11        We do not take care of muted state anymore since this is done by consumers anyway.
     12        Covered by existing tests.
     13
     14        * platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp:
     15        (WebCore::RealtimeIncomingAudioSourceCocoa::OnData):
     16        * platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.h:
     17
    1182020-04-07  Claudio Saavedra  <csaavedra@igalia.com>
    219
  • trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp

    r248288 r259632  
    3535#include "LibWebRTCAudioFormat.h"
    3636#include "Logging.h"
    37 #include "WebAudioBufferList.h"
    38 #include "WebAudioSourceProviderAVFObjC.h"
    3937#include <pal/avfoundation/MediaTimeAVFoundation.h>
    4038
     
    7068void RealtimeIncomingAudioSourceCocoa::OnData(const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames)
    7169{
     70#if !RELEASE_LOG_DISABLED
     71    if (!(++m_chunksReceived % 200)) {
     72        callOnMainThread([identifier = LOGIDENTIFIER, this, protectedThis = makeRef(*this), chunksReceived = m_chunksReceived] {
     73            ALWAYS_LOG_IF(loggerPtr(), identifier, "chunk ", chunksReceived);
     74        });
     75    }
     76#endif
     77
    7278    CMTime startTime = CMTimeMake(m_numberOfFrames, sampleRate);
    7379    auto mediaTime = PAL::toMediaTime(startTime);
    7480    m_numberOfFrames += numberOfFrames;
    7581
    76     AudioStreamBasicDescription newDescription = streamDescription(sampleRate, numberOfChannels);
     82    if (!m_audioBufferList || m_sampleRate != sampleRate || m_numberOfChannels != numberOfChannels) {
     83        callOnMainThread([identifier = LOGIDENTIFIER, this, protectedThis = makeRef(*this), sampleRate, numberOfChannels] {
     84            ALWAYS_LOG_IF(loggerPtr(), identifier, "new audio buffer list for sampleRate ", sampleRate, " and ", numberOfChannels, " channel(s)");
     85        });
    7786
    78     // FIXME: We should not need to do the extra memory allocation and copy.
    79     // Instead, we should be able to directly pass audioData pointer.
    80     WebAudioBufferList audioBufferList { CAAudioStreamDescription(newDescription), WTF::safeCast<uint32_t>(numberOfFrames) };
    81     audioBufferList.buffer(0)->mDataByteSize = numberOfChannels * numberOfFrames * bitsPerSample / 8;
    82     audioBufferList.buffer(0)->mNumberChannels = numberOfChannels;
     87        m_sampleRate = sampleRate;
     88        m_numberOfChannels = numberOfChannels;
     89        m_streamDescription = streamDescription(sampleRate, numberOfChannels);
     90        m_audioBufferList = makeUnique<WebAudioBufferList>(m_streamDescription);
     91    }
    8392
    84     if (muted())
    85         memset(audioBufferList.buffer(0)->mData, 0, audioBufferList.buffer(0)->mDataByteSize);
    86     else
    87         memcpy(audioBufferList.buffer(0)->mData, audioData, audioBufferList.buffer(0)->mDataByteSize);
     93    auto& bufferList = *m_audioBufferList->buffer(0);
     94    bufferList.mDataByteSize = numberOfChannels * numberOfFrames * bitsPerSample / 8;
     95    bufferList.mNumberChannels = numberOfChannels;
     96    bufferList.mData = const_cast<void*>(audioData);
    8897
    89 #if !RELEASE_LOG_DISABLED
    90     ALWAYS_LOG_IF(loggerPtr() && !(++m_chunksReceived % 200), LOGIDENTIFIER, "chunk ", m_chunksReceived);
    91 #endif
    92 
    93     audioSamplesAvailable(mediaTime, audioBufferList, CAAudioStreamDescription(newDescription), numberOfFrames);
     98    audioSamplesAvailable(mediaTime, *m_audioBufferList, m_streamDescription, numberOfFrames);
    9499}
    95100
  • trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.h

    r240120 r259632  
    3131
    3232#include "RealtimeIncomingAudioSource.h"
    33 
     33#include "WebAudioBufferList.h"
    3434#include <CoreAudio/CoreAudioTypes.h>
    3535
     
    5252    uint64_t m_numberOfFrames { 0 };
    5353
     54    int m_sampleRate { 0 };
     55    size_t m_numberOfChannels { 0 };
     56    CAAudioStreamDescription m_streamDescription;
     57    std::unique_ptr<WebAudioBufferList> m_audioBufferList;
    5458#if !RELEASE_LOG_DISABLED
    5559    size_t m_chunksReceived { 0 };
Note: See TracChangeset for help on using the changeset viewer.