Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Video Player Memory Leak

Discussion in 'Audio & Video' started by Piflik, Jul 16, 2021.

  1. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    The Video Player seems to have a memory leak when playing videos from an Url. Each time I load a new clip, the Memory Profiler shows an additional WindowsVideoMedia_DecodeTexture that never gets destroyed. This happens even if I just alternate between two different videos (both are loaded from my harddrive).

    It happens both in the Editor and in a build. In the Editor textures persist in memory when exiting playmode.

    Interestingly, the increase in memory usage the Windows Task Manager reports, is more than 10 times the size of that texture.

    After a couple of video loads, the Editor first stops playing the video's audio track and then stops loading the video altogether. It logs the following error message:

    Code (CSharp):
    1. WindowsVideoMedia error 0x8007000e while reading C:\Path\To\Video.mp4
    2.  
    3. Context: IMFSourceReader::WaitForSample in StepAllStreams
    4. Error details: Ran out of memory
    5.  
    6. Track types:
    7.    Unreadable Track
     
  2. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    256
    Hi Piflik!

    Can you please tell us what version of Unity is doing this?

    We have fixed a leak recently, having to do with destroying videos while a seek is ongoing. This fix landed in 2021.2.0a5. The backports to 2021.1, 2020.3 and 2019.4 are ongoing at the moment. If you are in a position to test with the latest 2021.2 alpha, you'll be able to see if the situation you are describing was addressed by the fix.

    If not, then feel free to submit a bug with your test scenario so we can validate and work on a fix if your scenario differs from what was fixed already.

    Thanks for bringing this to our attention and hope this situation gets resolved quickly,

    Dominique
     
    MartinTilo likes this.
  3. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    256
    Hi again,

    For what it's worth: I just tried playing a movie (VideoPlayer playing into Camera) and grabbing a memory profiler capture while playback is ongoing. I do see the Texture2D object named WindowsVideoMedia_DecodeTexture, using 456 bytes in my case. When I exit playmode, this texture goes away, so the scenario I'm testing doesn't have the leak you are describing. It's either because the fix mentioned above is addressing the problem, or because your steps differ from mine.

    So we'll definitely need more information (version number, repro steps, project) to see if there's anything more to do than to do than to wait for the backports to be completed.

    Let us know what you can do on your end so we can better understand the situation.

    Dominique
     
  4. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    If I only play one video, the texture gets deleted properly, but if I play a second one, the first one persists. If I alternate between these two videos, they create more of these textures, until it stops loading the videos. When I exit playmode, the last one will be cleaned up.

    In the project I use APIOnly and display the texture on a RawImage. I am not sure if it happens with preloaded clips, too, I load the videos via Url.

    I had it in 2020.3.12f1 and also in 2020.3.14f1

    As a side note: Each of these textures produce an error message ("No texture data available to upload") when I switch the target platform.
     
    Last edited: Jul 23, 2021
  5. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    256
    Hi,

    Thanks for the additional info. I think it's worth logging a bug so we can take a detailed look at the exact steps. For instance, it's not clear to me what happens when you "play a second one". Are you using a second VideoPlayer? Or just using a single VideoPlayer and changing the clip while it's playing? Or do you stop it before changing the clip? Etc. All of these details can lead to very different code paths internally so I want to make sure we're looking exactly at what you're experiencing.

    Please report back to this thread with the bug id when you have logged it so I can immediately start helping our customer QAs analyzing the issue.

    Dominique
     
  6. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Case ID is 1351889.

    I have one Videoplayer.
    I load one video from an Url and play it. At some point in the future, I play another video. The first video may or may not have been played till the end, but in any case I call
    VideoPlayer.Stop()
    before starting the new one.

    Relevant code:
    Code (CSharp):
    1.  
    2.  
    3. VideoPlayer _videoPlayer;
    4.  
    5. void Awake() {
    6.     _videoPlayer = GetComponent<VideoPlayer>();
    7.     _videoPlayer.prepareCompleted += StartPlay;
    8. }
    9.  
    10. public override void Play(string path) {  
    11.     _videoPlayer.Stop();              
    12.     _videoPlayer.source = UnityEngine.Video.VideoSource.Url;
    13.     _videoPlayer.url = path;
    14.  
    15.     _videoPlayer.Prepare();
    16. }
    17.  
    18. private void StartPlay(VideoPlayer vp) {
    19.     _videoPlayer.Play();
    20. }
    21.  
     
  7. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    256
    OK, thanks for submitting this!

    The customer QA who will pick this up will probably contact you soon, asking for more details about how to reproduce. If you could zip up a small project that has your code and relevant movie files and include it in the bug report, it'll help us replicate your setup.

    Have a good week-end,

    Dominique
     
  8. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    After further testing (mainly trying to reproduce it in a minimal project) it seems like the culprit is setting the VideoPlayer's
    frame
    property to 0 before loading the next video. If I don't do that, it doesn't leak. For now I can circumvent the issue that way.
     
    Last edited: Jul 26, 2021
    JumpWire likes this.
  9. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Here is the reproduction project.

    It has only one scene and one script. When you start Play in the scene, it gets the files in the "Videos" folder and each time the "Create Leak" button is pressed, it tries to load it into the VideoPlayer and create another WindowsVideoMedia_DecodeTexture. I have included one of my test videos in the folder, so it should be testable as is, but if you want to test if it only happens with certain videos, you can just put other videos in that folder..

    You can see these Textures using the Memory Profiler in Detailed mode when you navigate to "Not Saved" > "Texture2D" and scroll to the bottom of that list. These will persist until the Editor is restarted.

    I first noticed this leak in Unity 2020.3.12f1 and confirmed it in 2020.3.14f1
     

    Attached Files: