Skip to content

Commit

Permalink
Add StreamScreenObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Aug 31, 2024
1 parent 9a691cf commit b7a8935
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
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
}
}

0 comments on commit b7a8935

Please sign in to comment.