@@ -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
352356void *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
362368static uint8_t trae_pcm_buffer[640 ];
369+ static uint8_t trae_aac_buffer[8192 ];
363370static 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
0 commit comments