Example #1
0
        private static void Main(string[] args)
        {
            //The url to push.
            var pushUrl = "rtmp://localhost:1395/live/stream";

            //Push frame rate.
            var frameRate = 24;

            //Define the width and height
            var width  = 1280;
            var height = 720;

            //All images in the folder should have the same width and height.
            var imageFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
            var images      = Directory.GetFiles(imageFolder);

            Console.WriteLine("Press any key to start push.");
            Console.ReadLine();

            var pusher = new Pusher();

            pusher.StartPush(pushUrl, width, height, frameRate);
            var encoder = new Encoder(width, height, frameRate);

            encoder.FrameEncoded += (sender, e) =>
            {
                //A frame encoded.
                var packet = e.Packet;
                pusher.PushPacket(packet);
                Console.WriteLine($"Packet pushed, size:{packet.Size}.");
            };

            for (int x = 0; x < 1; x++)
            {
                for (int i = 0; i < images.Length; i++)
                {
                    var      bitmap = new Bitmap(images[i]);
                    Graphics g      = Graphics.FromImage(bitmap);
                    g.DrawString(((x * images.Length) + i).ToString(), new Font(FontFamily.GenericSansSerif, 20, FontStyle.Bold), Brushes.LawnGreen, new PointF(1, 20));
                    g.Dispose();
                    Rectangle  rect      = new Rectangle(0, 0, width, height);
                    BitmapData bmpData   = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                    IntPtr     ptr       = bmpData.Scan0;
                    int        pixels    = width * height;
                    byte[]     rgbValues = new byte[pixels * 4];
                    Marshal.Copy(ptr, rgbValues, 0, pixels * 4);
                    bitmap.UnlockBits(bmpData);
                    encoder.AddImage(rgbValues);
                    bitmap.Dispose();
                }
            }
            //Flush
            encoder.Flush();

            pusher.StopPush();
            pusher.Dispose();
            encoder.Dispose();
            Console.WriteLine("Finished!");
            Console.ReadLine();
        }
Example #2
0
 internal void Stop()
 {
     try
     {
         _encoder.FrameEncoded -= Encoder_FrameEncoded;
         _encoder.Flush();
         _encoder.Dispose();
         _pusher.StopPush();
         _pusher.Dispose();
         IsStarted = false;
     }
     catch
     {
     }
 }
Example #3
0
        private static void Main()
        {
            //The url to push.
            var pushUrl = "rtmp://localhost:1935/live/stream";

            //Push frame rate.
            var frameRate    = 15;
            var waitInterval = 1000 / frameRate;


            var screenWidth  = GetSystemMetrics(0);
            var screenHeight = GetSystemMetrics(1);

            //Define the width and height
            var width  = screenWidth;
            var height = screenHeight;

            var pusher = new Pusher();

            pusher.StartPush(pushUrl, width, height, frameRate);

            var stopEvent = new ManualResetEvent(false);

            var thread = new Thread(() =>
            {
                var encoder           = new Encoder(width, height, frameRate, 1024 * 800);
                encoder.FrameEncoded += (sender, e) =>
                {
                    //A frame encoded.
                    var packet = e.Packet;
                    pusher.PushPacket(packet);
                    Console.WriteLine($"Packet pushed, size:{packet.Size}.");
                };
                var screenDc = GetDC(IntPtr.Zero);
                var bitmap   = new Bitmap(screenWidth, screenHeight);

                try
                {
                    while (!stopEvent.WaitOne(1))
                    {
                        var start = Environment.TickCount;
                        using (var graphic = Graphics.FromImage(bitmap))
                        {
                            var imageDc = graphic.GetHdc();
                            BitBlt(imageDc, 0, 0, width, height, screenDc, 0, 0, 0x00CC0020);
                        }
                        encoder.AddImage(bitmap);
                        var timeUsed   = Environment.TickCount - start;
                        var timeToWait = waitInterval - timeUsed;
                        Thread.Sleep(timeToWait < 0? 0: timeToWait);
                    }
                    encoder.Flush();
                }
                finally
                {
                    encoder.Dispose();
                    bitmap.Dispose();
                    ReleaseDC(IntPtr.Zero, screenDc);
                }
            });

            thread.Start();

            Console.ReadLine();

            stopEvent.Set();

            thread.Join();
            pusher.StopPush();
            pusher.Dispose();

            Console.WriteLine("Stopped!");
            Console.ReadLine();
        }