void EventsTimeChanged(object sender, MediaPlayerTimeChanged e) { Time = e.NewTime; if (LastFrame == DateTime.MinValue) { var sz = _mPlayer.GetVideoSize(0); _source.settings.vlcWidth = sz.Width; _source.settings.vlcHeight = sz.Height; } LastFrame = Helper.Now; }
private void EventsPlayerPositionChanged(object sender, MediaPlayerPositionChanged e) { Size size = _player.GetVideoSize(0); if (!size.IsEmpty) { _player.Events.PlayerPositionChanged -= EventsPlayerPositionChanged; _player.Events.PlayerEncounteredError -= EventsPlayerEncounteredError; UISync.Execute(() => SetVideoSize(size)); UISync.Execute(StopPlayer); } }
protected override void DoBackgroundWork() { try { frameEncodeInterval = 1000 / this.cameraSpec.vlc_transcode_fps; w = this.cameraSpec.h264_video_width; h = this.cameraSpec.h264_video_height; if (w <= 0 || h <= 0) { w = h = 0; } IVideoPlayer player = null; while (!Exit) { try { frameNumber = 0; lastFrameEncoded = 0; nextFrameEncodeTime = 0; lastTimestampUpdateTime = -1; frameTimer.Start(); IMediaPlayerFactory factory = new MediaPlayerFactory(); player = factory.CreatePlayer <IVideoPlayer>(); player.Events.TimeChanged += new EventHandler <Declarations.Events.MediaPlayerTimeChanged>(Events_TimeChanged); int b = cameraSpec.vlc_transcode_buffer_time; string[] args = new string[] { ":rtsp-caching=" + b, ":realrtsp-caching=" + b, ":network-caching=" + b, ":udp-caching=" + b, ":volume=0", cameraSpec.wanscamCompatibilityMode ? ":demux=h264" : "", cameraSpec.wanscamCompatibilityMode ? ":h264-fps=" + cameraSpec.wanscamFps : "" }; string url = cameraSpec.imageryUrl; if (cameraSpec.wanscamCompatibilityMode) { url = "http://127.0.0.1:" + MJpegWrapper.cfg.webport + "/" + cameraSpec.id + ".wanscamstream"; } IMedia media = factory.CreateMedia <IMedia>(url, args); memRender = player.CustomRenderer2; //memRender.SetExceptionHandler(ExHandler); memRender.SetCallback(delegate(Bitmap frame) { // We won't consume the bitmap here. For efficiency's sake under light load, we will only encode the bitmap as jpeg when it is requested by a client. frameNumber++; if (!player.Mute) { player.ToggleMute(); } if (frameTimer.ElapsedMilliseconds >= nextFrameEncodeTime) { EventWaitHandle oldWaitHandle = newFrameWaitHandle; newFrameWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset); oldWaitHandle.Set(); } //long time = frameCounter.ElapsedMilliseconds; //if (time >= nextFrameEncodeTime) //{ // lastFrame = ImageConverter.GetJpegBytes(frame); // nextFrameEncodeTime = time + frameEncodeInterval; //} //latestBitmap = new Bitmap(frame); // frame.Clone() actually doesn't copy the data and exceptions get thrown }); memRender.SetFormat(new BitmapFormat(w, h, ChromaType.RV24)); //memRender.SetFormatSetupCallback(formatSetupCallback); player.Open(media); player.Play(); if (w == 0) { // Need to auto-detect video size. while (!Exit) { Thread.Sleep(50); Size s = player.GetVideoSize(0); if (s.Width > 0 && s.Height > 0) { lock (MJpegWrapper.cfg) { w = this.cameraSpec.h264_video_width = (ushort)s.Width; h = this.cameraSpec.h264_video_height = (ushort)s.Height; MJpegWrapper.cfg.Save(Globals.ConfigFilePath); } throw new Exception("Restart"); } } } else { while (!Exit) { Thread.Sleep(50); } } } catch (ThreadAbortException ex) { throw ex; } catch (Exception ex) { if (ex.Message != "Restart") { Logger.Debug(ex); int waitedTimes = 0; while (!Exit && waitedTimes++ < 100) { Thread.Sleep(50); } } } finally { frameTimer.Stop(); frameTimer.Reset(); if (player != null) { player.Stop(); } newFrameWaitHandle.Set(); } } } catch (ThreadAbortException) { } catch (Exception ex) { Logger.Debug(ex); } newFrameWaitHandle.Set(); }