From fa0b64bdcea3000292fed79dc4d18e3e24dbb690 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= jaakko.keranen@iki.fi
Date: Mon, 4 Dec 2023 11:05:39 +0200
Subject: [PATCH 1/1] Player: Avoid copying memory when detecting audio format
BubbleID #64
src/audio/player.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/src/audio/player.c b/src/audio/player.c
index 67ca9521..01e54191 100644
--- a/src/audio/player.c
+++ b/src/audio/player.c
@@ -475,12 +475,11 @@ static iRangecc mediaType_(const iString *str) {
return part;
}
-static iContentSpec contentSpec_Player_(const iPlayer *d) {
+static iContentSpec detectContentSpec_Player_(const iPlayer *d) {
iContentSpec content;
iZap(content);
const size_t dataSize = size_InputBuf(d->data);
const iRangecc mediaType = mediaType_(&d->mime);
if (equal_Rangecc(mediaType, "audio/wave") || equal_Rangecc(mediaType, "audio/wav") ||
equal_Rangecc(mediaType, "audio/x-wav") || equal_Rangecc(mediaType, "audio/x-pn-wav")) {
@@ -499,6 +498,12 @@ static iContentSpec contentSpec_Player_(const iPlayer *d) {
/* TODO: Could try decoders to see if one works? */
content.type = none_DecoderType;
}
buf = iClob(new_Buffer());
/* This holds a reference to the data block. The decoder runs in a background
thread so it needs its own copy of the data. */
open_Buffer(buf, &d->data->data);
if (content.type == wav_DecoderType && dataSize >= 44) {
/* Read the RIFF/WAVE header. */
iStream *is = stream_Buffer(buf);
@@ -713,6 +718,8 @@ void updateSourceData_Player(iPlayer *d, const iString *mimeType, const iBlock *
break;
}
/* The old parts cannot have changed. */
/* NOTE: The decoder will hold a reference to the data block, which means
appending here will cause a copy-on-write detach to occur. */
appendData_Block(&input->data, constBegin_Block(data) + oldSize, newSize - oldSize);
break;
}
@@ -769,7 +776,7 @@ iBool start_Player(iPlayer *d) {
return iTrue;
}
#endif
if (!content.output.freq) {
return iFalse;
}
--
2.25.1
text/plain
This content has been proxied by September (ba2dc).