public IEnumerator CallVideoDecoderMethods() { var context = NativeMethods.ContextCreate(0); const int width = 1280; const int height = 720; var renderTexture = CreateRenderTexture(width, height); var receiveTexture = CreateRenderTexture(width, height); var source = NativeMethods.ContextCreateVideoTrackSource(context); var track = NativeMethods.ContextCreateVideoTrack(context, "video", source); var renderer = NativeMethods.CreateVideoRenderer(context, OnVideoFrameResize, true); var rendererId = NativeMethods.GetVideoRendererId(renderer); NativeMethods.VideoTrackAddOrUpdateSink(track, renderer); var renderEvent = NativeMethods.GetRenderEventFunc(context); var updateTextureEvent = NativeMethods.GetUpdateTextureFunc(context); yield return(new WaitForSeconds(1.0f)); VideoTrackSource.EncodeData data = new VideoTrackSource.EncodeData(renderTexture, source); IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VideoTrackSource.EncodeData))); Marshal.StructureToPtr(data, ptr, true); VideoEncoderMethods.Encode(renderEvent, ptr); yield return(new WaitForSeconds(1.0f)); // this method is not supported on Direct3D12 VideoDecoderMethods.UpdateRendererTexture(updateTextureEvent, receiveTexture, rendererId); yield return(new WaitForSeconds(1.0f)); yield return(new WaitForSeconds(1.0f)); Marshal.FreeHGlobal(ptr); NativeMethods.VideoTrackRemoveSink(track, renderer); NativeMethods.DeleteVideoRenderer(context, renderer); NativeMethods.ContextDeleteRefPtr(context, track); NativeMethods.ContextDeleteRefPtr(context, source); NativeMethods.ContextDestroy(0); UnityEngine.Object.DestroyImmediate(renderTexture); UnityEngine.Object.DestroyImmediate(receiveTexture); }
public IEnumerator CallVideoEncoderMethods() { var context = NativeMethods.ContextCreate(0); var peer = NativeMethods.ContextCreatePeerConnection(context); var stream = NativeMethods.ContextCreateMediaStream(context, "MediaStream"); var streamId = NativeMethods.MediaStreamGetID(stream).AsAnsiStringWithFreeMem(); Assert.IsNotEmpty(streamId); const int width = 1280; const int height = 720; var renderTexture = CreateRenderTexture(width, height); var source = NativeMethods.ContextCreateVideoTrackSource(context); var track = NativeMethods.ContextCreateVideoTrack(context, "video", source); var error = NativeMethods.PeerConnectionAddTrack(peer, track, streamId, out var sender); Assert.That(error, Is.EqualTo(RTCErrorType.None)); var callback = NativeMethods.GetRenderEventFunc(context); yield return(new WaitForSeconds(1.0f)); VideoTrackSource.EncodeData data = new VideoTrackSource.EncodeData(renderTexture, source); IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VideoTrackSource.EncodeData))); Marshal.StructureToPtr(data, ptr, true); VideoEncoderMethods.Encode(callback, ptr); yield return(new WaitForSeconds(1.0f)); Marshal.FreeHGlobal(ptr); Assert.That(NativeMethods.PeerConnectionRemoveTrack(peer, sender), Is.EqualTo(RTCErrorType.None)); NativeMethods.ContextDeleteRefPtr(context, track); NativeMethods.ContextDeleteRefPtr(context, stream); NativeMethods.ContextDeleteRefPtr(context, source); NativeMethods.ContextDeletePeerConnection(context, peer); NativeMethods.ContextDestroy(0); UnityEngine.Object.DestroyImmediate(renderTexture); }