From c7d165989c9a9d9f9e287203d931057034d99d0f Mon Sep 17 00:00:00 2001 From: Yu Sung Date: Wed, 29 Oct 2025 17:42:11 +0900 Subject: [PATCH] =?UTF-8?q?refactor(agora):=20=EB=B3=80=EA=B2=BD=EC=9D=84?= =?UTF-8?q?=20=EC=9A=A9=EC=9D=B4=ED=95=98=EA=B2=8C=20=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20RTM=EA=B3=BC=20RTC=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20=EC=9E=AC=EB=B0=B0=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SodaLive/Sources/Agora/Agora.swift | 103 +++++++++++------- .../Sources/Live/Room/LiveRoomViewModel.swift | 8 +- 2 files changed, 68 insertions(+), 43 deletions(-) diff --git a/SodaLive/Sources/Agora/Agora.swift b/SodaLive/Sources/Agora/Agora.swift index d1a70f6..1027671 100644 --- a/SodaLive/Sources/Agora/Agora.swift +++ b/SodaLive/Sources/Agora/Agora.swift @@ -12,24 +12,28 @@ import AgoraRtmKit final class Agora { static let shared = Agora() - var rtcEngineDelegate: AgoraRtcEngineDelegate? - var rtmDelegate: AgoraRtmDelegate? - - var rtcEngine: AgoraRtcEngineKit? - - var rtmKit: AgoraRtmKit? - var rtmChannel: AgoraRtmChannel? - func initialize() { + initRtcEngine() + initRtmClient() + } + + func deInit() { + deInitRtcEngine() + deInitRtmClient() + } + + // MARK: RTC + var rtcEngine: AgoraRtcEngineKit? + var rtcEngineDelegate: AgoraRtcEngineDelegate? + + func initRtcEngine() { rtcEngine = AgoraRtcEngineKit.sharedEngine(withAppId: AGORA_APP_ID, delegate: rtcEngineDelegate) rtcEngine?.setChannelProfile(.liveBroadcasting) rtcEngine?.enableAudio() rtcEngine?.enableAudioVolumeIndication(500, smooth: 3, reportVad: true) - - rtmKit = AgoraRtmKit(appId: AGORA_APP_ID, delegate: rtmDelegate) } - func deInit() { + func deInitRtcEngine() { if let rtcEngine = rtcEngine { rtcEngine.leaveChannel(nil) @@ -37,20 +41,56 @@ final class Agora { AgoraRtcEngineKit.destroy() } } - - rtmChannel?.leave(completion: nil) - rtmKit?.logout(completion: nil) rtcEngine = nil - rtmChannel = nil - rtmKit = nil + } + + func joinRtcChannel(rtcToken: String, channelName: String) { + let userId = UserDefaults.int(forKey: .userId) + + rtcEngine?.joinChannel( + byToken: rtcToken, + channelId: channelName, + info: nil, + uid: UInt(userId), + joinSuccess: nil + ) + + rtcEngine?.setAudioProfile(.musicHighQualityStereo) + rtcEngine?.setAudioScenario(.gameStreaming) } func setRole(role: AgoraClientRole) { self.rtcEngine?.setClientRole(role) } - func joinChannel( - roomInfo: GetRoomInfoResponse, + func mute(_ isMute: Bool) { + rtcEngine?.muteLocalAudioStream(isMute) + } + + func speakerMute(_ isMute: Bool) { + rtcEngine?.muteAllRemoteAudioStreams(isMute) + } + + // MARK: RTM + var rtmKit: AgoraRtmKit? + var rtmChannel: AgoraRtmChannel? + var rtmDelegate: AgoraRtmDelegate? + + func initRtmClient() { + rtmKit = AgoraRtmKit(appId: AGORA_APP_ID, delegate: rtmDelegate) + } + + func deInitRtmClient() { + rtmChannel?.leave(completion: nil) + rtmKit?.logout(completion: nil) + rtmChannel = nil + rtmKit = nil + } + + func rtmLogin( + creatorId: Int, + rtmToken: String, + channelName: String, rtmChannelDelegate: AgoraRtmChannelDelegate, onConnectSuccess: @escaping (Bool) -> Void, onConnectFail: @escaping () -> Void @@ -61,36 +101,25 @@ final class Agora { let userId = UserDefaults.int(forKey: .userId) - rtcEngine?.joinChannel( - byToken: roomInfo.rtcToken, - channelId: roomInfo.channelName, - info: nil, - uid: UInt(userId), - joinSuccess: nil - ) - - rtcEngine?.setAudioProfile(.musicHighQualityStereo) - rtcEngine?.setAudioScenario(.gameStreaming) - rtmChannel = rtmKit?.createChannel( - withId: roomInfo.channelName, + withId: channelName, delegate: rtmChannelDelegate ) rtmKit?.login( - byToken: roomInfo.rtmToken, + byToken: rtmToken, user: String(userId), completion: { [unowned self] loginErrorCode in if loginErrorCode == .ok { self.rtmChannel?.join(completion: { joinChannelErrorCode in if joinChannelErrorCode == .channelErrorOk { - if userId == roomInfo.creatorId { + if userId == creatorId { self.setRole(role: .broadcaster) } else { self.setRole(role: .audience) } - onConnectSuccess(userId == roomInfo.creatorId) + onConnectSuccess(userId == creatorId) } else { onConnectFail() } @@ -128,14 +157,6 @@ final class Agora { } } - func mute(_ isMute: Bool) { - rtcEngine?.muteLocalAudioStream(isMute) - } - - func speakerMute(_ isMute: Bool) { - rtcEngine?.muteAllRemoteAudioStreams(isMute) - } - func sendMessageToGroup(textMessage: String, completion: @escaping AgoraRtmSendChannelMessageBlock) { let message = AgoraRtmMessage(text: textMessage) rtmChannel?.send(message, completion: completion) diff --git a/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift b/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift index 6695a71..74c5f93 100644 --- a/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift +++ b/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift @@ -369,8 +369,12 @@ final class LiveRoomViewModel: NSObject, ObservableObject { self.isActiveRoulette = data.isActiveRoulette self.isLoading = true - self.agora.joinChannel( - roomInfo: data, + + self.agora.joinRtcChannel(rtcToken: data.rtcToken, channelName: data.channelName) + self.agora.rtmLogin( + creatorId: data.creatorId, + rtmToken: data.rtmToken, + channelName: data.channelName, rtmChannelDelegate: self, onConnectSuccess: self.agoraConnectSuccess, onConnectFail: self.agoraConnectFail