public IEnumerator CallVideoEncoderMethods()
        {
            var       context       = NativeMethods.ContextCreate(0, encoderType);
            const int width         = 1280;
            const int height        = 720;
            var       renderTexture = CreateRenderTexture(width, height);
            var       stream        =
                NativeMethods.ContextCreateVideoStream(context, renderTexture.GetNativeTexturePtr(), width, height);
            var callback = NativeMethods.GetRenderEventFunc(context);

            // TODO::
            // note:: You must call `InitializeEncoder` method after `NativeMethods.ContextCaptureVideoStream`
            VideoEncoderMethods.InitializeEncoder(callback);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.Encode(callback);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.FinalizeEncoder(callback);
            yield return(new WaitForSeconds(1.0f));

            NativeMethods.ContextDeleteVideoStream(context, stream);
            NativeMethods.ContextDestroy(0);

            UnityEngine.Object.DestroyImmediate(renderTexture);
        }
Example #2
0
        public IEnumerator CallVideoEncoderMethods()
        {
            var context  = NativeMethods.ContextCreate(0, encoderType);
            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       track         = NativeMethods.ContextCreateVideoTrack(context, "video", renderTexture.GetNativeTexturePtr());
            var       sender        = NativeMethods.PeerConnectionAddTrack(peer, track, streamId);

            var callback = NativeMethods.GetRenderEventFunc(context);

            // TODO::
            // note:: You must call `InitializeEncoder` method after `NativeMethods.ContextCaptureVideoStream`
            NativeMethods.ContextSetVideoEncoderParameter(context, track, width, height);
            VideoEncoderMethods.InitializeEncoder(callback, track);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.Encode(callback, track);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.FinalizeEncoder(callback, track);
            yield return(new WaitForSeconds(1.0f));

            NativeMethods.PeerConnectionRemoveTrack(peer, sender);
            NativeMethods.ContextDeleteMediaStreamTrack(context, track);
            NativeMethods.ContextDeleteMediaStream(context, stream);
            NativeMethods.ContextDeletePeerConnection(context, peer);
            NativeMethods.ContextDestroy(0);
            UnityEngine.Object.DestroyImmediate(renderTexture);
        }
        public IEnumerator CallVideoDecoderMethods()
        {
            if (encoderType == EncoderType.Hardware)
            {
                //Todo: If Support Codec VP8/VP9 on HardwareEncoder or Support Codec H264 on Decoder, it can test on hardware encoder.
                yield break;
            }

            var       context        = NativeMethods.ContextCreate(0, encoderType, true);
            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);
            var       rendererId     = NativeMethods.GetVideoRendererId(renderer);

            NativeMethods.VideoTrackAddOrUpdateSink(track, renderer);

            var renderEvent        = NativeMethods.GetRenderEventFunc(context);
            var updateTextureEvent = NativeMethods.GetUpdateTextureFunc(context);

            NativeMethods.ContextSetVideoEncoderParameter(context, track, width, height, renderTexture.graphicsFormat, renderTexture.GetNativeTexturePtr());
            VideoEncoderMethods.InitializeEncoder(renderEvent, track);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.Encode(renderEvent, track);
            yield return(new WaitForSeconds(1.0f));

            // this method is not supported on Direct3D12
            VideoDecoderMethods.UpdateRendererTexture(updateTextureEvent, receiveTexture, rendererId);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.FinalizeEncoder(renderEvent, track);
            yield return(new WaitForSeconds(1.0f));

            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, encoderType, true);
            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);

            Assert.AreEqual(CodecInitializationResult.NotInitialized, NativeMethods.GetInitializationResult(context, track));

            // todo:: You must call `InitializeEncoder` method after `NativeMethods.ContextCaptureVideoStream`
            NativeMethods.ContextSetVideoEncoderParameter(
                context, track, width, height, renderTexture.graphicsFormat, renderTexture.GetNativeTexturePtr());
            VideoEncoderMethods.InitializeEncoder(callback, track);
            yield return(new WaitForSeconds(1.0f));

            // todo:: NativeMethods.GetInitializationResult returns CodecInitializationResult.NotInitialized
            Assert.AreEqual(CodecInitializationResult.Success, NativeMethods.GetInitializationResult(context, track));

            VideoEncoderMethods.Encode(callback, track);
            yield return(new WaitForSeconds(1.0f));

            VideoEncoderMethods.FinalizeEncoder(callback, track);
            yield return(new WaitForSeconds(1.0f));

            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);
        }