Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: VideoStreamer - .NET Video Player  (Read 46165 times)

0 Members and 1 Guest are viewing this topic.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
VideoStreamer - .NET Video Player
« on: July 20, 2013, 06:05:25 am »
What is it?

VideoStreamer is a simple video player I wrote in C++ with a C binding to .NET to provide a way for me to render video in C# with SFML 2.x. Internally it uses FFMPEG for reading/decoding the video clips.


Where is it used?

I use VideoStreamer in my own project that I hope to be releasing soontm. (I use videos as backgrounds)  :)


Special Thanks

I also want to give thanks for inspiration from Ceylo's sfeMovie Project that helped me write VideoStreamer. And of course big thanks to Laurent for SFML.


Licensing

VideoStreamer is licensed with the ZLIP/PNG, the license same as SFML. That way you can do whatever you want with it. Feel free to write patches and make pull requests on bitbucket. If you look at the code you will see that it is a bit ugly and that I am not a C++ developer. Personally I am happy with the performance, but if anyone wants to post improvements I am wide open to suggestions.


Features

  • Complete video/audio playback
  • Basic control with play/pause/stop
  • Supports any codec/format FFMPEG supports
  • Seek anywhere during video/audio playback
  • Supports video/audio syncing to elapsed time
  • Supports audio resampling in case the input format is not signed 16 bit integers
  • Should be cross platform with the current design (see "Modules" below)


Modules

  • VideoStreamer (static compile) - Written in C++ and wraps the FFMPEG functionality
  • CVideoStreamer (dynamic/shared compile) - Written in C++ with a C interface to expose ViceoStreamer to .NET
  • VideoStreamerNET - Written in C#/.NET that controls the decoding and playback of video/audio frames


Where do I get it?

Get if from here and remember you will have to provide the paths to FFMPEG libs in order to compile it. There is also a sample project that shows how to use VideoStreamer and this following example is a snippet of the sample project.


How do I use VideoStreamer?

Here is a snippet from the sample program (see "Screenshots" below) on how to use VideoStreamer in your .NET application.


            RenderWindow window = new RenderWindow(new VideoMode(640, 480), "VideoStreamer Example");
            window.SetFramerateLimit(60);
            //Create our video stream
            stream = new VideoStreamerNET.VideoStream(@"VIDEO FILE PATH");
            //Create a clock to track elapsed time
            Clock frameclock = new Clock();
            //Create a specialized sprite renderer to draw our video frame
            VideoStreamerNET.VideoDrawer framesprite = new VideoStreamerNET.VideoDrawer(stream);
            //Start playing
            stream.Play();
            //Keep looping while our window is open
            while (window.IsOpen())
            {
                //Handle current events
                window.DispatchEvents();
                //Clear current window
                window.Clear();
                //Get our current frame time and restart the frame clock
                Time frametime = frameclock.Restart();
                //Update the current frame inside our video streamer
                stream.Update(frametime);
                //Draw the current video frame
                window.Draw(framesprite);
                //Now display everything on the screen
                window.Display();
            }


Screenshots

Here is the sample application playing back a test video.



Also here is the breakdown of what is called when and where and as you can see SFML uses most of the processing time for drawing. Meaning vertical sync is working hard to slow down the application.  :)
Toshiba Satellite - Intel i7 2.40ghz 8gb Intel HD Graphics 4000

« Last Edit: June 03, 2014, 04:21:04 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: VideoStreamer - .NET Video Player
« Reply #1 on: July 20, 2013, 08:41:18 am »
does this use SFML 1.6 ?  :-\

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: VideoStreamer - .NET Video Player
« Reply #2 on: July 20, 2013, 11:05:30 am »
Why not cross-platform P/Invoke but M$-only C++/CLI?  :-\

@up
I think it's 2.0, .NET just uses ugly UpperCamelCase convention.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: VideoStreamer - .NET Video Player
« Reply #3 on: July 20, 2013, 12:50:57 pm »
does this use SFML 1.6 ?  :-\

No, it uses SFML 2.0.

Why not cross-platform P/Invoke but M$-only C++/CLI?  :-\

@up
I think it's 2.0, .NET just uses ugly UpperCamelCase convention.

Unfortunately I could not find any good solid .NET FFMPEG wrappers that are not abandoned or outdated so I started working on it originally in C# with P/Invoke. But I was having trouble with all of the structures FFMPEG was using so I went with the C++/CLI design. In the future when I add audio support I will probably have to rewrite some parts of it so I may change over to a P/Invoke design now that I have a working design.

And yes .NET uses the UpperCammelCase convention :D Personally I like it better than lowerCamelCase  8)
« Last Edit: July 20, 2013, 12:54:15 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
VideoStreamer - Update Version 2.0
« Reply #4 on: July 21, 2013, 11:42:59 pm »
Update - Version 2.0

I completely rewrote VideoStreamer without the C++/CLI interface. VideoStreamer now is cleanly split into 3 separate projects.

  • VideoStreamer - C++ (static compile) wrapper for FFMPEG to provide the decoding functionality
  • CVideoStreamer - C (dynamic compile) interface for VideoStreamer that provides access to the .NET interface
  • VideoStreamerNET - .NET final control over the decoding and .NET interface

I am not on linux using mono so I can not provide cross platform checks for compatibility, but with this rewrite it should be cross platform once you set your compilers up with new projects.

If anyone has any questions/comments/suggestions please post them :D And I am planning on finishing audio support soon.
« Last Edit: August 14, 2013, 08:21:57 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
VideoStreamer - Update Version 3.0
« Reply #5 on: July 24, 2013, 10:22:42 pm »
Update - Version 3.0

This is a big update. I entirely redid the way I handle video packets/frames and changed it so I can get audio and video data in the same data frame. This way it keeps the C#/.NET side cleaner and easier to call into the C++ FFMPEG wrapper. As before full seeking support is included for both the video and audio.

Changes
  • Added playback support for audio

Todo
  • Figure out how to set up building with CMake (help anyone?)

Also I have updated and cleaned up the first/main post.
« Last Edit: August 01, 2013, 08:02:48 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
VideoStreamer - Update Version 3.1
« Reply #6 on: August 01, 2013, 08:07:00 pm »
Update - Version 3.1

This is a small update, the biggest part was I added CMakeLists.txt to the VideoStreamer and CVideoStreamer projects.

Changes

  • Split the header files into an include folder and the code files into an src folder
  • Added CMakeLists.txt for cross platform building
  • Added comments to the class varibles

Todo

  • Add more comments and documentaion
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

erthon

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: VideoStreamer - .NET Video Player
« Reply #7 on: August 14, 2013, 07:15:27 pm »
After searching for a SFML based ffmpeg wrapper I found your code and liked it despite it being C# and M$ dependend in the higher levels. What I'm going to do is to wrap it up in C++ and use it as a base for my own project which involves playing a number - up to 16 - video streams in parallel so performance will be an issue.

After browsing through the implementation of the VideoStream and DataFrame I found some places where there's some space for optimizations.

First I'd suggest not to do the copying "by hand" and byte by byte in the constructors of DataFrame. A simply memcpy does the job perfectly in less time. Initializing the array would be done using memset().

Second I am thinking about reusing the DataFrame in GetNextDataFrame() by passing it as a parameter - GetNextDataFrame(DataFrame*) - so there's no need to do the allocate - deallocate - 2step all the time. One could use some sort of a ring buffer and avoid trashing the heap to much over time. Maybe making the AVPacket used in GetNextDataFrame a class member and reusing it would go in the same direction.

Keep up the good work. I'm going to post my changes after testing and cleaning them up.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: VideoStreamer - .NET Video Player
« Reply #8 on: August 14, 2013, 08:19:52 pm »
After searching for a SFML based ffmpeg wrapper I found your code and liked it despite it being C# and M$ dependend in the higher levels.

I would love to know what is MS specific at the higher levels. The initial release contained MS specific code with C++/CLI code, but that has since been removed.

Quote
What I'm going to do is to wrap it up in C++ and use it as a base for my own project which involves playing a number - up to 16 - video streams in parallel so performance will be an issue.

Sounds very cool, I would love to see your project when it is done.

Quote
First I'd suggest not to do the copying "by hand" and byte by byte in the constructors of DataFrame. A simply memcpy does the job perfectly in less time. Initializing the array would be done using memset().

Yes this should be changed, if I ever get back around to working on this code I will do that.

Quote
Second I am thinking about reusing the DataFrame in GetNextDataFrame() by passing it as a parameter - GetNextDataFrame(DataFrame*) - so there's no need to do the allocate - deallocate - 2step all the time. One could use some sort of a ring buffer and avoid trashing the heap to much over time. Maybe making the AVPacket used in GetNextDataFrame a class member and reusing it would go in the same direction.

Well the reason a new DataFrame is created on every GetNextDataFrame is because in the C# code the frames are put in a queue and then read back when they are needed.

Also if you plan on just using this for C++ you should take a look at Ceylo's sfeMovie Project(linked in first post) as part of my video player is implemented just in C#.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

erthon

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: VideoStreamer - .NET Video Player
« Reply #9 on: August 14, 2013, 10:59:01 pm »
I would love to know what is MS specific at the higher levels. The initial release contained MS specific code with C++/CLI code, but that has since been removed.

It's C#/.NET. They invented it. Maybe just prejudice. And there's MONO. Doesn't matter.

Quote
Yes this should be changed, if I ever get back around to working on this code I will do that.

Done.

Quote
Well the reason a new DataFrame is created on every GetNextDataFrame is because in the C# code the frames are put in a queue and then read back when they are needed.

What I'm planning to do is to have a pool queue of preallocated DataFrames which are then used to decode, taken from this queue and pushed at the end of the processing queue from where they are consumed and pushed back at the end of the pool queue. So it's going to be just moving some pointers around, not creating a new DataFrame every time GetNextDataFrame gets called. Shouldn't be to hard to implement in C#, either. And using tbb::concurrent_bounded_queue the threading overhead is marginal.

Quote
Also if you plan on just using this for C++ you should take a look at Ceylo's sfeMovie Project(linked in first post) as part of my video player is implemented just in C#.

Exactly this was my first start - but it simply does much more than I need - gimme a frame, next frame please etc. - so I stumbled upon your two classes which provide exactly what I need. So Thank you for their existence.

rcurtis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: VideoStreamer - .NET Video Player
« Reply #10 on: February 07, 2014, 05:15:28 pm »
Is there any documentation for building this?  I have all the C++ code happily building DLLs but the test program blows up when it tries to load a video -
An unhandled exception of type 'System.DllNotFoundException' occurred in VideoStreamerNET.dll

Additional information: Unable to load DLL 'CVideoStreamer': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

 I have included the CVideoStreamer.dll in the project and set its properties to "Copy if newer" and can see it sitting right next to the exe.  Not sure what do to.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: VideoStreamer - .NET Video Player
« Reply #11 on: February 07, 2014, 06:03:10 pm »
There is no documentation/tutorials at the moment, but I hope soon to have my own site for this and my other projects up soon.  :)

I have included the CVideoStreamer.dll in the project and set its properties to "Copy if newer" and can see it sitting right next to the exe.  Not sure what do to.

Make sure you are also copying the FFMPEG dlls.

Quote
avcodec-55.dll
avformat-55.dll
avutil-52.dll
swresample-0.dll
swscale-2.dll
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

rcurtis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: VideoStreamer - .NET Video Player
« Reply #12 on: February 07, 2014, 09:56:16 pm »
Thanks for the quick reply, yes it was the ffmpeg dlls that were missing.  I used a dll dependency viewer to figure it out it was missing.  It seems to play videos much smoother than the one I was working with for Java.  I have not tested streaming with alpha channel yet, but the only other thing missing is documentation/tutorial type stuff.  Great project!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: VideoStreamer - .NET Video Player
« Reply #13 on: February 07, 2014, 10:03:06 pm »
Glad it is working out well for you :D

And be sure you let us all see your finished project when it is done  ;)
« Last Edit: February 07, 2014, 10:05:03 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

rcurtis

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: VideoStreamer - .NET Video Player
« Reply #14 on: February 11, 2014, 03:08:32 pm »
zsbzsb,

  I have been playing with your video player for a while and have found it gets very unhappy with video's that do not have an audio track.  I am playing FLV files, the only ones that do not crash all contain an audio track.  Reading through the code, it looks like it should be ok with not having audio, but in practice it seems to insist.  I can supply you with test videos if your interested in having a look.

Thanks,
Robert

 

anything