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

refactroing capture code #9

Merged
merged 1 commit into from
Sep 10, 2023
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
24 changes: 23 additions & 1 deletion Example/HPLiveKit/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21678"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21679"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -24,15 +24,37 @@
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Publish"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nfg-N4-wIh">
<rect key="frame" x="40" y="512" width="63" height="35"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Mute"/>
<connections>
<action selector="muteButtonTapped" destination="vXZ-lx-hvc" eventType="touchUpInside" id="j6K-bd-gYV"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EMc-fb-Fvy">
<rect key="frame" x="271" y="512" width="64" height="35"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Front"/>
<connections>
<action selector="swapButtonTapped" destination="vXZ-lx-hvc" eventType="touchUpInside" id="ovo-9T-lDF"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="nfg-N4-wIh" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="24" id="4cb-u9-DuG"/>
<constraint firstItem="nfg-N4-wIh" firstAttribute="centerY" secondItem="gmk-fz-ljG" secondAttribute="centerY" id="5oX-lO-zc2"/>
<constraint firstAttribute="trailingMargin" secondItem="EMc-fb-Fvy" secondAttribute="trailing" constant="24" id="DMT-ai-SvK"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="gmk-fz-ljG" secondAttribute="bottom" constant="120" id="ZvJ-pj-2ln"/>
<constraint firstItem="gmk-fz-ljG" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="bI8-Sv-oLv"/>
<constraint firstItem="EMc-fb-Fvy" firstAttribute="centerY" secondItem="gmk-fz-ljG" secondAttribute="centerY" id="vPy-lc-6gj"/>
</constraints>
</view>
<connections>
<outlet property="button" destination="gmk-fz-ljG" id="hC2-v3-Dw8"/>
<outlet property="muteButton" destination="nfg-N4-wIh" id="zWJ-66-JRM"/>
<outlet property="swapButton" destination="EMc-fb-Fvy" id="b2a-7m-kKp"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
Expand Down
17 changes: 16 additions & 1 deletion Example/HPLiveKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class ViewController: UIViewController {

@IBOutlet private var button: UIButton!

@IBOutlet private var muteButton: UIButton!
@IBOutlet private var swapButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -28,7 +31,7 @@ class ViewController: UIViewController {
@objc private func buttonTapped() {
switch liveState {
case .ready, .stop, .error:
let info = LiveStreamInfo(streamId: "sample1", url: "rtmp://192.168.11.23/live/haha")
let info = LiveStreamInfo(streamId: "sample1", url: "rtmp://192.168.11.3/live/haha")
liveSession?.startLive(streamInfo: info)
liveState = .start
button.setTitle("Stop", for: .normal)
Expand All @@ -41,6 +44,18 @@ class ViewController: UIViewController {
}
}

@IBAction func muteButtonTapped() {
liveSession?.mute.toggle()

muteButton.setTitle(liveSession?.mute == true ? "Unmute" : "Mute", for: .normal)
}

@IBAction func swapButtonTapped() {
liveSession?.captureDevicePositionFront.toggle()

swapButton.setTitle(liveSession?.captureDevicePositionFront == true ? "Front" : "Back", for: .normal)
}

func configureLiveSession() {
let defaultVideoConfiguration = LiveVideoConfigurationFactory.createHigh3()
let defaultAudioConfiguration = LiveAudioConfigurationFactory.defaultAudioConfiguration
Expand Down
114 changes: 63 additions & 51 deletions Sources/HPLiveKit/Capture/CaptureManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,78 @@ import UIKit
import CoreMedia

public protocol CaptureManagerDelegate: AnyObject {
func captureOutput(captureManager: CaptureManager, video: CMSampleBuffer)
func captureOutput(captureManager: CaptureManager, audio: CMSampleBuffer)
func captureOutput(captureManager: CaptureManager, video: CMSampleBuffer)
func captureOutput(captureManager: CaptureManager, audio: CMSampleBuffer)
}

public class CaptureManager: NSObject {
// video, audio configuration
private let audioConfiguration: LiveAudioConfiguration
private let videoConfiguration: LiveVideoConfiguration

// video,audio data source
private let videoCapture: LiveVideoCapture
private let audioCapture: LiveAudioCapture

weak var delegate: CaptureManagerDelegate?

public var preview: UIView? {
get {
videoCapture.preview
}
set {
videoCapture.preview = newValue
}
// video, audio configuration
private let audioConfiguration: LiveAudioConfiguration
private let videoConfiguration: LiveVideoConfiguration

// video,audio data source
private let videoCapture: LiveVideoCapture
private let audioCapture: LiveAudioCapture

weak var delegate: CaptureManagerDelegate?

// TODO: remove preview from CaptureManager
public var preview: UIView? {
get {
videoCapture.preview
}

public var mute: Bool = false {
didSet {
audioCapture.muted = mute
}
set {
videoCapture.preview = newValue
}

public init(audioConfiguration: LiveAudioConfiguration, videoConfiguration: LiveVideoConfiguration) {
self.audioConfiguration = audioConfiguration
self.videoConfiguration = videoConfiguration

videoCapture = LiveVideoCapture(videoConfiguration: videoConfiguration)
audioCapture = LiveAudioCapture(configuration: audioConfiguration)

super.init()

videoCapture.delegate = self
audioCapture.delegate = self
}

public var mute: Bool = false {
didSet {
audioCapture.muted = mute
}

public func startCapturing() {
videoCapture.running = true
audioCapture.running = true
}

public var captureDevicePositionFront: Bool = true {
didSet {
if videoCapture.captureDevicePosition == .front {
videoCapture.captureDevicePosition = .back
} else {
videoCapture.captureDevicePosition = .front
}
}
}

public func stopCapturing() {
videoCapture.running = false
audioCapture.running = false
}

public init(audioConfiguration: LiveAudioConfiguration, videoConfiguration: LiveVideoConfiguration) {
self.audioConfiguration = audioConfiguration
self.videoConfiguration = videoConfiguration

videoCapture = LiveVideoCapture(videoConfiguration: videoConfiguration)
audioCapture = LiveAudioCapture(configuration: audioConfiguration)

super.init()

videoCapture.delegate = self
audioCapture.delegate = self
}

public func startCapturing() {
videoCapture.running = true
audioCapture.running = true
}

public func stopCapturing() {
videoCapture.running = false
audioCapture.running = false
}
}

extension CaptureManager: AudioCaptureDelegate, VideoCaptureDelegate {
func captureOutput(capture: LiveAudioCapture, sampleBuffer: CMSampleBuffer) {
delegate?.captureOutput(captureManager: self, audio: sampleBuffer)
}

func captureOutput(capture: LiveVideoCapture, video sampleBuffer: CMSampleBuffer) {
delegate?.captureOutput(captureManager: self, video: sampleBuffer)
}
func captureOutput(capture: LiveAudioCapture, sampleBuffer: CMSampleBuffer) {
delegate?.captureOutput(captureManager: self, audio: sampleBuffer)
}
func captureOutput(capture: LiveVideoCapture, video sampleBuffer: CMSampleBuffer) {
delegate?.captureOutput(captureManager: self, video: sampleBuffer)
}
}
Loading
Loading