Skip to content

Commit 366386c

Browse files
committed
Demo refactor; fix setVideoConfiguration
1 parent 6626f8d commit 366386c

File tree

4 files changed

+59
-42
lines changed

4 files changed

+59
-42
lines changed

Assets/API-Example/app-screenshare-sample/AgoraScreenShare.cs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using UnityEngine.UI;
55
using agora_utilities;
66

7-
public class AgoraScreenShare : MonoBehaviour
7+
public class AgoraScreenShare : MonoBehaviour
88
{
99

1010
[SerializeField]
@@ -19,27 +19,27 @@ public class AgoraScreenShare : MonoBehaviour
1919
[SerializeField]
2020
Toggle ScreenToggle;
2121

22-
public Text logText;
22+
public Text logText;
2323
private Logger logger;
24-
public IRtcEngine mRtcEngine = null;
25-
private const float Offset = 100;
26-
private Texture2D mTexture;
27-
private Rect mRect;
24+
public IRtcEngine mRtcEngine = null;
25+
private const float Offset = 100;
26+
private Texture2D mTexture;
27+
private Rect mRect;
2828
public RawImage rawImage;
2929
public RawImage textureImage;
30-
public Vector2 cameraSize = new Vector2(640, 480);
31-
public int cameraFPS = 15;
30+
public Vector2 cameraSize = new Vector2(640, 480);
31+
public int cameraFPS = 15;
3232
bool running = false;
3333
int timestamp = 0;
3434
bool _sharingImage = false;
3535

3636
// Use this for initialization
37-
void Start ()
38-
{
37+
void Start()
38+
{
3939
bool appReady = CheckAppId();
4040
if (!appReady) return;
4141

42-
InitEngine();
42+
InitEngine();
4343
InitTexture();
4444

4545
#if !UNITY_WEBGL || UNITY_EDITOR
@@ -86,7 +86,7 @@ public void WebShareScreen()
8686
mRtcEngine.StartScreenCaptureForWeb();
8787
}
8888

89-
89+
9090
// On Native only, after stopping screenshare, you need to choose the
9191
// external source mode again before joining the channel.
9292
// On Web, it is preferred to enable external source while in the channel
@@ -128,8 +128,8 @@ void HandleToggleValueChange(bool isScreenOn)
128128
minBitrate = 1,
129129
orientationMode = ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE,
130130
degradationPreference = DEGRADATION_PREFERENCE.MAINTAIN_FRAMERATE,
131-
mirrorMode = VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_DISABLED
132-
// note: mirrorMode is not effective for WebGL
131+
mirrorMode = VIDEO_MIRROR_MODE_TYPE.VIDEO_MIRROR_MODE_DISABLED
132+
// note: mirrorMode is not effective for WebGL
133133
};
134134
mRtcEngine.SetVideoEncoderConfiguration(config);
135135

@@ -199,18 +199,18 @@ void StopSharing()
199199
}
200200

201201
void InitEngine()
202-
{
202+
{
203203
mRtcEngine = IRtcEngine.GetEngine(APP_ID);
204-
mRtcEngine.SetLogFile("log.txt");
205-
mRtcEngine.SetChannelProfile(CHANNEL_PROFILE.CHANNEL_PROFILE_LIVE_BROADCASTING);
206-
mRtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
207-
mRtcEngine.EnableAudio();
208-
mRtcEngine.EnableVideo();
209-
mRtcEngine.EnableVideoObserver();
204+
mRtcEngine.SetLogFile("log.txt");
205+
mRtcEngine.SetChannelProfile(CHANNEL_PROFILE.CHANNEL_PROFILE_LIVE_BROADCASTING);
206+
mRtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
207+
mRtcEngine.EnableAudio();
208+
mRtcEngine.EnableVideo();
209+
mRtcEngine.EnableVideoObserver();
210210

211211
// Web: calling this before joining creates publish error
212212
// as we publish new created canvas source
213-
//mRtcEngine.SetExternalVideoSource(true, false);
213+
//mRtcEngine.SetExternalVideoSource(true, false);
214214

215215
mRtcEngine.OnJoinChannelSuccess += OnJoinChannelSuccessHandler;
216216
mRtcEngine.OnLeaveChannel += OnLeaveChannelHandler;
@@ -219,16 +219,16 @@ void InitEngine()
219219
mRtcEngine.OnConnectionLost += OnConnectionLostHandler;
220220
mRtcEngine.OnUserJoined += OnUserJoinedHandler;
221221
mRtcEngine.OnUserOffline += OnUserOfflineHandler;
222-
}
222+
}
223223

224-
void JoinChannel()
225-
{
224+
void JoinChannel()
225+
{
226226
int ret = mRtcEngine.JoinChannelByKey(TOKEN, CHANNEL_NAME, "", 0);
227227
// int ret = mRtcEngine.JoinChannel(CHANNEL_NAME, "", 0);
228228
Debug.Log(string.Format("JoinChannel ret: ${0}", ret));
229-
}
229+
}
230230

231-
bool CheckAppId()
231+
bool CheckAppId()
232232
{
233233
logger = new Logger(logText);
234234
return logger.DebugAssert(APP_ID.Length > 10, "<color=red>[STOP] Please fill in your appId in Canvas!!!!</color>");
@@ -268,12 +268,12 @@ void OnSDKWarningHandler(int warn, string msg)
268268
{
269269
logger.UpdateLog(string.Format("OnSDKWarning warn: {0}, msg: {1}", warn, msg));
270270
}
271-
271+
272272
void OnSDKErrorHandler(int error, string msg)
273273
{
274274
logger.UpdateLog(string.Format("OnSDKError error: {0}, msg: {1}", error, msg));
275275
}
276-
276+
277277
void OnConnectionLostHandler()
278278
{
279279
logger.UpdateLog(string.Format("OnConnectionLost "));
@@ -283,8 +283,8 @@ void OnApplicationQuit()
283283
{
284284
if (mRtcEngine != null)
285285
{
286-
mRtcEngine.LeaveChannel();
287-
mRtcEngine.DisableVideoObserver();
286+
mRtcEngine.LeaveChannel();
287+
mRtcEngine.DisableVideoObserver();
288288
IRtcEngine.Destroy();
289289
mRtcEngine = null;
290290
}
@@ -309,7 +309,7 @@ private void makeVideoView(uint uid)
309309
{
310310
return; // reuse
311311
}
312-
312+
313313
// create a GameObject and assign to this new user
314314
VideoSurface videoSurface = makeImageSurface(uid.ToString());
315315
if (!ReferenceEquals(videoSurface, null))
@@ -339,7 +339,7 @@ public VideoSurface makeImageSurface(string goName)
339339
GameObject canvas = GameObject.Find("Canvas");
340340
if (canvas != null)
341341
{
342-
go.transform.parent = canvas.transform;
342+
go.transform.SetParent(canvas.transform);
343343
Debug.Log("add video view");
344344
}
345345
else
@@ -353,7 +353,7 @@ public VideoSurface makeImageSurface(string goName)
353353
Debug.Log("position x " + xPos + " y: " + yPos);
354354
go.transform.localPosition = new Vector3(xPos, yPos, 0f);
355355
//go.transform.localPosition = new Vector3(10, 10, 0f);
356-
go.transform.localScale = new Vector3(3 * 1.6666f, 3f, 1f);
356+
go.transform.localScale = new Vector3(1 * 1.33333f, 1f, 1f);
357357

358358
// configure videoSurface
359359
VideoSurface videoSurface = go.AddComponent<VideoSurface>();

Assets/WebGLTemplates/AgoraTemplate/AgoraWebSDK/agorartcenginev2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,10 @@ async function SetVideoEncoderConfiguration(
531531
};
532532
if (localTracks.videoTrack == null) {
533533
AgoraRTC.createCameraVideoTrack({ encoderConfig: updatedConfig });
534-
} else {
534+
} else if (!localTracks.videoTrack.customVideoEnabled) {
535535
localTracks.videoTrack && await localTracks.videoTrack.setEncoderConfiguration(updatedConfig);
536536
}
537+
client_manager.setVideoConfiguration(updatedConfig);
537538
}
538539

539540
// Setting Live Transcoding Configuration

Assets/WebGLTemplates/AgoraTemplate/AgoraWebSDK/libs/clientmanager.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class ClientManager {
1515
this._storedChannelProfile = 0; // channel profile saved before join is called
1616
this._inChannel = false;
1717
this._streamMessageRetry = false;
18+
this._customVideoConfiguration = {
19+
bitrateMax:undefined,
20+
bitrateMin:undefined,
21+
optimizationMode:"detail",
22+
frameRate:undefined,
23+
width:undefined,
24+
height:undefined
25+
};
1826
}
1927

2028
manipulate() {}
@@ -40,7 +48,7 @@ class ClientManager {
4048
_logger("created new client");
4149
this.client = AgoraRTC.createClient({ mode: mode, codec: "vp8" });
4250
if (mode == "live") {
43-
this.client_role = 2;
51+
this.client_role = 1;
4452
setClientMode_LIVE(); // let multichannel know
4553
} // else default is "rtc"
4654
}
@@ -576,18 +584,19 @@ class ClientManager {
576584
await this.client.unpublish(localTracks.videoTrack);
577585
}
578586

579-
var stream = mainCanvas.captureStream(15); // 25 FPS
587+
var stream = mainCanvas.captureStream(this._customVideoConfiguration.frameRate??15); // 15 FPS
580588
var CustomVideoTrackInitConfig = {
581-
bitrateMax: 400,
582-
bitrateMin: 200,
589+
bitrateMax: this._customVideoConfiguration.bitrateMax??400,
590+
bitrateMin: this._customVideoConfiguration.bitrateMin??200,
583591
mediaStreamTrack: stream.getTracks()[0],
584-
optimizationMode: "detail",
592+
optimizationMode: this._customVideoConfiguration.optimizationMode
585593
};
586594

587595
[localTracks.videoTrack] = await Promise.all([
588596
AgoraRTC.createCustomVideoTrack(CustomVideoTrackInitConfig),
589597
]);
590598
localTracks.videoTrack.play("local-player");
599+
localTracks.videoTrack.customVideoEnabled = true;
591600
await this.client.publish(localTracks.videoTrack);
592601
} else if (enable == 0) {
593602
localTracks.videoTrack.stop();
@@ -824,7 +833,14 @@ class ClientManager {
824833
event_manager.raise
825834
return -1;
826835
}
827-
828836
return 0;
829837
}
838+
839+
setVideoConfiguration(config) {
840+
if (config.width) this._customVideoConfiguration.width = config.width;
841+
if (config.height) this._customVideoConfiguration.height = config.height;
842+
if (config.frameRate) this._customVideoConfiguration.frameRate = config.frameRate;
843+
if (config.bitrateMin) this._customVideoConfiguration.bitrateMin = config.bitrateMin;
844+
if (config.bitrateMax) this._customVideoConfiguration.bitrateMax = config.bitrateMax;
845+
}
830846
}

Assets/WebGLTemplates/AgoraTemplate/AgoraWebSDK/libs/engineglobals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// conversion to new engine
2-
var SDK_VERSION = "0.3.5.0.463100";
2+
var SDK_VERSION = "0.3.5.0.463100.f1";
33

44
let client_manager = new ClientManager();
55
let dataBuilder = DataBuilder();

0 commit comments

Comments
 (0)