Skip to content

Commit 3821c05

Browse files
committed
添加线程处理变声
Change-Id: Ie76b14156612ed0c3c3c5017d4318a15b021983a
1 parent b1215d5 commit 3821c05

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

Source/SDK/LinkVideo/FLV/TIoTAVCaptionFLV.mm

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ @interface TIoTAVCaptionFLV ()<AVCaptureVideoDataOutputSampleBufferDelegate,AVCa
3939
@property (nonatomic, assign) int captureVideoFPS;
4040
@property (nonatomic, strong) AVCaptureSessionPreset resolutionRatioValue;
4141
@property (nonatomic, strong) TIoTPCMXEchoRecord *pcmRecord;
42+
43+
@property (nonatomic, strong) dispatch_queue_t audioEncodeQueue;
4244
@end
4345

4446
@implementation TIoTAVCaptionFLV
@@ -52,6 +54,8 @@ -(instancetype) initWithAudioConfig:(TIoTAVCaptionFLVAudioType)audioSampleRate c
5254
_isEchoCancel = NO;
5355
_pitch = 0;
5456
_devicePosition = AVCaptureDevicePositionBack;
57+
58+
_audioEncodeQueue = dispatch_queue_create("com.audio.aacencode", DISPATCH_QUEUE_SERIAL);
5559
[self onInit];
5660
}
5761
return self;
@@ -350,6 +354,8 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CM
350354

351355
#pragma mark - PCM XEcho record_callback
352356
void *ijk_soundtouch_handle = NULL;
357+
TPCircularBuffer aac_circularBuffer;
358+
353359
//trae_voice_changer_t *trae_voice_handle = NULL;
354360
- (void)setPitch:(int)pitch {
355361
_pitch = ((pitch >= 0) && (pitch <= 3 ))?pitch:2; //0 NONE 2萝莉,3 大叔
@@ -360,27 +366,36 @@ - (void)setPitch:(int)pitch {
360366
}
361367

362368
static uint8_t trae_pcm_buffer[640];
369+
static uint8_t trae_aac_buffer[8192];
363370
static void record_callback(uint8_t *buffer, int size, void *u)
364371
{
365372
// NSData *oridata = [NSData dataWithBytes:buffer length:size];
366373
// [_originfileHandle writeData:oridata];
367374

368375
TIoTAVCaptionFLV *vc = (__bridge TIoTAVCaptionFLV *)(u);
369376
memset(trae_pcm_buffer, 0, 640);
370-
UInt32 len = [vc.pcmRecord getData:trae_pcm_buffer :640];
377+
UInt32 len = [vc.pcmRecord getData:&pcm_circularBuffer :trae_pcm_buffer :640];
371378
if (len < 640) {
372379
return;
373380
}
374381
if (vc.pitch != 0) {
375-
static int tmpChannel = vc.pcmRecord.pcmStreamDescription.mChannelsPerFrame;
382+
376383
// int put_n_sample = (size/2) / tmpChannel;
377384
// [TRAESoundTouch voice_handle_process:(short *)buffer output:(short *)trae_pcm_buffer frames:320];
378385
[TRAESoundTouch voice_handle_process:(short *)trae_pcm_buffer output:(short *)trae_pcm_buffer frames:320];
379386
}
380387

381-
NSData *data = [NSData dataWithBytes:trae_pcm_buffer length:640];
382-
// [_fileHandle writeData:data];
383-
[vc.aacEncoder encodePCMData:data];
388+
[vc.pcmRecord addData:&aac_circularBuffer :trae_pcm_buffer :640];
389+
dispatch_async(vc.audioEncodeQueue, ^{
390+
static int tmpChannelDataLen = vc.pcmRecord.pcmStreamDescription.mChannelsPerFrame * 2048;
391+
UInt32 aaclen = [vc.pcmRecord getData:&aac_circularBuffer :trae_aac_buffer :tmpChannelDataLen];
392+
if (aaclen < tmpChannelDataLen) {
393+
return;
394+
}
395+
NSData *data = [NSData dataWithBytes:trae_aac_buffer length:tmpChannelDataLen];
396+
// [_fileHandle writeData:data];
397+
[vc.aacEncoder encodePCMData:data];
398+
});
384399
}
385400

386401

@@ -522,7 +537,8 @@ -(BOOL) startCapture {
522537
// [fileManager removeItemAtPath:originFile error:nil];
523538
// [fileManager createFileAtPath:originFile contents:nil attributes:nil];
524539
// _originfileHandle = [NSFileHandle fileHandleForWritingAtPath:originFile];
525-
540+
[self.pcmRecord Init_buffer:&aac_circularBuffer :8192];
541+
526542
flv_init_load();
527543

528544
[self startCamera];
@@ -534,6 +550,8 @@ -(BOOL) startCapture {
534550
-(void) stopCapture{
535551
[self stopCarmera];
536552
[self.pcmRecord stop_record];
553+
554+
[self.pcmRecord Destory_buffer:&aac_circularBuffer];
537555
}
538556

539557
- (void) startCamera

Source/SDK/LinkVideo/FLV/TIoTPCMXEchoRecord.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#import <AudioUnit/AudioUnit.h>
44
#import <TPCircularBuffer/TPCircularBuffer.h>
55

6+
extern TPCircularBuffer pcm_circularBuffer;
67
typedef void(*RecordCallback)(uint8_t *buffer, int size, void *u);
78

89
NS_ASSUME_NONNULL_BEGIN
@@ -15,8 +16,10 @@ NS_ASSUME_NONNULL_BEGIN
1516
- (void)start_record;
1617
- (void)stop_record;
1718

18-
19-
-(UInt32)getData:(void *)buf_ :(UInt32)size_;
19+
-(BOOL) Init_buffer:(TPCircularBuffer*)buffer_ :(UInt32)size_;
20+
-(void) Destory_buffer:(TPCircularBuffer*)buffer_;
21+
-(UInt32)addData:(TPCircularBuffer*)buffer_ :(void *)buf_ :(UInt32)size_;
22+
-(UInt32)getData:(TPCircularBuffer*)buffer_ :(void *)buf_ :(UInt32)size_;
2023
@end
2124

2225
NS_ASSUME_NONNULL_END

Source/SDK/LinkVideo/FLV/TIoTPCMXEchoRecord.mm

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static OSStatus record_callback(void *inRefCon, AudioUnitRenderActionFlags *ioAc
102102
UInt32 bufferSize = list.mBuffers[0].mDataByteSize;
103103
uint8_t *bufferData = (uint8_t *)list.mBuffers[0].mData;
104104
// NSLog(@"record_callback__________size : %d", bufferSize);
105-
[r addData:bufferData :bufferSize];
105+
[r addData:&pcm_circularBuffer :bufferData :bufferSize];
106106
if (r->callback)
107107
r->callback(bufferData, bufferSize, r->user);
108108
return error;
@@ -137,7 +137,7 @@ OSStatus outputRender_cb(void *inRefCon, AudioUnitRenderActionFlags *ioActionFla
137137

138138
- (void)start_record
139139
{
140-
[self Init_buffer:4096];
140+
[self Init_buffer:&pcm_circularBuffer :4096];
141141
pcm_buffer_size = 0;
142142
AudioOutputUnitStart(audioUnit);
143143
}
@@ -146,7 +146,7 @@ - (void)stop_record
146146
{
147147
AudioOutputUnitStop(audioUnit);
148148
pcm_buffer_size = 0;
149-
[self Destory_buffer];
149+
[self Destory_buffer:&pcm_circularBuffer];
150150
}
151151

152152
- (void)set_record_callback:(RecordCallback)c user:(nonnull void *)u
@@ -165,38 +165,38 @@ - (void)dealloc
165165

166166

167167

168-
-(BOOL)Init_buffer:(UInt32)size_
168+
-(BOOL) Init_buffer:(TPCircularBuffer*)buffer_ :(UInt32)size_
169169
{
170-
return TPCircularBufferInit(&pcm_circularBuffer, size_ );
170+
return TPCircularBufferInit(buffer_, size_ );
171171
}
172172

173-
-(void)Destory_buffer
173+
-(void) Destory_buffer:(TPCircularBuffer*)buffer_
174174
{
175-
TPCircularBufferCleanup(&pcm_circularBuffer );
175+
TPCircularBufferCleanup(buffer_ );
176176
}
177177

178-
-(UInt32)addData:(void *)buf_ :(UInt32)size_
178+
-(UInt32)addData:(TPCircularBuffer*)buffer_ :(void *)buf_ :(UInt32)size_
179179
{
180180
uint32_t availableBytes = 0;
181-
TPCircularBufferHead(&pcm_circularBuffer, &availableBytes);
181+
TPCircularBufferHead(buffer_, &availableBytes);
182182
if (availableBytes <= 0)
183183
return 0;
184184

185185
UInt32 len = (availableBytes >= size_ ? size_ : availableBytes);
186-
TPCircularBufferProduceBytes(&pcm_circularBuffer, (void*)buf_, size_);
186+
TPCircularBufferProduceBytes(buffer_, (void*)buf_, size_);
187187
return len;
188188
}
189189

190-
-(UInt32)getData:(void *)buf_ :(UInt32)size_
190+
-(UInt32)getData:(TPCircularBuffer*)buffer_ :(void *)buf_ :(UInt32)size_
191191
{
192192
uint32_t availableBytes = 0;
193-
void *bufferTail = TPCircularBufferTail(&pcm_circularBuffer, &availableBytes);
193+
void *bufferTail = TPCircularBufferTail(buffer_, &availableBytes);
194194
if (availableBytes >= size_)
195195
{
196196
UInt32 len = 0;
197197
len = (size_ > availableBytes ? availableBytes : size_);
198198
memcpy(buf_, bufferTail, len);
199-
TPCircularBufferConsume(&pcm_circularBuffer, len);
199+
TPCircularBufferConsume(buffer_, len);
200200
// NSLog(@"ggggggggggg=====len = %ld", len);
201201
return len;
202202
}

0 commit comments

Comments
 (0)