Click here to Skip to main content
Click here to Skip to main content

Motion JPEG Streaming Server

By , 23 Apr 2012
 

For mobile clients:

Samsung Galaxy SII

Other clients (such as Firefox, Chrome and VLC player):

Streaming the desktop images to different remote clients

Introduction

Motion JPEG is a stream of JPG images over HTTP protocol and it is commonly used for many multimedia applications specially in digital cameras (IP cameras). Although there are many ways to send images as a video to any remote computer  MJPEG is the preferred one since normally it does not need any client software to be installed in the remote computer!. that is because it is supported by most of the common web browsers like Firefox and Chrome but unfortunately, it is not yet supported by Internet Explorer directly (but there are some plug-ins that make it possible to work with IE).

Motivation 

In many I needed to stream a video of my desktop, camera, or whatever to a remote computer which has no client software to see my stream... In such a case, using Motion-JPEG streams is preferred because it needs no more than an internet browser. In this article I'm not going to describe the internal structure of an MJPEG stream (for more information, please visit Motion_JPEG) instead of that I'm going to describe how to use this library for MJPEG streaming.

How to use this library

The main class in our library is ImageSreamingSever() which contains the following methods and properties:

Methods:

  • void Start(): Starts the server on a specified TCP port.
  • void Stop(): Stops the server and closes any open connections.

Properties:

  • ImagesSource: The images collection (IEnumerable<Image>) to be streamed to any connected client.
  • Interval: The delay time (in milliseconds) between each sending image from the ImagesSource collection.
  • IsRunning: Is a read-only bool property indicating the status of the server.
  • Clients: Returns a read-only collection of the currently connected socket of each client.

Example:

In order to implement a streaming server that can send a video of your desktop screen to any connected client, you'll need only two lines of code using our ImageStreamingServer, as follow:

ImageStreamingServer server = new ImageStreamingServer(); 
server.Start(8080);

That's it !!!.. 

Now if you open (for example) the chrome internet explorer and write the IP of the server followed by colon and then the port number, in our example it is something like this :

http://127.0.0.1:8080

you will properly see a video of your computer screen in the explorer .. and same if you do this from another computer (with the correct IP of your server), you should see something like this :

Google Chrome

Implementing your own ImagesSource 

Actually I've chosen IEnumerable<Image> as a source of images because of the flexibility that this interface is providing, for example the default ImagesSource property of the class ImageStreamingServer is an IEnumerable<Image> of the desktop screen, but at the same time you are free to implement any other type of image source you like by changing the value of that property.

Suppose you want to make the images source from the camera or from the images file of any folder in your computer, then your code should be something like this:

IEnumerable<Image> CameraImagesSource() 
{ 
    // Your code must be here 
} 

IEnumerable<Image> FolderImagesSource(string path) 
{ 
    // Your code must be here 
}

As a real example, let's implement a source of images that streams all "*.jpg" photos from a specified folder path as follows:

private IEnumerable<Image> FolderImagesSource(string path)
{
    var files = System.IO.Directory.GetFiles(path, "*.jpg");

    foreach (var file in files)
        yield return Image.FromFile(file);

    yield break;
}

private void Form1_Load(object sender, EventArgs e)
{
    var source = FolderImagesSource(@"C:\Pictures\");

    _Server = new ImageStreamingServer();
    _Server.ImagesSource = source;
    _Server.Start(8080);
}

Note: the above code can be shortened as follows (thanks for IEnumerable extensions and LINQ):

var source = System.IO.Directory.GetFiles(@"C:\Pictures\", "*.jpg").Select(s => Image.FromFile(s));

_Server = new ImageStreamingServer(source);
_Server.Start(8080);

Supported client software

To see the streaming video of our server, you will need to use any software that supports Motion-JPEG streaming such as a Mozilla Firefox, Google's Chrome or VideoLAN Player (VLC) :

Free client software for Windows PC

And finally for your mobile (android) use FireFox or any other free app that can be found on Google's market such as tinyCamMonitor (that was used in the picture below):

Samsung Galaxy SII

Future versions 

In the future versions of the library I'm going to add a variety of images sources such as:

  • Images from a specific region of the screen.
  • Images from a specific window in the system.
  • Streaming images from a camera. 
  • All photos from any folder in a disk.
  • And any other suggested sources of images you may want !? ...

And till that time, fill free to enjoy this version ;-)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ragheed Al-Tayeb
Team Leader Hawk International for Finance & Construction Ltd.
Yemen Yemen
Member
Bachelor degree of Computer Sciences, Software developer using .NET (Visual Basic & C#).
I like the development of an Artificial Intelligence Systems, GPS Applications, GDI+, APIs, N-Tier Applications, Distributed Systems and Network Monitoring Systems.
Mobile Development (Windows Mobile & Android).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAbout MJPEG Streaming ServermemberMember 998370612 Apr '13 - 6:02 
which software process model is used in MJPEG streaming Server ? Please tell me soon.
QuestionVideo StreamingmemberFábio Pinho29 Mar '13 - 4:30 
Hello,
 
My name is Fabio, and i'm developing a c# app, where i need to stream mjpeg video from c# to a mobile device. I am having serious problems to do it but your snippet seems pretty promissor. I've been trying to stream sets of images, and that works fine, but i really cant change it to stream video frames because the IEnumerable don't accept it.
 
My question is, do you already change your functions to allow video streaming? If you didn't, how do o plan to do it?
 
Thanks for your help, and for your excellent work
AnswerRe: Video Streamingmemberjoshua53679 Apr '13 - 20:44 
Hi, you may create a class to read video frames and output IEnumerable<Image> to the image streaming server. I appended a sample class where you can call AssignNewFrame function to update the image to current video frame. There is also an example of how to use the class ImageStreamSource with the mjpeg streaming server.
 
public class ImageStreamSource : IEnumerable
{
    bool IsActive = true;
    Image latestImage = null;
    ManualResetEvent newFrameMre = new ManualResetEvent(true);
 
    public ImageStreamSource()
    {
        
    }
 
    public void AssignNewFrame(Image item)
    {
        latestImage = item;
        newFrameMre.Set();
    }
 
    // IEnumerable Member
    public IEnumerator GetEnumerator()
    {
        yield break;
    }
 
    public IEnumerable<Image> GetImageEnumerator()
    {
        while (IsActive)
        {
            newFrameMre.WaitOne();
            yield return latestImage;
        }
 
        yield break;
    }
 
    public void Stop()
    {
        IsActive = false;
    }
}
 
//Initialization
ImageStreamSource imageStreamingSource = new ImageStreamSource();
ImageStreamingServer imageStreamingServer = new ImageStreamingServer(imageStreamingSource.GetImageEnumerator());
 
//Usage
Bitmap frame = new Bitmap("some video frame");
imageStreamingSource.AssignNewFrame(frame);

GeneralRe: Video StreamingmemberFábio Pinho9 Apr '13 - 22:19 
Hi Joshua,
 
Thanks a lot for your help. In the mean time i kept working with your framework and had already done something like what you did, but your code is better Smile | :) Now it works almost fine, but once i have much to process in each frame after a few seconds the frames start arriving with delay to the mobile. It is possible to discard the old frames in memory and jump to the most recent ones?
 
Thanks again for your help
BugError when Call Stop()memberHong Thai8 Mar '13 - 5:41 
Hi,
 
When I create a button with method _Server.Stop()the mjpeg stream not stop and app hang. Please check again
Questionhow to connect web camera as a source?memberAl-hassaan17 Feb '13 - 7:45 
heyy bro,thanx for the nice article,but i need to stream images from webcamera,,,
please guide me as soon as possible
QuestionAny chance for VB.net Example?memberitayzoro9 Feb '13 - 22:07 
10nx very nice
GeneralMy vote of 5memberKarell Ste-Marie1 Feb '13 - 6:18 
Great article, well written, good content
QuestionThx!memberzuppaman15 Jan '13 - 6:02 
Great example.
Thx for sharing man!
QuestionWebcammembermornej18 Dec '12 - 23:00 
Hi,
 
How do I connect a webcam on the local server to this?
 
Thanks

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 23 Apr 2012
Article Copyright 2012 by Ragheed Al-Tayeb
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid