Ejemplo n.º 1
0
        public void TestToxAvSendVideo()
        {
            _toxAv2.OnVideoFrameReceived += (sender, e) =>
            {
                Console.WriteLine("Received frame, width: {0}, height: {1}", e.Frame.Width, e.Frame.Height);
            };

            for (int i = 0; i < 100; i++)
            {
                var random = new Random();
                int width  = 800;
                int height = 600;

                byte[] y = new byte[width * height];
                byte[] u = new byte[(height / 2) * (width / 2)];
                byte[] v = new byte[(height / 2) * (width / 2)];

                var frame = new ToxAvVideoFrame(800, 600, y, u, v);

                var  error  = ToxAvErrorSendFrame.Ok;
                bool result = _toxAv1.SendVideoFrame(0, frame, out error);

                if (!result || error != ToxAvErrorSendFrame.Ok)
                {
                    Assert.Fail("Failed to send video frame, error: {0}, result: {1}", error, result);
                }

                DoIterate();
            }
        }
Ejemplo n.º 2
0
        public bool SendVideoFrame(int friendNumber, ToxAvVideoFrame frame)
        {
            ToxAvErrorSendFrame error;
            var retVal = _toxAv.SendVideoFrame(friendNumber, frame, out error);

            ToxErrorViewModel.Instance.RelayError(error);
            return(retVal);
        }
Ejemplo n.º 3
0
        public static BitmapSource ToxAvFrameToBitmap(ToxAvVideoFrame frame)
        {
            byte[] data = new byte[frame.Width * frame.Height * 4];
            Yuv420ToBgr((ushort)frame.Width, (ushort)frame.Height, frame.Y, frame.U, frame.V, (uint)frame.YStride, (uint)frame.UStride, (uint)frame.VStride, data);

            int bytesPerPixel = (PixelFormats.Bgra32.BitsPerPixel + 7) / 8;
            int stride        = 4 * ((frame.Width * bytesPerPixel + 3) / 4);

            var source = BitmapSource.Create(frame.Width, frame.Height, 96d, 96d, PixelFormats.Bgra32, null, data, stride);

            source.Freeze();

            return(source);
        }
Ejemplo n.º 4
0
        public static Bitmap ToxAvFrameToBitmap(ToxAvVideoFrame toxFrame, bool withLibYuv = false)
        {
            if (toxFrame == null)
            {
                return(null);
            }
            if (toxFrame.Height <= 0 || toxFrame.Height % 2 > 0)
            {
                return(null);
            }
            if (toxFrame.Width <= 0 || toxFrame.Width % 2 > 0)
            {
                return(null);
            }

            fromFrameTotal.Start();

            Bitmap     bmp        = new Bitmap(toxFrame.Width, toxFrame.Height, PixelFormat.Format32bppArgb);
            BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);

            try
            {
                if (withLibYuv)
                {
                    fromFrameInner.Start();
                    int argb_stride = toxFrame.Width * 4;
                    NativeMethods.Yuv420ToBgra_LibYuvX86(toxFrame.Y, toxFrame.YStride, toxFrame.U, toxFrame.UStride, toxFrame.V, toxFrame.VStride, bitmapData.Scan0, argb_stride, toxFrame.Width, toxFrame.Height);
                    fromFrameInner.Stop();
                }
                else
                {
                    fromFrameInner.Start();
                    NativeMethods.Yuv420ToBgra(toxFrame.Width, toxFrame.Height, toxFrame.Y, toxFrame.U, toxFrame.V, toxFrame.YStride, toxFrame.UStride, toxFrame.VStride, bitmapData.Scan0);
                    fromFrameInner.Stop();
                }
            }
            finally
            {
                bmp.UnlockBits(bitmapData);
            }

            fromFrameTotal.Stop();
            fromFrameCounter++;

            return(bmp);
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            if (args.Length == 0 || (args.Length == 1 && args[0].Equals("-h", StringComparison.OrdinalIgnoreCase)))
            {
                Console.WriteLine("tfutils_test:");
                Console.WriteLine("  -i fileName  input image filename");
                Console.WriteLine("  -o fileName  [optional] output image filename");
                Console.WriteLine("  -n count     count of test iterations");
                return;
            }

            string inputFileName  = null;
            string outputFileName = null;
            int    count          = 0;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].Equals("-i", StringComparison.OrdinalIgnoreCase) || args[i].Equals("/i", StringComparison.OrdinalIgnoreCase))
                {
                    if (i + 1 < args.Length)
                    {
                        inputFileName = args[++i];
                    }
                }
                else if (args[i].Equals("-o", StringComparison.OrdinalIgnoreCase) || args[i].Equals("/o", StringComparison.OrdinalIgnoreCase))
                {
                    if (i + 1 < args.Length)
                    {
                        outputFileName = args[++i];
                    }
                }
                else if (args[i].Equals("-n", StringComparison.OrdinalIgnoreCase) || args[i].Equals("/n", StringComparison.OrdinalIgnoreCase))
                {
                    if (i + 1 < args.Length)
                    {
                        String countStr = args[++i];
                        int    result   = -1;
                        if (int.TryParse(countStr, out result))
                        {
                            count = result;
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(inputFileName))
            {
                Console.WriteLine("Input filename not supplied");
                return;
            }
            if (string.IsNullOrEmpty(outputFileName))
            {
                // do nothing
            }
            if (count <= 0)
            {
                Console.WriteLine("Count of iterations not supplied or negative: " + count.ToString());
                return;
            }

            try
            {
                Image inputImage = Image.FromFile(inputFileName);

                using (Bitmap inputBitmap = new Bitmap(inputImage))
                {
                    if (inputBitmap.PixelFormat != PixelFormat.Format32bppArgb)
                    {
                        Console.WriteLine("Failed to prepare 32 bpp image from file: " + inputFileName);
                        return;
                    }

                    Console.WriteLine(string.Format("Input file  : {0}", inputFileName));
                    Console.WriteLine(string.Format("Image size  : {0}x{1}", inputBitmap.Width, inputBitmap.Height));
                    Console.WriteLine(string.Format("Image format: {0}", inputBitmap.PixelFormat));
                    Console.WriteLine(string.Format("Output file : {0}", (!string.IsNullOrEmpty(outputFileName) ? outputFileName : "N/A")));
                    Console.WriteLine(string.Format("Count       : {0}", count));
                    Console.WriteLine();

                    VideoUtils.ResetTimingInfo();

                    for (int i = 0; i < count; i++)
                    {
                        ToxAvVideoFrame toxFrame     = VideoUtils.BitmapToToxAvFrame(inputBitmap);
                        Bitmap          outputBitmap = VideoUtils.ToxAvFrameToBitmap(toxFrame);
                        outputBitmap.Dispose();
                    }

                    Console.WriteLine(">>>> " + VideoUtils.GetToFrameTimingInfo());
                    Console.WriteLine("<<<< " + VideoUtils.GetFromFrameTimingInfo());

                    if (!string.IsNullOrEmpty(outputFileName))
                    {
                        ToxAvVideoFrame toxFrame     = VideoUtils.BitmapToToxAvFrame(inputBitmap);
                        Bitmap          outputBitmap = VideoUtils.ToxAvFrameToBitmap(toxFrame);
                        outputBitmap.Save(outputFileName, inputImage.RawFormat);
                        outputBitmap.Dispose();
                    }

                    Console.WriteLine();
                    Console.WriteLine("Another round with LibYuv ...");
                    if (VideoUtils.CpuHasSsse3)
                    {
                        Console.WriteLine("CPU has SSSE3 ...");
                    }
                    Console.WriteLine();

                    VideoUtils.ResetTimingInfo();

                    for (int i = 0; i < count; i++)
                    {
                        ToxAvVideoFrame toxFrame     = VideoUtils.BitmapToToxAvFrame(inputBitmap, true);
                        Bitmap          outputBitmap = VideoUtils.ToxAvFrameToBitmap(toxFrame, true);
                        outputBitmap.Dispose();
                    }

                    Console.WriteLine(">>>> " + VideoUtils.GetToFrameTimingInfo());
                    Console.WriteLine("<<<< " + VideoUtils.GetFromFrameTimingInfo());

                    if (!string.IsNullOrEmpty(outputFileName))
                    {
                        ToxAvVideoFrame toxFrame     = VideoUtils.BitmapToToxAvFrame(inputBitmap, true);
                        Bitmap          outputBitmap = VideoUtils.ToxAvFrameToBitmap(toxFrame, true);
                        outputFileName = AppendFileNameSuffix(outputFileName, "-libyuv");
                        outputBitmap.Save(outputFileName, inputImage.RawFormat);
                        outputBitmap.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            //Console.WriteLine();
            //Console.WriteLine("Press ENTER to exit ...");
            //Console.ReadLine();
        }
Ejemplo n.º 6
0
 public bool SendVideoFrame(int friendNumber, ToxAvVideoFrame frame)
 {
     ToxAvErrorSendFrame error;
     var retVal = _toxAv.SendVideoFrame(friendNumber, frame, out error);
     ToxErrorViewModel.Instance.RelayError(error);
     return retVal;
 }