代码拉取完成,页面将自动刷新
using System.Text;
using Agora.Rtc;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using io.agora.rtc.demo;
namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.SetEncryption
{
public class EncryptionSample : MonoBehaviour
{
[FormerlySerializedAs("appIdInput")]
[SerializeField]
private AppIdInput _appIdInput;
[Header("_____________Basic Configuration_____________")]
[FormerlySerializedAs("APP_ID")]
[SerializeField]
private string _appID = "";
[FormerlySerializedAs("TOKEN")]
[SerializeField]
private string _token = "";
[FormerlySerializedAs("CHANNEL_NAME")]
[SerializeField]
private string _channelName = "";
[SerializeField]
public ENCRYPTION_MODE EncrytionMode = ENCRYPTION_MODE.AES_128_GCM2;
private string secret = "Hello_Unity";
public Text LogText;
internal Logger Log;
internal IRtcEngine RtcEngine;
// Start is called before the first frame update
private void Start()
{
LoadAssetData();
if (CheckAppId())
{
InitRtcEngine();
SetEncryption();
JoinChannel();
}
}
private void Update()
{
PermissionHelper.RequestMicrophontPermission();
}
private bool CheckAppId()
{
Log = new Logger(LogText);
return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
}
//Show data in AgoraBasicProfile
[ContextMenu("ShowAgoraBasicProfileData")]
private void LoadAssetData()
{
if (_appIdInput == null) return;
_appID = _appIdInput.appID;
_token = _appIdInput.token;
_channelName = _appIdInput.channelName;
}
private void InitRtcEngine()
{
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
RtcEngineContext context = new RtcEngineContext();
context.appId = _appID;
context.channelProfile = CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING;
context.audioScenario = AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT;
context.areaCode = AREA_CODE.AREA_CODE_GLOB;
RtcEngine.Initialize(context);
RtcEngine.InitEventHandler(new UserEventHandler(this));
}
private byte[] GetEncryptionSaltFromServer()
{
return Encoding.UTF8.GetBytes("EncryptionKdfSaltInBase64Strings");
}
private void SetEncryption()
{
byte[] kdfSal = this.GetEncryptionSaltFromServer();
var config = new EncryptionConfig
{
encryptionMode = EncrytionMode,
encryptionKey = secret,
encryptionKdfSalt = kdfSal
};
Log.UpdateLog(string.Format("encryption mode: {0} secret: {1}", EncrytionMode, secret));
var nRet= RtcEngine.EnableEncryption(true, config);
this.Log.UpdateLog("EnableEncryption: " + nRet);
}
private void JoinChannel()
{
RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
RtcEngine.EnableAudio();
RtcEngine.EnableVideo();
RtcEngine.JoinChannel(_token, _channelName, "", 0);
}
private void OnLeaveBtnClick()
{
RtcEngine.LeaveChannel();
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
if (RtcEngine == null) return;
RtcEngine.InitEventHandler(null);
RtcEngine.LeaveChannel();
RtcEngine.Dispose();
}
#region -- Video Render UI Logic ---
internal static void MakeVideoView(uint uid, string channelId = "")
{
var go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
return; // reuse
}
// create a GameObject and assign to this new user
var videoSurface = MakeImageSurface(uid.ToString());
if (ReferenceEquals(videoSurface, null)) return;
// configure videoSurface
if (uid == 0)
{
videoSurface.SetForUser(uid, channelId);
}
else
{
videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
}
videoSurface.OnTextureSizeModify += (int width, int height) =>
{
var transform = videoSurface.GetComponent<RectTransform>();
if (transform)
{
//If render in RawImage. just set rawImage size.
transform.sizeDelta = new Vector2(width / 2, height / 2);
transform.localScale = Vector3.one;
}
else
{
//If render in MeshRenderer, just set localSize with MeshRenderer
float scale = (float)height / (float)width;
videoSurface.transform.localScale = new Vector3(-1, 1, scale);
}
Debug.Log("OnTextureSizeModify: " + width + " " + height);
};
videoSurface.SetEnable(true);
}
// VIDEO TYPE 1: 3D Object
private static VideoSurface MakePlaneSurface(string goName)
{
var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
if (go == null)
{
return null;
}
go.name = goName;
var mesh = go.GetComponent<MeshRenderer>();
if (mesh != null)
{
Debug.LogWarning("VideoSureface update shader");
mesh.material = new Material(Shader.Find("Unlit/Texture"));
}
// set up transform
go.transform.Rotate(-90.0f, 0.0f, 0.0f);
go.transform.position = Vector3.zero;
go.transform.localScale = new Vector3(0.25f, 0.5f, 0.5f);
// configure videoSurface
var videoSurface = go.AddComponent<VideoSurface>();
return videoSurface;
}
// Video TYPE 2: RawImage
private static VideoSurface MakeImageSurface(string goName)
{
GameObject go = new GameObject();
if (go == null)
{
return null;
}
go.name = goName;
// to be renderered onto
go.AddComponent<RawImage>();
// make the object draggable
go.AddComponent<UIElementDrag>();
var canvas = GameObject.Find("VideoCanvas");
if (canvas != null)
{
go.transform.parent = canvas.transform;
Debug.Log("add video view");
}
else
{
Debug.Log("Canvas is null video view");
}
// set up transform
go.transform.Rotate(0f, 0.0f, 180.0f);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = new Vector3(2f, 3f, 1f);
// configure videoSurface
var videoSurface = go.AddComponent<VideoSurface>();
return videoSurface;
}
internal static void DestroyVideoView(uint uid)
{
var go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
Destroy(go);
}
}
#endregion
internal string GetChannelName()
{
return _channelName;
}
}
#region -- Agora Event ---
internal class UserEventHandler : IRtcEngineEventHandler
{
private readonly EncryptionSample _encryptionSample;
internal UserEventHandler(EncryptionSample encryptionSample)
{
_encryptionSample = encryptionSample;
}
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
int build = 0;
_encryptionSample.Log.UpdateLog(string.Format("sdk version: {0}",
_encryptionSample.RtcEngine.GetVersion(ref build)));
_encryptionSample.Log.UpdateLog(string.Format(
"onJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", connection.channelId,
connection.localUid, elapsed));
EncryptionSample.MakeVideoView(0);
}
public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
{
_encryptionSample.Log.UpdateLog("OnLeaveChannelSuccess");
EncryptionSample.MakeVideoView(0);
}
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{
_encryptionSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
EncryptionSample.MakeVideoView(uid, _encryptionSample.GetChannelName());
}
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
{
_encryptionSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
(int)reason));
EncryptionSample.DestroyVideoView(uid);
}
public override void OnError(int error, string msg)
{
_encryptionSample.Log.UpdateLog(string.Format("OnSDKError error: {0}, msg: {1}", error, msg));
}
public override void OnConnectionLost(RtcConnection connection)
{
_encryptionSample.Log.UpdateLog(string.Format("OnConnectionLost "));
}
public override void OnEncryptionError(RtcConnection connection, ENCRYPTION_ERROR_TYPE errorType)
{
_encryptionSample.Log.UpdateLog("OnEncryptionError: " + errorType);
}
}
#endregion
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。