//========================================================================== /// <summary> /// Frees the provided library. /// </summary> /// <param name="library"> /// The library to free. /// </param> /// <returns> /// The number how ofter the library is still referenced; will be <c>0</c> /// in case the library is no longer referenced and has been disposed. /// </returns> public static uint Free(LibVLCLibrary library) { if (library == null) { throw new ArgumentNullException("library"); } lock (m_LibraryHandles) { LibraryHandle library_handle; if (m_LibraryHandles.TryGetValue(library.Directory, out library_handle)) { if (library_handle.Library == library) { if (--library_handle.ReferenceCounter == 0) { library_handle.Library.Dispose(); m_LibraryHandles.Remove(library.Directory); } if (library_handle == m_DefaultLibraryHandle) { m_DefaultLibraryHandle = null; } return(library_handle.ReferenceCounter); } } } throw new ArgumentException("The provided library has not been loaded using the Load method!", "library"); }
public void Start(int Width, int Height) { if (store.Count > 0) store[0].Stop(); store.Add(this); this.Width = Width; this.Height = Height; library = LibVLCLibrary.Load(null); inst = library.m_libvlc_new(4, new string[] {":sout-udp-caching=0", ":udp-caching=0", ":rtsp-caching=0", ":tcp-caching=0"}); //libvlc_new() // Load the VLC engine m = library.libvlc_media_new_location(inst, playurl); // Create a new item mp = library.libvlc_media_player_new_from_media(m); // Create a media player playing environement library.libvlc_media_release(m); // No need to keep the media now vlc_lock_delegate = new LibVLCLibrary.libvlc_video_lock_cb(vlc_lock); vlc_unlock_delegate = new LibVLCLibrary.libvlc_video_unlock_cb(vlc_unlock); vlc_picture_delegate = new LibVLCLibrary.libvlc_video_display_cb(vlc_picture); library.libvlc_video_set_callbacks(mp, vlc_lock_delegate, vlc_unlock_delegate, vlc_picture_delegate); library.libvlc_video_set_format(mp, "RV24", (uint)Width, (uint)Height, (uint)Width*4); library.libvlc_media_player_play(mp); // play the media_player }
private void EventManager_Event(ref LibVLCLibrary.libvlc_event_t e, IntPtr userData) { Debug.WriteLine(String.Format("EventManager_Event({0})", e.type), "MediaPlayer"); if (e.type == LibVLCLibrary.libvlc_event_e.libvlc_MediaPlayerPlaying) { signalPlaying.Release(); } if (e.type == LibVLCLibrary.libvlc_event_e.libvlc_MediaPlayerEndReached) { endReached = true; } if (e.type == LibVLCLibrary.libvlc_event_e.libvlc_MediaPlayerStopped) { stopped = true; } if (e.type == LibVLCLibrary.libvlc_event_e.libvlc_MediaPlayerEncounteredError) { errorOccured = true; signalPlaying.Release(); } if (e.type == LibVLCLibrary.libvlc_event_e.libvlc_VlmMediaInstanceStatusError) { throw new Exception("libvlc_VlmMediaInstanceStatusError occurred. this shouldn't happen."); } }
//========================================================================== /// <summary> /// Initializes a new instance of the class <see cref="Instance"/>. /// </summary> /// <param name="library"> /// May be <c>null</c> to use the default library. /// </param> public Instance(LibVLCLibrary library) { if(library == null) { m_Library = LibVLCLibrary.Load(null); m_IsLibraryOwner = true; } else { m_Library = library; m_IsLibraryOwner = false; } try { m_Handle = m_Library.libvlc_new(); if(m_Handle == IntPtr.Zero) throw new LibVLCException(m_Library); } catch { if(m_IsLibraryOwner) try { LibVLCLibrary.Free(m_Library); } catch { // ... } throw; } }
//========================================================================== private static string GetErrorMessage(LibVLCLibrary library) { if(library == null) throw new ArgumentNullException("library"); return library.libvlc_errmsg(); }
//========================================================================== private void Dispose(bool disposing) { if (m_MediaPlayerHandle != IntPtr.Zero) { m_Library.libvlc_media_player_release(m_MediaPlayerHandle); if (disposing) { UnregisterMediaPlayer(m_MediaPlayerHandle); } m_MediaPlayerHandle = IntPtr.Zero; } if (disposing) { if (m_Instance != null) { if (m_IsInstanceOwner) { m_Instance.Dispose(); } m_Instance = null; } if (m_Library != null) { if (m_IsLibraryOwner) { LibVLCLibrary.Free(m_Library); } m_Library = null; } } }
//========================================================================== internal Track(int index, LibVLCLibrary.libvlc_track_description_t trackDescription, LibVLCLibrary.libvlc_media_track_info_t? trackInfo) { m_Name = trackDescription.psz_name; // Title 1 - [Deutsch] // Title 2 - [English] Match match = m_NameLanguageRegex.Match(m_Name); if(match.Success) { m_Title = match.Groups[1].Value; m_Language = match.Groups[2].Value; m_Culture = GetCulture(m_Language); } else { m_Title = trackDescription.psz_name; m_Language = null; m_Culture = null; } if(trackInfo.HasValue) { m_Codec = Encoding.ASCII.GetString(BitConverter.GetBytes(trackInfo.Value.i_codec)); for(int i = 0; i < m_Codec.Length; ++i) if(m_Codec[i] == '\0') { m_Codec = m_Codec.Substring(0, i); break; } } else m_Codec = null; m_Index = index; }
//========================================================================== internal VideoTrack(int index, LibVLCLibrary.libvlc_track_description_t trackDescription, LibVLCLibrary.libvlc_media_track_info_t? trackInfo) : base(index, trackDescription, trackInfo) { if(trackInfo.HasValue) { m_Width = (int)trackInfo.Value.video.i_width; m_Height = (int)trackInfo.Value.video.i_height; } }
//========================================================================== private static string GetErrorMessage(LibVLCLibrary library) { if (library == null) { throw new ArgumentNullException("library"); } return(library.libvlc_errmsg()); }
//========================================================================== internal AudioTrack(int index, LibVLCLibrary.libvlc_track_description_t trackDescription, LibVLCLibrary.libvlc_media_track_info_t? trackInfo) : base(index, trackDescription, trackInfo) { if(trackInfo.HasValue) { m_BitRate = (int)trackInfo.Value.audio.i_rate; m_Channels = (int)trackInfo.Value.audio.i_channels; } }
public void Stop() { store.Remove(this); if (library == null) return; library.libvlc_media_player_stop(mp); // Stop playing library.libvlc_media_player_release(mp); // Free the media_player library.libvlc_release(inst); LibVLCLibrary.Free(library); library = null; if (imageIntPtr != null) Marshal.FreeHGlobal(imageIntPtr); }
//========================================================================== private void Dispose(bool disposing) { if (m_Handle != IntPtr.Zero) { m_Library.libvlc_release(m_Handle); m_Handle = IntPtr.Zero; } if (disposing) { if (m_Library != null) { if (m_IsLibraryOwner) { LibVLCLibrary.Free(m_Library); } m_Library = null; } } }
//========================================================================== /// <summary> /// Initializes a new instance of the class <see cref="Instance"/>. /// </summary> /// <param name="library"> /// May be <c>null</c> to use the default library. /// </param> public Instance(LibVLCLibrary library) { if (library == null) { m_Library = LibVLCLibrary.Load(null); m_IsLibraryOwner = true; } else { m_Library = library; m_IsLibraryOwner = false; } try { m_Handle = m_Library.libvlc_new(); if (m_Handle == IntPtr.Zero) { throw new LibVLCException(m_Library); } } catch { if (m_IsLibraryOwner) { try { LibVLCLibrary.Free(m_Library); } catch { // ... } } throw; } }
//========================================================================== private void Dispose(bool disposing) { if(m_MediaPlayerHandle != IntPtr.Zero) { m_Library.libvlc_media_player_release(m_MediaPlayerHandle); if(disposing) UnregisterMediaPlayer(m_MediaPlayerHandle); m_MediaPlayerHandle = IntPtr.Zero; } if(disposing) { if(m_Instance != null) { if(m_IsInstanceOwner) m_Instance.Dispose(); m_Instance = null; } if(m_Library != null) { if(m_IsLibraryOwner) LibVLCLibrary.Free(m_Library); m_Library = null; } } }
//========================================================================== public MediaPlayer(LibVLCLibrary library, Instance instance) { if(library != null) if(instance != null) if(instance.Library != library) throw new ArgumentException("The provided instance has not been created with the provided library!", "instance"); if(instance != null) if(library == null) library = instance.Library; if(library == null) { m_Library = LibVLCLibrary.Load(null); m_IsLibraryOwner = true; } else { m_Library = library; m_IsLibraryOwner = false; } try { if(instance == null) { m_Instance = new Instance(m_Library); m_IsInstanceOwner = true; } else { m_Instance = instance; m_IsInstanceOwner = false; } try { m_MediaPlayerHandle = CreateHandle(); } catch { if(m_IsInstanceOwner) m_Instance.Dispose(); m_Instance = null; throw; } } catch { if(m_IsLibraryOwner) LibVLCLibrary.Free(m_Library); m_Library = null; throw; } }
//========================================================================== private static void EventManager_Event(ref LibVLCLibrary.libvlc_event_t e, IntPtr userData) { Debug.WriteLine(String.Format("EventManager_Event({0})", e.type), "MediaPlayer"); MediaPlayer media_player = GetMediaPlayer(e.p_obj); if(media_player == null) return; if(e.type == LibVLCLibrary.libvlc_event_e.libvlc_MediaPlayerPlaying) { // Ensure no more than one video tracks will be rendered... int track_count = media_player.m_Library.libvlc_video_get_track_count(media_player.m_MediaPlayerHandle); if(track_count > 2) { int video_track_index = media_player.m_Library.libvlc_video_get_track(media_player.m_MediaPlayerHandle); media_player.m_Library.libvlc_video_set_track(media_player.m_MediaPlayerHandle, -1); if(video_track_index > 0) media_player.m_Library.libvlc_video_set_track(media_player.m_MediaPlayerHandle, video_track_index - 1); } } media_player.RaiseEvent((MediaPlayerEvent)e.type); }
//========================================================================== /// <summary> /// Initializes a new <see cref="LibVLCException"/> with the most recent /// <c>LibVLC</c> error. /// </summary> /// <param name="library"> /// The <see cref="LibVLCLibrary"/> which will be used to obtain the /// most recent error for the calling thread. /// </param> /// <remarks> /// <see cref="LibVLCLibrary.libvlc_errmsg"/> is called to get the /// message to initialize <see cref="Exception.Message"/> with. /// </remarks> public LibVLCException(LibVLCLibrary library) : this(GetErrorMessage(library)) { // ... }
//------------------------------------------------------------------------ public LibraryHandle(string libVLCDirectory) { ReferenceCounter = 0; Library = new LibVLCLibrary(libVLCDirectory); }
//========================================================================== private void Dispose(bool disposing) { if(m_Handle != IntPtr.Zero) { m_Library.libvlc_release(m_Handle); m_Handle = IntPtr.Zero; } if(disposing) if(m_Library != null) { if(m_IsLibraryOwner) LibVLCLibrary.Free(m_Library); m_Library = null; } }
private void GenerateImpl(int thumbnailWidth, int secondStep, string thumbnailDir, string filepath, NamedPipeClient<string> mediaScribeServer) { //TODO - ensure that these don't get wrapped in quotes, so we don't have to strip them out. filepath = filepath.Substring(1, filepath.Length - 2); thumbnailDir = thumbnailDir.Substring(1, thumbnailDir.Length - 2); if (false == Directory.Exists(thumbnailDir)) Directory.CreateDirectory(thumbnailDir); if (null == library) { #region start VLC string[] vlcargs = { "--intf", "dummy", /* no interface */ "--vout", "dummy", /* we don't want video (output) */ "--no-audio", /* we don't want audio (decoding) */ "--no-video-title-show", /* nor the filename displayed */ "--no-stats", /* no stats */ "--no-sub-autodetect-file", /* we don't want subtitles */ "--no-spu", "--no-osd", // "--no-inhibit", /* we don't want interfaces */ "--no-disable-screensaver", /* we don't want interfaces */ "--no-snapshot-preview", /* no blending in dummy vout */ }; library = LibVLCLibrary.Load(null); IntPtr m; /* Load the VLC engine */ inst = library.libvlc_new(vlcargs); /* Create a new item */ m = library.libvlc_media_new_path(inst, filepath); /* Create a media player playing environement */ mp = library.libvlc_media_player_new_from_media(m); //hook up the libvlc event listening int event_index = 0; IntPtr media_player_event_manager = library.libvlc_media_player_event_manager(mp); LibVLCLibrary.libvlc_callback_t m_EventManagerEventCallback = EventManager_Event; while (event_index < m_Events.Length) if (library.libvlc_event_attach(media_player_event_manager, m_Events[event_index++], m_EventManagerEventCallback, IntPtr.Zero) != 0) throw new LibVLCException(library); #endregion } else { library.libvlc_media_player_release(mp); IntPtr m = library.libvlc_media_new_path(inst, filepath); mp = library.libvlc_media_player_new_from_media(m); //hook up the libvlc event listening int event_index = 0; IntPtr media_player_event_manager = library.libvlc_media_player_event_manager(mp); LibVLCLibrary.libvlc_callback_t m_EventManagerEventCallback = EventManager_Event; while (event_index < m_Events.Length) if (library.libvlc_event_attach(media_player_event_manager, m_Events[event_index++], m_EventManagerEventCallback, IntPtr.Zero) != 0) throw new LibVLCException(library); } //configure time tracking TimeSpan time = new TimeSpan(0, 0, 0); TimeSpan step = new TimeSpan(0, 0, secondStep); //wait until VLC is playing signalPlaying = new SemaphoreSlim(0, 1); /* play the media_player */ int successCode = library.libvlc_media_player_play(mp); signalPlaying.Wait(); if (errorOccured) { string errormsg = library.libvlc_errmsg(); throw new Exception("libvlc_MediaPlayerEncounteredError error: " + errormsg ?? "unknown error... ah crap."); } endReached = false; stopped = false; int thumbnailHeight = -1; long trackLength = library.libvlc_media_player_get_length(mp); //while there's still video left, collect screenshots while (endReached != true && stopped != true) { //if we're flagged to cancel, exit out. if (State == GeneratorState.Cancelling) return; //take screenshot string fileName = time.TotalSeconds + ".jpg"; string thumbnailFullPath = Path.Combine(thumbnailDir, fileName); int result = -1; do { if (endReached) break; //if we're flagged to cancel, exit out. if (State == GeneratorState.Cancelling) return; int tryCount = 3; bool successfulSnapshot = false; Exception error = null; do { try { //if we're flagged to cancel, exit out. if (State == GeneratorState.Cancelling) return; successfulSnapshot = false; result = library.libvlc_video_take_snapshot(mp, 0, thumbnailFullPath, (uint)thumbnailWidth, 0); successfulSnapshot = true; } catch (Exception ex) { tryCount--; error = ex; } } while (!successfulSnapshot && tryCount > 0); if (null != error && tryCount == 0) { throw error; } } while (false == File.Exists(thumbnailFullPath)); if (result == 0) { mediaScribeServer.PushMessage(time.TotalSeconds.ToString().PadLeft(6, '0') + " " + fileName); //Thread.Sleep(70); } //increment the time time += step; //signalPosChanged.Reset(); library.libvlc_media_player_set_time(mp, (long)time.TotalMilliseconds); if (library.libvlc_media_player_get_state(mp) == LibVLCLibrary.libvlc_state_t.libvlc_Playing) library.libvlc_media_player_pause(mp); if (library.libvlc_media_player_get_time(mp) > trackLength) return; if (endReached) return; } }
//========================================================================== public MediaPlayer(LibVLCLibrary library, Instance instance) { if (library != null) { if (instance != null) { if (instance.Library != library) { throw new ArgumentException("The provided instance has not been created with the provided library!", "instance"); } } } if (instance != null) { if (library == null) { library = instance.Library; } } if (library == null) { m_Library = LibVLCLibrary.Load(null); m_IsLibraryOwner = true; } else { m_Library = library; m_IsLibraryOwner = false; } try { if (instance == null) { m_Instance = new Instance(m_Library); m_IsInstanceOwner = true; } else { m_Instance = instance; m_IsInstanceOwner = false; } try { m_MediaPlayerHandle = CreateHandle(); } catch { if (m_IsInstanceOwner) { m_Instance.Dispose(); } m_Instance = null; throw; } } catch { if (m_IsLibraryOwner) { LibVLCLibrary.Free(m_Library); } m_Library = null; throw; } }
//========================================================================== internal SubtitleTrack(int index, LibVLCLibrary.libvlc_track_description_t trackDescription, LibVLCLibrary.libvlc_media_track_info_t? trackInfo) : base(index, trackDescription, trackInfo) { // ... }