Browse Source

跑通ios推流和预览

Melon 1 year ago
parent
commit
61e293d630

+ 1 - 0
ios/Classes/DaniulivePlugin.swift

@@ -55,6 +55,7 @@ public class DaniulivePlugin: NSObject, FlutterPlugin {
     }
     
     private func enterRoom(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
+        PublishManager.install()
         PlayManager.install()
         ManagerCenter.put(IPublishManager.self, PublishManager())
         result(nil)

+ 2 - 5
ios/Classes/managers/PlayEventHandler.swift

@@ -16,11 +16,8 @@ class PlayEventHandler: NSObject, SmartPlayerDelegate {
 //        print("PLAY_EVENT_ID: \(nID)")
         
         if nID == PLAY_EVENT_ID.EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO {
-            let width: Int = .init(param1)
-            let height: Int = .init(param2)
-            
-            playManager.width = width
-            playManager.height = height
+            let width = Int(param1)
+            let height = Int(param2)
             
             print("[event]视频解码分辨率信息: \(width)*\(height) .")
         }

+ 2 - 20
ios/Classes/managers/PlayManager.swift

@@ -17,8 +17,6 @@ public class PlayManager: IPlayManager {
     var smartPlayView: UnsafeMutableRawPointer??
     var externalHandler:IExternalYuvHandler?
     var isPlaying: Bool = false
-    var _width: Int = 0
-    var _height: Int = 0
     var ret: Int = 0
     
     init(_ url: String) {
@@ -40,24 +38,6 @@ public class PlayManager: IPlayManager {
         }
     }
     
-    public var width: Int {
-        get {
-            return _width
-        }
-        set(newValue) {
-            _width = newValue
-        }
-    }
-
-    public var height: Int {
-        get {
-            return _height
-        }
-        set(newValue) {
-            _height = newValue
-        }
-    }
-    
     public func setView(_ view: UIView?) {
         renderView = view
         renderViewMounted = false
@@ -190,6 +170,8 @@ public class PlayManager: IPlayManager {
     }
     
     private func doDispose() {
+        stop()
+        
         if smartPlayView != nil {
             SmartPlayerSDK.smartPlayeReleasePlayView(smartPlayView!)
             print("SmartPlayeReleasePlayView!")

+ 122 - 5
ios/Classes/managers/PublishManager.swift

@@ -6,8 +6,34 @@
 //
 
 public class PublishManager: IPublishManager {
+    public static var hasInstalled = false
+    
+    private let libPublisher: SmartPublisherSDK
+    private let videoStreamingQulity: DNVideoStreamingQuality = .init(1) // 固定为480P
     private var _profile: PublishProfile = .empty()
+    private var eventHandler: PublishEventHandler?
     private var _isPublishing: Bool = false
+    private var ret: Int = 0
+    private var renderView: UIView?
+    private var isVideoMute: Bool = false
+    
+    init() {
+        libPublisher = SmartPublisherSDK()
+        initPublisher()
+    }
+    
+    public static func install() {
+        if hasInstalled {
+            return
+        }
+        let ret = SmartPublisherSDK.smartPublisherSetSDKClientKey(DaniuConfig.CLIENT_ID, in_key: DaniuConfig.SECRET_KEY, reserve1: 0, reserve2: nil)
+        if ret == 0 {
+            hasInstalled = true
+            print("SmartPublisherSDK install success.")
+        } else {
+            print("SmartPublisherSDK install fail.")
+        }
+    }
     
     public var isPublishing: Bool {
         return _isPublishing
@@ -19,15 +45,106 @@ public class PublishManager: IPublishManager {
     
     public func setProfile(_ value: PublishProfile) {
         _profile = value
+        ret = libPublisher.smartPublisherSetFPS(value.frameRate)
+        
+        print("SmartPublisherSetFPS -> fps=\(value.frameRate) ret=\(ret)")
+    }
+    
+    public func setView(_ view: UIView) {
+        renderView = view
+        ret = libPublisher.smartPublisherSetVideoPreview(view)
+        
+        print("SmartPublisherSetVideoPreview ret=\(ret)")
+    }
+    
+    public func start() {
+        if isPublishing {
+            return
+        }
+        
+        ret = libPublisher.smartPublisherStartCapture(videoStreamingQulity)
+        ret = libPublisher.smartPublisherStartPublisher(_profile.url)
+        
+        print("SmartPublisherStartPublisher ret=\(ret)")
+        
+        _isPublishing = true
     }
     
-    public func start() {}
+    public func stop() {
+        if !isPublishing {
+            return
+        }
+        
+        ret = libPublisher.smartPublisherStopPublisher()
+        ret = libPublisher.smartPublisherStopCaputure()
+        
+        print("SmartPublisherStopPublisher ret=\(ret)")
+        
+        _isPublishing = false
+    }
     
-    public func stop() {}
+    public func muteVideo(_ isMute: Bool) {
+        if !isPublishing {
+            return
+        }
+        if isMute {
+            if !isVideoMute {
+                ret = libPublisher.smartPublisherStopCaputure()
+            }
+        } else {
+            if isVideoMute {
+                ret = libPublisher.smartPublisherStartCapture(videoStreamingQulity)
+            }
+        }
+        
+        print("SmartPublisherSetMute -> isMute=\(isMute) ret=\(ret)")
+    }
     
-    public func muteVideo(_ isMute: Bool) {}
+    public func muteAudio(_ isMute: Bool) {
+        if !isPublishing {
+            return
+        }
+        ret = libPublisher.smartPublisherSetMute(isMute)
+        print("SmartPublisherSetMute -> isMute=\(isMute) ret=\(ret)")
+    }
     
-    public func muteAudio(_ isMute: Bool) {}
+    public func setVolume(_ volume: Int) {
+        if !isPublishing {
+            return
+        }
+        
+        print("SmartPublishSetInputAudioVolume -> volume=\(volume) [Unsupport!]")
+    }
     
-    public func setVolume(_ volume: Int) {}
+    private func initPublisher() {
+        ret = libPublisher.smartPublisherInit(1, video_opt: 1)
+        eventHandler = PublishEventHandler()
+        libPublisher.delegate = eventHandler
+        
+        let orientation = _profile.isLandscape ? 2 : 1
+        ret = libPublisher.smartPublisherSetPublishOrientation(orientation)
+        
+        ret = libPublisher.smartPublisherSetVideoEncoderType(1, isHwEncoder: true) // H.264 硬编码
+        if ret == 0 {
+            print("Great, it support h.264 hardware encoder!")
+        }
+        ret = libPublisher.smartPublisherSetAudioEncoderType(1, isHwEncoder: true) // ACC 硬编码
+        if ret == 0 {
+            print("Great, it support acc hardware encoder!")
+        }
+        
+        ret = libPublisher.smartPublisherSetMirror(true) // 开启镜像
+    }
+    
+    deinit {
+        doDispose()
+    }
+    
+    private func doDispose() {
+        stop()
+        
+        libPublisher.smartPublisherSetVideoPreview(nil)
+        renderView = nil
+        libPublisher.smartPublisherUnInit()
+    }
 }

+ 8 - 1
ios/Classes/managers/interfaces/EventEnums.swift

@@ -10,5 +10,12 @@ public let EVENT_DANIULIVE_PLAYER_SDK: Int = 0x01000000
 public let EVENT_DANIULIVE_PUBLISHER_SDK: Int = 0x02000000
 
 public enum PLAY_EVENT_ID {
-    public static let EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO = EVENT_DANIULIVE_PLAYER_SDK | 0x7 /*视频解码分辨率信息*/
+    public static let EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO = EVENT_DANIULIVE_PLAYER_SDK | 0x7 /* 视频解码分辨率信息 */
 }
+
+//public enum DNVideoStreamingQuality {
+//    case DN_VIDEO_QUALITY_LOW //! < 视频分辨率:低清 352*288.
+//    case DN_VIDEO_QUALITY_MEDIUM //! < 视频分辨率:标清 640*480.
+//    case DN_VIDEO_QUALITY_HIGH //! < 视频分辨率:高清 1280*720.
+//    case DN_VIDEO_QUALITY_1080P //! < 视频分辨率:1920*1080.
+//}

+ 0 - 9
ios/Classes/managers/interfaces/IPlayManager.swift

@@ -7,15 +7,6 @@
 
 /// 拉流管理
 public protocol IPlayManager: IManager{
-    var width:Int{
-        get
-        set
-    }
-    var height:Int{
-        get
-        set
-    }
-    
     /// 开始拉流
     /// - Returns: void
     func play() -> Void

+ 2 - 0
ios/Classes/managers/interfaces/IPublishManager.swift

@@ -17,4 +17,6 @@ public protocol IPublishManager: IManager {
     func muteVideo(_ isMute: Bool)->Void
     func muteAudio(_ isMute: Bool)->Void
     func setVolume(_ volume: Int)->Void
+    
+    func setView(_ view: UIView)
 }

+ 3 - 1
ios/Classes/renders/PublishRenderView.swift

@@ -5,4 +5,6 @@
 //  Created by vinno on 2023/6/19.
 //
 
-import Foundation
+public class PublishRenderView : UIView{
+    
+}

+ 54 - 6
ios/Classes/views/PublishPlatformView.swift

@@ -22,17 +22,65 @@ public class PublishPlatformViewFactory: NSObject, FlutterPlatformViewFactory {
 }
 
 public class PublishPlatformView: NSObject, FlutterPlatformView {
-    private var _view: UIView
-    private var frame: CGRect
-    private var channel: FlutterMethodChannel?
+    private let renderView: UIView
+    private let frame: CGRect
+    private let channel: FlutterMethodChannel
+    private let publishManager: IPublishManager?
     
     init(_ frame: CGRect, _ messager: FlutterBinaryMessenger, _ viewId: Int64) {
         self.frame = frame
-        self._view = UIView()
-        self.channel = FlutterMethodChannel(name: "\(PlayPlatformViewFactory.SIGN)_\(viewId)", binaryMessenger: messager)
+        self.renderView = PublishRenderView()
+        self.channel = FlutterMethodChannel(name: "\(PublishPlatformViewFactory.SIGN)_\(viewId)", binaryMessenger: messager)
+        self.publishManager = ManagerCenter.get(IPublishManager.self)
+        super.init()
+        self.channel.setMethodCallHandler(handle)
     }
     
     public func view() -> UIView {
-        return _view
+        return renderView
+    }
+    
+    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
+        defer {
+            CommonUtils.logFlutterMethodCall(call)
+        }
+        
+        switch call.method {
+        case "mount":
+            self.mount(call, result)
+        case "muteVideo":
+            self.muteVideo(call, result)
+        case "muteAudio":
+            self.muteAudio(call, result)
+        case "setVolume":
+            self.setVolume(call, result)
+        default:
+            CommonUtils.logError(call: call, errCode: -1, errMsg: "method not Implemented")
+            result(FlutterMethodNotImplemented)
+        }
+    }
+
+    
+    private func mount(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
+        publishManager?.setView(renderView)
+        result(nil)
+    }
+    
+    private func muteVideo(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
+        let isMute = CommonUtils.getParamByKey(call, result, "isMute") as! Bool
+        publishManager?.muteVideo(isMute)
+        result(nil)
+    }
+    
+    private func muteAudio(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
+        let isMute = CommonUtils.getParamByKey(call, result, "isMute") as! Bool
+        publishManager?.muteAudio(isMute)
+        result(nil)
+    }
+    
+    private func setVolume(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
+        let volume = CommonUtils.getParamByKey(call, result, "volume") as! Int
+        publishManager?.setVolume(volume)
+        result(nil)
     }
 }

+ 3 - 0
ios/daniuliveSDK/include/SmartPublisherSDK.h

@@ -18,6 +18,7 @@
 //设置delegate
 @protocol SmartPublisherDelegate;
 
+NS_ASSUME_NONNULL_BEGIN
 /**
  * 美颜类型
  */
@@ -46,6 +47,8 @@ typedef enum DNCameraPosition{
     DN_CAMERA_POSITION_FRONT         //!< 前置摄像头.
 } DNCameraPosition;
 
+NS_ASSUME_NONNULL_END
+
 @interface SmartPublisherSDK : NSObject
 
 //Event callback