JianGongYun/ClientDemo/MainWindow.xaml.cs

131 lines
5.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using JianGongYun.TRTC.Components;
using JianGongYun.TRTC.Utils;
using ManageLiteAV;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ClientDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
const uint SDKAppID = 1400463444;
const string SDKAppKEY = "6ee2586282eb8ab5bff3f917b44500c4ffd9bbd3d820258b1fa8cdd470cfd1ee";
/// <summary>
/// TRTC实例
/// </summary>
public static ITRTCCloud lTRTCCloud;
/// <summary>
/// 设备管理器
/// </summary>
public static ITXDeviceManager lTXDeviceManager;
public static TRTCCloudCallback TRTCCloudCallback = new TRTCCloudCallback();
public MainWindow()
{
InitializeComponent();
lTRTCCloud = ITRTCCloud.getTRTCShareInstance();//创建TRTC实例
Console.WriteLine(lTRTCCloud.getSDKVersion());
lTRTCCloud.addCallback(TRTCCloudCallback);//注册回调
var roomPars = new TRTCParams
{
sdkAppId = SDKAppID,
userId = "666",
userSig = GenTestUserSig("666"),
roomId = 0,//使用strRoomId
strRoomId = "53245345",
role = TRTCRoleType.TRTCRoleAudience
};
lTRTCCloud.enterRoom(ref roomPars, TRTCAppScene.TRTCAppSceneLIVE);//创建房间
StartVideo();
}
//53245345房间id/教师id 666自己id
public void StartVideo()
{
lTRTCCloud.startRemoteView("53245345", TRTCVideoStreamType.TRTCVideoStreamTypeBig, IntPtr.Zero);
AddCustomVideoView(main, "53245345", TRTCVideoStreamType.TRTCVideoStreamTypeBig, false);
lTRTCCloud.startRemoteView("53245345", TRTCVideoStreamType.TRTCVideoStreamTypeSub, IntPtr.Zero);
AddCustomVideoView(sub, "53245345", TRTCVideoStreamType.TRTCVideoStreamTypeSub, false);
}
public void StopVideo()
{
lTRTCCloud.stopRemoteView("53245345", TRTCVideoStreamType.TRTCVideoStreamTypeBig);
RemoveCustomVideoView(main, "53245345", TRTCVideoStreamType.TRTCVideoStreamTypeBig, false);
lTRTCCloud.stopRemoteView("53245345", TRTCVideoStreamType.TRTCVideoStreamTypeSub);
RemoveCustomVideoView(sub, "53245345", TRTCVideoStreamType.TRTCVideoStreamTypeSub, false);
}
private static void RemoveCustomVideoView(Panel parent, string userId, TRTCVideoStreamType streamType, bool local = false)
{
TXLiteAVVideoView videoView = null;
string key = string.Format("{0}_{1}", userId, streamType);
if (VideoViews.TryGetValue(key, out videoView))
{
videoView.RemoveEngine(lTRTCCloud);
parent.Dispatcher.Invoke(new Action(() =>
{
parent.Children.Remove(videoView);
}));
VideoViews.Remove(key);
}
}
/// <summary>
/// 视频画面
/// </summary>
public static Dictionary<string, TXLiteAVVideoView> VideoViews = new Dictionary<string, TXLiteAVVideoView>();
/// <summary>
/// 添加自定义渲染 View 并绑定渲染回调
/// </summary>
private static TXLiteAVVideoView AddCustomVideoView(Panel parent, string userId, TRTCVideoStreamType streamType, bool local = false)
{
TXLiteAVVideoView videoView = new TXLiteAVVideoView();
videoView.RegEngine(userId, streamType, lTRTCCloud, local);
videoView.SetRenderMode(streamType == TRTCVideoStreamType.TRTCVideoStreamTypeBig ? TRTCVideoFillMode.TRTCVideoFillMode_Fill : TRTCVideoFillMode.TRTCVideoFillMode_Fit);
videoView.SetBinding(TXLiteAVVideoView.WidthProperty, new Binding("ActualWidth") { Source = parent });
videoView.SetBinding(TXLiteAVVideoView.HeightProperty, new Binding("ActualHeight") { Source = parent });
parent.Dispatcher.Invoke(new Action(() =>
{
parent.Children.Add(videoView);
}));
string key = string.Format("{0}_{1}", userId, streamType);
VideoViews.Add(key, videoView);
return videoView;
}
protected override void OnClosing(CancelEventArgs e)
{
StopVideo();
lTRTCCloud.exitRoom();
lTRTCCloud.removeCallback(TRTCCloudCallback);//注册回调
ITRTCCloud.destroyTRTCShareInstance();//销毁TRTC实例
lTRTCCloud.Dispose();
lTRTCCloud = null;
base.OnClosing(e);
}
public static string GenTestUserSig(string userId)
{
if (SDKAppID == 0 || string.IsNullOrEmpty(SDKAppKEY)) return null;
TLSSigAPIv2 api = new TLSSigAPIv2((int)SDKAppID, SDKAppKEY);
// 统一转换为UTF8SDK内部是用UTF8编码。
return api.GenSig(Util.UTF16To8(userId));
}
}
}