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 #endif
/// The recorder instance. /// The recorder instance.
public lazy var recorder = AVRecorder() public lazy var recorder = AVRecorder()
/// Specifies the drawable object. /// Specifies the drawable object.
public weak var drawable: NetStreamDrawable? { public weak var drawable: NetStreamDrawable? {
get { get {

View File

@ -3,18 +3,24 @@ import CoreImage
/// The `NetStream` class is the foundation of a RTMPStream, HTTPStream. /// The `NetStream` class is the foundation of a RTMPStream, HTTPStream.
open class NetStream: NSObject { open class NetStream: NSObject {
private static let queueKey = DispatchSpecificKey<UnsafeMutableRawPointer>() /// The lockQueue.
private static let queueValue = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 1)
public let lockQueue: DispatchQueue = { public let lockQueue: DispatchQueue = {
let queue = DispatchQueue(label: "com.haishinkit.HaishinKit.NetStream.lock") let queue = DispatchQueue(label: "com.haishinkit.HaishinKit.NetStream.lock")
queue.setSpecific(key: queueKey, value: queueValue) queue.setSpecific(key: queueKey, value: queueValue)
return queue return queue
}() }()
open private(set) var mixer = AVMixer() private static let queueKey = DispatchSpecificKey<UnsafeMutableRawPointer>()
open var metadata: [String: Any?] = [:] private static let queueValue = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 1)
open var context: CIContext? {
/// 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 { get {
mixer.videoIO.context mixer.videoIO.context
} }
@ -24,10 +30,11 @@ open class NetStream: NSObject {
} }
#if os(iOS) || os(macOS) #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 { get {
var torch: Bool = false var torch: Bool = false
ensureLockQueue { lockQueue.sync {
torch = self.mixer.videoIO.torch torch = self.mixer.videoIO.torch
} }
return torch return torch
@ -39,8 +46,8 @@ open class NetStream: NSObject {
} }
} }
/// Specify stream video orientation. /// Specify the video orientation for stream.
open var videoOrientation: AVCaptureVideoOrientation { public var videoOrientation: AVCaptureVideoOrientation {
get { get {
mixer.videoIO.orientation mixer.videoIO.orientation
} }
@ -50,8 +57,8 @@ open class NetStream: NSObject {
} }
#endif #endif
/// Specify stream audio compression properties. /// Specify the audio compression properties.
open var audioSettings: Setting<AudioCodec, AudioCodec.Option> { public var audioSettings: Setting<AudioCodec, AudioCodec.Option> {
get { get {
mixer.audioIO.codec.settings mixer.audioIO.codec.settings
} }
@ -60,8 +67,8 @@ open class NetStream: NSObject {
} }
} }
/// Specify stream video compression properties. /// Specify the video compression properties.
open var videoSettings: Setting<VideoCodec, VideoCodec.Option> { public var videoSettings: Setting<VideoCodec, VideoCodec.Option> {
get { get {
mixer.videoIO.encoder.settings 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> { open var captureSettings: Setting<AVMixer, AVMixer.Option> {
get { get {
mixer.settings 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 { get {
mixer.recorder.outputSettings mixer.recorder.outputSettings
} }
@ -94,6 +102,8 @@ open class NetStream: NSObject {
} }
#if os(iOS) || os(macOS) #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) { open func attachCamera(_ camera: AVCaptureDevice?, onError: ((_ error: NSError) -> Void)? = nil) {
lockQueue.async { lockQueue.async {
do { 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) { open func attachAudio(_ audio: AVCaptureDevice?, automaticallyConfiguresApplicationAudioSession: Bool = false, onError: ((_ error: NSError) -> Void)? = nil) {
lockQueue.async { lockQueue.async {
do { 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.focusPointOfInterest = focus
mixer.videoIO.exposurePointOfInterest = exposure mixer.videoIO.exposurePointOfInterest = exposure
} }
#endif #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) { open func appendSampleBuffer(_ sampleBuffer: CMSampleBuffer, withType: AVMediaType, options: [NSObject: AnyObject]? = nil) {
switch withType { switch withType {
case .audio: 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 { mixer.videoIO.lockQueue.sync {
self.mixer.videoIO.registerEffect(effect) 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 { mixer.videoIO.lockQueue.sync {
self.mixer.videoIO.unregisterEffect(effect) 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 { mixer.audioIO.lockQueue.sync {
self.mixer.audioIO.registerEffect(effect) 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 { mixer.audioIO.lockQueue.sync {
self.mixer.audioIO.unregisterEffect(effect) 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) #if !os(tvOS)
/// Specifies the orientation of AVCaptureVideoOrientation. /// Specifies the orientation of AVCaptureVideoOrientation.
var orientation: AVCaptureVideoOrientation { get set } var orientation: AVCaptureVideoOrientation { get set }
/// Specifies the position of AVCaptureDevice. /// Specifies the position of AVCaptureDevice.
var position: AVCaptureDevice.Position { get set } var position: AVCaptureDevice.Position { get set }
#endif #endif
/// The videoFormatDescription which is the current CMSampleBuffer. /// The videoFormatDescription which is the current CMSampleBuffer.
var videoFormatDescription: CMVideoFormatDescription? { get } var videoFormatDescription: CMVideoFormatDescription? { get }
/// Attaches a drawable to a new NetStream object. /// Attaches a drawable to a new NetStream object.
func attachStream(_ stream: NetStream?) func attachStream(_ stream: NetStream?)
/// Enqueue a CMSampleBuffer? to draw. /// Enqueue a CMSampleBuffer? to draw.
func enqueue(_ sampleBuffer: CMSampleBuffer?) func enqueue(_ sampleBuffer: CMSampleBuffer?)
} }