Search in sources :

Example 1 with StreamingProxy

use of org.red5.proxy.StreamingProxy in project red5-client by Red5.

the class StreamRelay method main.

/**
 * Creates a stream client to consume a stream from an end point and a proxy to relay the stream to another end point.
 *
 * @param args
 *            application arguments
 */
public static void main(String... args) {
    // handle the args
    if (args == null || args.length < 7) {
        System.out.println("Not enough args supplied. Usage: <source uri> <source app> <source stream name> <destination uri> <destination app> <destination stream name> <publish mode>");
    } else {
        // parse the args
        String sourceHost = args[0], destHost = args[3];
        String sourceApp = args[1], destApp = args[4];
        int sourcePort = 1935, destPort = 1935;
        sourceStreamName = args[2];
        String destStreamName = args[5];
        // live, record, or append
        String publishMode = args[6];
        // look to see if port was included in host string
        int colonIdx = sourceHost.indexOf(':');
        if (colonIdx > 0) {
            sourcePort = Integer.valueOf(sourceHost.substring(colonIdx + 1));
            sourceHost = sourceHost.substring(0, colonIdx);
            System.out.printf("Source host: %s port: %d\n", sourceHost, sourcePort);
        }
        colonIdx = destHost.indexOf(':');
        if (colonIdx > 0) {
            destPort = Integer.valueOf(destHost.substring(colonIdx + 1));
            destHost = destHost.substring(0, colonIdx);
            System.out.printf("Destination host: %s port: %d\n", destHost, destPort);
        }
        // create a timer
        timer = new Timer();
        // create our publisher
        proxy = new StreamingProxy();
        proxy.setHost(destHost);
        proxy.setPort(destPort);
        proxy.setApp(destApp);
        proxy.init();
        proxy.setConnectionClosedHandler(new Runnable() {

            @Override
            public void run() {
                System.out.println("Publish connection has been closed, source will be disconnected");
                client.disconnect();
            }
        });
        proxy.setExceptionHandler(new ClientExceptionHandler() {

            @Override
            public void handleException(Throwable throwable) {
                throwable.printStackTrace();
                System.exit(2);
            }
        });
        proxy.start(destStreamName, publishMode, new Object[] {});
        // wait for the publish state
        do {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (!proxy.isPublished());
        System.out.println("Publishing...");
        // create the consumer
        client = new RTMPClient();
        client.setStreamEventDispatcher(new StreamEventDispatcher());
        client.setStreamEventHandler(new INetStreamEventHandler() {

            @Override
            public void onStreamEvent(Notify notify) {
                System.out.printf("onStreamEvent: %s\n", notify);
                ObjectMap<?, ?> map = (ObjectMap<?, ?>) notify.getCall().getArguments()[0];
                String code = (String) map.get("code");
                System.out.printf("<:%s\n", code);
                if (StatusCodes.NS_PLAY_STREAMNOTFOUND.equals(code)) {
                    System.out.println("Requested stream was not found");
                    client.disconnect();
                } else if (StatusCodes.NS_PLAY_UNPUBLISHNOTIFY.equals(code) || StatusCodes.NS_PLAY_COMPLETE.equals(code)) {
                    System.out.println("Source has stopped publishing or play is complete");
                    client.disconnect();
                }
            }
        });
        client.setConnectionClosedHandler(new Runnable() {

            @Override
            public void run() {
                System.out.println("Source connection has been closed, proxy will be stopped");
                proxy.stop();
            }
        });
        client.setExceptionHandler(new ClientExceptionHandler() {

            @Override
            public void handleException(Throwable throwable) {
                throwable.printStackTrace();
                System.exit(1);
            }
        });
        // connect the consumer
        Map<String, Object> defParams = client.makeDefaultConnectionParams(sourceHost, sourcePort, sourceApp);
        // add pageurl and swfurl
        defParams.put("pageUrl", "");
        defParams.put("swfUrl", "app:/Red5-StreamRelay.swf");
        // indicate for the handshake to generate swf verification data
        client.setSwfVerification(true);
        // connect the client
        client.connect(sourceHost, sourcePort, defParams, new IPendingServiceCallback() {

            @Override
            public void resultReceived(IPendingServiceCall call) {
                System.out.println("connectCallback");
                ObjectMap<?, ?> map = (ObjectMap<?, ?>) call.getResult();
                String code = (String) map.get("code");
                if ("NetConnection.Connect.Rejected".equals(code)) {
                    System.out.printf("Rejected: %s\n", map.get("description"));
                    client.disconnect();
                    proxy.stop();
                } else if ("NetConnection.Connect.Success".equals(code)) {
                    // 1. Wait for onBWDone
                    timer.schedule(new BandwidthStatusTask(), 2000L);
                } else {
                    System.out.printf("Unhandled response code: %s\n", code);
                }
            }
        });
        // keep sleeping main thread while the proxy runs
        do {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (!proxy.isRunning());
        // kill the timer
        // timer.cancel();
        System.out.println("Stream relay exit");
    }
}
Also used : IPendingServiceCallback(org.red5.server.api.service.IPendingServiceCallback) Notify(org.red5.server.net.rtmp.event.Notify) INetStreamEventHandler(org.red5.client.net.rtmp.INetStreamEventHandler) IPendingServiceCall(org.red5.server.api.service.IPendingServiceCall) RTMPClient(org.red5.client.net.rtmp.RTMPClient) StreamingProxy(org.red5.proxy.StreamingProxy) Timer(java.util.Timer) ClientExceptionHandler(org.red5.client.net.rtmp.ClientExceptionHandler) ObjectMap(org.red5.io.utils.ObjectMap)

Aggregations

Timer (java.util.Timer)1 ClientExceptionHandler (org.red5.client.net.rtmp.ClientExceptionHandler)1 INetStreamEventHandler (org.red5.client.net.rtmp.INetStreamEventHandler)1 RTMPClient (org.red5.client.net.rtmp.RTMPClient)1 ObjectMap (org.red5.io.utils.ObjectMap)1 StreamingProxy (org.red5.proxy.StreamingProxy)1 IPendingServiceCall (org.red5.server.api.service.IPendingServiceCall)1 IPendingServiceCallback (org.red5.server.api.service.IPendingServiceCallback)1 Notify (org.red5.server.net.rtmp.event.Notify)1