Files
HaishinKit.swift/HaishinKit/Sources/Extension/Data+Extension.swift
2025-07-13 12:43:53 +09:00

47 lines
1.6 KiB
Swift

import CoreMedia
import Foundation
package extension Data {
var bytes: [UInt8] {
withUnsafeBytes {
guard let pointer = $0.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
return []
}
return [UInt8](UnsafeBufferPointer(start: pointer, count: count))
}
}
func makeBlockBuffer(advancedBy: Int = 0) -> CMBlockBuffer? {
var blockBuffer: CMBlockBuffer?
let length = count - advancedBy
return withUnsafeBytes { (buffer: UnsafeRawBufferPointer) -> CMBlockBuffer? in
guard let baseAddress = buffer.baseAddress else {
return nil
}
guard CMBlockBufferCreateWithMemoryBlock(
allocator: kCFAllocatorDefault,
memoryBlock: nil,
blockLength: length,
blockAllocator: nil,
customBlockSource: nil,
offsetToData: 0,
dataLength: length,
flags: 0,
blockBufferOut: &blockBuffer) == noErr else {
return nil
}
guard let blockBuffer else {
return nil
}
guard CMBlockBufferReplaceDataBytes(
with: baseAddress.advanced(by: advancedBy),
blockBuffer: blockBuffer,
offsetIntoDestination: 0,
dataLength: length) == noErr else {
return nil
}
return blockBuffer
}
}
}