/
DesktopScreenShare.cs
129 lines (112 loc) · 4.32 KB
/
DesktopScreenShare.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using agora_gaming_rtc;
using AgoraNative;
/// <summary>
/// this is an example of using ScreenSharing APIs for Desktops
/// </summary>
public class DesktopScreenShare : PlayerViewControllerBase
{
Dropdown WindowOptionDropdown;
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
readonly List<AgoraNativeBridge.RECT> WinDisplays = new List<AgoraNativeBridge.RECT>();
#else
List<uint> MacDisplays;
#endif
int CurrentDisplay = 0;
protected override void SetupUI()
{
base.SetupUI();
Dropdown dropdown = GameObject.Find("Dropdown").GetComponent<Dropdown>();
if (dropdown != null)
{
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
MacDisplays = AgoraNativeBridge.GetMacDisplayIds();
WindowList list = AgoraNativeBridge.GetMacWindowList();
if (list != null)
{
dropdown.options = list.windows.Select(w =>
new Dropdown.OptionData(w.kCGWindowOwnerName + " | " + w.kCGWindowNumber)).ToList();
}
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
// Monitor Display info
var winDispInfoList = AgoraNativeBridge.GetWinDisplayInfo();
if (winDispInfoList != null)
{
foreach (var dpInfo in winDispInfoList)
{
Debug.Log("Adding display:" + WinDisplays.Count);
WinDisplays.Add(dpInfo.MonitorInfo.monitor);
}
}
// Window ID info
Dictionary<string, System.IntPtr> winWinIdList;
AgoraNativeBridge.GetDesktopWindowHandlesAndTitles(out winWinIdList);
if (winWinIdList != null)
{
dropdown.options = (winWinIdList.Select(w =>
new Dropdown.OptionData(string.Format("{0, -20} | {1}",
w.Key.Substring(0, System.Math.Min(w.Key.Length, 20)), w.Value))).ToList());
}
#endif
WindowOptionDropdown = dropdown;
}
Button button = GameObject.Find("ShareWindowButton").GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(OnShareWindowClick);
}
button = GameObject.Find("ShareDisplayButton").GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(ShareDisplayScreen);
}
button = GameObject.Find("StopShareButton").GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(() => { mRtcEngine.StopScreenCapture(); });
}
GameObject quad = GameObject.Find("DisplayPlane");
if (ReferenceEquals(quad, null))
{
Debug.Log("Error: failed to find DisplayPlane");
return;
}
else
{
quad.AddComponent<VideoSurface>();
}
}
void ShareDisplayScreen()
{
ScreenCaptureParameters sparams = new ScreenCaptureParameters
{
captureMouseCursor = true,
frameRate = 15
};
mRtcEngine.StopScreenCapture();
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
mRtcEngine.StartScreenCaptureByDisplayId(MacDisplays[CurrentDisplay], default(Rectangle), sparams);
CurrentDisplay = (CurrentDisplay + 1) % MacDisplays.Count;
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
mRtcEngine.StartScreenCaptureByDisplayId((uint)CurrentDisplay, default(Rectangle), sparams);
CurrentDisplay = (CurrentDisplay + 1) % WinDisplays.Count;
#endif
}
void OnShareWindowClick()
{
char[] delimiterChars = { '|' };
if (WindowOptionDropdown == null) return;
string option = WindowOptionDropdown.options[WindowOptionDropdown.value].text;
if (string.IsNullOrEmpty(option))
{
return;
}
string wid = option.Split(delimiterChars, System.StringSplitOptions.RemoveEmptyEntries)[1];
Debug.LogWarning(wid + " is chosen");
mRtcEngine.StopScreenCapture();
mRtcEngine.StartScreenCaptureByWindowId(int.Parse(wid), default(Rectangle), default(ScreenCaptureParameters));
}
}