Revert "audio playback improvement"

This reverts commit 46834a8a8b.
This commit is contained in:
True Zhuanjia committed 2026-04-14 08:43:13 +08:00
1 parent fcfe83572f
commit 0195aeb765
1 file changed
+4 -31
+4 -31
View File
@@ -15,7 +15,6 @@
#import "DataManager.h" #import "DataManager.h"
#import <VideoToolbox/VideoToolbox.h> #import <VideoToolbox/VideoToolbox.h>
#include <stdatomic.h>
#define SDL_MAIN_HANDLED #define SDL_MAIN_HANDLED
#import <SDL.h> #import <SDL.h>
@@ -57,7 +56,6 @@ static AVAudioEngine *audioEngine;
static AVAudioPlayerNode *audioPlayerNode; static AVAudioPlayerNode *audioPlayerNode;
static AVAudioPCMBuffer *pcmBuffer; static AVAudioPCMBuffer *pcmBuffer;
static AVAudioFormat *audioFormat; static AVAudioFormat *audioFormat;
static atomic_long systemAudioQueuedFrames;
static bool muteInBackground; static bool muteInBackground;
static bool fullColorRange; static bool fullColorRange;
@@ -66,20 +64,6 @@ static VideoDecoderRenderer* renderer;
static BandwidthTracker *bwTracker; static BandwidthTracker *bwTracker;
static void SystemAudioConsumeQueuedFrames(long frameCount)
{
long currentFrames;
long newFrames;
do {
currentFrames = atomic_load(&systemAudioQueuedFrames);
newFrames = currentFrames - frameCount;
if (newFrames < 0) {
newFrames = 0;
}
} while (!atomic_compare_exchange_weak(&systemAudioQueuedFrames, &currentFrames, newFrames));
}
int DrDecoderSetup(int videoFormat, int width, int height, int redrawRate, void* context, int drFlags) int DrDecoderSetup(int videoFormat, int width, int height, int redrawRate, void* context, int drFlags)
{ {
[renderer setupWithVideoFormat:videoFormat width:width height:height frameRate:redrawRate fullRange:fullColorRange]; [renderer setupWithVideoFormat:videoFormat width:width height:height frameRate:redrawRate fullRange:fullColorRange];
@@ -364,7 +348,6 @@ void ArCleanup(void)
} }
void AudioEngineInit(int sampleRate, int channelCount) { void AudioEngineInit(int sampleRate, int channelCount) {
atomic_store(&systemAudioQueuedFrames, 0);
audioEngine = [[AVAudioEngine alloc] init]; audioEngine = [[AVAudioEngine alloc] init];
audioPlayerNode = [[AVAudioPlayerNode alloc] init]; audioPlayerNode = [[AVAudioPlayerNode alloc] init];
@@ -443,28 +426,20 @@ void ArDecodeAndPlaySample(char* sampleData, int sampleLength)
float* fbuf = (float*)audioBuffer; float* fbuf = (float*)audioBuffer;
if(useSystemAudioEngine){ if(useSystemAudioEngine){
long maxQueuedFrames = (long)audioConfig.samplesPerFrame * 10; // 创建 AVAudioPCMBuffer
while (atomic_load(&systemAudioQueuedFrames) > maxQueuedFrames) {
[NSThread sleepForTimeInterval:0.001f];
}
AVAudioFrameCount frameCount = decodeLen; AVAudioFrameCount frameCount = decodeLen;
AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioFormat frameCapacity:frameCount]; AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioFormat frameCapacity:frameCount];
buffer.frameLength = frameCount; buffer.frameLength = frameCount;
// 拷贝数据到 buffer
for (int ch = 0; ch < audioConfig.channelCount; ch++) { for (int ch = 0; ch < audioConfig.channelCount; ch++) {
float *dst = buffer.floatChannelData[ch]; float *dst = buffer.floatChannelData[ch];
for (int i = 0; i < decodeLen; i++) { for (int i = 0; i < decodeLen; i++) {
dst[i] = fbuf[i * audioConfig.channelCount + ch] * volume; // 非交错数据 dst[i] = fbuf[i * audioConfig.channelCount + ch] * volume; // 非交错数据
} }
} }
// 播放
if(!audioSessionInterrupted) { if(!audioSessionInterrupted) [audioPlayerNode scheduleBuffer:buffer completionHandler:nil];
atomic_fetch_add(&systemAudioQueuedFrames, frameCount);
[audioPlayerNode scheduleBuffer:buffer completionHandler:^{
SystemAudioConsumeQueuedFrames(frameCount);
}];
}
} }
else{ else{
@@ -560,7 +535,6 @@ void ClSetControllerLED(uint16_t controllerNumber, uint8_t r, uint8_t g, uint8_t
// won't be able to acquire it if LiStartConnection is in // won't be able to acquire it if LiStartConnection is in
// progress. // progress.
LiInterruptConnection(); LiInterruptConnection();
atomic_store(&systemAudioQueuedFrames, 0);
[audioPlayerNode stop]; [audioPlayerNode stop];
[audioEngine stop]; [audioEngine stop];
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
@@ -583,7 +557,6 @@ void ClSetControllerLED(uint16_t controllerNumber, uint8_t r, uint8_t g, uint8_t
switch (type) { switch (type) {
case AVAudioSessionInterruptionTypeBegan: case AVAudioSessionInterruptionTypeBegan:
audioSessionInterrupted = true; audioSessionInterrupted = true;
atomic_store(&systemAudioQueuedFrames, 0);
[audioPlayerNode stop]; [audioPlayerNode stop];
[audioEngine stop]; [audioEngine stop];
break; break;