Integration
This tutorial mainly introduces how to implement a basic audio and video call.
Prerequisites
Xcode 9.0 or later.
A Mac computer with OS X 10.10 or later.
A valid developer signature for your project.
Step 1. Import TRTC SDK
1. Run the following command in the terminal window to install CocoaPods. If you have installed CocoaPods, skip this step.
sudo gem install cocoapods
2. After going to the TRTCDemo root directory, enter the following command to create the Podfile file for your project.
pod init
3. Edit the Podfile file as follows and change Your Target to your own project name.
platform :osx, '10.10'target 'Your Target' dopod 'TXLiteAVSDK_TRTC_Mac', :podspec => 'https://liteav.sdk.qcloud.com/pod/liteavsdkspec/TXLiteAVSDK_TRTC_Mac.podspec'end
4. Enter the following command to update the local library file and install the SDK.
pod install
Note:
After the pod command is executed, a project file with the .xcworkspace suffix integrated with the SDK is generated. Double-click the .xcworkspace file to open it.
Step 2. Configure project
1. After opening the .xcworkspace file, add TXLiteAVSDK_TRTC.xcframework and ScreenCaptureKit.framework to the Frameworks, Libraries, and Embedded Content section in General tab.
2. Search for User Script Sandboxing in Build Settings tab and set its value to No.
3. Add Privacy-Microphone Usage Description and Privacy-Microphone Usage Description to Info.plist tab, and fill in the target prompt words used by the Microphone/Camera to obtain the permissions to use the microphone and camera.
4. Check the following in the App Sandbox section of Signing & Capabilities.
Step 3. Create TRTC instance
1. Add a module reference to the SDK in the AppDelegate.h file:
@import TXLiteAVSDK_TRTC_Mac;
2. Add the following properties to the AppDelegate.h file and declare the
toastTip:
method.#import <Cocoa/Cocoa.h>@import TXLiteAVSDK_TRTC_Mac;@interface AppDelegate : NSObject <NSApplicationDelegate>@property (nonatomic, strong) NSWindow *window; // Add window property@property (nonatomic, strong) TRTCCloud *trtcCloud; // Add trtcCloud property@property (nonatomic, strong) NSView *localCameraVideoView; // Add localCameraVideoView property- (void)toastTip:(NSString *)tip; // Declare the toastTip: method@end
3. Implement the
toastTip:
method in the AppDelegate.m file.// Implement toastTip: method- (void)toastTip:(NSString *)tip {NSAlert *alert = [[NSAlert alloc] init];[alert setMessageText:tip];[alert runModal];}
4. Call the create TRTC instance initialization interface in the
didFinishLaunchingWithOptions()
method , and set up the event callbacks .- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// Create trtc instance(singleton) and set up event listeners_trtcCloud = [TRTCCloud sharedInstance];_trtcCloud.delegate = self;return YES;}// Listen to the "onError" event, and print logs for errors such as "Camera is not authorized"- (void)onError:(TXLiteAVError)errCodeerrMsg:(nullable NSString *)errMsgextInfo:(nullable NSDictionary *)extInfo{if (ERR_CAMERA_NOT_AUTHORIZED == errCode) {NSString *errorInfo = @"Current application is not authorized to use the camera:";errorInfo = [errorInfo stringByAppendingString : errMsg];[self toastTip:errorInfo];}}
Step 4. Enter the room
1. Click
Create Application
in the Tencent RTC console to get the SDKAppID under Application Overview.
2. Select SDKAppID down in the UserSig Tools, enter your UserID, and click
Generate
to get your own UserSig.
3. After setting the TRTCParams for room entry, call the
enterRoom
to enter the room.As an Anchor:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// ...Other codes// Please replace each field in TRTCParams with your own parametersTRTCParams *trtcParams = [[TRTCParams alloc] init];trtcParams.sdkAppId = 1400000123; // Please replace with your own SDKAppIDtrtcParams.roomId = 123321; // Please replace with your own room numbertrtcParams.userId = @"denny"; // Please replace with your own useridtrtcParams.userSig = @""; // Please replace with your own userSigtrtcParams.role = TRTCRoleAnchor;// If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];return YES;}// Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered- (void)onEnterRoom:(NSInteger)result {if (result > 0) {[self toastTip:@"Enter room succeed!"];} else {[self toastTip:@"Enter room failed!"];}}
As an audience:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// ...Other codes// Please replace each field in TRTCParams with your own parametersTRTCParams *trtcParams = [[TRTCParams alloc] init];trtcParams.sdkAppId = 1400000123; // Please replace with your own SDKAppIDtrtcParams.roomId = 123321; // Please replace with your own room numbertrtcParams.userId = @"denny"; // Please replace with your own useridtrtcParams.userSig = @""; // Please replace with your own userSigtrtcParams.role = TRTCRoleAudience;// If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];return YES;}// Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered- (void)onEnterRoom:(NSInteger)result {if (result > 0) {[self toastTip:@"Enter room succeed!"];} else {[self toastTip:@"Enter room failed!"];}}
Note:
If you enter the room as an audience, sdkAppId and roomId need to be the same as on the anchor side, while userId and userSig need to be replaced with your own values.
Step 5. Turn on Camera
Initialize localCameraVideoView in
didFinishLaunchingWithOptions()
method, and call the setLocalRenderParams
to set the local preview render parameters.// Create a windowself.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];[self.window center];[self.window setTitle:@"TRTCDemo_Mac"];[self.window makeKeyAndOrderFront:nil];self.window.releasedWhenClosed = NO;// Initialize localCameraVideoViewself.localCameraVideoView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300)];[self.window.contentView addSubview:self.localCameraVideoView];// Adjust the localCameraVideoView frame to match the size of the windowself.localCameraVideoView.frame = self.window.contentView.bounds;// Set the preview mode of the local screenTRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;[self.trtcCloud setLocalRenderParams:trtcRenderParams];// Start a preview of the local camera[_trtcCloud startLocalPreview:_localCameraVideoView];
Step 6. Turn on microphone
Call
startLocalAudio
to enable microphone capture. This interface requires you to determine the capture mode by the quality
parameter. It is recommended to select one of the following modes that is suitable for your project according to your needs.// Enable microphone acquisition and set the current scene to: Voice mode// For high noise suppression capability, strong and weak network resistance[self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];// Enable microphone acquisition, and set the current scene to: Music mode// For high fidelity acquisition, low sound quality loss, recommended to use with professional sound cards[self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
Step 7. Play/stop video streaming
After you enter denny's room as an audience by following steps 1-4 to create a new project, you can play a video of the remote user by calling the
startRemoteView
.// Play denny's camera footage[self.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
Then, you can call the
stopRemoteView
to stop the videos of a remote user. Alternatively, you can also stop the videos of all remote users via the stopAllRemoteView
.// Stop denny's camera footage[self.trtcCloud stopRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];// Stop all camera footages[self.trtcCloud stopAllRemoteView];
Step 8. Play/stop the audio stream
Mute the voice of remote user denny by calling the
muteRemoteAudio("denny", true)
.// Mute user with id denny[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
You can also unmute him later by calling the
muteRemoteAudio("denny", false)
.// Unmute user with id denny[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
Step 9. Exit the room
Call the
exitRoom
to exit the current room, the SDK will notify you after the check-out through the onExitRoom(int reason)
callback event.// Exit current room[self.trtcCloud exitRoom];// Listen for the onExitRoom callback to find out why you checked out- (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...");}}
FAQs
Contact us
If you have any suggestions or feedback, please contact
info_rtc@tencent.com
.