Skip to content

Commit

Permalink
fix audioCapture crash
Browse files Browse the repository at this point in the history
  • Loading branch information
chenliming committed Oct 12, 2016
1 parent fabae8d commit a1a7e9a
Show file tree
Hide file tree
Showing 36 changed files with 102 additions and 89 deletions.
4 changes: 2 additions & 2 deletions LFLiveKit/LFLiveKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFLiveKit.h
// LFLiveKit
//
// Created by admin on 16/5/24.
// Copyright © 2016年 admin. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#if __has_include(<LFLiveKit/LFLiveKit.h>)
Expand Down
5 changes: 3 additions & 2 deletions LFLiveKit/LFLiveSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// LFLiveSession.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
//
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/LFLiveSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFLiveSession.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFLiveSession.h"
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/capture/LFAudioCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFAudioCapture.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/capture/LFAudioCapture.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFAudioCapture.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFAudioCapture.h"
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/capture/LFVideoCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFVideoCapture.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/capture/LFVideoCapture.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFVideoCapture.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFVideoCapture.h"
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/coder/LFAudioEncoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFAudioEncoding.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/coder/LFH264VideoEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFH264VideoEncoder
// LFLiveKit
//
// Created by feng on 7/5/16.
// Copyright (c) 2014 zhanqi.tv. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#if __has_include(<LFLiveKit/LFLiveKit.h>)
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/coder/LFH264VideoEncoder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFH264VideoEncoder
// LFLiveKit
//
// Created by feng on 7/5/16.
// Copyright (c) 2014 zhanqi.tv. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <CoreMedia/CoreMedia.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/coder/LFHardwareAudioEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFHardwareAudioEncoder.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#if __has_include(<LFLiveKit/LFLiveKit.h>)
Expand Down
29 changes: 14 additions & 15 deletions LFLiveKit/coder/LFHardwareAudioEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFHardwareAudioEncoder.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFHardwareAudioEncoder.h"
Expand Down Expand Up @@ -35,7 +35,7 @@ - (instancetype)initWithAudioStreamConfiguration:(nullable LFLiveAudioConfigurat
if (!aacBuf) {
aacBuf = malloc(_configuration.bufferLength);
}


#ifdef DEBUG
enabledWriteVideoFile = NO;
Expand All @@ -48,7 +48,6 @@ - (instancetype)initWithAudioStreamConfiguration:(nullable LFLiveAudioConfigurat
- (void)dealloc {
if (aacBuf) free(aacBuf);
if (leftBuf) free(leftBuf);
if(m_converter) AudioConverterDispose(m_converter);
}

#pragma mark -- LFAudioEncoder
Expand All @@ -60,7 +59,7 @@ - (void)encodeAudioData:(nullable NSData*)audioData timeStamp:(uint64_t)timeStam
if (![self createAudioConvert]) {
return;
}

if(leftLength + audioData.length >= self.configuration.bufferLength){
///<  发送
NSInteger totalSize = leftLength + audioData.length;
Expand Down Expand Up @@ -100,7 +99,7 @@ - (void)encodeBuffer:(char*)buf timeStamp:(uint64_t)timeStamp{
AudioBufferList buffers;
buffers.mNumberBuffers = 1;
buffers.mBuffers[0] = inBuffer;


// 初始化一个输出缓冲列表
AudioBufferList outBufferList;
Expand Down Expand Up @@ -134,15 +133,15 @@ - (void)encodeBuffer:(char*)buf timeStamp:(uint64_t)timeStamp{
}

- (void)stopEncoder {

}

#pragma mark -- CustomMethod
- (BOOL)createAudioConvert { //根据输入样本初始化一个编码转换器
if (m_converter != nil) {
return TRUE;
}

AudioStreamBasicDescription inputFormat = {0};
inputFormat.mSampleRate = _configuration.audioSampleRate;
inputFormat.mFormatID = kAudioFormatLinearPCM;
Expand All @@ -152,14 +151,14 @@ - (BOOL)createAudioConvert { //根据输入样本初始化一个编码转换器
inputFormat.mBitsPerChannel = 16;
inputFormat.mBytesPerFrame = inputFormat.mBitsPerChannel / 8 * inputFormat.mChannelsPerFrame;
inputFormat.mBytesPerPacket = inputFormat.mBytesPerFrame * inputFormat.mFramesPerPacket;

AudioStreamBasicDescription outputFormat; // 这里开始是输出音频格式
memset(&outputFormat, 0, sizeof(outputFormat));
outputFormat.mSampleRate = inputFormat.mSampleRate; // 采样率保持一致
outputFormat.mFormatID = kAudioFormatMPEG4AAC; // AAC编码 kAudioFormatMPEG4AAC kAudioFormatMPEG4AAC_HE_V2
outputFormat.mChannelsPerFrame = (UInt32)_configuration.numberOfChannels;;
outputFormat.mFramesPerPacket = 1024; // AAC一帧是1024个字节

const OSType subtype = kAudioFormatMPEG4AAC;
AudioClassDescription requestedCodecs[2] = {
{
Expand All @@ -177,16 +176,16 @@ - (BOOL)createAudioConvert { //根据输入样本初始化一个编码转换器
OSStatus result = AudioConverterNewSpecific(&inputFormat, &outputFormat, 2, requestedCodecs, &m_converter);;
UInt32 outputBitrate = _configuration.audioBitrate;
UInt32 propSize = sizeof(outputBitrate);
// UInt32 outputPacketSize = 0;

// UInt32 outputPacketSize = 0;

if(result == noErr) {
result = AudioConverterSetProperty(m_converter, kAudioConverterEncodeBitRate, propSize, &outputBitrate);
}

// if(result == noErr) {
// AudioConverterGetProperty(m_converter, kAudioConverterPropertyMaximumOutputPacketSize, &propSize, &outputPacketSize);
// }
// if(result == noErr) {
// AudioConverterGetProperty(m_converter, kAudioConverterPropertyMaximumOutputPacketSize, &propSize, &outputPacketSize);
// }

return YES;
}
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/coder/LFHardwareVideoEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFHardwareVideoEncoder.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#if __has_include(<LFLiveKit/LFLiveKit.h>)
Expand Down
23 changes: 18 additions & 5 deletions LFLiveKit/coder/LFHardwareVideoEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// LFHardwareVideoEncoder.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFHardwareVideoEncoder.h"
#import <VideoToolbox/VideoToolbox.h>

Expand All @@ -21,6 +20,7 @@ @interface LFHardwareVideoEncoder (){
@property (nonatomic, strong) LFLiveVideoConfiguration *configuration;
@property (nonatomic, weak) id<LFVideoEncodingDelegate> h264Delegate;
@property (nonatomic) NSInteger currentVideoBitRate;
@property (nonatomic) BOOL isBackGround;

@end

Expand All @@ -32,7 +32,8 @@ - (instancetype)initWithVideoStreamConfiguration:(LFLiveVideoConfiguration *)con
NSLog(@"USE LFHardwareVideoEncoder");
_configuration = configuration;
[self resetCompressionSession];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterBackground:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:) name:UIApplicationDidBecomeActiveNotification object:nil];
#ifdef DEBUG
enabledWriteVideoFile = NO;
[self initForFilePath];
Expand Down Expand Up @@ -72,6 +73,7 @@ - (void)resetCompressionSession {
}

- (void)setVideoBitRate:(NSInteger)videoBitRate {
if(_isBackGround) return;
VTSessionSetProperty(compressionSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(videoBitRate));
NSArray *limit = @[@(videoBitRate * 1.5/8), @(1)];
VTSessionSetProperty(compressionSession, kVTCompressionPropertyKey_DataRateLimits, (__bridge CFArrayRef)limit);
Expand All @@ -90,11 +92,12 @@ - (void)dealloc {
CFRelease(compressionSession);
compressionSession = NULL;
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -- LFVideoEncoder
- (void)encodeVideoData:(CVPixelBufferRef)pixelBuffer timeStamp:(uint64_t)timeStamp {

if(_isBackGround) return;
frameCount++;
CMTime presentationTimeStamp = CMTimeMake(frameCount, (int32_t)_configuration.videoFrameRate);
VTEncodeInfoFlags flags;
Expand All @@ -120,6 +123,16 @@ - (void)setDelegate:(id<LFVideoEncodingDelegate>)delegate {
_h264Delegate = delegate;
}

#pragma mark -- Notification
- (void)willEnterBackground:(NSNotification*)notification{
_isBackGround = YES;
}

- (void)willEnterForeground:(NSNotification*)notification{
[self resetCompressionSession];
_isBackGround = NO;
}

#pragma mark -- VideoCallBack
static void VideoCompressonOutputCallback(void *VTref, void *VTFrameRef, OSStatus status, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer){
if (!sampleBuffer) return;
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/coder/LFVideoEncoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFVideoEncoding.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/configuration/LFLiveAudioConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFLiveAudioConfiguration.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/configuration/LFLiveAudioConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFLiveAudioConfiguration.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFLiveAudioConfiguration.h"
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/configuration/LFLiveVideoConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFLiveVideoConfiguration.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/configuration/LFLiveVideoConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFLiveVideoConfiguration.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/1.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFLiveVideoConfiguration.h"
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/objects/LFAudioFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFAudioFrame.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#if __has_include(<LFLiveKit/LFLiveKit.h>)
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/objects/LFAudioFrame.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFAudioFrame.m
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import "LFAudioFrame.h"
Expand Down
4 changes: 2 additions & 2 deletions LFLiveKit/objects/LFFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LFFrame.h
// LFLiveKit
//
// Created by 倾慕 on 16/5/2.
// Copyright © 2016年 倾慕. All rights reserved.
// Created by LaiFeng on 16/5/20.
// Copyright © 2016年 LaiFeng All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
Loading

0 comments on commit a1a7e9a

Please sign in to comment.