modify declaration of NetStream

This commit is contained in:
shogo4405 2022-07-07 03:05:25 +09:00
parent 17edfc4a24
commit 2a75e45a13
3 changed files with 44 additions and 36 deletions

View File

@ -155,7 +155,7 @@ public class AVMixer {
#endif
/// The recorder instance.
public lazy var recorder = AVRecorder()
/// Specifies the drawable object.
public weak var drawable: NetStreamDrawable? {
get {

View File

@ -3,18 +3,24 @@ import CoreImage
/// The `NetStream` class is the foundation of a RTMPStream, HTTPStream.
open class NetStream: NSObject {
private static let queueKey = DispatchSpecificKey<UnsafeMutableRawPointer>()
private static let queueValue = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 1)
/// The lockQueue.
public let lockQueue: DispatchQueue = {
let queue = DispatchQueue(label: "com.haishinkit.HaishinKit.NetStream.lock")
queue.setSpecific(key: queueKey, value: queueValue)
return queue
}()
open private(set) var mixer = AVMixer()
open var metadata: [String: Any?] = [:]
open var context: CIContext? {
private static let queueKey = DispatchSpecificKey<UnsafeMutableRawPointer>()
private static let queueValue = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 1)
/// The mixer object.
public private(set) var mixer = AVMixer()
/// Specifies the metadata for the stream.
public var metadata: [String: Any?] = [:]
/// Specifies the context object.
public var context: CIContext? {
get {
mixer.videoIO.context
}
@ -24,10 +30,11 @@ open class NetStream: NSObject {
}
#if os(iOS) || os(macOS)
open var torch: Bool {
/// Specifiet the device torch indicating wheter the turn on(TRUE) or not(FALSE).
public var torch: Bool {
get {
var torch: Bool = false
ensureLockQueue {
lockQueue.sync {
torch = self.mixer.videoIO.torch
}
return torch
@ -39,8 +46,8 @@ open class NetStream: NSObject {
}
}
/// Specify stream video orientation.
open var videoOrientation: AVCaptureVideoOrientation {
/// Specify the video orientation for stream.
public var videoOrientation: AVCaptureVideoOrientation {
get {
mixer.videoIO.orientation
}
@ -50,8 +57,8 @@ open class NetStream: NSObject {
}
#endif
/// Specify stream audio compression properties.
open var audioSettings: Setting<AudioCodec, AudioCodec.Option> {
/// Specify the audio compression properties.
public var audioSettings: Setting<AudioCodec, AudioCodec.Option> {
get {
mixer.audioIO.codec.settings
}
@ -60,8 +67,8 @@ open class NetStream: NSObject {
}
}
/// Specify stream video compression properties.
open var videoSettings: Setting<VideoCodec, VideoCodec.Option> {
/// Specify the video compression properties.
public var videoSettings: Setting<VideoCodec, VideoCodec.Option> {
get {
mixer.videoIO.encoder.settings
}
@ -70,7 +77,7 @@ open class NetStream: NSObject {
}
}
/// Specify stream avsession properties.
/// Specify the avsession properties.
open var captureSettings: Setting<AVMixer, AVMixer.Option> {
get {
mixer.settings
@ -80,7 +87,8 @@ open class NetStream: NSObject {
}
}
open var recorderSettings: [AVMediaType: [String: Any]] {
/// Specifies the recorder properties.
public var recorderSettings: [AVMediaType: [String: Any]] {
get {
mixer.recorder.outputSettings
}
@ -94,6 +102,8 @@ open class NetStream: NSObject {
}
#if os(iOS) || os(macOS)
/// Attaches the camera object.
/// - Warning: This method can't use appendSampleBuffer at the same time.
open func attachCamera(_ camera: AVCaptureDevice?, onError: ((_ error: NSError) -> Void)? = nil) {
lockQueue.async {
do {
@ -104,6 +114,8 @@ open class NetStream: NSObject {
}
}
/// Attaches the microphone object.
/// - Warning: This method can't use appendSampleBuffer at the same time.
open func attachAudio(_ audio: AVCaptureDevice?, automaticallyConfiguresApplicationAudioSession: Bool = false, onError: ((_ error: NSError) -> Void)? = nil) {
lockQueue.async {
do {
@ -114,12 +126,15 @@ open class NetStream: NSObject {
}
}
open func setPointOfInterest(_ focus: CGPoint, exposure: CGPoint) {
/// Set the point of interest.
public func setPointOfInterest(_ focus: CGPoint, exposure: CGPoint) {
mixer.videoIO.focusPointOfInterest = focus
mixer.videoIO.exposurePointOfInterest = exposure
}
#endif
/// Append a CMSampleBuffer?.
/// - Warning: This method can't use attachCamera or attachAudio method at the same time.
open func appendSampleBuffer(_ sampleBuffer: CMSampleBuffer, withType: AVMediaType, options: [NSObject: AnyObject]? = nil) {
switch withType {
case .audio:
@ -135,37 +150,31 @@ open class NetStream: NSObject {
}
}
open func registerVideoEffect(_ effect: VideoEffect) -> Bool {
/// Register a video effect.
public func registerVideoEffect(_ effect: VideoEffect) -> Bool {
mixer.videoIO.lockQueue.sync {
self.mixer.videoIO.registerEffect(effect)
}
}
open func unregisterVideoEffect(_ effect: VideoEffect) -> Bool {
/// Unregister a video effect.
public func unregisterVideoEffect(_ effect: VideoEffect) -> Bool {
mixer.videoIO.lockQueue.sync {
self.mixer.videoIO.unregisterEffect(effect)
}
}
open func registerAudioEffect(_ effect: AudioEffect) -> Bool {
/// Register a audio effect.
public func registerAudioEffect(_ effect: AudioEffect) -> Bool {
mixer.audioIO.lockQueue.sync {
self.mixer.audioIO.registerEffect(effect)
}
}
open func unregisterAudioEffect(_ effect: AudioEffect) -> Bool {
/// Unregister a audio effect.
public func unregisterAudioEffect(_ effect: AudioEffect) -> Bool {
mixer.audioIO.lockQueue.sync {
self.mixer.audioIO.unregisterEffect(effect)
}
}
private func ensureLockQueue(callback: () -> Void) {
if DispatchQueue.getSpecific(key: NetStream.queueKey) == NetStream.queueValue {
callback()
} else {
lockQueue.sync {
callback()
}
}
}
}

View File

@ -6,18 +6,17 @@ public protocol NetStreamDrawable: AnyObject {
#if !os(tvOS)
/// Specifies the orientation of AVCaptureVideoOrientation.
var orientation: AVCaptureVideoOrientation { get set }
/// Specifies the position of AVCaptureDevice.
var position: AVCaptureDevice.Position { get set }
#endif
/// The videoFormatDescription which is the current CMSampleBuffer.
var videoFormatDescription: CMVideoFormatDescription? { get }
/// Attaches a drawable to a new NetStream object.
func attachStream(_ stream: NetStream?)
/// Enqueue a CMSampleBuffer? to draw.
func enqueue(_ sampleBuffer: CMSampleBuffer?)
}