Skip to content

Commit

Permalink
Implementation of Volume Control.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Aug 21, 2024
1 parent fe5fa28 commit 42d7176
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
16 changes: 12 additions & 4 deletions SRTHaishinKit/SRTStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public actor SRTStream {

extension SRTStream: HKStream {
// MARK: HKStream
public var soundTransform: SoundTransform? {
get async {
await player.soundTransfrom
}
}

public func setSoundTransform(_ soundTransform: SoundTransform) async {
await player.setSoundTransform(soundTransform)
}

public var audioSettings: AudioCodecSettings {
ingestor.audioSettings
}
Expand Down Expand Up @@ -158,10 +168,8 @@ extension SRTStream: HKStream {
}
}

public func attachAudioPlayer(_ audioPlayer: AudioPlayer?) {
Task {
await player.attachAudioPlayer(audioPlayer)
}
public func attachAudioPlayer(_ audioPlayer: AudioPlayer?) async {
await player.attachAudioPlayer(audioPlayer)
}

public func addOutput(_ observer: some HKStreamOutput) {
Expand Down
5 changes: 5 additions & 0 deletions Sources/HKStream/AudioPlayerNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final actor AudioPlayerNode {
}
return 0.0
}
private(set) var soundTransfrom = SoundTransform()
private(set) var isPaused = false
private let playerNode: AVAudioPlayerNode
private var audioTime = AudioTime()
Expand All @@ -37,6 +38,10 @@ final actor AudioPlayerNode {
self.playerNode = playerNode
}

func setSoundTransfrom(_ soundTransfrom: SoundTransform) {
soundTransfrom.apply(playerNode)
}

func enqueue(_ audioBuffer: AVAudioBuffer, when: AVAudioTime) async {
format = audioBuffer.format
guard let audioBuffer = audioBuffer as? AVAudioPCMBuffer, await player?.isConnected(self) == true else {
Expand Down
13 changes: 8 additions & 5 deletions Sources/HKStream/HKStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import CoreMedia
public protocol HKStream: Actor, MediaMixerOutput {
/// The current state of the stream.
var readyState: HKStreamReadyState { get }

/// The sound transform value control.
var soundTransform: SoundTransform? { get async }
/// The audio compression properties.
var audioSettings: AudioCodecSettings { get }

/// The video compression properties.
var videoSettings: VideoCodecSettings { get }

Expand All @@ -23,6 +23,9 @@ public protocol HKStream: Actor, MediaMixerOutput {
/// Sets the video compression properties.
func setVideoSettings(_ videoSettings: VideoCodecSettings)

/// Sets the sound transform value control.
func setSoundTransform(_ soundTransfrom: SoundTransform) async

/// Appends a CMSampleBuffer.
/// - Parameters:
/// - sampleBuffer:The sample buffer to append.
Expand All @@ -34,15 +37,15 @@ public protocol HKStream: Actor, MediaMixerOutput {
/// - when: The audio time to append.
func append(_ audioBuffer: AVAudioBuffer, when: AVAudioTime)

/// Attaches an audio player instance for playback.
func attachAudioPlayer(_ audioPlayer: AudioPlayer?) async

/// Adds an output observer.
func addOutput(_ obserber: some HKStreamOutput)

/// Removes an output observer.
func removeOutput(_ observer: some HKStreamOutput)

/// Attaches an audio player instance for playback.
func attachAudioPlayer(_ audioPlayer: AudioPlayer?)

/// Dispatch a network monitor event.
func dispatch(_ event: NetworkMonitorEvent) async
}
19 changes: 14 additions & 5 deletions Sources/HKStream/HKStreamPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import Foundation
/// An actor that provides a stream playback feature.
public final actor HKStreamPlayer {
public private(set) var isRunning = false
/// The sound transform value control.
public var soundTransfrom: SoundTransform? {
get async {
return await audioPlayerNode?.soundTransfrom
}
}
private lazy var mediaLink = MediaLink()
private lazy var audioCodec = AudioCodec()
private lazy var videoCodec = VideoCodec()
Expand All @@ -15,6 +21,11 @@ public final actor HKStreamPlayer {
self.stream = stream
}

/// Sets the sound transform value control.
public func setSoundTransform(_ soundTransfrom: SoundTransform) async {
await audioPlayerNode?.setSoundTransfrom(soundTransfrom)
}

/// Appends a sample buffer for playback.
public func append(_ buffer: CMSampleBuffer) {
switch buffer.formatDescription?.mediaType {
Expand All @@ -33,11 +44,9 @@ public final actor HKStreamPlayer {
}

/// Attaches an audio player.
public func attachAudioPlayer(_ audioPlayer: AudioPlayer?) {
Task {
audioPlayerNode = await audioPlayer?.makePlayerNode()
await mediaLink.setAudioPlayer(audioPlayerNode)
}
public func attachAudioPlayer(_ audioPlayer: AudioPlayer?) async {
audioPlayerNode = await audioPlayer?.makePlayerNode()
await mediaLink.setAudioPlayer(audioPlayerNode)
}
}

Expand Down
12 changes: 10 additions & 2 deletions Sources/HKStream/SoundTransform.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import AVFoundation
import Foundation

/// The SoundTransform class
public struct SoundTransform {
/// A structure represents the volume value controller.
public struct SoundTransform: Equatable, Sendable {
/// The default volume.
public static let defaultVolume: Float = 1.0
/// The default panning of the sound.
public static let defaultPan: Float = 0

/// The volume, ranging from 0 (silent) to 1 (full volume)
public var volume = SoundTransform.defaultVolume
/// The panning of the sound
public var pan = SoundTransform.defaultPan

/// Creates a new instance.
public init(volume: Float = SoundTransform.defaultVolume, pan: Float = SoundTransform.defaultPan) {
self.volume = volume
self.pan = pan
}

func apply(_ playerNode: AVAudioPlayerNode?) {
playerNode?.volume = volume
playerNode?.pan = pan
Expand Down
10 changes: 10 additions & 0 deletions Sources/RTMP/RTMPStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@ public actor RTMPStream {

extension RTMPStream: HKStream {
// MARK: HKStream
public var soundTransform: SoundTransform? {
get async {
await player.soundTransfrom
}
}

public var audioSettings: AudioCodecSettings {
ingestor.audioSettings
}
Expand All @@ -701,6 +707,10 @@ extension RTMPStream: HKStream {
ingestor.videoSettings = videoSettings
}

public func setSoundTransform(_ soundTransform: SoundTransform) async {
await player.setSoundTransform(soundTransform)
}

public func append(_ sampleBuffer: CMSampleBuffer) {
switch sampleBuffer.formatDescription?.mediaType {
case .video:
Expand Down

0 comments on commit 42d7176

Please sign in to comment.