usingUnity.WebRTC;usingUnityEngine;usingUnityEngine.Experimental.Rendering;usingDepthkit;usingUnity.RenderStreaming;usingSystem.Collections;usingSystem;namespaceDepthkit{publicclassDepthkitMetadataChannel:DataChannelBase{ [SerializeField]privateClipm_clip=null;privatefloatlastMetadataRequestTime=0.0f;privateboolreceived=false;privateboolisStarted=false;internalenumMessageCode{Received,Request}voidAwake(){OnStartedChannel+=channelStarted;OnStoppedChannel+=channelStopped;}publicoverridevoidSetChannel(stringconnectionId,RTCDataChannelchannel){Debug.Log("DepthkitMetadataChannel::SetChannel()");base.SetChannel(connectionId,channel);}publicvoidSendMetadata(){Debug.Log("DepthkitMetadataChannel::SendMetadata()");// Load metadata from clipMetadatametadata=parseJsonMetadata(m_clip.metadataFile.text);// Modify metadata with the current clip bounds from the MeshSource componentBoundsbounds=m_clip.gameObject.GetComponent<MeshSource>().GetLocalBounds();metadata.boundsSize=bounds.size;metadata.boundsCenter=newVector3(bounds.center.x,bounds.center.y*-1.0f,bounds.center.z*-1.0f);// Modify metadata texture size with the video resolution to be sent over the networkVideoStreamSendervideoSender=GetComponent<VideoStreamSender>();if(videoSender){metadata.textureWidth=(int)videoSender.width;metadata.textureHeight=(int)videoSender.height;}Channel.Send(JsonUtility.ToJson(metadata));}protectedoverridevoidOnMessage(byte[]bytes){Debug.Log("DepthkitMetadataChannel::OnMessage()");if(IsLocal){MessageCodemsg;if(Enum.TryParse<MessageCode>(System.Text.Encoding.UTF8.GetString(bytes,0,bytes.Length),outmsg)){switch(msg){caseMessageCode.Request:Debug.Log("Remote requested metadata, sending.");received=false;SendMetadata();break;caseMessageCode.Received:Debug.Log("Remote acknowledged receipt of metadata.");received=true;break;default:break;}}}else{m_clip.LoadMetadata(System.Text.Encoding.UTF8.GetString(bytes,0,bytes.Length));StudioMeshSourcestudioMeshSource=m_clip.gameObject.GetComponent<StudioMeshSource>();if(studioMeshSource){studioMeshSource.ResetVolumeBounds();}Debug.Log("Remote metadata texture size: "+m_clip.metadata.textureWidth+"x"+m_clip.metadata.textureHeight);received=true;Channel.Send(MessageCode.Received.ToString());}}voidchannelStarted(stringconnectionId){Debug.Log("DepthkitMetadataChannel::channelStarted("+connectionId+")");isStarted=true;if(IsLocal){SendMetadata();}else{lastMetadataRequestTime=Time.timeSinceLevelLoad;}}voidchannelStopped(stringconnectionId){Debug.Log("DepthkitMetadataChannel::channelStopped("+connectionId+")");received=false;isStarted=false;}protectedvoidUpdate(){if(isStarted&&!IsLocal&&!received&&Time.timeSinceLevelLoad-lastMetadataRequestTime>2.5f){lastMetadataRequestTime=Time.timeSinceLevelLoad;Debug.Log("Requesting metadata");Channel.Send(MessageCode.Request.ToString());}}privateMetadataFromSinglePerspective(Metadata.MetadataSinglePerspectivemd){returnnewDepthkit.Metadata{_versionMajor=0,_versionMinor=4,format=md.format,boundsCenter=md.boundsCenter,boundsSize=md.boundsSize,textureWidth=md.textureWidth,textureHeight=md.textureHeight,perspectives=newMetadata.Perspective[]{newMetadata.Perspective{depthImageSize=md.depthImageSize,depthPrincipalPoint=md.depthPrincipalPoint,depthFocalLength=md.depthFocalLength,farClip=md.farClip,nearClip=md.nearClip,extrinsics=md.extrinsics,crop=md.crop,clipEpsilon=md.clipEpsilon}}};}privateMetadataparseJsonMetadata(stringjsonString){constfloateps=0.00000001f;Metadatametadata;Metadata.MetadataVersionmdVersion=JsonUtility.FromJson<Metadata.MetadataVersion>(jsonString);// Read and upgrade old single perspective format.if(mdVersion._versionMajor==0&&mdVersion._versionMinor<3){Metadata.MetadataSinglePerspectivemd=JsonUtility.FromJson<Metadata.MetadataSinglePerspective>(jsonString);//for version 1.0 (from Depthkit Visualize) fill in defaults for missing parametersif(mdVersion.format=="perpixel"&&mdVersion._versionMinor==1){md.numAngles=1;// check if we have a zero'd crop (is this possible?), if so default to full windowif(md.crop.x<=eps&&md.crop.y<=eps&&md.crop.z<=eps&&md.crop.w<=eps){md.crop=newVector4(0.0f,0.0f,1.0f,1.0f);}md.extrinsics=Matrix4x4.identity;}if(md.clipEpsilon<eps){md.clipEpsilon=0.005f;// default depth clipping epsilon, set for older versions of metadata}metadata=FromSinglePerspective(md);metadata.numRows=1;metadata.numColumns=1;}else{// Read multiperspective format.metadata=JsonUtility.FromJson<Metadata>(jsonString);if(mdVersion._versionMinor==3){metadata.numRows=1;metadata.numColumns=metadata.numAngles;}}metadata.perspectivesCount=metadata.perspectives.Length;returnmetadata;}}}
usingSystem.Linq;usingUnityEngine;usingUnityEngine.UI;namespaceUnity.RenderStreaming.Samples{classSimpleConnection:MonoBehaviour{#pragma warning disable 0649 [SerializeField]privateSignalingManagersignalingManager; [SerializeField]privateSingleConnectionsingleConnection; [SerializeField]privatestringconnectionId;#pragma warning restore 0649internalenumConnectionState{Disconnected,Connecting,Connected,Disconnecting}privateConnectionStateconnectionState=ConnectionState.Disconnected;voidAwake(){foreach(varaudioinsingleConnection.Streams.OfType<AudioStreamReceiver>()){audio.OnUpdateReceiveAudioSource+=source=>{Debug.Log("Audio source updated, configuring");source.loop=true;source.Play();};}}voidStart(){connectionState=ConnectionState.Disconnected;for(inti=0;i<Microphone.devices.Length;i++){Debug.Log("Audio Device "+i+": "+Microphone.devices[i]);}if(!signalingManager.runOnAwake){signalingManager.Run();}}privatefloatconnectionCreatedTime=0.0f;protectedvoidUpdate(){// Note: this delay of 1 second is intended to allow time for establishing a connection with the signaling// server in leu of a proper API for detecting that the signaling server is connected.// If modifying this script to be powered by a GUI where the user initiates the CreateConnection call below,// there will be a built in delay by virtue of the user interaction and this delay can likely be removedif(Time.timeSinceLevelLoad<2.5f)return;if(singleConnection.IsConnected(connectionId)){if(connectionState==ConnectionState.Connecting){Debug.Log("Connected "+connectionId);connectionState=ConnectionState.Connected;}}else{if(connectionState==ConnectionState.Disconnected){Debug.Log("Creating connection "+connectionId);singleConnection.CreateConnection(connectionId);connectionState=ConnectionState.Connecting;connectionCreatedTime=Time.timeSinceLevelLoad;}elseif(connectionState==ConnectionState.Connected){Debug.Log("Deleting connection "+connectionId);singleConnection.DeleteConnection(connectionId);connectionState=ConnectionState.Disconnected;}elseif(connectionState==ConnectionState.Connecting&&Time.timeSinceLevelLoad-connectionCreatedTime>5.0f){if(singleConnection.ExistConnection(connectionId))singleConnection.DeleteConnection(connectionId);Debug.Log("Timed out creating connection, deleting "+connectionId);connectionState=ConnectionState.Disconnected;}}//Debug.Log("Connection Exists: " + singleConnection.ExistConnection(connectionId) + ". IsConnected: " + singleConnection.IsConnected(connectionId) + ". ConnectionState: " + connectionState);}voidOnDestroy(){if(singleConnection.IsConnected(connectionId)){singleConnection.DeleteConnection(connectionId);}}voidOnApplicationQuit(){if(singleConnection.IsConnected(connectionId)){singleConnection.DeleteConnection(connectionId);}}}}
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnity.RenderStreaming;publicclassVideoSenderBitrateOverride:MonoBehaviour{publicVideoStreamSendervideoStreamSender;publicuintminBitrate=5000;publicuintmaxBirate=30000;// Start is called before the first frame updatevoidStart(){videoStreamSender.SetBitrate(minBitrate,maxBirate);}// Update is called once per framevoidUpdate(){}}
/************************************************************************************Depthkit Unity SDK License v1Copyright 2016-2019 Scatter All Rights reserved.Licensed under the Scatter Software Development Kit License Agreement (the "License");you may not use this SDK except in compliance with the License,which is provided at the time of installation or download,or which otherwise accompanies this software in either electronic or hard copy form.You may obtain a copy of the License at http://www.depthkit.tv/license-agreement-v1Unless required by applicable law or agreed to in writing,the SDK distributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions and limitations under the License.************************************************************************************/usingUnityEngine;usingSystem;usingSystem.Collections;usingDepthkit;usingUnity.RenderStreaming;namespaceDepthkit{/// <summary>/// Implementation of the Depthkit player with the Unity VideoPlayer-based backend./// </summary> [AddComponentMenu("Depthkit/Players/Depthkit WebRTC Player")]publicclassWebrtcPlayer:Depthkit.ClipPlayer{ [SerializeField]protectedVideoStreamReceiverm_videoReceiver; [SerializeField, HideInInspector]protectedAudioSourcem_audioSource;protectedintm_frame=0;voidStart(){m_frame=0;}publicoverridevoidCreatePlayer(){m_audioSource=gameObject.GetComponent<AudioSource>();if(m_audioSource==null){m_audioSource=gameObject.AddComponent<AudioSource>();// Note this just creates the audio source, it must be manually hooked up as the target of the AudioStreamReceiver}}publicoverrideboolIsPlayerCreated(){returnm_videoReceiver!=null;}publicoverrideboolIsPlayerSetup(){returnIsPlayerCreated();}/// <summary>/// Sets the video from a path. Assumed relative to data foldder file path.</summary>publicoverridevoidSetVideoPath(stringpath){}/// <summary>/// Get the absolute path to the video.</summary>publicoverridestringGetVideoPath(){return"";}publicoverridevoidStartVideoLoad(){}publicoverrideIEnumeratorLoad(){yieldreturnnull;}publicoverridevoidOnMetadataUpdated(Depthkit.Metadatametadata){/* do nothing */}publicoverrideIEnumeratorLoadAndPlay(){yieldreturnnull;}publicoverridevoidPlay(){}publicoverridevoidPause(){}publicoverridevoidStop(){}publicoverrideintGetCurrentFrame(){returnm_frame;}publicoverridedoubleGetCurrentTime(){return0.0;}publicoverridedoubleGetDuration(){returnDouble.PositiveInfinity;}publicoverrideTextureGetTexture(){++m_frame;returnm_videoReceiver?.texture;}publicoverrideboolIsTextureFlipped(){returnfalse;}publicoverrideGammaCorrectionGammaCorrectDepth(){if(QualitySettings.activeColorSpace==ColorSpace.Linear){#if UNITY_2018_2_OR_NEWERreturnGammaCorrection.LinearToGammaSpace;#elif UNITY_2017_1_OR_NEWER//https://issuetracker.unity3d.com/issues/regression-videos-are-dark-when-using-linear-color-space?page=1Debug.LogWarning("Unity Video Player does not display correct color on Windows between version 2017.1 and 2018.2. Use AVPro, switch to Gamma Color Space, or upgrade Unity to use Depthkit with this project.");returnGammaCorrection.LinearToGammaSpace2x;#elsereturnGammaCorrection.LinearToGammaSpace;#endif}else{returnGammaCorrection.None;}}publicoverrideGammaCorrectionGammaCorrectColor(){if(QualitySettings.activeColorSpace==ColorSpace.Linear){#if UNITY_2018_2_OR_NEWERreturnGammaCorrection.None;#elif UNITY_2017_1_OR_NEWERreturnGammaCorrection.LinearToGammaSpace;#elsereturnGammaCorrection.None;#endif}else{returnGammaCorrection.None;}}publicoverrideboolIsPlaying(){returnm_videoReceiver!=null&&m_videoReceiver.texture!=null;}publicoverridevoidRemoveComponents(){}publicoverridestringGetPlayerTypeName(){returntypeof(Depthkit.WebrtcPlayer).Name;}publicnewstaticstringGetPlayerPrettyName(){return"WebRTC Player";}publicoverridevoidSeek(floattoTime){}publicoverrideuintGetVideoWidth(){returnm_videoReceiver!=null&&m_videoReceiver.texture!=null?(uint)m_videoReceiver.texture.width:0u;}publicoverrideuintGetVideoHeight(){returnm_videoReceiver!=null&&m_videoReceiver.texture!=null?(uint)m_videoReceiver.texture.height:0u;}publicoverrideboolSupportsPosterFrame(){returntrue;}}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.