iOS
Overview
Welcome to our comprehensive guide designed to assist developers in migrating from Twilio Video to Tencent RTC (TRTC) on the iOS platform. This document provides a detailed comparison at the API level between Twilio and TRTC, and outlines each step required to implement basic RTC video functionality.
For those planning to develop a new application, you can directly visit the Tencent RTC website and documentation for all the necessary resources and information.
Create a TRTC Application
Create a new application of Tencent RTC (TRTC), then you can get the following necessary information for the authentication of TRTC service.
SDKAppID
SDKAppID is used to uniquely identify a TRTC application. It is generated automatically when an application is created.
SDKSecretKey
SDKSecretKey is one of the key parameters to be used to generate the security signature. The generated signature ensures the access of TRTC service when calling the initialization and login API of the SDK.
Install and Set Up the SDK
Environment Requirements
Xcode 9.0 or later
iPhone or iPad with iOS 9.0 or later
A valid developer signature for your project
Install the SDK
You can either use CocoaPods or manually download and import the SDK into your project. For using CocoaPods, make sure the CocoaPods is installed and the Podfile is created by the following commands:
sudo gem install cocoapods pod init
Edit the created Podfile and fill a TRTC version according to your project needs:
pod 'TXLiteAVSDK_TRTC', 'version'
Then update the local repository and install the SDK by the following commands:
pod install pod update
Project Configuration
To use the audio/video features of the SDK, you need to grant the application mic and camera permissions. Add the two items(Privacy - Microphone Usage Description and Privacy - Camera Usage Description) to the Info.plist of your Xcode application.
Import the SDK
After completing the above steps, you can import the APIs provided by the TRTC SDK into your project by the following ways based on your needs.
1. Using Objective-C or Swift APIs
// Import the SDK module @import TXLiteAVSDK_TRTC;
// Import the header file #import "TXLiteAVSDK_TRTC/TRTCCloud.h"
2. Using C++ APIs
#include "TXLiteAVSDK_TRTC/cpp_interface/ITRTCCloud.h"
Join a Room
@IBAction func createARoom(sender: AnyObject) { let connectOptions = ConnectOptions(token: accessToken) { (builder) in builder.roomName = "my-room" } room = TwilioVideoSDK.connect(options: connectOptions, delegate: self) } // MARK: RoomDelegate func roomDidConnect(room: Room) { print("Did connect to Room") if let localParticipant = room.localParticipant { print("Local identity \(localParticipant.identity)") // Set the delegate of the local particiant to receive callbacks localParticipant.delegate = self } }
// Create trtc instance(singleton) and set up event listeners self.trtcCloud = [TRTCCloud sharedInstance]; self.trtcCloud.delegate = self; // Package the room entry parameter // Please replace each field in TRTCParams with your own parameters TRTCParams *params = [[TRTCParams alloc] init]; params.sdkAppId = 1400000123; // Please replace with your own SDKAppID params.roomId = 123321; // Please replace with your own room number params.userId = @"denny"; // Please replace with your own userid params.userSig = @"xxx"; // Please replace with your own userSig params.role = TRTCRoleAnchor; // If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE" [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE]; // Listen to the onEnterRoom event of the SDK and get notification whether the room is successfully entered - (void)onEnterRoom:(NSInteger)result { if (result > 0) { [self toastTip:@"Enter room succeed!"]; } else { [self toastTip:@"Enter room failed!"]; } }
Publish Local Video/Audio
T// start camera var cameraSource = CameraSource(delegate: self), let captureDevice = CameraSource.captureDevice(position: .front) cameraSource.startCapture(device: captureDevice, completion: nil) // Use CameraSource to produce video from the device's front camera. if let camera = TVICameraCapturer(source: .frontCamera), let videoTrack = TVILocalVideoTrack(capturer: camera) { // TVIVideoView is a TVIVideoRenderer and can be added to any TVIVideoTrack. let renderer = TVIVideoView(frame: view.bounds) // Add renderer to the video track videoTrack.addRenderer(renderer) self.localVideoTrack = videoTrack self.camera = camera self.view.addSubview(renderer) }
self.trtcCloud = [TRTCCloud sharedInstance]; // Set the preview mode of the local video image: Enable horizontal mirroring and set the fill mode for the video image TRTCRenderParams *param = [[TRTCRenderParams alloc] init]; param.fillMode = TRTCVideoFillMode_Fill; param.mirrorType = TRTCVideoMirrorTypeAuto; [self.trtcCloud setLocalRenderParams:param]; // Enable local camera preview(`localCameraVideoView` is used to render the local video image) [self.trtcCloud startLocalPreview:YES view:localCameraVideoView]; // Enable mic and set `quality` to `SPEECH` for the voice mode [self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech]; [self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
Subscribe Remote Video/Audio
// MARK: RemoteParticipantDelegate /* * In the Participant Delegate, we can respond when the Participant adds a Video * Track by rendering it on screen. */ func didSubscribeToVideoTrack(videoTrack: RemoteVideoTrack, publication: RemoteVideoTrackPublication, participant: RemoteParticipant) { if let remoteView = VideoView.init(frame: self.view.bounds, delegate:self) { videoTrack.addRenderer(remoteView) self.view.addSubview(remoteView) self.remoteView = remoteView } } // MARK: VideoViewDelegate // Lastly, we can subscribe to important events on the VideoView func videoViewDimensionsDidChange(view: VideoView, dimensions: CMVideoDimensions) { self.view.setNeedsLayout() }
self.trtcCloud = [TRTCCloud sharedInstance]; // Play back the camera (primary stream) image of `denny` [self.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
Leave a Room
//TodisconnectfromaRoom,wecall: room?.disconnect() //ThisresultsinacallbacktoRoomDelegate#roomDidDisconnect(room:Room,error:Error?) //MARK:RoomDelegate funcroomDidDisconnect(room:Room,error:Error?){ print("Disconnectedfromroom\(room.name)") }
self.trtcCloud = [TRTCCloud sharedInstance]; // Exit the current room [self.trtcCloud exitRoom]; // Listen the `onExitRoom` callback to get notification - (void)onExitRoom:(NSInteger)reason { if (reason == 0) { NSLog(@"Exit current room by calling the 'exitRoom' api of sdk ..."); } else if (reason == 1) { NSLog(@"Kicked out of the current room by server through the restful api..."); } else if (reason == 2) { NSLog(@"Current room is dissolved by server through the restful api..."); } }
Conclusion
Compared to Twilio Video, this migration guide outlines how to use Tencent RTC (TRTC) to build a basic video RTC experience for the iOS platform. The API level "mapping" for each step in this guide will facilitate the developers to switch from Twilio Video to TRTC in a quick and straightforward way.
TRTC has more feature sets that can help developers implement cost-effective, low-latency and high-quality interactive audio/video services. For comprehensive details on TRTC features and the implementation specification, please refer to the Tencent RTC website. If you require developer support or any further assistance regarding the TRTC integration, feel free to contact us, or you can join our Discord and Telegram. We will ensure a smooth integration and address any queries you may have.