Snippets

Depthkit Frame stepped AVPro 2.x timeline classes for 360 video capture

Created by Michael Allison last modified
//MediaPlayerControlBehavior.cs

// You need to define AVPRO_PACKAGE_TIMELINE manually to use this script
// We could set up the asmdef to reference the package, but the package doesn't 
// existing in Unity 2017 etc, and it throws an error due to missing reference
//#define AVPRO_PACKAGE_TIMELINE
#if (UNITY_2018_1_OR_NEWER && AVPRO_PACKAGE_TIMELINE)
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using System.Collections.Generic;
using System.Reflection;

//-----------------------------------------------------------------------------
// Copyright 2020-2021 RenderHeads Ltd.  All rights reserved.
//-----------------------------------------------------------------------------

namespace RenderHeads.Media.AVProVideo.Playables
{
    public class MediaPlayerControlBehaviour : PlayableBehaviour
    {
        public MediaPlayer mediaPlayer = null;

        public MediaReference mediaReference = null;
        public float audioVolume = 1f;
        public double startTime = -1.0;
        public bool pauseOnEnd = true;
        public bool seekEveryFrame = false;
        public MethodInfo stopRenderCoroutine = null;

        public override void OnBehaviourPlay(Playable playable, FrameData info)
        {
            if (mediaPlayer != null)
            {
                if (Application.isPlaying)
                {
                    if (mediaReference != null && mediaReference != mediaPlayer.MediaReference)
                    {
                        mediaPlayer.OpenMedia(mediaReference, true);
                        if (mediaPlayer.Control != null)
                        {
                            if (seekEveryFrame)
                            {
                                mediaPlayer.Control.Seek(startTime);
                                WaitForFinishSeek();
                            }
                            else
                            {
                                mediaPlayer.Control.SeekFast(startTime);
                                mediaPlayer.Play();
                            }
                        }
                    }
                    else
                    {
                        if (mediaPlayer.Control != null)
                        {
                            if(seekEveryFrame)
                            {
                                mediaPlayer.Control.Seek(startTime);
                                WaitForFinishSeek();
                            }
                            else
                            {
                                mediaPlayer.Control.SeekFast(startTime);
                            }
                        }
                        if(!seekEveryFrame) mediaPlayer.Play();
                    }
                }
            }
        }

        public override void OnBehaviourPause(Playable playable, FrameData info)
        {
            if (mediaPlayer != null)
            {
                if (pauseOnEnd)
                {
                    mediaPlayer.Pause();
                }
            }
        }

        public override void OnGraphStart(Playable playable)
        {
            base.OnGraphStart(playable);
            if(seekEveryFrame) stopRenderCoroutine?.Invoke(mediaPlayer, null);
        }

        void WaitForFinishSeek()
        {
            while (mediaPlayer.Control.IsSeeking())
            {
                mediaPlayer.Player.Update();
                mediaPlayer.Player.EndUpdate();
            }

            mediaPlayer.Player.Render();
        }

        public override void ProcessFrame(Playable playable, FrameData info, object playerData)
        {
            if (mediaPlayer != null)
            {
                if (mediaPlayer.Control != null)
                {
                    if (seekEveryFrame)
                    {
                        mediaPlayer.Control.Seek(startTime > 0 ? startTime + playable.GetTime() : playable.GetTime());
                        WaitForFinishSeek();
                    }
                }
            }
        }
    }
}
#endif

//MediaPlayerControlAsset.cs

// You need to define AVPRO_PACKAGE_TIMELINE manually to use this script
// We could set up the asmdef to reference the package, but the package doesn't 
// existing in Unity 2017 etc, and it throws an error due to missing reference
//#define AVPRO_PACKAGE_TIMELINE
#if (UNITY_2018_1_OR_NEWER && AVPRO_PACKAGE_TIMELINE)
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using System.Collections.Generic;
using System.Reflection;

//-----------------------------------------------------------------------------
// Copyright 2020-2021 RenderHeads Ltd.  All rights reserved.
//-----------------------------------------------------------------------------

namespace RenderHeads.Media.AVProVideo.Playables
{
	[System.Serializable]
	public class MediaPlayerControlAsset : PlayableAsset
	{
		public Object binding { get; set; }
		//public ExposedReference<MediaPlayer> mediaPlayer;

		public MediaReference mediaReference;

		[Range(0f, 1f)]
		public float audioVolume = 1f;
		public double startTime = -1.0;
		public bool pauseOnEnd = true;
		public bool seekEveryFrame = false;

		public override Playable CreatePlayable (PlayableGraph graph, GameObject owner)
		{
			var playable = ScriptPlayable<MediaPlayerControlBehaviour>.Create(graph);

			var behaviour = playable.GetBehaviour();
			//behaviour.mediaPlayer = mediaPlayer.Resolve(graph.GetResolver());
			behaviour.audioVolume = audioVolume;
			behaviour.pauseOnEnd = pauseOnEnd;
			behaviour.startTime = startTime;
			behaviour.mediaReference = mediaReference;
			behaviour.mediaPlayer = (MediaPlayer)binding;
			behaviour.seekEveryFrame = seekEveryFrame;
			behaviour.stopRenderCoroutine = behaviour.mediaPlayer.GetType().GetMethod("StopRenderCoroutine", BindingFlags.NonPublic | BindingFlags.Instance);

			return playable;
		}
	}
}
#endif

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.