February 16, 2017

How to Integrate Video Streaming Into Your C or C++ Application Using Nex Gen Media

The Nex Gen Media Server is a small-footprint shared library that allows users to easily build video media and telephony applications. It supports several popular streaming protocols such as RTMP, RTSP and Apple's HTTP Live, and can capture live video streams and adapt them so they can be received by another type of device. For instance, using the NGMS you could capture a HD video feed and convert it so that it may be received by an iPhone over a 3G connection. This makes it a particularly useful tool for developers, so let's take a closer look and see just how you can integrate the NGMS API to control streaming features directly in a C application:


1. Download and read the NGMS user guide

As always, the first step lies of any process lies in understanding its backbone. To that end, you'll need to download and read the NGMS user guide from http://ngmsvid.com/ngms.php and its respective API reference guide from http://ngmsvid.com/develop.php before you begin coding. These cover the basics of the library and its main utilities. Then, proceed to download the NGMS package for Linux. Once you've done that, unzip its contents into the directory of your choice.

2. Set up the application

In order for NGMS to be directly integrated into an application, you'll need to include ngms/include/ngmslib.h into your code. You'll also have to include selected libraries such as ngms/lib/libngms.so and ngms/lib/libxcode.so. Be aware that libngms.so depends on libngms.so, so be sure to specify that in the linker options.

3. Create a simple makefile

Here is an example of what things should look like:
#Example Makefile CC=gcc CFLAGS=-ggdb INCLUDES+= -I ngms/include LDFLAGS+= -L ngms/lib -lngms -xlcode -crypto all: myapp %.o: %.c $(CC) $(CFLAGS) $(INCLUDES) -o $@ -c $< myapp: myapp.o $(CC) -fpic -o myapp myapp.o $(LDFLAGS)

And here is the source to myapp.c. 

/** * * Example myapp application * */ typedef unsigned int uint32_t; typedef unsigned long long uint64_t; #include <stdio.h> #include "ngmslib.h" int main(int argc, char *argv[]) { NGMSLIB_STREAM_PARAMS_T ngmsConfig; NGMS_RC_T returnCode; returnCode = ngmslib_open(&ngmsConfig); if(NGMS_RC_OK != returnCode) { fprintf(stderr, "ngmslib_open failed\n"); return -1; } ngmsConfig.inputs[0] = "mediaTestFile.mp4"; ngmsConfig.output = "rtp://"; returnCode = ngmslib_stream(&ngmsConfig); if(NGMS_RC_OK != returnCode) { fprintf(stderr, "ngmslib_open failed\n"); } ngmslib_close(&ngmsConfig); return 0; }

It's worth mentioning that the code uses the NGMSLIB_STREAM_PARAMS_T struct type in order to control the NGMS library. To that end, you'll need to call to ngmslib_open so you can "preset" the struct. After that you can fill out whatever options you'd like in the struct, and then proceed to ngmslib_stream in order to create the output video.

4. Open the stream in VLC player and test it out

This one's easy. Just do:

VLC Player -> Open Network rtp://@:5004

Now you can stream a media file directly from your application. Since the ngmslib_stream function is what's called as a blocking operation, you can actually interrupt the stream by doing ngmslib_close from another thread and the ngmslib_stream call will exit.

5. Add in the final touches

You can also add support for an embedded Flash player by adding the following lines of code:

    ngmsConfig.rtmplive = "1935";     

ngmsConfig.live = "8080";

Or, instead of playing a file, you might want to change the input so that it's a live video stream. You can create two separate instances of the application, one of which will output the video to port 5006, while the other will capture video on port 5006 and output it to port 5004. It looks something like this:

//ngmsConfig.inputs[0] = "mediaTestFile.mp4";     

ngmsConfig.inputs[0] = "rtp://";     

ngmsConfig.strfilters[0] = "type=m2t";

In conclusion, it is fairly easy to add video streaming support to your own application. The aforementioned code was done using C, but C++ developers can adapt it by wrapping all the calls to ngmslib using the "extern "C"" keyword. Java developers can also do it, but it will require building a JNI interface and wrapping each of the calls down to NGMS. Still, the NGMS library is quite useful, with potential applications that include building your own video streaming client as well.

Click Here!