Exemplo n.º 1
0
        void OnImageReady(Texture image)
        {
            if (_texture != null)
            {
                Destroy(_texture);
            }

            //Video size != Video buffer size (FramePixels has video buffer size), so we will use
            //previously created playback texture size that based on video buffer size
            _texture = MediaPlayerHelper.GenVideoTexture(image.width, image.height);
            _texture.Apply();

            _image.texture = _texture;
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        private IEnumerator VideoPathPreparing(string path, bool playImmediately, IPathPreparedListener listener)
        {
            if (_cachedVideoPaths.ContainsKey(path))
            {
                listener.OnPathPrepared(_cachedVideoPaths[path], playImmediately);
                yield break;
            }

#if UNITY_EDITOR
            _lastEventMsg = "Path Preparing";
#endif

            if (UMPSettings.RuntimePlatform == UMPSettings.Platforms.Android)
            {
                /// Check if we try to play exported videos and wait when export process will be completed
                var exptPaths = UMPSettings.Instance.AndroidExportedPaths;
                var filePath  = path.Replace("file:///", "");

                foreach (var exptPath in exptPaths)
                {
                    if (exptPath.Contains(filePath))
                    {
                        while (!_isExportCompleted)
                        {
                            yield return(null);
                        }

                        if (_cachedVideoPaths.ContainsKey(filePath))
                        {
                            listener.OnPathPrepared(_cachedVideoPaths[filePath], playImmediately);
                            yield break;
                        }

                        break;
                    }
                }

                if ((_mediaPlayer.Options as PlayerOptionsAndroid).PlayerType ==
                    PlayerOptionsAndroid.PlayerTypes.LibVLC &&
                    MediaPlayerHelper.IsAssetsFile(path))
                {
                    var tempFilePath = System.IO.Path.Combine(Application.temporaryCachePath, filePath);
                    if (File.Exists(tempFilePath))
                    {
                        _cachedVideoPaths.Add(path, tempFilePath);
                        listener.OnPathPrepared(tempFilePath, playImmediately);
                        yield break;
                    }

                    var data = new byte[0];

#if UNITY_2017_2_OR_NEWER
                    var www = UnityWebRequest.Get(System.IO.Path.Combine(Application.streamingAssetsPath, filePath));
                    yield return(www.SendWebRequest());

                    data = www.downloadHandler.data;
#else
                    var www = new WWW(System.IO.Path.Combine(Application.streamingAssetsPath, filePath));
                    yield return(www);

                    data = www.bytes;
#endif
                    if (string.IsNullOrEmpty(www.error))
                    {
                        var tempFile = new FileInfo(tempFilePath);
                        tempFile.Directory.Create();
                        File.WriteAllBytes(tempFile.FullName, data);
                        _cachedVideoPaths.Add(path, tempFilePath);
                        path = tempFilePath;
                    }
                    else
                    {
                        Debug.LogError("Can't create temp file from asset folder: " + www.error);
                    }

                    www.Dispose();
                }
            }

            if (_videoServices.ValidUrl(path))
            {
                Video serviceVideo = null;
                _isParsing = true;

                yield return(_videoServices.GetVideos(path, (videos) =>
                {
                    _isParsing = false;
                    serviceVideo = VideoServices.FindVideo(videos, int.MaxValue, int.MaxValue);
                }, (error) =>
                {
                    _isParsing = false;
                    Debug.LogError(string.Format("[UniversalMediaPlayer.GetVideos] {0}", error));
                    OnPlayerEncounteredError();
                }));

                if (serviceVideo == null)
                {
                    Debug.LogError("[UniversalMediaPlayer.VideoPathPreparing] Can't get service video information");
                    OnPlayerEncounteredError();
                }
                else
                {
                    if (serviceVideo is YoutubeVideo)
                    {
                        yield return((serviceVideo as YoutubeVideo).Decrypt(UMPSettings.Instance.YoutubeDecryptFunction, (error) =>
                        {
                            Debug.LogError(string.Format("[UniversalMediaPlayer.Decrypt] {0}", error));
                            OnPlayerEncounteredError();
                        }));
                    }

                    path = serviceVideo.Url;
                }
            }

            listener.OnPathPrepared(path, playImmediately);
            yield return(null);
        }
        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);
            }
        }
Exemplo n.º 6
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);
            }
        }