Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ktoraskartwilio/13d02bba5f25c4a125eea1391dbece1f to your computer and use it in GitHub Desktop.
Save ktoraskartwilio/13d02bba5f25c4a125eea1391dbece1f to your computer and use it in GitHub Desktop.
ManageLocalVideoTransmission
//
// ViewController.swift
// VideoQuickStart
//
// Copyright © 2016-2017 Twilio, Inc. All rights reserved.
//
import UIKit
import TwilioVideo
class ViewController: UIViewController {
// Configure access token manually for testing, if desired! Create one manually in the console
// at https://www.twilio.com/user/account/video/dev-tools/testing-tools
var accessToken = "TWILIO_ACCESS_TOKEN"
// Configure remote URL to fetch token from
var tokenUrl = "TOKEN_URL"
// Video SDK components
var room: TVIRoom?
var localMedia: TVILocalMedia?
var camera: TVICameraCapturer?
var localVideoTrack: TVILocalVideoTrack?
var localAudioTrack: TVILocalAudioTrack?
var participant: TVIParticipant?
// MARK: UI Element Outlets and handles
@IBOutlet weak var remoteView: UIView!
@IBOutlet weak var previewView: UIView!
@IBOutlet weak var connectButton: UIButton!
@IBOutlet weak var disconnectButton: UIButton!
@IBOutlet weak var messageLabel: UILabel!
@IBOutlet weak var roomTextField: UITextField!
@IBOutlet weak var roomLine: UIView!
@IBOutlet weak var roomLabel: UILabel!
@IBOutlet weak var toggleVideoTransmissionButton: UIButton!
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
// LocalMedia represents the collection of tracks that we are sending to other Participants from our VideoClient.
localMedia = TVILocalMedia()
// Disconnect button will be displayed when the Client is connected to a Room.
self.disconnectButton.isHidden = true
self.roomTextField.delegate = self
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard))
self.view.addGestureRecognizer(tap)
}
// MARK: IBActions
@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
}
}
// Preparing the connect options with the access token that we fetched (or hardcoded).
let connectOptions = TVIConnectOptions.init(token: accessToken) { (builder) in
// Use the local media that we prepared earlier.
builder.localMedia = self.localMedia
// 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.roomTextField.text
}
// Connect to the Room using the options we provided.
room = TVIVideoClient.connect(with: connectOptions, delegate: self)
logMessage(messageText: "Attempting to connect to room \(self.roomTextField.text)")
}
@IBAction func toggleVideoTransmission(sender: AnyObject) {
logMessage(messageText: "Toggling transmission \(room!.name)")
self.localVideoTrack?.isEnabled = !(self.localVideoTrack?.isEnabled)!;
}
// MARK: Private
func startPreview() {
if PlatformUtils.isSimulator {
return
}
// Preview our local camera track in the local video preview view.
camera = TVICameraCapturer(source: .frontCamera, delegate: self)
localVideoTrack = localMedia?.addVideoTrack(true, capturer: camera!)
if (localVideoTrack == nil) {
logMessage(messageText: "Failed to add video track")
}
// We will flip camera on tap.
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.flipCamera))
self.previewView.addGestureRecognizer(tap)
}
}
}
extension ViewController : TVICameraCapturerDelegate {
func cameraCapturerPreviewDidStart(_ capturer: TVICameraCapturer) {
self.camera?.previewView?.frame = self.previewView.bounds;
self.previewView.addSubview((self.camera?.previewView)!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment