Skip to content

Commit 19d9bb5

Browse files
committed
Extract sample buffer streaming logic
1 parent 67cd3fd commit 19d9bb5

File tree

1 file changed

+76
-68
lines changed
  • packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation

1 file changed

+76
-68
lines changed

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -758,74 +758,7 @@ final class DefaultCamera: FLTCam, Camera {
758758
return
759759
}
760760

761-
if isStreamingImages {
762-
if let eventSink = imageStreamHandler?.eventSink,
763-
streamingPendingFramesCount < maxStreamingPendingFramesCount
764-
{
765-
if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
766-
streamingPendingFramesCount += 1
767-
768-
// Must lock base address before accessing the pixel data
769-
CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
770-
771-
let imageWidth = CVPixelBufferGetWidth(pixelBuffer)
772-
let imageHeight = CVPixelBufferGetHeight(pixelBuffer)
773-
774-
var planes: [[String: Any]] = []
775-
776-
let isPlanar = CVPixelBufferIsPlanar(pixelBuffer)
777-
let planeCount = isPlanar ? CVPixelBufferGetPlaneCount(pixelBuffer) : 1
778-
779-
for i in 0..<planeCount {
780-
let planeAddress: UnsafeMutableRawPointer?
781-
let bytesPerRow: Int
782-
let height: Int
783-
let width: Int
784-
785-
if isPlanar {
786-
planeAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, i)
787-
bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, i)
788-
height = CVPixelBufferGetHeightOfPlane(pixelBuffer, i)
789-
width = CVPixelBufferGetWidthOfPlane(pixelBuffer, i)
790-
} else {
791-
planeAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
792-
bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
793-
height = CVPixelBufferGetHeight(pixelBuffer)
794-
width = CVPixelBufferGetWidth(pixelBuffer)
795-
}
796-
797-
let length = bytesPerRow * height
798-
let bytes = Data(bytes: planeAddress!, count: length)
799-
800-
let planeBuffer: [String: Any] = [
801-
"bytesPerRow": bytesPerRow,
802-
"width": width,
803-
"height": height,
804-
"bytes": FlutterStandardTypedData(bytes: bytes),
805-
]
806-
planes.append(planeBuffer)
807-
}
808-
809-
// Lock the base address before accessing pixel data, and unlock it afterwards.
810-
// Done accessing the `pixelBuffer` at this point.
811-
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
812-
813-
let imageBuffer: [String: Any] = [
814-
"width": imageWidth,
815-
"height": imageHeight,
816-
"format": videoFormat,
817-
"planes": planes,
818-
"lensAperture": Double(captureDevice.lensAperture()),
819-
"sensorExposureTime": Int(captureDevice.exposureDuration().seconds * 1_000_000_000),
820-
"sensorSensitivity": Double(captureDevice.iso()),
821-
]
822-
823-
DispatchQueue.main.async {
824-
eventSink(imageBuffer)
825-
}
826-
}
827-
}
828-
}
761+
handleSampleBufferStreaming(sampleBuffer)
829762

830763
if isRecording && !isRecordingPaused {
831764
if videoWriter?.status == .failed, let error = videoWriter?.error {
@@ -906,6 +839,81 @@ final class DefaultCamera: FLTCam, Camera {
906839
}
907840
}
908841

842+
private func handleSampleBufferStreaming(_ sampleBuffer: CMSampleBuffer) {
843+
guard isStreamingImages,
844+
let eventSink = imageStreamHandler?.eventSink,
845+
streamingPendingFramesCount < maxStreamingPendingFramesCount
846+
else {
847+
return
848+
}
849+
850+
// Non-pixel buffer samples, such as audio samples, are ignored for streaming
851+
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
852+
return
853+
}
854+
855+
streamingPendingFramesCount += 1
856+
857+
// Must lock base address before accessing the pixel data
858+
CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
859+
860+
let imageWidth = CVPixelBufferGetWidth(pixelBuffer)
861+
let imageHeight = CVPixelBufferGetHeight(pixelBuffer)
862+
863+
var planes: [[String: Any]] = []
864+
865+
let isPlanar = CVPixelBufferIsPlanar(pixelBuffer)
866+
let planeCount = isPlanar ? CVPixelBufferGetPlaneCount(pixelBuffer) : 1
867+
868+
for i in 0..<planeCount {
869+
let planeAddress: UnsafeMutableRawPointer?
870+
let bytesPerRow: Int
871+
let height: Int
872+
let width: Int
873+
874+
if isPlanar {
875+
planeAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, i)
876+
bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, i)
877+
height = CVPixelBufferGetHeightOfPlane(pixelBuffer, i)
878+
width = CVPixelBufferGetWidthOfPlane(pixelBuffer, i)
879+
} else {
880+
planeAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
881+
bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
882+
height = CVPixelBufferGetHeight(pixelBuffer)
883+
width = CVPixelBufferGetWidth(pixelBuffer)
884+
}
885+
886+
let length = bytesPerRow * height
887+
let bytes = Data(bytes: planeAddress!, count: length)
888+
889+
let planeBuffer: [String: Any] = [
890+
"bytesPerRow": bytesPerRow,
891+
"width": width,
892+
"height": height,
893+
"bytes": FlutterStandardTypedData(bytes: bytes),
894+
]
895+
planes.append(planeBuffer)
896+
}
897+
898+
// Lock the base address before accessing pixel data, and unlock it afterwards.
899+
// Done accessing the `pixelBuffer` at this point.
900+
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
901+
902+
let imageBuffer: [String: Any] = [
903+
"width": imageWidth,
904+
"height": imageHeight,
905+
"format": videoFormat,
906+
"planes": planes,
907+
"lensAperture": Double(captureDevice.lensAperture()),
908+
"sensorExposureTime": Int(captureDevice.exposureDuration().seconds * 1_000_000_000),
909+
"sensorSensitivity": Double(captureDevice.iso()),
910+
]
911+
912+
DispatchQueue.main.async {
913+
eventSink(imageBuffer)
914+
}
915+
}
916+
909917
private func copySampleBufferWithAdjustedTime(_ sample: CMSampleBuffer, by offset: CMTime)
910918
-> CMSampleBuffer?
911919
{

0 commit comments

Comments
 (0)