コード例 #1
0
        //==========================================================================
        private IntPtr CreateHandle()
        {
            IntPtr handle = m_Library.libvlc_media_player_new(m_Instance.Handle);

            if (handle == IntPtr.Zero)
            {
                throw new LibVLCException(m_Library);
            }
            RegisterMediaPlayer(handle, this);
            try
            {
                IntPtr media_player_event_manager = m_Library.libvlc_media_player_event_manager(handle);
                if (media_player_event_manager == IntPtr.Zero)
                {
                    throw new LibVLCException(m_Library);
                }
                int event_index = 0;
                try
                {
                    while (event_index < m_Events.Length)
                    {
                        if (m_Library.libvlc_event_attach(media_player_event_manager, m_Events[event_index++], m_EventManagerEventCallback, IntPtr.Zero) != 0)
                        {
                            throw new LibVLCException(m_Library);
                        }
                    }
                }
                catch
                {
                    while (event_index > 0)
                    {
                        m_Library.libvlc_event_attach(media_player_event_manager, m_Events[--event_index], m_EventManagerEventCallback, IntPtr.Zero);
                    }
                    throw;
                }

                m_Library.libvlc_video_set_format_callbacks(handle, m_VideoFormatCallback, m_VideoCleanupCallback);
                m_Library.libvlc_video_set_callbacks(handle, m_VideoLockCallback, m_VideoUnlockCallback, m_VideoDisplayCallback);

                return(handle);
            }
            catch
            {
                UnregisterMediaPlayer(handle);
                m_Library.libvlc_media_player_release(handle);
                throw;
            }
        }
コード例 #2
0
        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;
            }
        }