Skip to content

Commit

Permalink
audio codec default change for iOS HLS Compatible #9
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Feb 2, 2016
1 parent 87eead4 commit fadf001
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lf/Codec/AACEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Foundation
import AVFoundation

final class AACEncoder:NSObject, Encoder, AVCaptureAudioDataOutputSampleBufferDelegate {

static let samplesPerFrame:UInt32 = 1024
static let defaultChannels:UInt32 = 1
static let defaultSampleRate:Double = 44100
Expand Down Expand Up @@ -40,7 +39,7 @@ final class AACEncoder:NSObject, Encoder, AVCaptureAudioDataOutputSampleBufferDe
_inDestinationFormat = AudioStreamBasicDescription()
_inDestinationFormat!.mSampleRate = sampleRate
_inDestinationFormat!.mFormatID = kAudioFormatMPEG4AAC
_inDestinationFormat!.mFormatFlags = 0
_inDestinationFormat!.mFormatFlags = UInt32(MPEG4ObjectID.AAC_LC.rawValue)
_inDestinationFormat!.mBytesPerPacket = 0
_inDestinationFormat!.mFramesPerPacket = AACEncoder.samplesPerFrame
_inDestinationFormat!.mChannelsPerFrame = channels
Expand Down Expand Up @@ -76,14 +75,15 @@ final class AACEncoder:NSObject, Encoder, AVCaptureAudioDataOutputSampleBufferDe

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {


if (inSourceFormat == nil) {
if let format:CMAudioFormatDescriptionRef = CMSampleBufferGetFormatDescription(sampleBuffer) {
inSourceFormat = CMAudioFormatDescriptionGetStreamBasicDescription(format).memory
} else {
return
}
}

var status:OSStatus = noErr
var blockBuffer:CMBlockBufferRef?
var inAudioBufferList:AudioBufferList = AudioBufferList()
Expand All @@ -93,7 +93,7 @@ final class AACEncoder:NSObject, Encoder, AVCaptureAudioDataOutputSampleBufferDe

var data:[UInt8] = [UInt8](count: Int(AACEncoder.defaultAACBufferSize), repeatedValue: 0)
var outOutputData:AudioBufferList = AudioBufferList(
mNumberBuffers: 1,
mNumberBuffers: channels,
mBuffers: AudioBuffer(
mNumberChannels: 1,
mDataByteSize: UInt32(data.count),
Expand All @@ -104,7 +104,7 @@ final class AACEncoder:NSObject, Encoder, AVCaptureAudioDataOutputSampleBufferDe
currentBufferList = inAudioBufferList
var outputDataPacketSize:UInt32 = 1
status = fillComplexBuffer(&outputDataPacketSize, outOutputData: &outOutputData, outPacketDescription: nil)

if (status == noErr)
{
var result:CMSampleBufferRef?
Expand Down
4 changes: 2 additions & 2 deletions lf/Codec/AVCEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class AVCEncoder:NSObject, Encoder, AVCaptureVideoDataOutputSampleBufferDe
var bitrate:Int32 = 160 * 1000
var profileLevel:CFString = kVTProfileLevel_H264_Baseline_3_0
var aspectRatio16by9:Bool = true
var keyframeInterval:Int = AVCEncoder.defaultFPS * 2
var keyframeInterval:Int = 2

let lockQueue:dispatch_queue_t = dispatch_queue_create("com.github.shogo4405.lf.AVCEncoder.lock", DISPATCH_QUEUE_SERIAL)
weak var delegate:VideoEncoderDelegate?
Expand Down Expand Up @@ -64,7 +64,7 @@ final class AVCEncoder:NSObject, Encoder, AVCaptureVideoDataOutputSampleBufferDe
kVTCompressionPropertyKey_ProfileLevel: profileLevel,
kVTCompressionPropertyKey_AverageBitRate: Int(bitrate),
kVTCompressionPropertyKey_ExpectedFrameRate: fps,
kVTCompressionPropertyKey_MaxKeyFrameInterval: keyframeInterval,
kVTCompressionPropertyKey_MaxKeyFrameInterval: fps * keyframeInterval,
kVTCompressionPropertyKey_AllowFrameReordering: false,
kVTCompressionPropertyKey_PixelTransferProperties: [
"ScalingMode": "Trim"
Expand Down
26 changes: 15 additions & 11 deletions lf/ISO/MP4AudioSpecificConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,31 @@ public struct AudioSpecificConfig: CustomStringConvertible {

public init(formatDescription: CMFormatDescriptionRef) {
let asbd:AudioStreamBasicDescription = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription).memory
type = AudioObjectType(formatID: asbd.mFormatID)
type = AudioObjectType(objectID: MPEG4ObjectID(rawValue: Int(asbd.mFormatFlags))!)
frequency = SamplingFrequency(sampleRate: asbd.mSampleRate)
channel = ChannelConfiguration(rawValue: UInt8(asbd.mChannelsPerFrame))!
}
}

public enum AudioObjectType:UInt8 {
case Null = 0
case AACMain = 1
case AACLC = 2
case AACSSR = 3
case AACLTP = 4
case AAC_Main = 1
case AAC_LC = 2
case AAC_SSR = 3
case AAC_LTP = 4
case SBR = 5
case AACScalable = 6
case AAC_Scalable = 6

public init (formatID: AudioFormatID) {
switch formatID {
case kAudioFormatMPEG4AAC:
self = .AACMain
public init (objectID: MPEG4ObjectID) {
switch objectID {
case .AAC_Main:
self = .AAC_Main
case .AAC_LC:
self = .AAC_LC
case .AAC_LTP:
self = .AAC_LTP
default:
self = .Null
self = Null
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lf/RTMP/RTMPStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public class RTMPStream: EventDispatcher, RTMPMuxerDelegate {

self.muxer.dispose()
self.muxer.delegate = self
self.sessionManager.startRunning()
self.chunkTypes.removeAll(keepCapacity: false)
self.rtmpConnection.doWrite(RTMPChunk(
type: .Zero,
Expand Down

0 comments on commit fadf001

Please sign in to comment.