//
// EXCStarLiveView.swift
// ExcellentLive
//
// Created by 高爽 on 2022/10/11.
//
import Foundation
import UIKit
import JKSwiftExtension
class EXCStarLiveView: UIView {
lazy var pusher:KSYGPUStreamerKit = KSYGPUStreamerKit.init(defaultCfg: ())
var timer: JKWeakTimer?
var isVideoConnect:Bool = false
let videoViewContainer:UIView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initStartLiveUI(){
self.backgroundColor("0xececec")
self.addSubview(videoViewContainer)
videoViewContainer.frame = self.bounds
videoViewContainer.backgroundColor = .clear
let buttonCloclse = UIButton().then { make in
make.backgroundColor(.red)
make.addTo(self)
make.snp.makeConstraints { make in
make.size.equalTo(CGSize.init(width: 100, height: 50))
make.top.equalTo(20)
make.right.equalTo(0)
}
make.addActionWithBlock { make in
self.closeLive()
}
}
let buttonStart = UIButton().then { make in
make.backgroundColor(.red)
make.addTo(self)
make.snp.makeConstraints { make in
make.size.equalTo(CGSize.init(width: 100, height: 50))
make.top.equalTo(100)
make.centerX.equalToSuperview()
}
make.addActionWithBlock { make in
self.startLive()
}
}
self.setCapturecfg()
self.startPreview()
}
func setCapturecfg(){
pusher.videoOrientation = UIApplication.shared.statusBarOrientation
pusher.videoFPS = 20;
pusher.gpuOutputPixelFormat = kCVPixelFormatType_32BGRA;
pusher.capturePixelFormat = kCVPixelFormatType_32BGRA;
pusher.previewDimension = CGSize.init(width: jk_kScreenW, height: jk_kScreenH);
pusher.streamDimension = CGSize.init(width: jk_kScreenW, height: jk_kScreenH);
pusher.cameraPosition = .front;
pusher.streamerBase.videoCodec = .VT264
pusher.maxAutoRetry = 2;
pusher.streamerBase.videoInitBitrate = 800;
pusher.streamerBase.videoMaxBitrate = 800;
pusher.streamerBase.videoMinBitrate = 800;
pusher.aCapDev.micVolume = 1;
}
func startPreview(){
pusher.preview.fillMode = kGPUImageFillModePreserveAspectRatioAndFill
pusher.startPreview(videoViewContainer)
pusher.videoProcessingCallback = {[weak self] sampleBuffer in
if sampleBuffer == nil{
return
}
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer!)
let outputbuffer = pixelBuffer
// SenseManager.share().renderItems(to: pixelBuffer)
self?.pusher.capToGpu.processPixelBuffer(outputbuffer, time: CMSampleBufferGetPresentationTimeStamp(sampleBuffer!))
}
}
func setSwitchLiveCamera(){
pusher.switchCamera()
}
func setliveMute(volume:Float32){
pusher.aCapDev.micVolume = volume
}
@objc func reconnectWithUrl(url:String){
isVideoConnect = true
pusher.streamerBase.stopStream()
timer = JKWeakTimer.scheduledTimer(timeInterval: 0.2, repeats: false, queue: DispatchQueue.main) { [weak self] timer in
timer.invalidate()
self?.pusher.streamerBase.startStream(URL.init(string: url))
}
}
func startLive(){
self.removeFromSuperview()
EXCLiveRoomManger.shared.liveViewController?.insertSubview(self, at: 0)
var infoDic:[String:Any] = [:]
infoDic["anchorid"] = EXCLiveRoomManger.shared.roomUser.anchorInfo.idInt
infoDic["anchorname"] = EXCLiveRoomManger.shared.roomUser.anchorInfo.name.jk.urlEncoded
infoDic["anchortitle"] = "anchortitleanchortitle"
infoDic["positon"] = "positon"
infoDic["token"] = EXCLiveRoomManger.shared.roomUser.anchorInfo.token
infoDic["photonum"] = EXCLiveRoomManger.shared.roomUser.anchorInfo.photo
EXCAPI.Live.api_LIVE_APPLY_CREATEwith(type: .roomNorWatch, infoDict: infoDic) { isSuccess, obj in
if isSuccess{
EXCSocketService.shared().roomLogout()
EXCSocketService.shared().liveShowOwnerLogin("", ipInfo: obj as! [AnyHashable : Any], proxyInfo: ["ip":EXCLiveRoomManger.shared.roomInfo.proxyIP as Any,"port":EXCLiveRoomManger.shared.roomInfo.proxyPort as Any], languageCode: EXCAPI.Util.getLanguageCode(), countryCode: EXCAPI.Util.getContryCode(), languageID: EXCAPI.Util.getLanguageCode())
}else{
HUD.showText(EXCAPI.Util.getErrorMsg())
}
}
}
func closeLive(){
if isVideoConnect == true{
pusher.stopPreview()
}
pusher.stopPreview()
self.subviews.forEach { $0.removeFromSuperview() }
NotificationCenter.default.removeObserver(self)
EXCLiveRoomManger.shared.quitLive()
}
}
swift-金山云预览推流界面
最新推荐文章于 2024-11-05 17:34:31 发布