Introduction
Recently I took a closer look at Nex Gen Media Server and their API framework. NGMS is a multi-purpose streaming server which supports some of the popular streaming protocols such as RTSP, RTMP, Apple's HTTP Live, and MPEG-2 Transport Stream. NGMS comes with transcoding support and is able to capture and reformat live video streams and adapt them to be received by another type of device, such as capturing an HD video feed and converting it to be received by an iPhone over 3g. My focus was to integrate the NGMS API to control the streaming features directly from my own C application. In this example I am using Ubuntu Linux 10.04.
The first thing is to download and read the NGMS User Guide from here http://ngmsvid.com/ngms.php and the API reference guide from here http://ngmsvid.com/develop.php . There are many configuration options in there but we will just stick to the basics. Then you need to download the NGMS package for linux. The version I used was NGMS v1.3.4. Once you download the package just unzip the contents into a directory of your choice. I used ~/ngmsStreaming
Setting up the Application
To integrate NGMS directly into my C application I had to include "ngms/include/ngmslib.h" into my code.
When building my application I had to include the libraries ngms/lib/libngms.so and ngms/lib/libxcode.so . It seems that libngms.so also depends on libcrypto.so, which needs to be specified in the linker options.
Here is the simple Makefile that I'm using:
#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)
Here is the source to my example application myapp.c.
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://127.0.0.1:5004";
returnCode = ngmslib_stream(&ngmsConfig);
if(NGMS_RC_OK != returnCode) {
fprintf(stderr, "ngmslib_open failed\n");
}
ngmslib_close(&ngmsConfig);
return 0;
}
The above code uses the NGMSLIB_STREAM_PARAMS_T struct type to control the ngms library. The first thing that needs to be done is a call to ngmslib_open to "preset" the struct. After that you can fill out any of the options in the struct to control what NGMS will do. Then you can "ngmslib_stream" to create the output video.
I'm able to open the stream in VLC player and play the video.
VLC Player -> Open Network rtp:
Viola! It was that easy!!! I can now stream a media file directly from my application!
Since the ngmslib_stream function call is a blocking operation, to interrupt the stream I can call ngmslib_close from another thread and the ngmslib_stream call will exit.
I was able to add support for an embedded Flash player by adding the following two lines of code.
ngmsConfig.rtmplive = "1935";
ngmsConfig.live = "8080";
With my browser I connect to http://127.0.0.1:8080/live and I get back a page with flash player playing the video.
Instead of playing a file I can change the input to be a live video stream. I can create two separate instances of my application. One instance will output the video to port 5006. The other instance will capture the video on port 5006, and output it to port 5004. This is all you have to do.
ngmsConfig.inputs[0] = "rtp://127.0.0.1:5006";
ngmsConfig.strfilters[0] = "type=m2t";
Conclusion
These few examples show its pretty easy to add video streaming support into your own application. I've used C here. If your application is C++ you can wrap all the calls to ngmslib using the "extern "C" " keyword. You could also do it in Java but it would required building a JNI interface to wrap each of the calls down to NGMS.
This was a pretty simple example. In the near future I will go over how the NGMS library can be used to build your own video streaming client.