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.
3. Edit the Podfile file as follows and change Your Target to your own project name.
platform :osx, '10.10'
target 'Your Target' do
pod '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.
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;
@property (nonatomic, strong) TRTCCloud *trtcCloud;
@property (nonatomic, strong) NSView *localCameraVideoView;
- (void)toastTip:(NSString *)tip;
@end
3. Implement the toastTip:
method in the AppDelegate.m file.
- (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 {
_trtcCloud = [TRTCCloud sharedInstance];
_trtcCloud.delegate = self;
return YES;
}
- (void)onError:(TXLiteAVError)errCode
errMsg:(nullable NSString *)errMsg
extInfo:(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 {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.roomId = 123321;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.role = TRTCRoleAnchor;
[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
return YES;
}
- (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 {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.roomId = 123321;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.role = TRTCRoleAudience;
[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
return YES;
}
- (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.
self.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;
self.localCameraVideoView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300)];
[self.window.contentView addSubview:self.localCameraVideoView];
self.localCameraVideoView.frame = self.window.contentView.bounds;
TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
[self.trtcCloud setLocalRenderParams:trtcRenderParams];
[_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.
[self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
[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
.
[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
.
[self.trtcCloud stopRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
[self.trtcCloud stopAllRemoteView];
Step 8. Play/stop the audio stream
Mute the voice of remote user denny by calling the muteRemoteAudio("denny", true)
.
[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
You can also unmute him later by calling the muteRemoteAudio("denny", false)
.
[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.
[self.trtcCloud exitRoom];
- (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
.
Was this page helpful?