| | 1 | | using System.Collections.Generic; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL.Components; |
| | 4 | | using DCL.Components.Video.Plugin; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.ECS7.InternalComponents; |
| | 7 | | using DCL.ECSRuntime; |
| | 8 | | using DCL.Models; |
| | 9 | | using Decentraland.Common; |
| | 10 | | using System; |
| | 11 | | using System.Linq; |
| | 12 | | using UnityEngine; |
| | 13 | |
|
| | 14 | | namespace DCL.ECSComponents |
| | 15 | | { |
| | 16 | | public class VideoPlayerHandler : IECSComponentHandler<PBVideoPlayer> |
| | 17 | | { |
| 1 | 18 | | private static readonly string[] NO_STREAM_EXTENSIONS = new[] { ".mp4", ".ogg", ".mov", ".webm" }; |
| | 19 | |
|
| | 20 | | private PBVideoPlayer lastModel = null; |
| | 21 | | internal WebVideoPlayer videoPlayer; |
| | 22 | |
|
| | 23 | | private readonly IInternalECSComponent<InternalVideoPlayer> videoPlayerInternalComponent; |
| | 24 | |
|
| 3 | 25 | | public VideoPlayerHandler(IInternalECSComponent<InternalVideoPlayer> videoPlayerInternalComponent) |
| | 26 | | { |
| 3 | 27 | | this.videoPlayerInternalComponent = videoPlayerInternalComponent; |
| 3 | 28 | | } |
| | 29 | |
|
| 0 | 30 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 31 | |
|
| | 32 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 33 | | { |
| 0 | 34 | | videoPlayerInternalComponent.RemoveFor(scene, entity); |
| 0 | 35 | | videoPlayer?.Dispose(); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBVideoPlayer model) |
| | 39 | | { |
| 4 | 40 | | if (lastModel != null && lastModel.Equals(model)) |
| 0 | 41 | | return; |
| | 42 | |
|
| | 43 | | // detect change of the state |
| 4 | 44 | | if (lastModel == null || lastModel.Src != model.Src) |
| | 45 | | { |
| 4 | 46 | | videoPlayer?.Dispose(); |
| | 47 | |
|
| 4 | 48 | | var id = entity.entityId.ToString(); |
| 8 | 49 | | bool isStream = !NO_STREAM_EXTENSIONS.Any(x => model.Src.EndsWith(x)); |
| 4 | 50 | | videoPlayer = new WebVideoPlayer(id, model.Src, isStream, DCLVideoTexture.videoPluginWrapperBuilder.Invo |
| 4 | 51 | | videoPlayerInternalComponent.PutFor(scene, entity, new InternalVideoPlayer() |
| | 52 | | { |
| | 53 | | videoPlayer = videoPlayer, |
| | 54 | | assignedMaterials = new List<InternalVideoPlayer.MaterialAssigned>(), |
| | 55 | | }); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | // detect change of the state |
| 4 | 59 | | if (lastModel == null || lastModel.IsPlaying() != model.IsPlaying()) |
| | 60 | | { |
| 6 | 61 | | if (model.IsPlaying()) { videoPlayer.Play(); } |
| 2 | 62 | | else { videoPlayer.Pause(); } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // detect change of the state |
| 4 | 66 | | float lastPosition = lastModel?.GetPosition() ?? 0.0f; |
| 4 | 67 | | if (Math.Abs(lastPosition - model.GetPosition()) > 0.01f) // 0.01s of tolerance |
| | 68 | | { |
| 0 | 69 | | videoPlayer.SetTime(model.GetPosition()); |
| | 70 | | } |
| | 71 | |
|
| 4 | 72 | | videoPlayer.SetVolume(model.GetVolume()); |
| | 73 | |
|
| 4 | 74 | | videoPlayer.SetPlaybackRate(model.GetPlaybackRate()); |
| | 75 | |
|
| 4 | 76 | | lastModel = model; |
| 4 | 77 | | } |
| | 78 | | } |
| | 79 | | } |