RenderHeads / UnityPlugin-AVProVideo

AVPro Video is a multi-platform Unity plugin for advanced video playback
https://www.renderheads.com/products/avpro-video/
231 stars 28 forks source link

Create Action to allow notification after Seek completes, and texture frame has been generated #895

Open pnostudiodeveloper opened 3 years ago

pnostudiodeveloper commented 3 years ago

Is your feature request related to a problem? Please describe. My app uses both AVPro Video and AVPro MovieCapture to render videos for users. It uses SeekToFrameRelative() to move through a paused video frame by frame, as fast as the device can handle. This requires notification of when the MediaPlayer has actually generated the texture for each frame after SeekToFrameRelative - not just the notification provided by SeekCompleted, which is sent before the seeked frame's texture has actually been created.

Describe the solution you'd like Create a reliable, safe way, to receive a notification from an action or a variable when the first texture has been rendered by MediaPlayer after a Seek operation.

Describe alternatives you've considered Using GetTextureFrameCount() - however, this did not seem to be working properly on one Android phone - Motorola Moto E5.

Additional context I created the following implementation that works successfully inside of my RenderFrame coroutine to check when the frame is available. Since I'm not an expert on memory handling, I'm hoping that you may be able to use your expertise to work this into a permanent solution available inside the framework. `

    RenderHeads.Media.AVProVideo.MediaPlayer player;
    IntPtr PreviousTexture;

    public IEnumerator RenderFrame()
    {
        PointIntPtr(PreviousFrame, player);

        player.SeekToFrameRelative(1);

        yield return new WaitUntil(() => HasNewFrame(PreviousTexture, player);
    }

    public void PointIntPtr(IntPtr intPtr, MediaPlayer mediaPlayer)
    {
        try
        {
            intPtr = ((Texture2D)mediaPlayer.TextureProducer.GetTexture(0)).GetNativeTexturePtr();
        }
        catch
        {
            Debug.Log("Could not point IntPtr to Texture: " + mediaPlayer.gameObject.name);
        }
    }

    public bool HasNewFrame(IntPtr oldFrame, MediaPlayer mediaPlayer)
    {
        try
        {
            IntPtr newFrame = ((Texture2D)mediaPlayer.TextureProducer.GetTexture(0)).GetNativeTexturePtr();

            return oldFrame != newFrame;
        }
        catch
        {
            Debug.Log("Could not point IntPtr to Texture: " + mediaPlayer.gameObject.name);

            return false;
        }
    }`
pnostudiodeveloper commented 3 years ago

Wanted to follow up, and note that this issue is generally also related to a past issue, where you guys had asked for a demo project: https://github.com/RenderHeads/UnityPlugin-AVProVideo/issues/702

I am emailing over a Demo Project - AVPro Frame Stepping Demo - as suggested in that post. The email its coming from is visualmusicdesign@gmail.com

This demo has both AVPro Video and MovieCapture in it.

It moves through a paused video frame by frame using a script attached to the Main Camera - Seek by Stepping.

While the script is running, you can see the following outputted on the Seek by Stepping component:

  1. The frame the video is currently on
  2. The overall speed the video is rendering at

The main reason I am following up, is that I've noticed that this operation works considerably less efficiently on Windows - about half the speed with one video.
My app actually has up to 4 videos loaded simultaneously for various effects - which is generally fine, and 100% with Unity VideoPlayer. The only time having 4 videos becomes a problem is when I am seeking by stepping on Windows with AVPro Video, where the process runs unusably slow.

I'm not sure why AVPro Video would be so much slower to return a texture using SeekToFrameRelative() on Windows instead of Mac, especially compared to Unity VideoPlayer.

Maybe there's nothing you can do, but I thought I would pass on the demo project, so that at least you guys can fully understand what both my app, and the user from issue 702 are trying to do, and if there are any improvements that can be made for either:

  1. Getting more reliable, quick notification when the texture is ready after seeking
  2. Improving Seek by Stepping speed on Windows

Thank you!

pnostudiodeveloper commented 3 years ago

Emailed over a demo project with multiple videos loaded.

This will make is much easier to see the main issue for my app is on Windows with AVPro video - that stepping by frame with multiple videos is unusably slow.

For both demo projects, open SampleScene.unity in the scenes folder.

- The Seek by Stepping script is attached to the Main Camera object.

- For both projects, you can see the current Capture Rate outputted in the Inspector on the Seek by Stepping component.
- 
- This measures the current speed of the capture compared to realtime playback of the video, the metric for the speed of rendering.

- The Capture Rate for me tops out at:

    - 0.6x on MacOS

    - 0.08x on Windows - decreasing over time

Thank you, appreciate the time you may spend looking into this! Peter