Search in sources :

Example 1 with INetStreamEventHandler

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

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

the class RTMPClientTest method test26.

// https://github.com/Red5/red5-client/issues/26
@Test
public void test26() throws InterruptedException {
    client.setStreamEventHandler(new INetStreamEventHandler() {

        @Override
        public void onStreamEvent(Notify notify) {
            log.info("ClientStream.dispachEvent: {}", notify);
        }
    });
    client.setServiceProvider(new ClientMethodHander());
    client.setConnectionClosedHandler(new Runnable() {

        @Override
        public void run() {
            System.out.println("Connection closed");
        }
    });
    client.setExceptionHandler(new ClientExceptionHandler() {

        @Override
        public void handleException(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    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();
            } else if ("NetConnection.Connect.Success".equals(code)) {
                // 1. Wait for onBWDone
                timer.schedule(new BandwidthStatusTask(), 2000L);
            }
        }
    };
    /*
         * client.connect("localhost", 1935, "live/remote/0586e318-6277-11e3-adc2-22000a1d91fe", new IPendingServiceCallback() {
         * @Override public void resultReceived(IPendingServiceCall result) { System.out.println("resultReceived: " + result); ObjectMap<?, ?> map = (ObjectMap<?, ?>) result.getResult();
         * String code = (String) map.get("code"); System.out.printf("Response code: %s\n", code); if ("NetConnection.Connect.Rejected".equals(code)) { System.out.printf("Rejected: %s\n",
         * map.get("description")); client.disconnect(); } else if ("NetConnection.Connect.Success".equals(code)) { System.out.println("success: " + result.isSuccess()); ArrayList<Object>
         * list = new ArrayList<>(); list.add(new Object[] { "fujifilm-x100s-video-test-1080p-full-hd-hdmp4_720.mp4" }); list.add(new Object[] {
         * "canon-500d-test-video-720-hd-30-fr-hdmp4_720.mp4" }); Object[] params = { "64", "cc-video-processed/", list }; //Object[] params = { "64", "cc-video-processed/" };
         * client.invoke("loadPlaylist", params, new IPendingServiceCallback() {
         * @Override public void resultReceived(IPendingServiceCall result) { System.out.println(result); } }); } } });
         */
    client.connect("localhost", 1935, "vod", connectCallback);
    do {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
    } while (!client.conn.isClosed());
    log.debug("Client not connected");
    timer.cancel();
    log.info("Exit");
}
Also used : IPendingServiceCallback(org.red5.server.api.service.IPendingServiceCallback) Notify(org.red5.server.net.rtmp.event.Notify) IPendingServiceCall(org.red5.server.api.service.IPendingServiceCall) ObjectMap(org.red5.io.utils.ObjectMap) Test(org.junit.Test)

Example 3 with INetStreamEventHandler

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

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

Example 5 with INetStreamEventHandler

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

the class BaseRTMPClientHandler method publish.

@Override
public void publish(Number streamId, String name, String mode, INetStreamEventHandler handler) {
    log.debug("publish - stream id: {}, name: {}, mode: {}", new Object[] { streamId, name, mode });
    // setup the netstream handler
    if (handler != null) {
        NetStreamPrivateData streamData = streamDataMap.get(streamId);
        if (streamData != null) {
            log.debug("Setting handler on stream data - handler: {}", handler);
            streamData.handler = handler;
        } else {
            log.debug("Stream data not found for stream id: {}", streamId);
        }
    }
    // setup publish parameters
    final Object[] params = new Object[2];
    params[0] = name;
    params[1] = mode;
    // call publish
    PendingCall pendingCall = new PendingCall("publish", params);
    conn.invoke(pendingCall, getChannelForStreamId(streamId));
}
Also used : PendingCall(org.red5.server.service.PendingCall) IClientSharedObject(org.red5.server.api.so.IClientSharedObject) ClientSharedObject(org.red5.server.so.ClientSharedObject)

Aggregations

ObjectMap (org.red5.io.utils.ObjectMap)4 IPendingServiceCall (org.red5.server.api.service.IPendingServiceCall)4 IPendingServiceCallback (org.red5.server.api.service.IPendingServiceCallback)4 Notify (org.red5.server.net.rtmp.event.Notify)4 Test (org.junit.Test)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 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 StreamingProxy (org.red5.proxy.StreamingProxy)1 IClientSharedObject (org.red5.server.api.so.IClientSharedObject)1 PendingCall (org.red5.server.service.PendingCall)1 ClientSharedObject (org.red5.server.so.ClientSharedObject)1