Android
private void createEngine() {
mAliRtcEngine = AliRtcEngine.getInstance(getApplicationContext());
mAliRtcEngine.setRtcEngineEventListener(new AliRtcEngineEventListener() {
/* Notification of the connection status between the SDK and the server. You must handle connection failures. */
@Override
public void onConnectionStatusChange(AliRtcEngine.AliRtcConnectionStatus aliRtcConnectionStatus, AliRtcEngine.AliRtcConnectionStatusChangeReason aliRtcConnectionStatusChangeReason) {
super.onConnectionStatusChange(aliRtcConnectionStatus, aliRtcConnectionStatusChangeReason);
if (aliRtcConnectionStatus == AliRtcEngine.AliRtcConnectionStatus.AliRtcConnectionStatusFailed) {
/* TODO: Required. We recommend notifying the user. This callback is triggered only after the SDK's internal recovery strategies have failed. */
} else {
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
}
}
/* The SDK attempts to handle local device exceptions. */
@Override
public void OnLocalDeviceException(AliRtcEngine.AliRtcEngineLocalDeviceType aliRtcEngineLocalDeviceType, AliRtcEngine.AliRtcEngineLocalDeviceExceptionType aliRtcEngineLocalDeviceExceptionType, String s) {
super.OnLocalDeviceException(aliRtcEngineLocalDeviceType, aliRtcEngineLocalDeviceExceptionType, s);
/* TODO: Required. We recommend notifying the user of the error. This callback is triggered only after the SDK's internal recovery strategies have failed. */
}
@Override
public void onJoinChannelResult(int result, String channel, String userId, int elapsed) {
super.onJoinChannelResult(result, channel, userId, elapsed);
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
Log.i(TAG, "onJoinChannelResult result=" + result + ",channel=" + channel + ",userId=" + userId + ",elapsed=" + elapsed);
}
@Override
public void onLeaveChannelResult(int result, AliRtcEngine.AliRtcStats stats) {
super.onLeaveChannelResult(result, stats);
Log.i(TAG, "onLeaveChannelResult result=" + result);
}
});
mAliRtcEngine.setRtcEngineNotify(new AliRtcEngineNotify() {
/* This callback is triggered 30 seconds before the authentication token expires. You must refresh the token. */
@Override
public void onAuthInfoWillExpire() {
super.onAuthInfoWillExpire();
/* TODO: Required. Your application must retrieve a new authentication token for the current channel and user, and then call refreshAuthInfo. */
}
/* You must handle this callback, as it is triggered when the application server kicks out a user. */
@Override
public void onBye(int code) {
super.onBye(code);
/* TODO: Recommended. Handle this event according to your application's logic. */
}
@Override
public void onRemoteUserOnLineNotify(String uid, int elapsed) {
super.onRemoteUserOnLineNotify(uid, elapsed);
Log.i(TAG, "onRemoteUserOnLineNotify uid=" + uid + ",elapsed=" + elapsed);
}
@Override
public void onRemoteUserOffLineNotify(String uid, AliRtcEngine.AliRtcUserOfflineReason aliRtcUserOfflineReason) {
super.onRemoteUserOffLineNotify(uid, aliRtcUserOfflineReason);
Log.i(TAG, "onRemoteUserOnLineNotify uid=" + uid + ",aliRtcUserOfflineReason=" + aliRtcUserOfflineReason);
}
@Override
public void onRemoteTrackAvailableNotify(String uid, AliRtcEngine.AliRtcAudioTrack aliRtcAudioTrack, AliRtcEngine.AliRtcVideoTrack aliRtcVideoTrack) {
super.onRemoteTrackAvailableNotify(uid, aliRtcAudioTrack, aliRtcVideoTrack);
Log.i(TAG, "onRemoteUserOnLineNotify uid=" + uid + ",aliRtcAudioTrack=" + aliRtcAudioTrack + ",aliRtcVideoTrack=" + aliRtcVideoTrack);
mMainHandler.post(new Runnable() {
@Override
public void run() {
if (aliRtcVideoTrack == AliRtcEngine.AliRtcVideoTrack.AliRtcVideoTrackCamera
|| aliRtcVideoTrack == AliRtcEngine.AliRtcVideoTrack.AliRtcVideoTrackBoth) {
AliRtcEngine.AliRtcVideoCanvas remote_canvas = new AliRtcEngine.AliRtcVideoCanvas();
SurfaceView remoteView = mAliRtcEngine.createRenderSurfaceView(mContext);
if (remoteView != null) {
remoteView.setZOrderOnTop(true);
remoteView.setZOrderMediaOverlay(true);
}
remote_canvas.view = remoteView;
mLocalSurfaceContainer.addView(remote_canvas.view, new FrameLayout.LayoutParams(
findViewById(R.id.local_surface_container).getWidth()/4,
findViewById(R.id.local_surface_container).getHeight()/4));
mAliRtcEngine.setRemoteViewConfig(remote_canvas,uid,AliRtcEngine.AliRtcVideoTrack.AliRtcVideoTrackCamera);
} else {
mAliRtcEngine.setRemoteViewConfig(null, uid, AliRtcEngine.AliRtcVideoTrack.AliRtcVideoTrackCamera);
}
}
});
}
});
}
iOS
- (AliRtcEngine *)engine{
if (!_engine) {
NSString *extras = @"";
/* Create the engine and pass the delegate. */
_engine = [AliRtcEngine sharedInstance:self extras:extras];
}
return _engine;
}
#pragma mark - "Delegates of engine"
/* This callback is triggered 30 seconds before the authentication token expires. You must refresh the token. */
- (void)onAuthInfoWillExpire{
/* TODO: Required. Your application must retrieve a new authentication token for the current channel and user, and then call refreshAuthInfo. */
}
/* Notification of the connection status between the SDK and the server. You must handle connection failures. */
- (void)onConnectionStatusChange:(AliRtcConnectionStatus)status reason:(AliRtcConnectionStatusChangeReason)reason {
if (status == AliRtcConnectionStatusFailed) {
/* TODO: Required. We recommend notifying the user. This callback is triggered only after the SDK's internal recovery strategies have failed. */
} else {
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
}
}
/* The SDK attempts to handle local device exceptions. */
- (void)onLocalDeviceException:(AliRtcLocalDeviceType)deviceType exceptionType:(AliRtcLocalDeviceExceptionType)exceptionType message:(NSString *_Nullable)msg {
/* TODO: Required. We recommend notifying the user of the error. This callback is triggered only after the SDK's internal recovery strategies have failed. */
}
/* You must handle this callback, as it is triggered when the application server kicks out a user. */
- (void)onBye:(int)code {
/* TODO: Recommended. Handle this event according to your application's logic. */
}
/* Notification for remote stream availability. In auto-subscribe mode, use this to manage the UI before the first frame is rendered. */
- (void)onRemoteTrackAvailableNotify:(NSString *_Nonnull)uid audioTrack:(AliRtcAudioTrack)audioTrack videoTrack:(AliRtcVideoTrack)videoTrack {
/* TODO: Required. Set a video rendering view for the SDK. */
dispatch_async(dispatch_get_main_queue(), ^{
if (videoTrack & AliRtcVideoTrackCamera) {
AliVideoCanvas *canvas = [[AliVideoCanvas alloc] init];
canvas.view = self.remoteRenderView;
canvas.renderMode = AliRtcRenderModeAuto;
[self.engine setRemoteViewConfig:canvas uid:uid forTrack:AliRtcVideoTrackCamera];
} else {
/* Clear the remote user's rendering view. Note that the user might have already left the channel. */
[self.engine setRemoteViewConfig:nil uid:uid forTrack:AliRtcVideoTrackCamera];
}
});
}
- (void)onRemoteUserOffLineNotify:(NSString *)uid offlineReason:(AliRtcUserOfflineReason)reason {
dispatch_async(dispatch_get_main_queue(), ^{
/* Also clear the remote user's rendering view here. */
[self.engine setRemoteViewConfig:nil uid:uid forTrack:AliRtcVideoTrackBoth];
});
}
- (void)onJoinChannelResult:(int)result channel:(NSString *)channel userId:(NSString *)userId elapsed:(int)elapsed {
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
}
- (void)onLeaveChannelResult:(int)result stats:(AliRtcStats)stats {
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
}
Windows
class AliEngineEventListenerImpl : public AliEngineEventListener {
public:
AliEngineEventListenerImpl(AliEngine* engine, HWND hWnd) {
mAliRtcEngine = engine;
mHWnd = hWnd;
};
virtual AliEngineEventListenerImpl() {};
virtual void OnConnectionStatusChange(int status, int reason) override {
if (status == AliEngineConnectionFailed) {
/* TODO: Required. We recommend notifying the user. This callback is triggered only after the SDK's internal recovery strategies have failed. */
} else {
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
}
};
virtual void OnLocalDeviceException(AliEngineLocalDeviceType deviceType, AliEngineLocalDeviceExceptionType exceptionType, const char* msg) override {
/* TODO: Required. We recommend notifying the user of the error. This callback is triggered only after the SDK's internal recovery strategies have failed. */
};
virtual void OnJoinChannelResult(int result, const char *channel, const char *userId, int elapsed) override {
/* TODO: Optional. Add business logic, typically for data statistics or UI updates. */
};
virtual void OnLeaveChannelResult(int result, AliEngineStats stats) override {
};
virtual void OnAuthInfoWillExpire() override {
/* TODO: Required. Your application must retrieve a new authentication token for the current channel and user, and then call refreshAuthInfo. */
};
virtual void OnBye(int code) override {
/* Recommended: Handle this event, which can be triggered if a user is kicked by the server. */
};
virtual void OnRemoteUserOnLineNotify(const char *uid, int elapsed) override {
};
virtual void OnRemoteUserOffLineNotify(const char *uid, AliEngineUserOfflineReason reason) override {
};
virtual void OnRemoteTrackAvailableNotify(const char *uid,
AliEngineAudioTrack audioTrack,
AliEngineVideoTrack videoTrack) {
AliEngineVideoCanvas remote_canvas;
if (videoTrack == AliEngineVideoTrackCamera
|| videoTrack == AliEngineVideoTrackBoth) {
RECT rect;
::GetWindowRect(mHWnd, &rect);
remote_canvas.displayView = remoteView;
remote_canvas.renderMode = AliEngineRenderModeAuto;
mAliRtcEngine->SetRemoteViewConfig(remote_canvas,uid,AliEngineVideoTrackCamera);
} else {
mAliRtcEngine->SetRemoteViewConfig(remote_canvas, uid, AliEngineVideoTrackCamera);
}
}
private:
AliEngine* mAliRtcEngine = nullptr;
/* Windows window handle */
HWND mHWnd;
};
private:
void createEngine() {
mAliRtcEngine = AliRtcEngine.Create("");
mLisenter = new AliEngineEventListenerImpl(mAliRtcEngine, mHWnd);
mAliRtcEngine->SetEngineEventListener(mLisenter);
}
HarmonyOS
this.rtcSdk = AliRtcEngine.getInstance(NULL, getContext(this));
this.engineEventListener: AliRtcEngineEventListener = new AliRtcEngineEventListener();
this.engineEventListener
.onRemoteUserOnline((uid: string, elapsed: boolean)=> {
// A user joins the channel.
})
.onRemoteUserOffline((uid:string, reason: AliRtcUserOfflineReason)=> {
// A user leaves the channel.
})
.onJoinChannel((result : number, channel : string, userId : string, elapsed : number)=> {
// Triggered when joining a channel.
})
.onLeaveChannel((result : number, stat : AliRtcStats)=> {
// Triggered when leaving a channel.
})
.onBye((code : AliRtcOnByeType)=> {
// Callback for when the user is kicked by the server or the channel is closed.
})
.onOccurError((error : number, msg : string)=> {
// Callback for when an error occurs.
})
.onConnectionStatusChange((status : number, reason : number)=> {
// Callback for when the network connection status changes.
})
;
this.rtcSdk?.setRtcEngineEventListener(this.engineEventListener);