Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Assets/Libs/Hypernex.CCK/Hypernex.CCK.Unity.dll
Binary file not shown.
Binary file modified Assets/Libs/YoutubeDLSharp/YoutubeDLSharp.dll
Binary file not shown.
130 changes: 130 additions & 0 deletions Assets/Scripts/Game/Video/FFVideoPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using FFmpeg.Unity;
using Hypernex.CCK.Unity;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Hypernex.Game.Video
{
public class FFVideoPlayer : IVideoPlayer
{
private VideoPlayerDescriptor desc;
private FFPlayUnity videoPlayer;
private FFTexturePlayer texture;
private FFAudioPlayer audio;
private BufferAudioSource buffer;
private AudioSource audioSource;
private Material material;

public FFVideoPlayer(VideoPlayerDescriptor descriptor)
{
GameObject attachedObject = descriptor.gameObject;
desc = descriptor;
videoPlayer = attachedObject.GetComponent<FFPlayUnity>();
if (videoPlayer == null)
{
videoPlayer = attachedObject.AddComponent<FFPlayUnity>();
videoPlayer.videoOffset = -0.5d;
texture = attachedObject.AddComponent<FFTexturePlayer>();
audio = attachedObject.AddComponent<FFAudioPlayer>();
audio.bufferSize = 2f;
videoPlayer.texturePlayer = texture;
videoPlayer.audioPlayer = audio;
}
audioSource = descriptor.AudioOutput;
if (audioSource == null) audioSource = attachedObject.GetComponent<AudioSource>();
if (audioSource == null) audioSource = attachedObject.AddComponent<AudioSource>();
buffer = audioSource.GetComponent<BufferAudioSource>();
if (buffer == null) buffer = audioSource.gameObject.AddComponent<BufferAudioSource>();
buffer.audioPlayer = videoPlayer.audioPlayer;
buffer.audioSource = audioSource;
videoPlayer.texturePlayer.OnDisplay += OnDisplay;
descriptor.AudioOutput = audioSource;
audioSource.outputAudioMixerGroup = Init.Instance.WorldGroup;
audioSource.spatialize = true;
descriptor.CurrentVideoPlayer = this;
}

private void OnDisplay(Texture2D tex)
{
foreach (Renderer renderer in desc.VideoOutputs)
{
if (renderer.material == null)
{
renderer.material = new Material(Shader.Find("Universal Render Pipeline/Lit"));
desc.ShaderEmissionProperty = "_EmissionMap";
}
renderer.material.mainTexture = tex;
renderer.material.mainTextureScale = new Vector2(1f, -1f);
if (desc.IsEmissive)
{
renderer.material.SetTexture(desc.ShaderEmissionProperty, tex);
renderer.material.SetTextureScale(desc.ShaderEmissionProperty, new Vector2(1f, -1f));
}
}
}

public static bool CanBeUsed() => true;

public static bool CanBeUsed(Uri source)
{
// if (source.Scheme != "file") return false;
// TODO: Check to see if file is in compatible format
return true;
}

public bool IsPlaying => videoPlayer.IsPlaying;
public bool Muted
{
get => audioSource.mute;
set => audioSource.mute = value;
}

public bool Looping { get; set; } = false;

public float Pitch
{
get => audioSource.pitch;
set => audioSource.pitch = value;
}

public float Volume
{
get => audioSource.volume;
set => audioSource.volume = value;
}

public double Position
{
get => videoPlayer.PlaybackTime;
set => videoPlayer.Seek(value);
}

public double Length => videoPlayer.GetLength();

private string url;
public string Source
{
get => url;
set
{
if (url != value)
{
url = value;
videoPlayer.Play(url);
}
}
}

public void Play() => videoPlayer.Resume();
public void Pause() => videoPlayer.Pause();
public void Stop() => videoPlayer.Pause();

public string GetFileHeader() => String.Empty;

public void Dispose()
{
Object.Destroy(material);
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Game/Video/FFVideoPlayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Scripts/Game/Video/UnityVideoPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public string Source
public void Play() => videoPlayer.Play();
public void Pause() => videoPlayer.Pause();
public void Stop() => videoPlayer.Stop();
public string GetFileHeader() => "file:///";

public void Dispose()
{
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Game/Video/VLCVideoPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public async void Stop()
DestroyRenderTexture();
}

public string GetFileHeader() => "file:///";

public bool IsStream { get; private set; }

public VideoOrientation? GetVideoOrientation()
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Game/Video/VideoPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static VideoPlayerManager()
#if VLC
Register<VLCVideoPlayer>(VLCVideoPlayer.CanBeUsed, VLCVideoPlayer.CanBeUsed);
#endif
Register<FFVideoPlayer>(FFVideoPlayer.CanBeUsed, FFVideoPlayer.CanBeUsed);
}

public static void Register<T>(Func<bool> canBeUsed, Func<Uri, bool> fileCanBePlayed) where T : IVideoPlayer
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Sandboxing/SandboxedTypes/Components/Video.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ public void LoadFromStream(Streaming.StreamDownload streamDownload)
{
if (!File.Exists(streamDownload.pathToFile))
return;
string filePath = "file:///" + streamDownload.pathToFile;
IVideoPlayer videoPlayer = videoPlayerDescriptor.Replace(
VideoPlayerManager.GetVideoPlayerType(new Uri(filePath)) ??
VideoPlayerManager.GetVideoPlayerType(new Uri(streamDownload.pathToFile)) ??
VideoPlayerManager.DefaultVideoPlayerType);
if (videoPlayer == null)
return;
string filePath = videoPlayer.GetFileHeader() + streamDownload.pathToFile;
videoPlayer.Source = filePath;
}
}
Expand Down
1 change: 1 addition & 0 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"com.unity.xr.interaction.toolkit": "2.5.4",
"com.unity.xr.management": "4.4.0",
"com.unity.xr.openxr": "1.11.0",
"net.virtualwebsite.ffmpegunity": "https://github.com/VirtualBrightPlayz/FFmpeg.Unity.git?path=/Packages/FFmpeg.Unity#dynload",
"com.unity.modules.accessibility": "1.0.0",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down
7 changes: 7 additions & 0 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@
},
"url": "https://packages.unity.com"
},
"net.virtualwebsite.ffmpegunity": {
"version": "https://github.com/VirtualBrightPlayz/FFmpeg.Unity.git?path=/Packages/FFmpeg.Unity#dynload",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "5a87f04a9c812610e03dbf3770eb337201105c5d"
},
"com.unity.modules.accessibility": {
"version": "1.0.0",
"depth": 0,
Expand Down