|
| 1 | +import AVFoundation |
| 2 | +import Foundation |
| 3 | +import HaishinKit |
| 4 | +import UIKit |
| 5 | + |
| 6 | +@objc(BroadcastVideoView) |
| 7 | +public class BroadcastVideoView: UIView { |
| 8 | + |
| 9 | + private var hkView: MTHKView? |
| 10 | + private var isAttached = false |
| 11 | + |
| 12 | + public override init(frame: CGRect) { |
| 13 | + super.init(frame: frame) |
| 14 | + setupView() |
| 15 | + } |
| 16 | + |
| 17 | + required init?(coder: NSCoder) { |
| 18 | + super.init(coder: coder) |
| 19 | + setupView() |
| 20 | + } |
| 21 | + |
| 22 | + private func setupView() { |
| 23 | + backgroundColor = .black |
| 24 | + |
| 25 | + // Create HKView for displaying HaishinKit video |
| 26 | + let hkView = MTHKView(frame: bounds) |
| 27 | + hkView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 28 | + hkView.videoGravity = .resizeAspectFill |
| 29 | + addSubview(hkView) |
| 30 | + self.hkView = hkView |
| 31 | + |
| 32 | + print("[BroadcastVideoView] View initialized") |
| 33 | + |
| 34 | + // Try to attach mixer if available |
| 35 | + tryAttachMixer() |
| 36 | + } |
| 37 | + |
| 38 | + private func tryAttachMixer() { |
| 39 | + guard !isAttached, let mixer = BroadcastManager.shared.mixer else { |
| 40 | + // Schedule retry if mixer not ready yet |
| 41 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in |
| 42 | + self?.tryAttachMixer() |
| 43 | + } |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + Task { @MainActor in |
| 48 | + guard let hkView = self.hkView else { return } |
| 49 | +// await hkView.attachStream(mixer) |
| 50 | + await mixer.addOutput(hkView) |
| 51 | + self.isAttached = true |
| 52 | + print("[BroadcastVideoView] Mixer attached to view") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public override func layoutSubviews() { |
| 57 | + super.layoutSubviews() |
| 58 | + hkView?.frame = bounds |
| 59 | + } |
| 60 | + |
| 61 | + deinit { |
| 62 | + print("[BroadcastVideoView] View deinitialized") |
| 63 | + } |
| 64 | +} |
| 65 | + |
0 commit comments