// %BANNER_BEGIN% // --------------------------------------------------------------------- // %COPYRIGHT_BEGIN% // Copyright (c) 2022-2023 Magic Leap, Inc. All Rights Reserved. // Use of this file is governed by the Magic Leap 2 Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2 // Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein. // %COPYRIGHT_END% // --------------------------------------------------------------------- // %BANNER_END% using System; using UnityEngine; namespace MagicLeap.Spectator { public class H264Encoder : IVideoEncoder { #region Private variables IVideoEncoder encoder = null; #endregion #region Public properties public int Width { get; private set; } public int Height { get; private set; } #endregion #region Public events public event Action onFrameEncoded { add => encoder.onFrameEncoded += value; remove => encoder.onFrameEncoded -= value; } #endregion #region Constructor / Destructor private H264Encoder(int width, int height, int bitrate) { Width = width; Height = height; #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN encoder = WinH264Encoder.Create(width, height, bitrate); #elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX encoder = MacH264Encoder.Create(width, height, bitrate); #elif UNITY_ANDROID encoder = MLH264Encoder.Create(width, height, bitrate); #endif } ~H264Encoder() { encoder = null; } #endregion #region Public methods public static H264Encoder Create(int width, int height, int bitrate) { H264Encoder encoder = new H264Encoder(width, height, bitrate); if (encoder.encoder == null) return null; return encoder; } public void Dispose() => encoder.Dispose(); public void AttachToCamera(Camera camera, Material material = null) => encoder.AttachToCamera(camera, material); public void Push(Texture frame, Material material = null) => encoder.Push(frame, material); public bool TryRequestSyncFrame() => encoder.TryRequestSyncFrame(); #endregion } }