@@ -23,33 +23,30 @@ @interface TIoTAVCaptionFLV ()<AVCaptureVideoDataOutputSampleBufferDelegate,AVCa
2323@property (nonatomic , strong ) AVCaptureSession *session;
2424// 队列
2525@property (nonatomic , strong ) dispatch_queue_t videoQueue;
26- @property (nonatomic , strong ) dispatch_queue_t AudioQueue;
2726
2827// 负责从 AVCaptureDevice 获得输入数据
2928@property (nonatomic , strong ) AVCaptureDeviceInput *deviceInput;
3029@property (nonatomic , strong ) AVCaptureVideoDataOutput *videoOutput;
3130@property (nonatomic , strong ) AVCaptureConnection *videoConnection;
32- @property (nonatomic , strong ) AVCaptureConnection *audioConnection;
3331// 拍摄预览图层
3432@property (nonatomic , strong ) AVCaptureVideoPreviewLayer *previewLayer;
3533@property (nonatomic , strong ) TIoTH264Encoder *h264Encoder;
3634@property (nonatomic , strong ) TIoTAACEncoder *aacEncoder;
37- // @property (nonatomic, strong) NSMutableData *data;
38- // @property (nonatomic, copy ) NSString *h264File;
39- // @property (nonatomic, strong) NSFileHandle *fileHandle;
4035@property (nonatomic , assign ) TIoTAVCaptionFLVAudioType audioRate;
36+ @property (nonatomic , assign ) int channel;
4137@property (nonatomic , assign ) int captureVideoFPS;
4238@property (nonatomic , strong ) AVCaptureSessionPreset resolutionRatioValue;
4339@property (nonatomic , strong ) TIoTPCMXEchoRecord *pcmRecord;
4440@end
4541
4642@implementation TIoTAVCaptionFLV
4743
48- -(instancetype ) initWithAudioConfig : (TIoTAVCaptionFLVAudioType)audioSampleRate {
44+ -(instancetype ) initWithAudioConfig : (TIoTAVCaptionFLVAudioType)audioSampleRate channel : ( int ) channel {
4945 self = [super init ];
5046 if (self) {
5147 tAVCaptionFLV = self;
5248 _audioRate = audioSampleRate;
49+ _channel = channel;
5350 _isEchoCancel = NO ;
5451 _devicePosition = AVCaptureDevicePositionBack;
5552 [self onInit ];
@@ -68,7 +65,6 @@ -(void) onInit{
6865
6966 muxerQueue = dispatch_queue_create (" FLV_Muxer_Queue" , DISPATCH_QUEUE_SERIAL);
7067
71- // _data = [NSMutableData new];
7268 _session = [AVCaptureSession new ];
7369
7470 self.resolutionRatioValue = AVCaptureSessionPreset352x288;
@@ -82,52 +78,17 @@ - (void)setupAudioCapture {
8278 return ;
8379 }
8480 AudioStreamBasicDescription inAudioStreamBasicDescription;
85- if (_isEchoCancel) {
8681
87- self.pcmRecord = [[TIoTPCMXEchoRecord alloc ] init ];
88- [self .pcmRecord set_record_callback: record_callback user: (__bridge void * _Nonnull)(self )];
89- // [self.record start_record];
90-
91- inAudioStreamBasicDescription = self.pcmRecord .pcmStreamDescription ;
92- self.aacEncoder = [[TIoTAACEncoder alloc ] initWithAudioDescription: inAudioStreamBasicDescription];
93- self.aacEncoder .delegate = self;
94- self.aacEncoder .audioType = _audioRate;
95- return ;
96- }
82+ self.pcmRecord = [[TIoTPCMXEchoRecord alloc ] initWithChannel: _channel isEcho: _isEchoCancel];
83+ [self .pcmRecord set_record_callback: record_callback user: (__bridge void * _Nonnull)(self )];
84+ // [self.record start_record];
85+
86+ inAudioStreamBasicDescription = self.pcmRecord .pcmStreamDescription ;
9787 self.aacEncoder = [[TIoTAACEncoder alloc ] initWithAudioDescription: inAudioStreamBasicDescription];
9888 self.aacEncoder .delegate = self;
9989 self.aacEncoder .audioType = _audioRate;
100-
101- AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio];
102-
103- NSError *error = nil ;
104-
105- AVCaptureDeviceInput *audioInput = [[AVCaptureDeviceInput alloc ]initWithDevice:audioDevice error: &error];
106-
107- if (error) {
108-
109- NSLog (@" Error getting audio input device:%@ " ,error.description);
110- }
111-
112- if ([self .session canAddInput: audioInput]) {
113-
114- [self .session addInput: audioInput];
115- }
116-
117- self.AudioQueue = dispatch_queue_create (" Audio Capture Queue" , DISPATCH_QUEUE_SERIAL);
118-
119- AVCaptureAudioDataOutput *audioOutput = [AVCaptureAudioDataOutput new ];
120- [audioOutput setSampleBufferDelegate: self queue: self .AudioQueue];
121-
122- if ([self .session canAddOutput: audioOutput]) {
123-
124- [self .session addOutput: audioOutput];
125- }
126-
127- self.audioConnection = [audioOutput connectionWithMediaType: AVMediaTypeAudio];
128-
129-
13090}
91+
13192- (AVCaptureDevice *)cameraWithPosition : (AVCaptureDevicePosition)position
13293{
13394 NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
@@ -349,17 +310,10 @@ - (void)calculatorCaptureFPS {
349310
350311#pragma mark - 实现 AVCaptureOutputDelegate:
351312- (void )captureOutput : (AVCaptureOutput *)captureOutput didOutputSampleBuffer : (CMSampleBufferRef)sampleBuffer fromConnection : (AVCaptureConnection *)connection {
352- if (connection == _videoConnection) { // Video
353-
354- if (self.videoLocalView ) { // 开关打开,才推送视频
355- [self .h264Encoder encode: sampleBuffer];
356-
357- [self calculatorCaptureFPS ];
358- }
359-
360- } else if (connection == _audioConnection) { // Audio
313+ if (self.videoLocalView ) { // 开关打开,才推送视频
314+ [self .h264Encoder encode: sampleBuffer];
361315
362- [self .aacEncoder encodeSampleBuffer: sampleBuffer ];
316+ [self calculatorCaptureFPS ];
363317 }
364318}
365319
@@ -415,6 +369,7 @@ - (void)gotEncodedData:(NSData*)data isKeyFrame:(BOOL)isKeyFrame {
415369
416370#pragma mark - TIoTAACEncoderDelegate
417371- (void )getEncoderAACData : (NSData *)data {
372+ // [_fileHandle writeData:data];
418373 encodeFlvData (0 , data);
419374}
420375
@@ -500,26 +455,22 @@ -(BOOL) startCapture {
500455// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
501456// NSString *documentsDirectory = [paths firstObject];
502457
503- // self. h264File = [documentsDirectory stringByAppendingPathComponent:@"lyh.h264 "];
504- // [fileManager removeItemAtPath:self. h264File error:nil];
505- // [fileManager createFileAtPath:self. h264File contents:nil attributes:nil];
506- // _fileHandle = [NSFileHandle fileHandleForWritingAtPath:self. h264File];
458+ // NSString * h264File = [documentsDirectory stringByAppendingPathComponent:@"lyh.aac "];
459+ // [fileManager removeItemAtPath:h264File error:nil];
460+ // [fileManager createFileAtPath:h264File contents:nil attributes:nil];
461+ // _fileHandle = [NSFileHandle fileHandleForWritingAtPath:h264File];
507462
508463 flv_init_load ();
509464
510465 [self startCamera ];
511466
512- if (_isEchoCancel) {
513- [self .pcmRecord start_record ];
514- }
467+ [self .pcmRecord start_record ];
515468 return YES ;
516469}
517470
518471-(void ) stopCapture {
519472 [self stopCarmera ];
520- if (_isEchoCancel) {
521- [self .pcmRecord stop_record ];
522- }
473+ [self .pcmRecord stop_record ];
523474}
524475
525476- (void ) startCamera
@@ -551,15 +502,6 @@ - (void) stopCarmera
551502// [_fileHandle closeFile];
552503// _fileHandle = NULL;
553504//
554- // 获取程序Documents目录路径
555- /* NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
556- NSString *documentsDirectory = [paths objectAtIndex:0];
557-
558- NSMutableString * path = [[NSMutableString alloc]initWithString:documentsDirectory];
559- [path appendString:@"/AACFile.aac"];
560-
561- [_data writeToFile:path atomically:YES];
562- */
563505}
564506
565507- (void )setCameraFPS : (int )fps {
0 commit comments