swift-金山云预览推流界面

//
//  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()
        
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值