Make a Cellular Video clip Chat Framework With Nex Gen Media Server (NGMS) API

Introduction

Nex Gen Media Server is finest called a multi-reason media streaming server to provide Are living and saved online video to many different gadgets. The exact same media server is usually embedded right into a cellular application to facilitate genuine time video clip interaction. Here we will use the NGMS API to aid creating a movie chat customer for Android gadgets utilizing the Android Software program Developer Kit (SDK) and Google Android Indigenous Developer Kit (NDK).

We are going to use NGMS to deliver all the community transportation RTP providers, video clip body compression and decompression. NGMS will run like a natively compiled shared item utilized by our Android app. We'll make use of a Java Indigenous Interface (JNI) bridging layer to invoke indigenous methods from Java application code.

Pre-requisistes

This short article assumes that you've a primary comprehension and at the least an intermediate volume of encounter building Android applications. I will not go more than the main points of starting and Android project in an iDE including Eclipse. When you've got Formerly designed Android applications, or a minimum of went by means of some illustration tutorials you should be capable of benefit from this tutorial to make a actual-time video clip streaming client.

For those who have not previously done so, you must obtain the Google Android SDK (Computer software Developer Kit). In this instance I employed Revision fifteen on the Linux 32bit equipment. The java application layer is made use of to build the particular Android app. The app code will interface with a JNI layer to invoke the NGMS API routines.

Additionally, you will really need to down load the Google Android NDK (Indigenous Developer Package). In this instance I made use of Revision 6b over a Linux 32bit equipment. The NDK might be useful for producing the native interface layer which serves as the glue amongst the indigenous code and Java software code. You should also Use a standard fully grasp from the C programming language.

It is best to 1st get started by creating a skeleton Android software employing an IDE for example Eclipse. In this instance, the appliance might be identified as ngmsclient. The Java package title will probably be termed com.illustration.ngmsclient and will concentrate on Android OS two.3 or bigger. The basis venture Listing need to have a folder referred to as "jni" which will have the indigenous resources used to interface with the Java application code. The example underneath assumes that you've a skeleton Android application Doing the job and may explain to you tips on how to combine an ExampleChat course into your Android project.

The Native Layer

The NGMS core embedded library is created in C which is packaged as a shared object file. Considering that the Android OS is predicated on Linux, the NGMS Main library will operate natively in just your Android software Place. You must obtain the NGMS core library for Android OS in the ngmsvid.com Web site. The critical elements are definitely the library documents libngms.so, libxcode.so, plus the header file ngmslib.h. The ngms bundled.so information will likely be packaged as well as your ngmsclient.apk to supply online video chat services.

The JNI Layer
We're going to establish a shared library termed ngmsglue.so that may serve as the glue involving the ngms API and our Java software code. The structure with the Java Indigenous Interface (JNI) Listing inside your ngmsclient challenge directory should really look like this.


jni/Android.mk

jni/ngmsglue.c

jni/ngms/incorporate/ngmslib.h

jni/ngms/lib/libngms.so

jni/ngms/lib/libxcode.so

ngmsglue.c will contain the code to control NGMS output and enter attributes. The code will keep two seperate NGMS API contexts, one particular for stream output parameters and one for stream input parameters. You'll want to notice which the naming Conference of each operate really should match the Java bundle and class identify from where you are invoking the indigenous code.

ngmsStartReceiver known as to initialize the NGMS capture input API to hear on port 5004 for your network movie stream encapsulated about MPEG-2 TS. Each individual successive movie body might be immediately demuxed and decoded into RGB565 pixel format. The complete frame can then be browse by contacting ngmsReceiveFrame.

ngmsStartSender is known as to initialize the NGMS stream output API to output encoded video clip frames to your distant host more than port 5004. ngmsTransmitFrame is called to encode and transmit only one raw frame in NV21 pixel format.


/*

* ngmsglue.c

*

* JNI layer to access the NGMS streaming and capture API

*

*/
#consist of

#contain

#contain

#contain

#include things like "ngmslib.h"

/**

* Retains the configuration for the NGMS RTP receiver and decoder

*/

static NGMSLIB_STREAM_PARAMS_T ngmsReceiver;

/**

* Retains the configuration for your NGMS RTP sender and encoder

*/

static NGMSLIB_STREAM_PARAMS_T ngmsSender;

/**

* Begins the NGMS seize service

*/

jint Java_com_example_ngmsclient_ExampleChat_ngmsStartReceiver (JNIEnv* _env, jobject _thiz)



char xcoderConfig[512];

char sdpConfig[512];

int i;

/*

* Prior to accomplishing nearly anything call ngmslib_open to open the NGMS service

*/

i = ngmslib_open(&ngmsReceiver);

if (i!= NGMS_RC_OK)



__android_log_print(ANDROID_LOG_ERROR, "ngms", "ngmslib_open unsuccessful with error %d", i);

return -one;



/*

* Build an xcoder configuration key price string

*/

sprintf(xcoderConfig, "vc=rgb565,vx=320,vy=240");

/*

* Construct an SDP configuration for that RTP receiver

* Right here we setup NGMS to read data in excess of a MPEG-2 Transportation Stream.

*/

sprintf(sdpConfig, "sdp://"

"m=video 5004 RTP/AVP 33n"

"a=rtpmap:33 MP2T/90000n"

"a=fmtp:33n");

/*

* Customize the seize configuration parameters

*/

ngmsReceiver.inputs[0] = sdpConfig;

ngmsReceiver.strxcode = xcoderConfig;

ngmsReceiver.islive = 1;

ngmsReceiver.noaud = 1;

/*

* Begin the RTP receiver

*/

i = ngmslib_stream(&ngmsReceiver);

if (i!= NGMS_RC_OK)



__android_log_print(ANDROID_LOG_ERROR, "ngms", "ngmslib_stream unsuccessful with error %d", i);

ngmslib_close(&ngmsReceiver);

return -one;



return 0;



/**

* Stops the NGMS RTP seize assistance

*/

jint Java_com_example_ngmsclient_ExampleChat_ngmsStopReceiver (JNIEnv* _env, jobject _thiz)



/*

* Quit the RTP receiver

*/

ngmslib_close(&ngmsReceiver);

return 0;



/**

* NGMS Callback operation which blocks right until a whole movie frame is been given and decoded

*/

jint Java_com_example_ngmsclient_ExampleChat_ngmsReceiveFrame (JNIEnv* _env, jobject _thiz, jbyteArray frameBytes)



jstring str;

jboolean duplicate;

jbyte* buffer = (*_env)->GetByteArrayElements(_env, frameBytes, ©);

jsize max = (*_env)->GetArrayLength(_env, frameBytes);

int frameSize;

frameSize = ngmslib_readVidFrame(&ngmsReceiver, buffer, max, NULL);

(*_env)->ReleaseByteArrayElements(_env, frameBytes, buffer, JNI_ABORT);

return frameSize;



/**

* Starts off the NGMS streaming provider

*/

jint Java_com_example_ngmsclient_ExampleChat_ngmsStartSender (JNIEnv* _env, jobject _thiz, jstring remoteAddress)



char xcoderConfig[512];

char filter[512];

char place[512];

const best apps to earn money in Pakistan char *p;

int i;

/*

* Right before accomplishing just about anything phone ngmslib_open to open the NGMS support

*/

i = ngmslib_open(&ngmsSender);

if (i!= NGMS_RC_OK)



__android_log_print(ANDROID_LOG_ERROR, "ngms", "ngmslib_open failed with mistake %d", i);

return -1;



/*

* Receive the destination host string

*/

p = (*_env)->GetStringUTFChars(_env, remoteAddress, 0);

sprintf(destination, "rtp://%s:5004", p);

(*_env)->ReleaseStringUTFChars(_env, remoteAddress, p);

/*

* Personalize the stream configuration parameters

*/

ngmsSender.output = vacation spot;

sprintf(xcoderConfig, "vc=h264,vp=sixty six,vb=250,vx=320,vy=240,vgmax=2000,vgmin=1500,vfr=fifteen,vcfrout=-one,vth=one,vl=1,vsc=one,vf=2");

ngmsSender.strxcode = xcoderConfig;

sprintf(filter, "type=yuv420sp, ngmsSender.strfilters[0] = filter;

ngmsSender.inputs[0]= "/dev/dummyvideo";

ngmsSender.noaud = one;

/*

* Begin the RTP sender

*/

i = ngmslib_stream(&ngmsSender);

if (i!= NGMS_RC_OK)



__android_log_print(ANDROID_LOG_ERROR, "ngms", "ngmslib_stream failed with mistake %d", i);

ngmslib_close(&ngmsSender);

return -1;



return 0;



/**

* Stops the NGMS streaming assistance

*/

jint Java_com_example_ngmsclient_ExampleChat_ngmsStopSender (JNIEnv* _env, jobject _thiz)



/*

* Cease the RTP sender

*/

ngmslib_close(&ngmsSender);

return 0;



/**

* Encodes and transmits only one online video body applying community RTP transport

*/

jint Java_com_example_ngmsclient_ExampleChat_ngmsTransmitFrame (JNIEnv* _env, jobject _thiz, jbyteArray frameBytes)



jboolean duplicate;

jbyte* buffer = (*_env)->GetByteArrayElements(_env, frameBytes, ©);

jsize measurement = (*_env)->GetArrayLength(_env, frameBytes);

int i;

i = ngmslib_onVidFrame(&ngmsSender, buffer, sizing);

(*_env)->ReleaseByteArrayElements(_env, frameBytes, buffer, JNI_ABORT);

return i;




We will build the makefile Android.mk that is utilized by the Android NDK to develop our ngmsglue library.


#Example Android.mk Makefile
LOCAL_PATH:= $(phone my-dir)

consist of $(CLEAR_VARS)

LOCAL_MODULE:= ngmsglue

LOCAL_SRC_FILES:= ngmsglue.c

LOCAL_C_INCLUDES:= ngms/include things like

LOCAL_LDFLAGS:= -Lngms/lib

LOCAL_LDLIBS:= -llog -lm -lngms -lxcode

consist of $(BUILD_SHARED_LIBRARY)


To develop the ngmsglue.so shared library simply do the following.


cd [YOUR NGMSCLIENT Job DIR]/jni

$ANDROID_NDK_HOME/ndk-build

This should produce the output file [YOUR NGMSCLIENT Task DIR]/libs/armeabi/ibngmsglue.so

You might have to manually copy the libraries ngms/lib/libngms.so and ngms/lib/libxcode.so in to the libs/armeabi output directory since the contents of this directory are going to be immediately packaged to the job.apk software.

The Java Software Layer

The java software layer includes the application degree logic with the Android application we are developing. To be able to use the native code through the previous section we will require to create a very simple software using the Android SDK. This instance demonstrates utilizing the ExampleChat class that may be employed by your individual software to offer true-time interactive movie streaming. It does not display all of the actions necessary to make a complete Android video streaming software. An intermediate degree Android developer needs to be capable to complete the undertaking by constructing upon and such as the ExampleChat class within their total-blown software.

The ExampleChat class revealed under is employed to control the NGMS stream sender plus the NGMS stream receiver. The tactic startSender might be named to begin streaming online video with the native digicam utilizing the digicam preview system. Each and every movie body output through the digicam is handed down to the NGMS API where by Will probably be encoded and transmitted for the distant celebration working with native RTP encapsulation. The strategy starReceiver is usually identified as to start receiving video from a distant instance of a similar app. startReceiver launches a thread which performs a blocking poll for the following out there obtained and decoded movie body. Alongside one another, these two approaches can be employed to determine a bi-directional video stream among two remote occasions of exactly the same app across a network.


/*

* ExampleChat.java

*

*/
bundle com.illustration.ngmsclient;

import android.components.Digital camera;

import android.components.Digital camera.Parameters;

import android.components.Digital camera.PreviewCallback;

import android.graphics.ImageFormat;

community class ExampleChat



personal boolean myReceiverRunning;

personal Digital camera myCamera;

personal PreviewCallback myCameraCallback;

/**

* Adjust this price on the desired destination where by the online video will be streamed

*/

non-public static remaining Desired destination = "ten.0.0.1";

/**

* strategy to initialize and start sending video from the local digicam

*/

community void startSender () throws Exception



myCamera = Digital camera.open();

Parameters parameters = myCamera.getParameters();

parameters.setPreviewFormat(ImageFormat.NV21);

parameters.setPreviewFrameRate(fifteen);

parameters.setPreviewSize(320, 240);

myCamera.setParameters(parameters);

/**

* Connect with the NGMS indigenous system to complete streaming initialization

*/

ngmsStartSender(Spot);

myCameraCallback = new PreviewCallback()



public void onPreviewFrame(byte[] frameBytes, Digicam digital camera)



/**

* Phone the NGMS native plan to encode and transmit the movie frame

* into the remote destination applying RTP transport.

*/

ngmsTransmitFrame(frameBytes);





myCamera.startPreview();

myCamera.setPreviewCallback(myCameraCallback);



/**

* technique to halt sending video clip

*/

general public void stopSender () throws Exception



ngmsStopSender();



/**

* method to start out receiving distant video

*/

community void startReceiver () throws Exception



/**

* Contact the NGMS indigenous method to conduct input initialization

*/

ngmsStartReceiver();

/**

* Start out the thread which can poll for been given video clip frames

*/

new Thread(myVideoFrameReceiver).get started();



/**

* approach to halt receiving remote online video

*/

public void stopReceiver () throws Exception



myReceiverRunning = Wrong;

ngmsStopReceiver();



/**

* Thread utilized to check for decoded movie frames

* been given through the NGMS indigenous layer

*/

non-public Runnable myVideoFrameReceiver = new Runnable()



public void run()



/**

* Allocate a buffer to carry a online video body 320 x 240 pixels

* in RGB565 format, using 16 bits (two bytes) for every pixel.

*/

byte[] frameBytes = new byte[ 320 * 240 * two ];

int size;

myReceiverRunning = correct;

when(myReceiverRunning)



/**

* Block till a frame has actually been received and decoded from the native layer

*/

measurement = ngmsReceiveFrame(frameBytes);

if(dimension > 0)



/**

* The frameBytes array signifies a online video body in RGB565 structure.

* These bytes is usually converted into a Bitmap item for Display screen

* on the monitor.

*/









/**

* Static initialization to load indigenous libraries into address space

*/

static



Method.loadLibrary("xcode");

Program.loadLibrary("ngms");

Technique.loadLibrary("ngmsglue");



/**

* Outline native approach prototypes

*/

community native int ngmsStartReceiver ();

public indigenous int ngmsStopReceiver ();

general public indigenous int ngmsReceiveFrame (byte[] frameBytes);

community native int ngmsStartSender (String remoteAddress);

general public indigenous int ngmsStopSender ();

community native int ngmsTransmitFrame (byte[] frameBytes);




When you have integrated the ExampleChat course into your own Android project ensure that all a few with the native libraries exist inside the venture /libs Listing making sure that they will be incorporated into your.apk output.


libs/armeabi/libngms.so

libs/armeabi/libxcode.so

libs/armeabi/libngmsglue.so

This example provides movie output for the location IP handle 10.0.0.one. To properly permit two customers to stream online video bi-directionally you need to alter this IP handle to match your remote shopper.

Conclusion

The above mentioned example demonstrates ways to stream Dwell online video through the camera object in real-time and how to get a remote Stay stream and extract Every decoded movie body for Show. The same approach flow might be placed on include audio streaming in-buy to produce a full movie chat software.

Leave a Reply

Your email address will not be published. Required fields are marked *