Skip to content

Commit

Permalink
refactroing pps,sps
Browse files Browse the repository at this point in the history
  • Loading branch information
huiping192 committed Sep 10, 2023
1 parent 4ca7ccd commit bf33f24
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions Sources/HPLiveKit/Coder/LiveVideoH264Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class LiveVideoH264Encoder: VideoEncoder {
guard let compressionSession = compressionSession else { return }
VTCompressionSessionCompleteFrames(compressionSession, untilPresentationTimeStamp: CMTime.indefinite)
}

private func convertVideoFrame(sampleBuffer: CMSampleBuffer, isKeyFrame: Bool) -> VideoFrame? {
guard let bufferData = CMSampleBufferGetDataBuffer(sampleBuffer)?.data else {
return nil
Expand All @@ -221,37 +221,22 @@ class LiveVideoH264Encoder: VideoEncoder {
private func receiveSpsAndPps(sampleBuffer: CMSampleBuffer) {
guard let format = CMSampleBufferGetFormatDescription(sampleBuffer) else { return }

// sps
var sparameterSetSize: size_t = 0
var sparameterSetCount: size_t = 0

var sps: UnsafePointer<UInt8>?

let spsStatusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, parameterSetIndex: 0, parameterSetPointerOut: &sps, parameterSetSizeOut: &sparameterSetSize, parameterSetCountOut: &sparameterSetCount, nalUnitHeaderLengthOut: nil)

if spsStatusCode != noErr {
print("Receive h264 sps error")
return
}

// pps
var pparameterSetSize: size_t = 0
var pparameterSetCount: size_t = 0
var pps: UnsafePointer<UInt8>?
let ppsStatusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, parameterSetIndex: 1, parameterSetPointerOut: &pps, parameterSetSizeOut: &pparameterSetSize, parameterSetCountOut: &pparameterSetCount, nalUnitHeaderLengthOut: nil)
self.sps = receiveParameterSet(formatDescription: format, index: 0)
self.pps = receiveParameterSet(formatDescription: format, index: 1)
}

private func receiveParameterSet(formatDescription: CMFormatDescription, index: Int) -> Data {
var size = 0
var parameterSetPointerOut: UnsafePointer<UInt8>?

if ppsStatusCode != noErr {
print("Receive h264 pps error")
return
}
let statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(formatDescription, parameterSetIndex: 0, parameterSetPointerOut: &parameterSetPointerOut, parameterSetSizeOut: &size, parameterSetCountOut: nil, nalUnitHeaderLengthOut: nil)

guard let spsBytes = sps, let ppsBytes = pps else {
print("Receive h264 sps,pps error")
return
guard let parameterSetPointerOut = parameterSetPointerOut, statusCode != noErr else {
print("Receive h264 ParameterSet error, \(statusCode)")
return Data()
}

self.sps = Data(bytes: spsBytes, count: sparameterSetSize)
self.pps = Data(bytes: ppsBytes, count: pparameterSetSize)
return Data(bytes: parameterSetPointerOut, count: size)
}

}
Expand Down

0 comments on commit bf33f24

Please sign in to comment.