Azar Shaikh
Azar Shaikh

Reputation: 449

C# Best approach to send screen difference over socket

I am working on screen sharing project. I am sending only screen differences over socket comparing previous and actual buffer. It working

I am sending 8 to 9 FPS to client using Format16bppRgb555 to reduce overall bytes size of Bitmap

byte[] wholescreensize= new byte[1360 * 768 * 2];// Its around 2 Mb

My problem Is when full screen is changed.

I am getting about 45-60 kb of PNG image using below function

45kb * 10 (FPS) = 450 kb

It is possible to reduce beyond 45 kb.

I am not interested to reduce FPS as it live screen sharing app.

JPEG Compression or LZ4/GZIP also not making much difference as PNG image already compressed

private void SendImgDiffToClient(byte[] contents,Rectangle rectangle)
{   

    //Converting Small Portion to Bitmap.Bcoz Image.FromStrem not working here error Parameter is not Valid
    byte[] byteArrayout = new byte[contents.Length];

    var bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format16bppRgb555);
    var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb555);
    Marshal.Copy(contents, 0, bitmap_data.Scan0, byteArrayout.Length);
    bitmap.UnlockBits(bitmap_data);

    //Converting Small Bitmap to Png Byte Array and Sending to Client
    using (MemoryStream ms = new MemoryStream())
    { 
        Image msImage = (Image)bitmap;
        msImage.Save(ms, ImageFormat.Png);

        msImage.Dispose();
        byteArrayout = ms.ToArray();
    }  

    SendtoClient(byteArrayout);
}

My Questing is what is a best approach to reduce bytes in such scenario.

Upvotes: 3

Views: 972

Answers (1)

Greg
Greg

Reputation: 2600

Video streaming is essentially what you're doing; and modern video compression algorithms have lots of enhancements. Perhaps they can track or move an artifact, or otherwise distort said artifact as part of their functionality. Perhaps they can stream the data in a progressively building manner, so that static items eventually acquire more detail (similar to progressive jpeg images.) They do lots of things all at the same time. You can try to research them further, and take inspiration from them, or you could pick and use one.

This is to say that many people here seem to prefer the solution of using a readily available video compression library. Especially if you are worried about streaming bandwidth.

If you don't want to use an existing video library, then you have to decide how much effort you want to put in, versus how sloppy you want to be with consuming more bandwidth than otherwise necessary.

Upvotes: 2

Related Questions