Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StreamScreenObject. #1555

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HaishinKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@
BCFB355524FA27EA00DC5108 /* PlaybackViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCFB355324FA275600DC5108 /* PlaybackViewController.swift */; };
BCFB355A24FA40DD00DC5108 /* PlaybackContainerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCFB355924FA40DD00DC5108 /* PlaybackContainerViewController.swift */; };
BCFC51FE2AAB420700014428 /* IOAudioMixerTrack.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCFC51FD2AAB420700014428 /* IOAudioMixerTrack.swift */; };
BCFDF6322C82E50600BA9024 /* StreamScreenObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCFDF6312C82E50600BA9024 /* StreamScreenObject.swift */; };
BCFF640B29C0C44B004EFF2F /* SampleVideo_360x240_5mb_2ch.ts in Resources */ = {isa = PBXBuildFile; fileRef = BCFF640A29C0C44B004EFF2F /* SampleVideo_360x240_5mb_2ch.ts */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -747,6 +748,7 @@
BCFB355324FA275600DC5108 /* PlaybackViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaybackViewController.swift; sourceTree = "<group>"; };
BCFB355924FA40DD00DC5108 /* PlaybackContainerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaybackContainerViewController.swift; sourceTree = "<group>"; };
BCFC51FD2AAB420700014428 /* IOAudioMixerTrack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOAudioMixerTrack.swift; sourceTree = "<group>"; };
BCFDF6312C82E50600BA9024 /* StreamScreenObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamScreenObject.swift; sourceTree = "<group>"; };
BCFF640A29C0C44B004EFF2F /* SampleVideo_360x240_5mb_2ch.ts */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.typescript; path = SampleVideo_360x240_5mb_2ch.ts; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -1326,6 +1328,7 @@
BCDEB4F72BE441D300EEC6ED /* ScreenRenderer.swift */,
BC3004CD296B0A1700119932 /* Shape.swift */,
BC6FC91D29609A6800A746EE /* ShapeFactory.swift */,
BCFDF6312C82E50600BA9024 /* StreamScreenObject.swift */,
29B8768F1CD70AFE00FC07DA /* VideoEffect.swift */,
B317235F2C0940D800C7AED0 /* VideoRotator.swift */,
);
Expand Down Expand Up @@ -1968,6 +1971,7 @@
29B876AC1CD70B2800FC07DA /* AMF3Serializer.swift in Sources */,
BC31DBD22A653D1600C4DEA3 /* IOAudioMonitor.swift in Sources */,
B31723622C0948E300C7AED0 /* VTRotationSessionOption+Extension.swift in Sources */,
BCFDF6322C82E50600BA9024 /* StreamScreenObject.swift in Sources */,
BCB976DF26107B5600C9A649 /* TSField.swift in Sources */,
BC11024A2925147300D48035 /* IOCaptureUnit.swift in Sources */,
29B876921CD70AFE00FC07DA /* IOMixer.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Sources/IO/IOStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ extension IOStream: IOMixerDelegate {
}
}
#endif

}

extension IOStream: IOTellyUnitDelegate {
Expand Down
69 changes: 69 additions & 0 deletions Sources/Screen/StreamScreenObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import AVFoundation
import CoreGraphics
import CoreImage
import Foundation

/// An object that manages offscreen rendering a streaming video track source.
public final class StreamScreenObject: ScreenObject, ChromaKeyProcessable {
public var chromaKeyColor: CGColor?

/// The video is displayed within a player layer’s bounds.
public var videoGravity: AVLayerVideoGravity = .resizeAspect {
didSet {
guard videoGravity != oldValue else {
return
}
invalidateLayout()
}
}

private var sampleBuffer: CMSampleBuffer? {
didSet {
guard sampleBuffer != oldValue else {
return
}
if sampleBuffer == nil {
return
}
invalidateLayout()
}
}

override public func makeBounds(_ size: CGSize) -> CGRect {
guard parent != nil, let image = sampleBuffer?.formatDescription?.dimensions.size else {
return super.makeBounds(size)
}
let bounds = super.makeBounds(size)
switch videoGravity {
case .resizeAspect:
let scale = min(bounds.size.width / image.width, bounds.size.height / image.height)
let scaleSize = CGSize(width: image.width * scale, height: image.height * scale)
return super.makeBounds(scaleSize)
case .resizeAspectFill:
return bounds
default:
return bounds
}
}

override public func makeImage(_ renderer: some ScreenRenderer) -> CGImage? {
guard let sampleBuffer, let pixelBuffer = sampleBuffer.imageBuffer else {
return nil
}
let image = CIImage(cvPixelBuffer: pixelBuffer).transformed(by: videoGravity.scale(
bounds.size,
image: pixelBuffer.size
))
return renderer.context.createCGImage(image, from: videoGravity.region(bounds, image: image.extent))
}
}

extension StreamScreenObject: IOStreamObserver {
// MARK: IOStreamObserver
public func stream(_ stream: IOStream, didOutput audio: AVAudioBuffer, when: AVAudioTime) {
}

public func stream(_ stream: IOStream, didOutput video: CMSampleBuffer) {
sampleBuffer = video
}
}