private void DrawFrame(VideoBuffer videoBuffer, PlaybackStatistics statistics)
        {
            Bitmap     bmp = img as Bitmap;
            BitmapData bd  = null;

            try
            {
                bd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//bgra32

                using (var md = videoBuffer.Lock())
                {
                    CopyMemory(bd.Scan0, md.value.scan0Ptr, videoBuff.stride * videoBuff.height);

                    //bitmap.WritePixels(
                    //    new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
                    //    md.value.scan0Ptr, videoBuffer.size, videoBuffer.stride,
                    //    0, 0
                    //);
                }
            }
            catch (Exception err)
            {
                //errBox.Text = err.Message;
                Debug.Print("DrawFrame:: " + err.Message);
            }
            finally
            {
                bmp.UnlockBits(bd);
            }
            imageBox.Image = bmp;
        }
Beispiel #2
0
        private void DrawFrame(VideoBuffer videoBuffer)
        {
            if (isRecording)
            {
                Bitmap     bitmap     = new Bitmap(videoBuffer.width, videoBuffer.height);
                BitmapData bitmapData = null;

                try
                {
                    bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                    using (var md = videoBuffer.Lock())
                    {
                        CopyMemory(bitmapData.Scan0, md.value.scan0Ptr, buffer.stride * buffer.height);
                    }
                }
                catch (Exception e)
                {
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }

                bitmaps.Enqueue(bitmap);
                //bitmaps.Add(bitmap);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initiate rendering loop
        /// </summary>
        /// <param name="videoBuffer"></param>
        public void InitPlayback(VideoBuffer videoBuffer)
        {
            if (videoBuffer == null)
            {
                throw new ArgumentNullException("videoBufferDescription");
            }
            VerifyAccess();

            TimeSpan renderinterval;

            try {
                int fps = AppDefaults.visualSettings.ui_video_rendering_fps;
                fps            = (fps <= 0 || fps > 100) ? 100 : fps;
                renderinterval = TimeSpan.FromMilliseconds(1000 / fps);
            } catch {
                renderinterval = TimeSpan.FromMilliseconds(1000 / 30);
            }

            var cancellationTokenSource = new CancellationTokenSource();

            renderSubscription.Disposable = Disposable.Create(() => {
                cancellationTokenSource.Cancel();
            });
            var bitmap            = PrepareForRendering(videoBuffer);
            var cancellationToken = cancellationTokenSource.Token;
            var dispatcher        = this.Dispatcher;      //Application.Current.Dispatcher;
            var renderingTask     = Task.Factory.StartNew(() => {
                var statistics = PlaybackStatistics.Start();
                using (videoBuffer.Lock()) {
                    try {
                        //start rendering loop
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            using (var processingEvent = new ManualResetEventSlim(false)) {
                                dispatcher.BeginInvoke(() => {
                                    using (Disposable.Create(() => processingEvent.Set())) {
                                        if (!cancellationToken.IsCancellationRequested)
                                        {
                                            //update statisitc info
                                            statistics.Update(videoBuffer);

                                            //render farme to screen
                                            DrawFrame(bitmap, videoBuffer, statistics);
                                        }
                                    }
                                });
                                processingEvent.Wait(cancellationToken);
                            }
                            cancellationToken.WaitHandle.WaitOne(renderinterval);
                        }
                    } catch (OperationCanceledException) {
                        //swallow exception
                    } catch (Exception error) {
                        dbg.Error(error);
                    }
                }
            }, cancellationToken);
        }
Beispiel #4
0
            public void Update(VideoBuffer videoBuffer)
            {
                //update rendering times history
                renderTimes.Enqueue(Stopwatch.GetTimestamp());

                //evaluate averange rendering fps
                avgRenderingFps = renderTimes.length / SecondsFromTicks(renderTimes.last - renderTimes.first);

                //update no signal indicator
                using (var md = videoBuffer.Lock()) {
                    signal          = md.value.signal;
                    md.value.signal = 0;
                }
                UpdateNoSignal();
            }
Beispiel #5
0
            public void Update(VideoBuffer videoBuffer)
            {
                //update rendering times history
                renderTimes.Enqueue(Stopwatch.GetTimestamp());

                //evaluate averange rendering fps
                avgRenderingFps = CalculateAvgFpsFromTimes(renderTimes);

                //update no signal indicator
                using (var md = videoBuffer.Lock()) {
                    signal          = md.value.signal;
                    md.value.signal = 0;
                }
                UpdateNoSignal();

                //evaluate averange rendering fps
                avgDecodingFps = CalculateAvgFpsFromTimes(decodeTimes);
            }
Beispiel #6
0
 private void DrawFrame(WriteableBitmap bitmap, VideoBuffer videoBuffer, PlaybackStatistics statistics)
 {
     VerifyAccess();
     if (isPaused)
     {
         return;
     }
     using (var md = videoBuffer.Lock()) {
         // internally calls lock\unlock, uses MILUtilities.MILCopyPixelBuffer
         bitmap.WritePixels(
             new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
             md.value.scan0Ptr, videoBuffer.size, videoBuffer.stride,
             0, 0
             );
     }
     renderingFps.Text        = String.Format("rendering fps: {0:F1}", statistics.avgRenderingFps);
     decodingFps.Text         = String.Format("decoding fps: {0:F1}", statistics.avgDecodingFps);
     noSignalPanel.Visibility =
         statistics.isNoSignal ? Visibility.Visible : Visibility.Hidden;
 }
Beispiel #7
0
        private void DrawFrame(WriteableBitmap bitmap, VideoBuffer videoBuffer, double averangeFps)
        {
            VerifyAccess();
            if (isPaused)
            {
                return;
            }

            bitmap.Lock();
            try {
                using (var ptr = videoBuffer.Lock()) {
                    bitmap.WritePixels(
                        new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
                        ptr.value, videoBuffer.size, videoBuffer.stride,
                        0, 0
                        );
                }
            } finally {
                bitmap.Unlock();
            }
            fpsCaption.Text = averangeFps.ToString("F1");
        }
        private void DrawFrame(VideoBuffer videoBuffer /*, PlaybackStatistics statistics*/)
        {
            Bitmap     bitmap     = image as Bitmap;
            BitmapData bitmapData = null;

            try
            {
                bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                using (var md = videoBuffer.Lock())
                {
                    CopyMemory(bitmapData.Scan0, md.value.scan0Ptr, this.videoBuffer.stride * this.videoBuffer.height);
                }
            }
            catch (Exception e)
            {
                //errBox.Text = err.Message;
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }
            pictureBox.Image = bitmap;
        }
        //private WriteableBitmap PrepareForRendering(VideoBuffer videoBuffer) {
        //    PixelFormat pixelFormat;
        //    if (videoBuffer.pixelFormat == PixFrmt.rgb24) {
        //        pixelFormat = PixelFormats.Rgb24;
        //    } else if (videoBuffer.pixelFormat == PixFrmt.bgra32) {
        //        pixelFormat = PixelFormats.Bgra32;
        //    } else if (videoBuffer.pixelFormat == PixFrmt.bgr24) {
        //        pixelFormat = PixelFormats.Bgr24;
        //    } else {
        //        pixelFormat = PixelFormats.Rgb24;
        //    }
        //    var bitmap = new WriteableBitmap(
        //        videoBuffer.width, videoBuffer.height,
        //        96, 96,
        //        pixelFormat, null
        //    );
        //    _imgVIew.Source = bitmap;
        //    return bitmap;
        //}
        void InitPlayback(VideoBuffer videoBuffer, bool isInitial)
        {
            if (videoBuffer == null)
            {
                dbg.Break();
                log.WriteError("video buffer is null");
            }

            TimeSpan renderinterval;

            try
            {
                int fps = 30;
                fps            = (fps <= 0 || fps > 100) ? 100 : fps;
                renderinterval = TimeSpan.FromMilliseconds(1000 / fps);
            }
            catch
            {
                renderinterval = TimeSpan.FromMilliseconds(1000 / 30);
            }

            var cancellationTokenSource = new CancellationTokenSource();

            playerDisposables.Add(Disposable.Create(() =>
            {
                cancellationTokenSource.Cancel();
            }));

            //var bitmap = PrepareForRendering(videoBuffer);
            img            = new Bitmap(videoBuffer.width, videoBuffer.height);
            imageBox.Image = img;

            var cancellationToken = cancellationTokenSource.Token;

            var renderingTask = Task.Factory.StartNew(delegate
            {
                var statistics = PlaybackStatistics.Start(Restart, isInitial);
                using (videoBuffer.Lock())
                {
                    try
                    {
                        //start rendering loop
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            using (var processingEvent = new ManualResetEventSlim(false))
                            {
                                var dispOp = disp.BeginInvoke((MethodInvoker) delegate
                                {
                                    using (Disposable.Create(() => processingEvent.Set()))
                                    {
                                        if (!cancellationToken.IsCancellationRequested)
                                        {
                                            //update statisitc info
                                            statistics.Update(videoBuffer);

                                            //render farme to screen
                                            //DrawFrame(bitmap, videoBuffer, statistics);
                                            DrawFrame(videoBuffer, statistics);
                                        }
                                    }
                                });
                                processingEvent.Wait(cancellationToken);
                            }
                            cancellationToken.WaitHandle.WaitOne(renderinterval);
                        }
                    }
                    catch (OperationCanceledException error)
                    {
                        //swallow exception
                        Debug.Print("OperationCanceledException:: " + error.Message);
                    }
                    catch (Exception error)
                    {
                        //dbg.Break();
                        //log.WriteError(error);
                        Debug.Print("InitPlayback:: " + error.Message);
                    }
                    finally
                    {
                    }
                }
            }, cancellationToken);
        }
Beispiel #10
0
        private void InitializePlayer(string videoUri, NetworkCredential account, Size size = default(Size))
        {
            OnConnect(this, null);

            dispatcher = Dispatcher.CurrentDispatcher;

            player = new HostedPlayer();
            disposables.Add(player);

            if (size != default(Size))
            {
                buffer = new VideoBuffer(size.Width, size.Height, PixFrmt.bgra32);
            }
            else
            {
                buffer = new VideoBuffer(720, 576, PixFrmt.bgra32);
            }
            player.SetVideoBuffer(buffer);

            MediaStreamInfo.Transport transport  = MediaStreamInfo.Transport.Tcp;
            MediaStreamInfo           streamInfo = null;

            if (account != null)
            {
                streamInfo = new MediaStreamInfo(videoUri, transport, new UserNameToken(account.UserName, account.Password));
            }
            else
            {
                streamInfo = new MediaStreamInfo(videoUri, transport);
            }

            disposables.Add(player.Play(streamInfo, this));

            TimeSpan renderInterval;

            try
            {
                int fps = 30;
                fps            = (fps <= 0 || fps > 100) ? 100 : fps;
                renderInterval = TimeSpan.FromMilliseconds(1000 / fps);
            }
            catch
            {
                renderInterval = TimeSpan.FromMilliseconds(1000 / 30);
            }

            var cancellationTokenSource = new CancellationTokenSource();

            disposables.Add(Disposable.Create(() => {
                cancellationTokenSource.Cancel();
            }));

            var cancellationToken = cancellationTokenSource.Token;

            var renderingTask = Task.Factory.StartNew(() => {
                using (buffer.Lock())
                {
                    try
                    {
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            using (var processingEvent = new ManualResetEventSlim(false))
                            {
                                var dispOp = dispatcher.BeginInvoke(() => {
                                    using (Disposable.Create(() => processingEvent.Set()))
                                    {
                                        if (!cancellationToken.IsCancellationRequested)
                                        {
                                            DrawFrame(buffer);
                                        }
                                    }
                                });
                                processingEvent.Wait(cancellationToken);
                            }
                            cancellationToken.WaitHandle.WaitOne(renderInterval);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        //swallow exception
                    }
                    catch (Exception e)
                    {
                    }
                    finally
                    {
                    }
                }
            }, cancellationToken);
        }
        private void InitPlayback(VideoBuffer videoBuffer, bool isInitial)
        {
            if (videoBuffer == null)
            {
                dbg.Break();
                log.WriteError("video buffer is null");
            }

            TimeSpan renderInterval;

            try
            {
                int fps = 30;
                fps            = (fps <= 0 || fps > 100) ? 100 : fps;
                renderInterval = TimeSpan.FromMilliseconds(1000 / fps);
            }
            catch
            {
                renderInterval = TimeSpan.FromMilliseconds(1000 / 30);
            }

            var cancellationTokenSource = new CancellationTokenSource();

            playerDisposables.Add(Disposable.Create(() => {
                cancellationTokenSource.Cancel();
            }));

            image            = new Bitmap(videoBuffer.width, videoBuffer.height);
            pictureBox.Image = image;

            var cancellationToken = cancellationTokenSource.Token;

            var renderingTask = Task.Factory.StartNew(delegate {
                //var statistics = PlaybackStatistics.Start(Restart, isInitial);
                using (videoBuffer.Lock())
                {
                    try
                    {
                        //start rendering loop
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            using (var processingEvent = new ManualResetEventSlim(false))
                            {
                                var dispOp = dispatcher.BeginInvoke(() => {
                                    using (Disposable.Create(() => processingEvent.Set()))
                                    {
                                        if (!cancellationToken.IsCancellationRequested)
                                        {
                                            DrawFrame(videoBuffer);
                                        }
                                    }
                                });
                                processingEvent.Wait(cancellationToken);
                            }
                            cancellationToken.WaitHandle.WaitOne(renderInterval);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        //swallow exception
                    }
                    catch (Exception error)
                    {
                        dbg.Break();
                        log.WriteError(error);
                    }
                    finally
                    {
                    }
                }
            }, cancellationToken);
        }
Beispiel #12
0
        /// <summary>
        /// Start Playback
        /// </summary>
        /// <param name="res"></param>
        public void InitPlayback(VideoBuffer videoBuffer)
        {
            if (videoBuffer == null)
            {
                throw new ArgumentNullException("videoBufferDescription");
            }
            VerifyAccess();

            TimeSpan renderinterval;

            try {
                int fps = AppDefaults.visualSettings.ui_video_rendering_fps;
                fps            = (fps <= 0 || fps > 100) ? 100 : fps;
                renderinterval = TimeSpan.FromMilliseconds(1000 / fps);
            } catch {
                renderinterval = TimeSpan.FromMilliseconds(1000 / 30);
            }

            var cancellationTokenSource = new CancellationTokenSource();

            renderSubscription.Disposable = Disposable.Create(() => {
                cancellationTokenSource.Cancel();
            });
            var bitmap            = PrepareForRendering(videoBuffer);
            var cancellationToken = cancellationTokenSource.Token;
            var dispatcher        = Application.Current.Dispatcher;
            var renderingTask     = Task.Factory.StartNew(() => {
                var statistics = new CircularBuffer <long>(100);
                using (videoBuffer.Lock()) {
                    try {
                        //start rendering loop
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            using (var processingEvent = new ManualResetEventSlim(false)) {
                                dispatcher.BeginInvoke(() => {
                                    using (Disposable.Create(() => processingEvent.Set())) {
                                        if (!cancellationToken.IsCancellationRequested)
                                        {
                                            //update statisitc info
                                            statistics.Enqueue(Stopwatch.GetTimestamp());
                                            //evaluate averange rendering fps
                                            var ticksEllapsed = statistics.last - statistics.first;
                                            double avgFps     = 0;
                                            if (ticksEllapsed > 0)
                                            {
                                                avgFps = ((double)statistics.length * (double)Stopwatch.Frequency) / (double)ticksEllapsed;
                                            }
                                            //render farme to screen
                                            DrawFrame(bitmap, videoBuffer, avgFps);
                                        }
                                    }
                                });
                                processingEvent.Wait(cancellationToken);
                            }
                            cancellationToken.WaitHandle.WaitOne(renderinterval);
                        }
                    } catch (OperationCanceledException) {
                        //swallow exception
                    } catch (Exception error) {
                        dbg.Error(error);
                    }
                }
            }, cancellationToken);
        }