1+ using System . Globalization ;
12using UnityEngine ;
23
34namespace SimpleWebRTC {
45 public class SwitchToNextCameraPosition : MonoBehaviour {
56
67 [ SerializeField ] private WebRTCConnection webRTCConnection ;
78 [ SerializeField ] private string cameraSwitchKeyword = "switch" ;
8- [ SerializeField ] private bool manualPositionSwitch = false ;
9- [ SerializeField ] private bool webRTCPositionSwitch = false ;
109 [ SerializeField ] private Transform [ ] cameraParentObjects ;
10+ [ Header ( "Trigger video streaming on sender" ) ]
11+ [ SerializeField ] private bool webRTCStartStopVideoStream = false ;
12+ [ Header ( "Trigger camera switch from receiver" ) ]
13+ [ SerializeField ] private bool webRTCPositionSwitch = false ;
1114 [ Header ( "Sending Camera Position to Sender" ) ]
12- [ SerializeField ] private bool enableSendingPosition = false ;
15+ [ SerializeField ] private bool syncCameraPosition = false ;
1316 [ SerializeField ] private float sendingIntervalInSeconds = 0.1f ;
1417
1518 private int cameraPositionCounter = 0 ;
1619 private float sendingIntervalCounter = 0 ;
1720
21+ // make sure the float decimal separator is converted correctly
22+ private NumberFormatInfo numberFormatInfo = new NumberFormatInfo { NumberDecimalSeparator = "." } ;
23+
1824 private void Start ( ) {
25+ webRTCConnection . Connect ( ) ;
26+
1927 if ( webRTCConnection . IsImmersiveSetupActive && cameraParentObjects . Length > 0 ) {
2028 webRTCConnection . VideoStreamingCamera . transform . SetPositionAndRotation ( Vector3 . zero , Quaternion . identity ) ;
2129 webRTCConnection . VideoStreamingCamera . transform . SetParent ( cameraParentObjects [ cameraPositionCounter ] , false ) ;
2230 }
2331 }
2432
2533 private void Update ( ) {
26- if ( manualPositionSwitch ) {
27- manualPositionSwitch = false ;
28- OnMessageReceived ( cameraSwitchKeyword ) ;
34+ // example: use the input system keyboard for starting/stopping the video stream
35+ //if (Keyboard.current.vKey.wasPressedThisFrame) {
36+ // webRTCConnection.StartVideoTransmission();
37+ //}
38+ //if (Keyboard.current.bKey.wasPressedThisFrame) {
39+ // webRTCConnection.StopVideoTransmission();
40+ //}
41+
42+ // example: use the OVR input for sending the camera switch
43+ //if (OVRInput.Get(OVRInput.Button.Start)) {
44+ // webRTCConnection.SendDataChannelMessage(cameraSwitchKeyword);
45+ //}
46+
47+ // use the boolean flag for starting/stopping the camera stream
48+ if ( webRTCStartStopVideoStream && webRTCConnection . IsSender && webRTCConnection . IsWebRTCActive ) {
49+ webRTCStartStopVideoStream = false ;
50+ if ( webRTCConnection . IsVideoTransmissionActive ) {
51+ webRTCConnection . StopVideoTransmission ( ) ;
52+ } else {
53+ webRTCConnection . StartVideoTransmission ( ) ;
54+ }
2955 }
30- if ( webRTCPositionSwitch && webRTCConnection . IsWebRTCActive ) {
56+
57+ // use the boolean flag for sending the camera switch
58+ if ( webRTCPositionSwitch && webRTCConnection . IsWebRTCActive && webRTCConnection . IsReceiver ) {
3159 webRTCPositionSwitch = false ;
3260 webRTCConnection . SendDataChannelMessage ( cameraSwitchKeyword ) ;
3361 }
34- if ( enableSendingPosition && webRTCConnection . IsWebRTCActive && webRTCConnection . IsImmersiveSetupActive && webRTCConnection . IsReceiver && webRTCConnection . ExperimentalSupportFor6DOF ) {
62+ if ( syncCameraPosition && webRTCConnection . IsWebRTCActive && webRTCConnection . IsImmersiveSetupActive && webRTCConnection . IsReceiver && webRTCConnection . ExperimentalSupportFor6DOF ) {
3563 sendingIntervalCounter += Time . deltaTime ;
3664 if ( sendingIntervalCounter >= sendingIntervalInSeconds ) {
3765 sendingIntervalCounter = 0 ;
@@ -40,12 +68,22 @@ private void Update() {
4068 }
4169 }
4270
71+ private void OnDestroy ( ) {
72+ webRTCConnection . Disconnect ( ) ;
73+ }
74+
4375 public void OnMessageReceived ( string message ) {
4476 if ( webRTCConnection . IsImmersiveSetupActive && webRTCConnection . IsSender ) {
4577 string [ ] trylocalPosition = message . Split ( "||||" ) ;
4678 bool isPositionMessage = trylocalPosition . Length == 3 ;
4779 if ( webRTCConnection . ExperimentalSupportFor6DOF && isPositionMessage ) {
48- webRTCConnection . VideoStreamingCamera . transform . localPosition = new Vector3 ( float . Parse ( trylocalPosition [ 0 ] ) , float . Parse ( trylocalPosition [ 1 ] ) , float . Parse ( trylocalPosition [ 2 ] ) ) ;
80+ Debug . Log ( $ "x = { trylocalPosition [ 0 ] } = { float . Parse ( trylocalPosition [ 0 ] , numberFormatInfo ) } , y = { trylocalPosition [ 1 ] } = { float . Parse ( trylocalPosition [ 1 ] , numberFormatInfo ) } , z = { trylocalPosition [ 2 ] } = { float . Parse ( trylocalPosition [ 2 ] , numberFormatInfo ) } ") ;
81+
82+ webRTCConnection . VideoStreamingCamera . transform . localPosition = new Vector3 (
83+ float . Parse ( trylocalPosition [ 0 ] , numberFormatInfo ) ,
84+ float . Parse ( trylocalPosition [ 1 ] , numberFormatInfo ) ,
85+ float . Parse ( trylocalPosition [ 2 ] , numberFormatInfo ) ) ;
86+
4987 } else if ( message . ToLower ( ) . Equals ( cameraSwitchKeyword . ToLower ( ) ) && cameraParentObjects . Length > 0 ) {
5088 cameraPositionCounter = ( cameraPositionCounter + 1 ) % cameraParentObjects . Length ;
5189 webRTCConnection . VideoStreamingCamera . transform . SetParent ( cameraParentObjects [ cameraPositionCounter ] , false ) ;
0 commit comments