Capturing parameters passed from client to Red5pro server application is a very simple and useful step if you wish to capture data from the client on connection for authentication, validation etc:.
When working with Red5pro, you have different types of clients that can connect to Red5pro – Flash(RTMP), Android/IOS(RTSP), and HTML5(RTC) clients. Each client transmits parameters in a different way. Hence for proper handling, it is recommended to check the client connection type and then devise your parameter collection logic.
In this article, we shall see how to build a simple logic that checks for client type and extract connection parameters accordingly. The recommended parameters capturing point is the appConnect method of your application adapter class. This is the method which gets invoked when a compatible client tried to connect to Red5pro.
PASSING PARAMETERS FOR RTMP CLIENTS (PASSED IN VIA QUERY STRING IN RTMP URL)
An RTMP client can send parameters through the connect method (as an array) or via the query string (as name value params).
nc.connect("rtmp://localhost/live?param1=value1¶m2=value2");
CAPTURING PARAMETERS FOR RTMP CLIENTS (PASSED IN VIA QUERY STRING IN RTMP URL): ON CONNECT
public boolean appConnect(IConnection conn, Object[] params)
{
IConnection connection = Red5.getConnectionLocal();
Map<string, object=""> map = getParametersMap(connection.getConnectParams());
return super.appConnect(conn, params);
}</string,>
private Map<string, object=""> getParametersMap(Map<string, object=""> content)
{
Map<string, object=""> map = new HashMap<string, object="">();
Iterator<entry<string, object="">> it = content.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<string, object=""> pair = it.next();
String key = pair.getKey();
Object value = pair.getValue();</string,></entry<string,></string,></string,></string,></string,>
if(key.equals("queryString"))
{
Map<string, object=""> qmap = new HashMap<string, object="">();
String[] parameters = String.valueOf(value).split("&");
for(int i = 0; i<parameters.length;i++) {="" string[]="" param="String.valueOf(parameters[i]).split("=");" string="" name="param[0];" if(name.indexof("?")="=" 0)="" "");="" val="param[1];" qmap.put(name,="" val);="" }="" map.putall(qmap);="" map.put(key,="" value);="" return="" map;="" [="" cc]="" <hr="">
</parameters.length;i++)></string,></string,>
<strong>PASSING PARAMETERS FOR RTSP CLIENTS (THROUGH RED5PRO SDK)</strong>
An Android/IOS, the client can send parameters through the official SDK to the Red5pro server application. An example is provided in the <a href="https://github.com/red5pro/streaming-android/blob/master/app/src/main/java/red5pro/org/testandroidproject/tests/PublishAuthTest/PublishAuthTest.java#L30" target="_blank" rel="noopener">public github repository containing test-examples</a> -
<strong>CAPTURING PARAMETERS FOR RTSP CLIENTS (PASSED VIA SDK): ON CONNECT</strong>
[cc lang="java" tab_size="2" lines="40"]
@Override
public boolean appConnect(IConnection conn, Object[] params)
{
IConnection connection = Red5.getConnectionLocal();
Map<string, object=""> map = new HashMap<string, object="">();
for(int i = 0; i<params.length;i++) {="" string[]="" param="String.valueOf(params[i]).split("=");" map.put(param[0],="" param[1]);="" }="" return="" super.appconnect(conn,="" params);="" [="" cc]="" <hr="">
</params.length;i++)></string,></string,>
<strong>PASSING PARAMETERS FOR WEBRTC CLIENTS (THROUGH RED5PRO HTML5 SDK)</strong>
Sample configuration object used to initialize the WebRTC publisher client. The snippet is taken from the Red5pro server distribution 'live' app.
[cc lang="javascript" tab_size="2" lines="40"]
var baseConfiguration = {
host: window.targetHost,
app: 'live',
iceServers: iceServers,
bandwidth: desiredBandwidth,
connectionParams: {param1:"<value-1>", param-n:"value-n"}
};</value-1>
CAPTURING PARAMETERS FOR WebRTC CLIENTS (PASSED VIA RED5PRO HTML5 SDK): ON CONNECT
public boolean appConnect(IConnection conn, Object[] params)
{
IConnection connection = Red5.getConnectionLocal();
Map<string, object=""> map = getParametersMap(connection.getConnectParams());</string,>
return super.appConnect(conn, params);
}
private Map<string, object=""> getParametersMap(Map<string, object=""> content)
{
Map<string, object=""> map = new HashMap<string, object="">();</string,></string,></string,></string,>
Iterator<entry<string, object="">> it = content.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<string, object=""> pair = it.next();
String key = pair.getKey();
Object value = pair.getValue();</string,></entry<string,>
if(key.indexOf("?") == 0) key = key.replace("?", "");
map.put(key, value);
}
return map;
}
Now that we know how to capture parameters for each connection type using the appConnect handler it is time to aggregate the code snippets in one place so that we can check for client type and extract parameters, depending on client type in one place. As we have been discussing till now Red5pro currently supports three types of connections that are registered as streaming connection types – RTMP, RTSP and RTC.
So inside our appConnect handler, we shall now write an aggregated approach which checks and collects parameters from a Red5pro connection on connect event and stores it inside a java Map Object.
AGGREGATED LOGIC TO EXTRACT PARAMETERS FROM A RED5PRO CONNECTION
[cc lang=”java” tab_size=”2″ lines=”100″]
static String RTSPCONNECTION = “com.red5pro.server.stream.rtsp.RTSPMinaConnection”;
static String RTMPCONNECTION = “org.red5.server.net.rtmp.RTMPMinaConnection”;
static String RTCCONNECTION = “com.red5pro.webrtc.RTCConnection”;
public boolean appConnect(IConnection conn, Object[] params)
{
IConnection connection = Red5.getConnectionLocal();
String connectionClassName = connection.getClass().getCanonicalName();
Map
if(connectionClassName.equalsIgnoreCase(RTMPCONNECTION))
{
params = getRTMPParameters(connection.getConnectParams());
}
else if(connectionClassName.equalsIgnoreCase(RTSPCONNECTION))
{
params = getRTSPParameters(params);
}
else if(connectionClassName.equalsIgnoreCase(RTCCONNECTION))
{
params = getRTCParameters(connection.getConnectParams());
}
return super.appConnect(conn, params);
}
private Map
{
Map
Iterator
while (it.hasNext())
{
Map.Entry
String key = pair.getKey();
Object value = pair.getValue();
if(key.indexOf(“?”) == 0) key = key.replace(“?”, “”);
map.put(key, value);
}
return map;
}
private Map
{
Map
for(int i = 0; i
{
Map
Iterator
while (it.hasNext())
{
Map.Entry
String key = pair.getKey();
Object value = pair.getValue();
if(key.equals(“queryString”))
{
Map
String[] parameters = String.valueOf(value).split(“&”);
for(int i = 0; i