/// <summary>
        ///  Create instance of MediaPlayerIPhone object with additional arguments
        /// </summary>
        /// <param name="monoObject">MonoBehaviour instanse</param>
        /// <param name="videoOutputObjects">Objects that will be rendering video output</param>
        /// <param name="options">Additional player options</param>
        public MediaPlayerIPhone(MonoBehaviour monoObject, GameObject[] videoOutputObjects, PlayerOptionsIPhone options)
        {
            _monoObject         = monoObject;
            _videoOutputObjects = videoOutputObjects;
            _options            = options;

            _wrapper = new WrapperInternal(_options);

            if (_wrapper.NativeIndex < 0)
            {
                Debug.LogError("Don't support video playback on current platform or you use incorrect UMP libraries!");
                throw new Exception();
            }

            if (_options != null)
            {
                _optionsLine = _options.GetOptions('\n');
                if (_options.FixedVideoSize != Vector2.zero)
                {
                    _videoBuffer = new PlayerBufferVideo((int)_options.FixedVideoSize.x, (int)_options.FixedVideoSize.y);
                    _wrapper.NativeSetPixelsBuffer(_videoBuffer.FramePixelsAddr, _videoBuffer.Width, _videoBuffer.Height);
                }
            }

            _wrapper.NativeInitPlayer(_optionsLine);
            _eventManager = new PlayerManagerEvents(_monoObject, this);
            _eventManager.PlayerPlayingListener += OnPlayerPlaying;
            _eventManager.PlayerPausedListener  += OnPlayerPaused;
        }
Exemple #2
0
        public void Stop(bool resetTexture)
        {
            if (_isStarted)
            {
                _wrapper.PlayerStop();

                if (_updateVideoTextureEnum != null)
                {
                    _monoObject.StopCoroutine(_updateVideoTextureEnum);
                    _updateVideoTextureEnum = null;
                }

                _framesCounter    = 0;
                _frameRate        = 0;
                _tmpFramesCounter = 0;
                _tmpTime          = 0;

                _isStarted      = false;
                _isPlaying      = false;
                _isLoad         = false;
                _isReady        = false;
                _isImageReady   = false;
                _isTextureExist = !resetTexture;

                if (resetTexture)
                {
                    if (_videoTexture != null)
                    {
                        UnityEngine.Object.Destroy(_videoTexture);
                        _videoTexture = null;
                    }
                }

                if (_videoBuffer != null)
                {
                    _videoBuffer.ClearFramePixels();
                    _videoBuffer = null;
                }

                if (_eventManager != null)
                {
                    _eventManager.StopListener();
                }
            }
        }
        /// <summary>
        ///  Create instance of MediaPlayerStandalone object with additional arguments
        /// </summary>
        /// <param name="monoObject">MonoBehaviour instanse</param>
        /// <param name="videoOutputObjects">Objects that will be rendering video output</param>
        /// <param name="options">Additional player options</param>
        public MediaPlayerStandalone(MonoBehaviour monoObject, GameObject[] videoOutputObjects, PlayerOptionsStandalone options)
        {
            _monoObject         = monoObject;
            _videoOutputObjects = videoOutputObjects;
            _options            = options;

            _wrapper = new WrapperStandalone(_options);

            if (_wrapper.NativeIndex < 0)
            {
                Debug.LogError("Don't support video playback on current platform or you use incorrect UMP libraries!");
                throw new Exception();
            }

            if (_options != null)
            {
                if (!string.IsNullOrEmpty(_options.DirectAudioDevice))
                {
                    _options.DirectAudioDevice = GetAudioDevice(_options.DirectAudioDevice);
                }

                _wrapper.NativeSetPixelsVerticalFlip(_options.FlipVertically);

                if (_options.FixedVideoSize != Vector2.zero)
                {
                    _videoBuffer = new PlayerBufferVideo((int)_options.FixedVideoSize.x, (int)_options.FixedVideoSize.y);
                    _wrapper.NativeSetPixelsBuffer(_videoBuffer.FramePixelsAddr, _videoBuffer.Width, _videoBuffer.Height);
                }

                if (_options.AudioOutputs != null && _options.AudioOutputs.Length > 0)
                {
                    _audioManager = new PlayerManagerAudios(_options.AudioOutputs);
                    _audioManager.AddListener(OnAudioFilterRead);
                }

                _arguments   = _options.GetOptions('\n').Split('\n');
                _logDetail   = _options.LogDetail;
                _logListener = _options.LogListener;
            }

            MediaPlayerInit();
        }
        /// <summary>
        ///  Create instance of MediaPlayerWebGL object with additional arguments
        /// </summary>
        /// <param name="monoObject">MonoBehaviour instanse</param>
        /// <param name="videoOutputObjects">Objects that will be rendering video output</param>
        /// <param name="options">Additional player options</param>
        public MediaPlayerWebGL(MonoBehaviour monoObject, GameObject[] videoOutputObjects, PlayerOptions options)
        {
            _monoObject         = monoObject;
            _videoOutputObjects = videoOutputObjects;
            _options            = options;

            _wrapper = new WrapperInternal(null);

            if (_options != null)
            {
                if (_options.FixedVideoSize != Vector2.zero)
                {
                    _videoBuffer = new PlayerBufferVideo((int)_options.FixedVideoSize.x, (int)_options.FixedVideoSize.y);
                }
            }

            _eventManager = new PlayerManagerEvents(_monoObject, this);
            _eventManager.PlayerPlayingListener += OnPlayerPlaying;
            _eventManager.PlayerPausedListener  += OnPlayerPaused;
        }
Exemple #5
0
        private void InitBufferSize(int width, int height)
        {
            _wrapper.NativePixelsBufferRelease();

            if (_videoBuffer != null)
            {
                if (_videoBuffer.Width != width ||
                    _videoBuffer.Height != height)
                {
                    _videoBuffer.ClearFramePixels();
                    _videoBuffer = null;
                }
            }

            if (_videoBuffer == null)
            {
                _videoBuffer = new PlayerBufferVideo(width, height);
                _wrapper.NativeSetPixelsBuffer(_videoBuffer.FramePixelsAddr, _videoBuffer.Width, _videoBuffer.Height);
                _isTextureExist = false;
            }
        }
        private IEnumerator UpdateVideoTexture()
        {
            while (true)
            {
                if (FramesCounter != _framesCounter)
                {
                    _framesCounter = FramesCounter;
                    UpdateFpsCounter();

                    _shareTexture = _wrapper.NativeGetTexture();

                    if (!_isTextureExist)
                    {
                        if (_videoTexture != null)
                        {
                            UnityEngine.Object.Destroy(_videoTexture);
                            _videoTexture = null;
                        }

                        if (_options.FixedVideoSize == Vector2.zero)
                        {
                            int width  = VideoWidth;
                            int height = VideoHeight;

                            if (_videoBuffer == null ||
                                (_videoBuffer != null &&
                                 _videoBuffer.Width != width || _videoBuffer.Height != height))
                            {
                                if (_videoBuffer != null)
                                {
                                    _videoBuffer.ClearFramePixels();
                                }

                                _videoBuffer = new PlayerBufferVideo(width, height);
                                _wrapper.NativeSetPixelsBuffer(_videoBuffer.FramePixelsAddr, _videoBuffer.Width, _videoBuffer.Height);
                            }
                        }

                        _videoTexture = Texture2D.CreateExternalTexture(_videoBuffer.Width, _videoBuffer.Height, TextureFormat.BGRA32, false, false, _shareTexture);
                        MediaPlayerHelper.ApplyTextureToRenderingObjects(_videoTexture, _videoOutputObjects);
                        //_wrapper.NativeHelperSetTexture(_pluginObj, _videoTexture.GetNativeTexturePtr());

                        _isTextureExist = true;
                    }

                    _videoTexture.UpdateExternalTexture(_shareTexture);
                }

                if (_wrapper.PlayerIsReady() && !_isReady)
                {
                    _isReady = true;

                    if (_isLoad)
                    {
                        _eventManager.ReplaceEvent(PlayerState.Paused, PlayerState.Prepared, _videoTexture);
                        Pause();
                    }
                    else
                    {
                        _eventManager.SetEvent(PlayerState.Prepared, _videoTexture);
                        _eventManager.SetEvent(PlayerState.Playing);
                    }
                }

                yield return(null);
            }
        }
        /// <summary>
        /// Stop current video playback
        /// </summary>
        /// <param name="resetTexture">Clear previous playback texture</param>
        public void Stop(bool resetTexture)
        {
            if (_playerObj != IntPtr.Zero && _isStarted)
            {
                _wrapper.PlayerStop(_playerObj);

                if (_updateVideoTextureEnum != null)
                {
                    _monoObject.StopCoroutine(_updateVideoTextureEnum);
                    _updateVideoTextureEnum = null;
                }

                _framesCounter    = 0;
                _frameRate        = 0;
                _tmpFramesCounter = 0;
                _tmpTime          = 0;

                _isStarted = false;
                _isPlaying = false;
                _isLoad    = false;
                _isReady   = false;

                _wrapper.NativeUpdateFramesCounter(0);
                _wrapper.NativeClearAudioSamples(0);
                _wrapper.NativePixelsBufferRelease();

                _isTextureExist = !resetTexture;

                if (resetTexture)
                {
                    if (_videoTexture != null)
                    {
                        UnityEngine.Object.Destroy(_videoTexture);
                        _videoTexture = null;
                    }
                }

                if (_videoBuffer != null &&
                    _options.FixedVideoSize == Vector2.zero)
                {
                    _videoBuffer.ClearFramePixels();
                    _videoBuffer = null;
                }

                if (_audioDataHandle.IsAllocated)
                {
                    _audioDataHandle.Free();
                }

                if (_audioManager != null)
                {
                    _audioManager.Stop();
                }

                if (_eventManager != null)
                {
                    _eventManager.StopListener();
                }

                if (_logManager != null)
                {
                    _logManager.StopListener();
                }
            }
        }
        private IEnumerator UpdateVideoTexture()
        {
            MediaTrackInfoExpanded[] tracks = null;
            var hasVideo = false;

            while (true)
            {
                if (_playerObj != IntPtr.Zero && _wrapper.PlayerIsPlaying(_playerObj))
                {
                    if (tracks == null)
                    {
                        tracks = TracksInfo;

                        if (tracks != null)
                        {
                            foreach (var track in tracks)
                            {
                                if (track is MediaTrackInfoVideo)
                                {
                                    hasVideo = true;
                                }
                            }
                        }
                        else
                        {
                            yield return(null);

                            continue;
                        }
                    }

                    if (FramesCounter != _framesCounter)
                    {
                        _framesCounter = FramesCounter;
                        UpdateFpsCounter(_framesCounter);

                        if (_videoBuffer == null)
                        {
                            int width  = _wrapper.NativeGetPixelsBufferWidth();
                            int height = _wrapper.NativeGetPixelsBufferHeight();

                            _videoBuffer = new PlayerBufferVideo(width, height);
                            _wrapper.NativeSetPixelsBuffer(_videoBuffer.FramePixelsAddr, _videoBuffer.Width, _videoBuffer.Height);
                        }

                        if (!_isTextureExist)
                        {
                            if (_videoTexture != null)
                            {
                                UnityEngine.Object.Destroy(_videoTexture);
                                _videoTexture = null;
                            }

                            _videoTexture = MediaPlayerHelper.GenPluginTexture(_videoBuffer.Width, _videoBuffer.Height);
                            MediaPlayerHelper.ApplyTextureToRenderingObjects(_videoTexture, _videoOutputObjects);
                            _wrapper.NativeSetTexture(_videoTexture.GetNativeTexturePtr());

                            _isTextureExist = true;
                        }

                        GL.IssuePluginEvent(_wrapper.NativeGetUnityRenderCallback(), _wrapper.NativeIndex);
                    }

                    if (!_isReady && (hasVideo ? (_videoTexture != null && _videoBuffer != null) : tracks != null))
                    {
                        _isReady = true;

                        if (_isLoad)
                        {
                            _eventManager.ReplaceEvent(PlayerState.Paused, PlayerState.Prepared, _videoTexture);
                            Pause();
                        }
                        else
                        {
                            _eventManager.SetEvent(PlayerState.Prepared, _videoTexture);
                            _eventManager.SetEvent(PlayerState.Playing);
                        }
                    }
                }

                yield return(null);
            }
        }
        private IEnumerator UpdateVideoTexture()
        {
            while (true)
            {
                if (FramesCounter > 0)
                {
                    UpdateFpsCounter();

                    if (!_isTextureExist)
                    {
                        if (_videoTexture != null)
                        {
                            UnityEngine.Object.Destroy(_videoTexture);
                            _videoTexture = null;
                        }

                        if (_options.FixedVideoSize == Vector2.zero)
                        {
                            int width  = VideoWidth;
                            int height = VideoHeight;

                            if (_videoBuffer == null ||
                                (_videoBuffer != null &&
                                 _videoBuffer.Width != width || _videoBuffer.Height != height))
                            {
                                if (_videoBuffer != null)
                                {
                                    _videoBuffer.ClearFramePixels();
                                }

                                _videoBuffer = new PlayerBufferVideo(width, height);
                            }
                        }

                        _videoTexture = MediaPlayerHelper.GenPluginTexture(_videoBuffer.Width, _videoBuffer.Height);
                        MediaPlayerHelper.ApplyTextureToRenderingObjects(_videoTexture, _videoOutputObjects);
                        _isTextureExist = true;
                        _isImageReady   = false;
                    }

                    _wrapper.NativeUpdateTexture(_videoTexture.GetNativeTexturePtr());

                    if (!_isImageReady)
                    {
                        _eventManager.SetEvent(PlayerState.ImageReady, _videoTexture);
                        _isImageReady = true;
                    }
                }

                if (_wrapper.PlayerIsReady() && !_isReady)
                {
                    _isReady = true;

                    if (_isLoad)
                    {
                        _eventManager.ReplaceEvent(PlayerState.Paused, PlayerState.Prepared, new Vector2(VideoWidth, VideoHeight));
                        Pause();
                    }
                    else
                    {
                        _eventManager.SetEvent(PlayerState.Prepared, new Vector2(VideoWidth, VideoHeight));
                        _eventManager.SetEvent(PlayerState.Playing);
                    }
                }

                yield return(null);
            }
        }
Exemple #10
0
        private void MediaPlayerInit()
        {
            _vlcObj = _wrapper.ExpandedLibVLCNew(_arguments);

            if (_vlcObj == IntPtr.Zero)
            {
                throw new Exception("Can't create new libVLC object instance");
            }

            _playerObj = _wrapper.ExpandedMediaPlayerNew(_vlcObj);

            if (_playerObj == IntPtr.Zero)
            {
                throw new Exception("Can't create new media player object instance");
            }

            _eventManagerPtr = _wrapper.ExpandedEventManager(_playerObj);
            _eventHandlerPtr = _wrapper.NativeMediaPlayerEventCallback();
            EventsAttach(_eventManagerPtr, _eventHandlerPtr);

            _eventManager = new PlayerManagerEvents(_monoObject, this);
            _eventManager.PlayerPlayingListener += OnPlayerPlaying;
            _eventManager.PlayerPausedListener  += OnPlayerPaused;

            if (_logDetail != LogLevels.Disable)
            {
                _wrapper.ExpandedLogSet(_vlcObj, _wrapper.NativeGetLogMessageCallback(), new IntPtr(_wrapper.NativeIndex));
            }

            _logManager                     = new PlayerManagerLogs(_monoObject, this);
            _logManager.LogDetail           = _logDetail;
            _logManager.LogMessageListener += _logListener;

            _lockPtr        = _wrapper.NativeGetVideoLockCallback();
            _displayPtr     = _wrapper.NativeGetVideoDisplayCallback();
            _formatSetupPtr = _wrapper.NativeGetVideoFormatCallback();

            _audioFormatPtr = _wrapper.NativeGetAudioSetupCallback();
            _audioPlayPtr   = _wrapper.NativeGetAudioPlayCallback();

            _wrapper.ExpandedVideoSetCallbacks(_playerObj, _lockPtr, IntPtr.Zero, _displayPtr, new IntPtr(_wrapper.NativeIndex));

            if (_options.FixedVideoSize == Vector2.zero)
            {
                _wrapper.ExpandedVideoSetFormatCallbacks(_playerObj, _formatSetupPtr, IntPtr.Zero);
            }
            else
            {
                _wrapper.ExpandedVideoSetFormat(_playerObj, PlayerBufferVideo.Chroma, (int)_options.FixedVideoSize.x, (int)_options.FixedVideoSize.y, PlayerBufferVideo.CalculatePitch((int)_options.FixedVideoSize.x));
            }

            _manageBufferSizeCallback = InitBufferSize;
            _wrapper.NativeSetBufferSizeCallback(Marshal.GetFunctionPointerForDelegate(_manageBufferSizeCallback));

            if (_audioManager != null && _audioManager.IsValid)
            {
                _wrapper.ExpandedAudioSetFormatCallbacks(_playerObj, _audioFormatPtr, IntPtr.Zero);
                _wrapper.ExpandedAudioSetCallbacks(_playerObj, _audioPlayPtr, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, new IntPtr(_wrapper.NativeIndex));
                _wrapper.NativeSetAudioParams(2, AudioSettings.outputSampleRate);
            }

            _mediaStats = new MediaStats();
        }
Exemple #11
0
        private IEnumerator UpdateVideoTexture()
        {
            while (true)
            {
                if (FramesCounter != _framesCounter)
                {
                    _framesCounter = FramesCounter;
                    UpdateFpsCounter();

                    if (_videoBuffer == null)
                    {
                        int width  = (int)_options.FixedVideoSize.x;
                        int height = (int)_options.FixedVideoSize.y;

                        if (_options.FixedVideoSize == Vector2.zero)
                        {
                            width  = VideoWidth;
                            height = VideoHeight;
                        }

                        _videoBuffer = new PlayerBufferVideo(width, height);
                        _wrapper.NativeSetPixelsBuffer(_videoBuffer.FramePixelsAddr, _videoBuffer.Width, _videoBuffer.Height);
                    }

                    if (!_isTextureExist)
                    {
                        if (_videoTexture != null)
                        {
                            UnityEngine.Object.Destroy(_videoTexture);
                            _videoTexture = null;
                        }

                        _videoTexture = MediaPlayerHelper.GenPluginTexture(_videoBuffer.Width, _videoBuffer.Height);
                        MediaPlayerHelper.ApplyTextureToRenderingObjects(_videoTexture, _videoOutputObjects);
                        _wrapper.NativeSetTexture(_videoTexture.GetNativeTexturePtr());
                        _isTextureExist = true;
                        _isImageReady   = false;
                    }

                    _wrapper.PlayerUpdateSurfaceTexture();
                    GL.IssuePluginEvent(_wrapper.NativeGetUnityRenderCallback(), _wrapper.NativeIndex);

                    if (!_isImageReady)
                    {
                        _eventManager.SetEvent(PlayerState.ImageReady, _videoTexture);
                        _isImageReady = true;
                    }
                }

                if (_wrapper.PlayerIsReady() && !_isReady)
                {
                    _isReady = true;

                    if (_isLoad)
                    {
                        _eventManager.ReplaceEvent(PlayerState.Paused, PlayerState.Prepared, new Vector2(VideoWidth, VideoHeight));
                        Pause();
                    }
                    else
                    {
                        _eventManager.SetEvent(PlayerState.Prepared, new Vector2(VideoWidth, VideoHeight));
                        _eventManager.SetEvent(PlayerState.Playing);
                    }
                }

                yield return(null);
            }
        }