两者都连接到同一个房间时如何显示对手视频?

时间:2020-07-23 07:03:20

标签: swift twilio-video

我已经在我的项目中集成了TwilioVideo功能。当两个用户都通过移动设备和网络连接到同一个房间时。在移动设备上显示的网络用户视频无需从网络上接受。

  var tokenUrl = "http://localhost:3000/token.php"

// Video SDK components
var room: Room?
var camera: CameraSource?
var localVideoTrack: LocalVideoTrack?
var localAudioTrack: LocalAudioTrack?
var remoteParticipant: RemoteParticipant?
var remoteView: VideoView?
var roomName = String()
var accessToken = String()

@IBAction func connect(sender: AnyObject) {
    // Configure access token either from server or manually.
    // If the default wasn't changed, try fetching from server.
    if (accessToken == "TWILIO_ACCESS_TOKEN") {
        do {
            accessToken = try TokenUtils.fetchToken(url: tokenUrl)
        } catch {
            let message = "Failed to fetch access token"
            logMessage(messageText: message)
            return
        }
    }
    
    // Prepare local media which we will share with Room Participants.
    self.prepareLocalMedia()
    
    // Preparing the connect options with the access token that we fetched (or hardcoded).
    let connectOptions = ConnectOptions(token: accessToken) { (builder) in
        
        // Use the local media that we prepared earlier.
        builder.audioTracks = self.localAudioTrack != nil ? [self.localAudioTrack!] : [LocalAudioTrack]()
        builder.videoTracks = self.localVideoTrack != nil ? [self.localVideoTrack!] : [LocalVideoTrack]()
        
        // Use the preferred audio codec
        if let preferredAudioCodec = Settings.shared.audioCodec {
            builder.preferredAudioCodecs = [preferredAudioCodec]
        }
        
        // Use the preferred video codec
        if let preferredVideoCodec = Settings.shared.videoCodec {
            builder.preferredVideoCodecs = [preferredVideoCodec]
        }
        
        // Use the preferred encoding parameters
        if let encodingParameters = Settings.shared.getEncodingParameters() {
            builder.encodingParameters = encodingParameters
        }

        // Use the preferred signaling region
        if let signalingRegion = Settings.shared.signalingRegion {
            builder.region = signalingRegion
        }
        
        // The name of the Room where the Client will attempt to connect to. Please note that if you pass an empty
        // Room `name`, the Client will create one for you. You can get the name or sid from any connected Room.
        builder.roomName = self.roomName
    }
    
    // Connect to the Room using the options we provided.
    room = TwilioVideoSDK.connect(options: connectOptions, delegate: self)
    
    logMessage(messageText: "Attempting to connect to room \(String(describing: self.roomName))")
    
   
   
}


 func prepareLocalMedia() {

    // We will share local audio and video when we connect to the Room.

    // Create an audio track.
    if (localAudioTrack == nil) {
        localAudioTrack = LocalAudioTrack(options: nil, enabled: true, name: "Microphone")

        if (localAudioTrack == nil) {
            logMessage(messageText: "Failed to create audio track")
        }
    }

    // Create a video track which captures from the camera.
    if (localVideoTrack == nil) {
        self.startPreview()
    }
 }

// Update our UI based upon if we are in a Room or not
func showRoomUI(inRoom: Bool)
{
    self.micButton.isHidden = !inRoom
    self.disconnectButton.isHidden = !inRoom
    self.navigationController?.setNavigationBarHidden(inRoom, animated: true)
    UIApplication.shared.isIdleTimerDisabled = inRoom

    // Show / hide the automatic home indicator on modern iPhones.
    self.setNeedsUpdateOfHomeIndicatorAutoHidden()
}



func logMessage(messageText: String) {
    NSLog(messageText)
    messageLabel.text = messageText
}

func renderRemoteParticipant(participant : RemoteParticipant) -> Bool {
    // This example renders the first subscribed RemoteVideoTrack from the RemoteParticipant.
    let videoPublications = participant.remoteVideoTracks
    for publication in videoPublications {
        if let subscribedVideoTrack = publication.remoteTrack,
            publication.isTrackSubscribed {
           setupRemoteVideoView()
            self.showRoomUI(inRoom: true)
            subscribedVideoTrack.addRenderer(self.remoteView!)
            self.remoteParticipant = participant
            return true
        }
    }
    return false
}

func renderRemoteParticipants(participants : Array<RemoteParticipant>) {
    for participant in participants {
        // Find the first renderable track.
        if participant.remoteVideoTracks.count > 0,
            renderRemoteParticipant(participant: participant) {
            break
        }
    }
}

func setupRemoteVideoView() {
    // Creating `VideoView` programmatically
    self.remoteView = VideoView(frame: CGRect.zero, delegate: self)

    self.view.insertSubview(self.remoteView!, at: 0)
    
    // `VideoView` supports scaleToFill, scaleAspectFill and scaleAspectFit
    // scaleAspectFit is the default mode when you create `VideoView` programmatically.
    self.remoteView!.contentMode = .scaleAspectFill;

    let centerX = NSLayoutConstraint(item: self.remoteView!,
                                     attribute: NSLayoutConstraint.Attribute.centerX,
                                     relatedBy: NSLayoutConstraint.Relation.equal,
                                     toItem: self.view,
                                     attribute: NSLayoutConstraint.Attribute.centerX,
                                     multiplier: 1,
                                     constant: 0);
    self.view.addConstraint(centerX)
    let centerY = NSLayoutConstraint(item: self.remoteView!,
                                     attribute: NSLayoutConstraint.Attribute.centerY,
                                     relatedBy: NSLayoutConstraint.Relation.equal,
                                     toItem: self.view,
                                     attribute: NSLayoutConstraint.Attribute.centerY,
                                     multiplier: 1,
                                     constant: 0);
    self.view.addConstraint(centerY)
    let width = NSLayoutConstraint(item: self.remoteView!,
                                   attribute: NSLayoutConstraint.Attribute.width,
                                   relatedBy: NSLayoutConstraint.Relation.equal,
                                   toItem: self.view,
                                   attribute: NSLayoutConstraint.Attribute.width,
                                   multiplier: 1,
                                   constant: 0);
    self.view.addConstraint(width)
    let height = NSLayoutConstraint(item: self.remoteView!,
                                    attribute: NSLayoutConstraint.Attribute.height,
                                    relatedBy: NSLayoutConstraint.Relation.equal,
                                    toItem: self.view,
                                    attribute: NSLayoutConstraint.Attribute.height,
                                    multiplier: 1,
                                    constant: 0);
    self.view.addConstraint(height)
}

我不应该在哪里调用setupRemoteVideoView()和self.showRoomUI(inRoom:true)方法来显示对手接受的视频。

0 个答案:

没有答案