在 agora.io 中为 android 使用令牌身份验证的 1v1 视频聊天
1v1 video chat using token authentication in agora.io for android
我正在尝试使用基于令牌的身份验证在我的 android 应用程序中实施 1v1 视频聊天。我可以生成令牌并在我的应用程序中使用它。但它不起作用。我想知道当我生成令牌时,我会为第 2 个用户使用 1 个 uid,还是会为第 2 个用户使用 2 个不同的 uid。如果我使用 1 个 uid,那么我将对这 2 个用户使用相同的令牌。如果我使用 2 个 uid,然后为这 2 个用户创建 2 个不同的令牌。我无法解决这个基于令牌的身份验证。它是如何工作的?你能帮帮我吗?
例如,1 位用户正在尝试进行视频通话。我可以使用此用户的 uid 生成令牌。当另一个用户加入此视频通话时,第二个用户将使用相同的令牌,还是第二个用户将使用其 uid 生成另一个令牌。我对这部分感到困惑。第二个用户将如何加入通话?谢谢
我的视频通话代码:
class VideoCallActivity : AppCompatActivity() {
private val db = Firebase.firestore
// Kotlin
// Fill the App ID of your project generated on Agora Console.
private val APP_ID = "app_id"
// Fill the channel name.
private val CHANNEL = "appointment"
private var TOKEN =
private var mRtcEngine: RtcEngine? = null
private lateinit var localContainer: FrameLayout
private lateinit var remoteContainer: FrameLayout
private var uniqueUserUidLocal: Int = 0
private var uniqueUserUidRemote: Int = 0
private val mRtcEventHandler = object : IRtcEngineEventHandler() {
// Listen for the remote user joining the channel to get the uid of the user.
override fun onUserJoined(uniqueUserUidRemote: Int, elapsed: Int) {
runOnUiThread {
// Call setupRemoteVideo to set the remote video view after getting uid from the onUserJoined callback.
setupRemoteVideo(uniqueUserUidRemote)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_video_call)
localContainer = findViewById<FrameLayout>(R.id.local_video_view_container)
remoteContainer = findViewById<FrameLayout>(R.id.remote_video_view_container)
val userId = intent.getStringExtra("chaplainUniqueUserId").toString()
uniqueUserUidRemote = userId.toInt(10)
getToken()
//initializeAndJoinChannel()
video_page_finnish_call_imageButton.setOnClickListener {
mRtcEngine?.stopPreview()
mRtcEngine?.leaveChannel()
RtcEngine.destroy()
finish()
}
}
private fun getToken(){
//getting token info from rest api
val retrofit = Retrofit.Builder()
.baseUrl("https://kadir.webprogrammer.fi/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val api = retrofit.create(TokenApiInterface::class.java)
//this part is not clear. which uid should be used remote uid or local uid
api.fetchAllData(uid = uniqueUserUidRemote.toString()).enqueue(object : Callback<TokenModelClass> {
override fun onResponse(
call: Call<TokenModelClass>,
response: Response<TokenModelClass>
) {
TOKEN = response.body()?.token ?: TOKEN
Log.e("TOKEN_1: ", TOKEN)
Log.e("TOKEN_2: ", uniqueUserUidRemote.toString())
initializeAndJoinChannel(TOKEN)
}
override fun onFailure(call: Call<TokenModelClass>, t: Throwable) {
Log.e("TOKEN: ", t.message.toString())
}
})
}
private fun initializeAndJoinChannel(TOKEN: String) {
try {
mRtcEngine = RtcEngine.create(baseContext, APP_ID, mRtcEventHandler)
} catch (e: Exception) {
}
// By default, video is disabled, and you need to call enableVideo to start a video stream.
mRtcEngine!!.enableVideo()
// Call CreateRendererView to create a SurfaceView object and add it as a child to the FrameLayout.
val localFrame = RtcEngine.CreateRendererView(baseContext)
localFrame.setZOrderMediaOverlay(true)
localContainer.addView(localFrame)
// Pass the SurfaceView object to Agora so that it renders the local video.
mRtcEngine!!.setupLocalVideo(VideoCanvas(localFrame, VideoCanvas.RENDER_MODE_FIT, 0))
//this uid is the local user uid, not the remote user uid
// Join the channel with a token.
mRtcEngine!!.joinChannel(TOKEN, CHANNEL, "", 0)
}
private fun setupRemoteVideo(uniqueUserUidRemote: Int) {
val remoteFrame = RtcEngine.CreateRendererView(baseContext)
//remoteFrame.setZOrderMediaOverlay(true)
remoteContainer.addView(remoteFrame)
mRtcEngine!!.setupRemoteVideo(
VideoCanvas(
remoteFrame,
VideoCanvas.RENDER_MODE_FIT,
uniqueUserUidRemote
)
)
}
override fun onDestroy() {
super.onDestroy()
mRtcEngine?.stopPreview()
mRtcEngine?.leaveChannel()
RtcEngine.destroy()
}
}
您生成的每个令牌对于一个频道和 UID 都是唯一的。因此,当您的应用加载时,它应该加载网络请求。使用从令牌服务器返回的令牌,您可以调用 joinChannel
方法,确保将相同的 UID 和频道名称传递给生成令牌时使用的 joinChannel 参数。
您可以在此处阅读更多相关信息:https://www.agora.io/en/blog/connecting-to-agora-with-tokens-android/
我正在尝试使用基于令牌的身份验证在我的 android 应用程序中实施 1v1 视频聊天。我可以生成令牌并在我的应用程序中使用它。但它不起作用。我想知道当我生成令牌时,我会为第 2 个用户使用 1 个 uid,还是会为第 2 个用户使用 2 个不同的 uid。如果我使用 1 个 uid,那么我将对这 2 个用户使用相同的令牌。如果我使用 2 个 uid,然后为这 2 个用户创建 2 个不同的令牌。我无法解决这个基于令牌的身份验证。它是如何工作的?你能帮帮我吗?
例如,1 位用户正在尝试进行视频通话。我可以使用此用户的 uid 生成令牌。当另一个用户加入此视频通话时,第二个用户将使用相同的令牌,还是第二个用户将使用其 uid 生成另一个令牌。我对这部分感到困惑。第二个用户将如何加入通话?谢谢
我的视频通话代码:
class VideoCallActivity : AppCompatActivity() {
private val db = Firebase.firestore
// Kotlin
// Fill the App ID of your project generated on Agora Console.
private val APP_ID = "app_id"
// Fill the channel name.
private val CHANNEL = "appointment"
private var TOKEN =
private var mRtcEngine: RtcEngine? = null
private lateinit var localContainer: FrameLayout
private lateinit var remoteContainer: FrameLayout
private var uniqueUserUidLocal: Int = 0
private var uniqueUserUidRemote: Int = 0
private val mRtcEventHandler = object : IRtcEngineEventHandler() {
// Listen for the remote user joining the channel to get the uid of the user.
override fun onUserJoined(uniqueUserUidRemote: Int, elapsed: Int) {
runOnUiThread {
// Call setupRemoteVideo to set the remote video view after getting uid from the onUserJoined callback.
setupRemoteVideo(uniqueUserUidRemote)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_video_call)
localContainer = findViewById<FrameLayout>(R.id.local_video_view_container)
remoteContainer = findViewById<FrameLayout>(R.id.remote_video_view_container)
val userId = intent.getStringExtra("chaplainUniqueUserId").toString()
uniqueUserUidRemote = userId.toInt(10)
getToken()
//initializeAndJoinChannel()
video_page_finnish_call_imageButton.setOnClickListener {
mRtcEngine?.stopPreview()
mRtcEngine?.leaveChannel()
RtcEngine.destroy()
finish()
}
}
private fun getToken(){
//getting token info from rest api
val retrofit = Retrofit.Builder()
.baseUrl("https://kadir.webprogrammer.fi/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val api = retrofit.create(TokenApiInterface::class.java)
//this part is not clear. which uid should be used remote uid or local uid
api.fetchAllData(uid = uniqueUserUidRemote.toString()).enqueue(object : Callback<TokenModelClass> {
override fun onResponse(
call: Call<TokenModelClass>,
response: Response<TokenModelClass>
) {
TOKEN = response.body()?.token ?: TOKEN
Log.e("TOKEN_1: ", TOKEN)
Log.e("TOKEN_2: ", uniqueUserUidRemote.toString())
initializeAndJoinChannel(TOKEN)
}
override fun onFailure(call: Call<TokenModelClass>, t: Throwable) {
Log.e("TOKEN: ", t.message.toString())
}
})
}
private fun initializeAndJoinChannel(TOKEN: String) {
try {
mRtcEngine = RtcEngine.create(baseContext, APP_ID, mRtcEventHandler)
} catch (e: Exception) {
}
// By default, video is disabled, and you need to call enableVideo to start a video stream.
mRtcEngine!!.enableVideo()
// Call CreateRendererView to create a SurfaceView object and add it as a child to the FrameLayout.
val localFrame = RtcEngine.CreateRendererView(baseContext)
localFrame.setZOrderMediaOverlay(true)
localContainer.addView(localFrame)
// Pass the SurfaceView object to Agora so that it renders the local video.
mRtcEngine!!.setupLocalVideo(VideoCanvas(localFrame, VideoCanvas.RENDER_MODE_FIT, 0))
//this uid is the local user uid, not the remote user uid
// Join the channel with a token.
mRtcEngine!!.joinChannel(TOKEN, CHANNEL, "", 0)
}
private fun setupRemoteVideo(uniqueUserUidRemote: Int) {
val remoteFrame = RtcEngine.CreateRendererView(baseContext)
//remoteFrame.setZOrderMediaOverlay(true)
remoteContainer.addView(remoteFrame)
mRtcEngine!!.setupRemoteVideo(
VideoCanvas(
remoteFrame,
VideoCanvas.RENDER_MODE_FIT,
uniqueUserUidRemote
)
)
}
override fun onDestroy() {
super.onDestroy()
mRtcEngine?.stopPreview()
mRtcEngine?.leaveChannel()
RtcEngine.destroy()
}
}
您生成的每个令牌对于一个频道和 UID 都是唯一的。因此,当您的应用加载时,它应该加载网络请求。使用从令牌服务器返回的令牌,您可以调用 joinChannel
方法,确保将相同的 UID 和频道名称传递给生成令牌时使用的 joinChannel 参数。
您可以在此处阅读更多相关信息:https://www.agora.io/en/blog/connecting-to-agora-with-tokens-android/