Search in sources :

Example 1 with RTMPClient

use of org.red5.client.net.rtmp.RTMPClient 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)

Example 2 with RTMPClient

use of org.red5.client.net.rtmp.RTMPClient in project red5-client by Red5.

the class StreamingProxy method init.

public void init(ClientType clientType) {
    switch(clientType) {
        case RTMPE:
            rtmpClient = new RTMPEClient();
            break;
        case RTMPS:
            rtmpClient = new RTMPSClient();
            break;
        case RTMP:
        default:
            rtmpClient = new RTMPClient();
            break;
    }
    log.debug("Initialized: {}", rtmpClient);
    setState(StreamState.STOPPED);
    // create a timer
    timer = new Timer();
}
Also used : RTMPSClient(org.red5.client.net.rtmps.RTMPSClient) Timer(java.util.Timer) RTMPEClient(org.red5.client.net.rtmpe.RTMPEClient) RTMPClient(org.red5.client.net.rtmp.RTMPClient)

Example 3 with RTMPClient

use of org.red5.client.net.rtmp.RTMPClient in project red5-client by Red5.

the class YouTubeConnectTest method testYouTubePublish.

@Test
public void testYouTubePublish() throws InterruptedException {
    log.info("\ntestYouTubePublish");
    String youtubeHost = "a.rtmp.youtube.com";
    int youtubePort = 1935;
    String youtubeApp = "live2";
    // System.getProperty("youtube.streamname");
    final String youtubePublishName = "dybx-y3ph-uqzx-30vx";
    log.info("youtubePublishName: {}", youtubePublishName);
    // if (youtubePublishName == null) {
    // log.info("You forgot to set a 'youtube.streamname' system property");
    // return;
    // }
    final RTMPClient client = new RTMPClient();
    client.setConnectionClosedHandler(new Runnable() {

        @Override
        public void run() {
            log.info("Test - exit");
        }
    });
    client.setExceptionHandler(new ClientExceptionHandler() {

        @Override
        public void handleException(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    client.setStreamEventDispatcher(new IEventDispatcher() {

        @Override
        public void dispatchEvent(IEvent event) {
            log.info("ClientStream.dispachEvent: {}", event);
        }
    });
    final INetStreamEventHandler netStreamEventHandler = new INetStreamEventHandler() {

        @Override
        public void onStreamEvent(Notify notify) {
            log.info("ClientStream.onStreamEvent: {}", notify);
        }
    };
    client.setStreamEventHandler(netStreamEventHandler);
    IPendingServiceCallback connectCallback = new IPendingServiceCallback() {

        @Override
        public void resultReceived(IPendingServiceCall call) {
            log.info("connectCallback");
            ObjectMap<?, ?> map = (ObjectMap<?, ?>) call.getResult();
            String code = (String) map.get("code");
            log.info("Response code: {}", code);
            if ("NetConnection.Connect.Rejected".equals(code)) {
                System.out.printf("Rejected: %s\n", map.get("description"));
                client.disconnect();
                finished.set(true);
            } else if ("NetConnection.Connect.Success".equals(code)) {
                client.createStream(new IPendingServiceCallback() {

                    @Override
                    public void resultReceived(IPendingServiceCall call) {
                        double streamId = (Double) call.getResult();
                        // live buffer 0.5s
                        @SuppressWarnings("unused") RTMPConnection conn = (RTMPConnection) Red5.getConnectionLocal();
                        // conn.ping(new Ping(Ping.CLIENT_BUFFER, streamId, 500));
                        // client.play(streamId, youtubePublishName, -1, -1);
                        client.publish(streamId, youtubePublishName, "live", netStreamEventHandler);
                    }
                });
            // push data out for the publish
            // test();
            }
        }
    };
    // connect
    client.connect(youtubeHost, youtubePort, youtubeApp, connectCallback);
    Thread.currentThread().join(30000L);
    client.disconnect();
    log.info("Test - end");
}
Also used : IPendingServiceCallback(org.red5.server.api.service.IPendingServiceCallback) IEvent(org.red5.server.api.event.IEvent) Notify(org.red5.server.net.rtmp.event.Notify) RTMPConnection(org.red5.server.net.rtmp.RTMPConnection) IPendingServiceCall(org.red5.server.api.service.IPendingServiceCall) IEventDispatcher(org.red5.server.api.event.IEventDispatcher) ObjectMap(org.red5.io.utils.ObjectMap) Test(org.junit.Test)

Example 4 with RTMPClient

use of org.red5.client.net.rtmp.RTMPClient in project red5-client by Red5.

the class YouTubeConnectTest method testLocalhostRed5Publish.

// @Test
public void testLocalhostRed5Publish() throws InterruptedException {
    log.info("\ntestLocalhostRed5Publish");
    String host = "localhost";
    int port = 1935;
    // check to see if a server is listening on 1935 before proceeding
    String app = "live";
    final String publishName = "test";
    final RTMPClient client = new RTMPClient();
    client.setConnectionClosedHandler(new Runnable() {

        @Override
        public void run() {
            log.info("Test - exit");
        }
    });
    client.setExceptionHandler(new ClientExceptionHandler() {

        @Override
        public void handleException(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    client.setStreamEventDispatcher(new IEventDispatcher() {

        @Override
        public void dispatchEvent(IEvent event) {
            log.info("ClientStream.dispachEvent() {}", event);
        }
    });
    final INetStreamEventHandler netStreamEventHandler = new INetStreamEventHandler() {

        @Override
        public void onStreamEvent(Notify notify) {
            log.info("ClientStream.dispachEvent() {}", notify);
        }
    };
    client.setStreamEventHandler(netStreamEventHandler);
    IPendingServiceCallback connectCallback = new IPendingServiceCallback() {

        @Override
        public void resultReceived(IPendingServiceCall call) {
            log.info("connectCallback");
            ObjectMap<?, ?> map = (ObjectMap<?, ?>) call.getResult();
            String code = (String) map.get("code");
            log.info("Response code: {}", code);
            if ("NetConnection.Connect.Rejected".equals(code)) {
                System.out.printf("Rejected: %s\n", map.get("description"));
                client.disconnect();
                finished.set(true);
            } else if ("NetConnection.Connect.Success".equals(code)) {
                client.createStream(new IPendingServiceCallback() {

                    @Override
                    public void resultReceived(IPendingServiceCall call) {
                        int streamId = (Integer) call.getResult();
                        // live buffer 0.5s
                        @SuppressWarnings("unused") RTMPConnection conn = (RTMPConnection) Red5.getConnectionLocal();
                        // conn.ping(new Ping(Ping.CLIENT_BUFFER, streamId, 500));
                        // client.play(streamId, youtubePublishName, -1, -1);
                        client.publish(streamId, publishName, "live", netStreamEventHandler);
                    }
                });
            // push data out for the publish
            // test();
            }
        }
    };
    // connect
    client.connect(host, port, app, connectCallback);
    Thread.currentThread().join(30000L);
    client.disconnect();
    log.info("Test - end");
}
Also used : IPendingServiceCallback(org.red5.server.api.service.IPendingServiceCallback) IEvent(org.red5.server.api.event.IEvent) Notify(org.red5.server.net.rtmp.event.Notify) RTMPConnection(org.red5.server.net.rtmp.RTMPConnection) IPendingServiceCall(org.red5.server.api.service.IPendingServiceCall) IEventDispatcher(org.red5.server.api.event.IEventDispatcher) ObjectMap(org.red5.io.utils.ObjectMap)

Aggregations

ObjectMap (org.red5.io.utils.ObjectMap)3 IPendingServiceCall (org.red5.server.api.service.IPendingServiceCall)3 IPendingServiceCallback (org.red5.server.api.service.IPendingServiceCallback)3 Notify (org.red5.server.net.rtmp.event.Notify)3 Timer (java.util.Timer)2 RTMPClient (org.red5.client.net.rtmp.RTMPClient)2 IEvent (org.red5.server.api.event.IEvent)2 IEventDispatcher (org.red5.server.api.event.IEventDispatcher)2 RTMPConnection (org.red5.server.net.rtmp.RTMPConnection)2 Test (org.junit.Test)1 ClientExceptionHandler (org.red5.client.net.rtmp.ClientExceptionHandler)1 INetStreamEventHandler (org.red5.client.net.rtmp.INetStreamEventHandler)1 RTMPEClient (org.red5.client.net.rtmpe.RTMPEClient)1 RTMPSClient (org.red5.client.net.rtmps.RTMPSClient)1 StreamingProxy (org.red5.proxy.StreamingProxy)1