/Multimedia/Vlc/VlcVideo.cs
C# | 247 lines | 158 code | 21 blank | 68 comment | 16 complexity | 735eda75204025eabdc6825bf0b7cd81 MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.IO;
- using Delta.Engine;
- using Delta.Multimedia.Vlc.LibVlc;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
-
- namespace Delta.Multimedia.Vlc
- {
- /// <summary>
- /// Video class using vlc library to natively play the video. Note: Needs
- /// native libvlc.dll, libvlccore.dll file, plus plugins for the video
- /// format (see \External\Vlc\plugins).
- /// </summary>
- public class VlcVideo : Video
- {
- #region TimePlayed (Public)
- /// <summary>
- /// Return the time in seconds how long this video already played.
- /// </summary>
- public override float TimePlayed
- {
- get
- {
- return player == null
- ? 0f
- : player.VideoPosition * 0.001f;
- }
- }
- #endregion
-
- #region Private
-
- #region vlcManager (Private)
- /// <summary>
- /// Vlc manager instance.
- /// </summary>
- private static VlcInstance vlcManager;
- #endregion
-
- #region player (Private)
- /// <summary>
- /// Controller of the current video displayer, it provides basic function
- /// for handling the video player such as playing or stopping.
- /// </summary>
- private VlcMediaPlayer player;
- #endregion
-
- #region nativeVideoData (Private)
- /// <summary>
- /// Video data
- /// </summary>
- private VlcMedia nativeVideoData;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Initialize vlcManager for all VlcVideo instances!
- /// </summary>
- static VlcVideo()
- {
- // Attach to the application closing for accurately disposing the
- // VLC manager instance.
- Application.ApplicationClosing += delegate
- {
- if (vlcManager != null)
- {
- vlcManager.Dispose();
- vlcManager = null;
- }
- };
-
- try
- {
- string currentPath = Directory.GetCurrentDirectory();
- string[] args = new[]
- {
- "-I",
- "dummy",
- "--ignore-config",
- "--plugin-path=" + Path.Combine(currentPath, "plugins"),
- };
- vlcManager = new VlcInstance(args);
- }
- catch (Exception ex)
- {
- Log.Warning("Failed to initialize the vlc system (unable to " +
- "play videos), because: " + ex);
- }
- }
-
- /// <summary>
- /// Create a VLC video.
- /// </summary>
- /// <param name="contentName">The name of the content file to load.</param>
- public VlcVideo(string contentName)
- : base(contentName)
- {
- }
- #endregion
-
- #region Methods (Private)
-
- #region LoadNativeData
- /// <summary>
- /// Load the native content data.
- /// </summary>
- /// <param name="filename">Filename</param>
- /// <returns>True if loading succeeded, otherwise False.</returns>
- protected override bool LoadNativeData(string filename)
- {
- // We can only continue if the VlcManager could be created
- if (vlcManager != null)
- {
- nativeVideoData = new VlcMedia(vlcManager, filename);
- return true;
- }
- return false;
- }
- #endregion
-
- #region PlayNative
- /// <summary>
- /// Native implementation of playing the native video data.
- /// Note: This method will only be called, if the video is loaded.
- /// </summary>
- protected override void PlayNative()
- {
- if (player != null)
- {
- StopNative();
- }
-
- player = new VlcMediaPlayer(nativeVideoData);
- player.Drawable = Application.Window.ViewportHandle;
- player.Play();
- }
- #endregion
-
- #region UpdateNative
- /// <summary>
- /// Native implementation of update, which simply checks the actual
- /// video state and updates the base classes state.
- /// </summary>
- protected override void UpdateNative()
- {
- VlcMediaState state = nativeVideoData.State;
- switch (state)
- {
- case VlcMediaState.Paused:
- State = MediaState.Paused;
- break;
-
- case VlcMediaState.Opening:
- case VlcMediaState.NothingSpecial:
- case VlcMediaState.Buffering:
- case VlcMediaState.Playing:
- State = MediaState.Playing;
- break;
-
- case VlcMediaState.Error:
- case VlcMediaState.Stopped:
- case VlcMediaState.Ended:
- // Only stop this and put the state to Stopped if we were playing
- // before, because sometimes the player still needs to initialize
- // the media (Buffering, etc.) but this happens async and our
- // engine is already in this update method, the state of the media
- // is Stopped or Ended from the last playback, then it will break
- // the playback because of going into this switch case.
- if (State != MediaState.Stopped)
- {
- StopNative();
- State = MediaState.Stopped;
- }
- break;
- } // switch
- }
- #endregion
-
- #region DrawNative
- /// <summary>
- /// Native implementation of drawing the native video data.
- /// Note: This method will only be called, if the video is loaded.
- /// </summary>
- /// <param name="drawArea">Draw area</param>
- protected override void DrawNative(Rectangle drawArea)
- {
- }
- #endregion
-
- #region StopNative
- /// <summary>
- /// Stops the native playing video data.
- /// Note: This method will only be called, if the video is currently played.
- /// </summary>
- protected override void StopNative()
- {
- // Just stop and dispose the player instead (we can recreate it)
- if (player != null)
- {
- // This never ends when we quit the app while the video is playing,
- // so we need to call Stop first!
- player.Stop();
- player.Dispose();
- player = null;
- }
- }
- #endregion
-
- #region DisposeNativeData
- /// <summary>
- /// Dispose the native data object.
- /// </summary>
- protected override void DisposeNativeData()
- {
- if (nativeVideoData != null)
- {
- nativeVideoData.Dispose();
- nativeVideoData = null;
- }
- if (player != null)
- {
- // This never ends when we quit the app while the video is playing,
- // so we need to call Stop first!
- player.Stop();
- player.Dispose();
- player = null;
- }
- }
- #endregion
-
- #region SetVolume
- /// <summary>
- /// Set the volume.
- /// </summary>
- /// <param name="setVolume">Volume value.</param>
- protected override void SetVolume(float setVolume)
- {
- player.Volume = (int)(setVolume * 100);
- }
- #endregion
-
- #endregion
- }
- }