SHARE
TWEET

Untitled

a guest Apr 5th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2. import ARKit
  3. import AgoraRtcEngineKit
  4.  
  5.  
  6. class ViewController: UIViewController {
  7.    
  8.     @IBOutlet weak var stackView: UIStackView!
  9.    
  10.     var agoraKit: AgoraRtcEngineKit!
  11.    
  12.     var tags: [Int] = (1...100).map { $0 }
  13.  
  14.     var sceneView: ARSCNView?
  15.    
  16.     let noseOptions = ["👃", "🐽", "💧", " "]
  17.    
  18.     var isARSupported: Bool {
  19.         return ARFaceTrackingConfiguration.isSupported
  20.     }
  21.  
  22.     override func viewDidLoad() {
  23.         super.viewDidLoad()
  24.        
  25.         initializeAgoraEngine()
  26.         setupVideo()
  27.         setChannelProfile()
  28.     }
  29.    
  30.     override func viewDidAppear(_ animated: Bool) {
  31.         super.viewDidAppear(animated)
  32.         showJoinAlert()
  33.     }
  34.  
  35.     func showJoinAlert() {
  36.         let alertController = UIAlertController(title: nil, message: "Ready to join channel.", preferredStyle: .alert)
  37.  
  38.         let action = UIAlertAction(title: "Join", style: .destructive) { (action:UIAlertAction) in
  39.             self.joinChannel()
  40.         }
  41.         alertController.addAction(action)
  42.         present(alertController, animated: true, completion: nil)
  43.     }
  44.  
  45.     func initializeAgoraEngine() {
  46.         agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: AppID, delegate: self)
  47.     }
  48.  
  49.     func setChannelProfile() {
  50.         agoraKit.setChannelProfile(.communication)
  51.     }
  52.    
  53.     func setupVideo() {
  54.         agoraKit.enableVideo()
  55.         agoraKit.setVideoEncoderConfiguration(
  56.             AgoraVideoEncoderConfiguration(size: AgoraVideoDimension640x360,
  57.                                            frameRate: .fps15,
  58.                                            bitrate: AgoraVideoBitrateStandard,
  59.                                            orientationMode: .adaptative)
  60.         )
  61.     }
  62.    
  63.     func joinChannel() {
  64.         agoraKit.setDefaultAudioRouteToSpeakerphone(true)
  65.        
  66.         guard let uid = tags.randomElement() else { return }
  67.        
  68.         agoraKit.joinChannel(byToken: nil,
  69.                              channelId: "DemoChannel",
  70.                              info: nil,
  71.                              uid: UInt(uid)) { [weak self] (sid, uid, elapsed) -> Void in
  72.                                
  73.             guard let _self = self else { return }
  74.             DispatchQueue.main.async {
  75.                 _self.setupLocalVideo(uid: uid)
  76.             }
  77.  
  78.         }
  79.         UIApplication.shared.isIdleTimerDisabled = true
  80.     }
  81.  
  82.     func setupLocalVideo(uid: UInt) {
  83.        
  84.        
  85.         let videoView = ARSCNView()
  86.         videoView.tag = Int(uid)
  87.         videoView.backgroundColor = UIColor.orange
  88.        
  89. //        let videoCanvas = AgoraRtcVideoCanvas()
  90. //        videoCanvas.uid = uid
  91. //        videoCanvas.view = videoView
  92. //        videoCanvas.renderMode = .hidden
  93. //        agoraKit.setupLocalVideo(videoCanvas)
  94.  
  95.         self.sceneView = videoView
  96.         self.sceneView?.delegate = self
  97.        
  98.         if isARSupported {
  99.             let configuration = ARFaceTrackingConfiguration()
  100.             sceneView?.session.run(configuration)
  101.         }
  102.        
  103.         let tapGuesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
  104.         tapGuesture.numberOfTapsRequired = 1
  105.         self.sceneView?.addGestureRecognizer(tapGuesture)
  106.  
  107.         stackView.addArrangedSubview(videoView)
  108.     }
  109.  
  110.     @IBAction func didClickHangUpButton(_ sender: UIButton) {
  111.         leaveChannel()
  112.     }
  113.    
  114.     func leaveChannel() {
  115.         agoraKit.leaveChannel(nil)
  116.         UIApplication.shared.isIdleTimerDisabled = false
  117.     }
  118.    
  119.     @IBAction func didClickMuteButton(_ sender: UIButton) {
  120.         sender.isSelected = !sender.isSelected
  121.         agoraKit.muteLocalAudioStream(sender.isSelected)
  122.     }
  123.    
  124.     func updateFeatures(for node: SCNNode, using anchor: ARFaceAnchor) {
  125.        
  126.         let child = node.childNode(withName: "nose", recursively: false) as? EmojiNode
  127.        
  128.         let vertices = [anchor.geometry.vertices[9]]
  129.        
  130.         child?.updatePosition(for: vertices)
  131.     }
  132.    
  133.     @IBAction func handleTap(_ sender: UITapGestureRecognizer) {
  134.         let location = sender.location(in: sceneView)
  135.         let results = sceneView?.hitTest(location, options: nil)
  136.         if let result = results?.first,
  137.             let node = result.node as? EmojiNode {
  138.             node.next()
  139.         }
  140.     }
  141. }
  142.  
  143. extension ViewController: AgoraRtcEngineDelegate {
  144.    
  145.     func rtcEngine(_ engine: AgoraRtcEngineKit, firstRemoteVideoDecodedOfUid uid:UInt, size:CGSize, elapsed:Int) {
  146.  
  147.         let videoView = UIView()
  148.         videoView.tag = Int(uid)
  149.         videoView.backgroundColor = UIColor.purple
  150.         stackView.addArrangedSubview(videoView)
  151.  
  152.         let videoCanvas = AgoraRtcVideoCanvas()
  153.         videoCanvas.uid = uid
  154.         videoCanvas.view = videoView
  155.         videoCanvas.renderMode = .hidden
  156.         agoraKit.setupRemoteVideo(videoCanvas)
  157.     }
  158.    
  159.     internal func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid:UInt, reason:AgoraUserOfflineReason) {
  160.  
  161.         guard let view = stackView.arrangedSubviews.first(where: { (view) -> Bool in
  162.             return view.tag == Int(uid)
  163.         }) else { return }
  164.        
  165.         stackView.removeArrangedSubview(view)
  166.     }
  167.    
  168.     func rtcEngine(_ engine: AgoraRtcEngineKit, didVideoMuted muted:Bool, byUid:UInt) {
  169. //        remoteVideoMutedIndicator.isHidden = !muted
  170.     }
  171. }
  172.  
  173. extension ViewController: ARSCNViewDelegate {
  174.    
  175.     func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
  176.        
  177.         guard let faceAnchor = anchor as? ARFaceAnchor,
  178.             let device = sceneView?.device else { return nil }
  179.        
  180.         let faceGeometry = ARSCNFaceGeometry(device: device)
  181.        
  182.         let node = SCNNode(geometry: faceGeometry)
  183.        
  184.         node.geometry?.firstMaterial?.fillMode = .lines
  185.        
  186.         node.geometry?.firstMaterial?.transparency = 0.0
  187.         let noseNode = EmojiNode(with: noseOptions)
  188.         noseNode.name = "nose"
  189.         node.addChildNode(noseNode)
  190.        
  191.         updateFeatures(for: node, using: faceAnchor)
  192.         return node
  193.     }
  194.    
  195.     func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
  196.        
  197.         guard let faceAnchor = anchor as? ARFaceAnchor,
  198.             let faceGeometry = node.geometry as? ARSCNFaceGeometry else {
  199.                 return
  200.         }
  201.        
  202.         faceGeometry.update(from: faceAnchor.geometry)
  203.     }
  204. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top