Title: [259632] trunk/Source/WebCore
Revision
259632
Author
[email protected]
Date
2020-04-07 04:14:01 -0700 (Tue, 07 Apr 2020)

Log 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:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (259631 => 259632)


--- trunk/Source/WebCore/ChangeLog	2020-04-07 09:47:36 UTC (rev 259631)
+++ trunk/Source/WebCore/ChangeLog	2020-04-07 11:14:01 UTC (rev 259632)
@@ -1,3 +1,20 @@
+2020-04-07  Youenn Fablet  <[email protected]>
+
+        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:
+
 2020-04-07  Claudio Saavedra  <[email protected]>
 
         [GTK] gtk_icon_info_free is deprecated since GTK+ 3.8

Modified: trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp (259631 => 259632)


--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp	2020-04-07 09:47:36 UTC (rev 259631)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.cpp	2020-04-07 11:14:01 UTC (rev 259632)
@@ -34,8 +34,6 @@
 #include "CAAudioStreamDescription.h"
 #include "LibWebRTCAudioFormat.h"
 #include "Logging.h"
-#include "WebAudioBufferList.h"
-#include "WebAudioSourceProviderAVFObjC.h"
 #include <pal/avfoundation/MediaTimeAVFoundation.h>
 
 #include <pal/cf/CoreMediaSoftLink.h>
@@ -69,28 +67,35 @@
 
 void RealtimeIncomingAudioSourceCocoa::OnData(const void* audioData, int bitsPerSample, int sampleRate, size_t numberOfChannels, size_t numberOfFrames)
 {
+#if !RELEASE_LOG_DISABLED
+    if (!(++m_chunksReceived % 200)) {
+        callOnMainThread([identifier = LOGIDENTIFIER, this, protectedThis = makeRef(*this), chunksReceived = m_chunksReceived] {
+            ALWAYS_LOG_IF(loggerPtr(), identifier, "chunk ", chunksReceived);
+        });
+    }
+#endif
+
     CMTime startTime = CMTimeMake(m_numberOfFrames, sampleRate);
     auto mediaTime = PAL::toMediaTime(startTime);
     m_numberOfFrames += numberOfFrames;
 
-    AudioStreamBasicDescription newDescription = streamDescription(sampleRate, numberOfChannels);
+    if (!m_audioBufferList || m_sampleRate != sampleRate || m_numberOfChannels != numberOfChannels) {
+        callOnMainThread([identifier = LOGIDENTIFIER, this, protectedThis = makeRef(*this), sampleRate, numberOfChannels] {
+            ALWAYS_LOG_IF(loggerPtr(), identifier, "new audio buffer list for sampleRate ", sampleRate, " and ", numberOfChannels, " channel(s)");
+        });
 
-    // FIXME: We should not need to do the extra memory allocation and copy.
-    // Instead, we should be able to directly pass audioData pointer.
-    WebAudioBufferList audioBufferList { CAAudioStreamDescription(newDescription), WTF::safeCast<uint32_t>(numberOfFrames) };
-    audioBufferList.buffer(0)->mDataByteSize = numberOfChannels * numberOfFrames * bitsPerSample / 8;
-    audioBufferList.buffer(0)->mNumberChannels = numberOfChannels;
+        m_sampleRate = sampleRate;
+        m_numberOfChannels = numberOfChannels;
+        m_streamDescription = streamDescription(sampleRate, numberOfChannels);
+        m_audioBufferList = makeUnique<WebAudioBufferList>(m_streamDescription);
+    }
 
-    if (muted())
-        memset(audioBufferList.buffer(0)->mData, 0, audioBufferList.buffer(0)->mDataByteSize);
-    else
-        memcpy(audioBufferList.buffer(0)->mData, audioData, audioBufferList.buffer(0)->mDataByteSize);
+    auto& bufferList = *m_audioBufferList->buffer(0);
+    bufferList.mDataByteSize = numberOfChannels * numberOfFrames * bitsPerSample / 8;
+    bufferList.mNumberChannels = numberOfChannels;
+    bufferList.mData = const_cast<void*>(audioData);
 
-#if !RELEASE_LOG_DISABLED
-    ALWAYS_LOG_IF(loggerPtr() && !(++m_chunksReceived % 200), LOGIDENTIFIER, "chunk ", m_chunksReceived);
-#endif
-
-    audioSamplesAvailable(mediaTime, audioBufferList, CAAudioStreamDescription(newDescription), numberOfFrames);
+    audioSamplesAvailable(mediaTime, *m_audioBufferList, m_streamDescription, numberOfFrames);
 }
 
 }

Modified: trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.h (259631 => 259632)


--- trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.h	2020-04-07 09:47:36 UTC (rev 259631)
+++ trunk/Source/WebCore/platform/mediastream/mac/RealtimeIncomingAudioSourceCocoa.h	2020-04-07 11:14:01 UTC (rev 259632)
@@ -30,7 +30,7 @@
 #if USE(LIBWEBRTC)
 
 #include "RealtimeIncomingAudioSource.h"
-
+#include "WebAudioBufferList.h"
 #include <CoreAudio/CoreAudioTypes.h>
 
 typedef const struct opaqueCMFormatDescription *CMFormatDescriptionRef;
@@ -51,6 +51,10 @@
 
     uint64_t m_numberOfFrames { 0 };
 
+    int m_sampleRate { 0 };
+    size_t m_numberOfChannels { 0 };
+    CAAudioStreamDescription m_streamDescription;
+    std::unique_ptr<WebAudioBufferList> m_audioBufferList;
 #if !RELEASE_LOG_DISABLED
     size_t m_chunksReceived { 0 };
 #endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to