Search in sources :

Example 1 with IEventDispatcher

use of org.red5.server.api.event.IEventDispatcher 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 2 with IEventDispatcher

use of org.red5.server.api.event.IEventDispatcher 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 3 with IEventDispatcher

use of org.red5.server.api.event.IEventDispatcher in project red5-server-common by Red5.

the class BaseRTMPHandler method messageReceived.

/**
 * {@inheritDoc}
 */
public void messageReceived(RTMPConnection conn, Packet packet) throws Exception {
    log.trace("messageReceived connection: {}", conn.getSessionId());
    if (conn != null) {
        final IRTMPEvent message = packet.getMessage();
        try {
            final Header header = packet.getHeader();
            final Number streamId = header.getStreamId();
            final Channel channel = conn.getChannel(header.getChannelId());
            final IClientStream stream = conn.getStreamById(streamId);
            if (isTrace) {
                log.trace("Message received - header: {}", header);
            }
            // set stream id on the connection
            conn.setStreamId(streamId);
            // increase number of received messages
            conn.messageReceived();
            // set the source of the message
            message.setSource(conn);
            // process based on data type
            final byte headerDataType = header.getDataType();
            if (isTrace) {
                log.trace("Header / message data type: {}", headerDataType);
            }
            switch(headerDataType) {
                case TYPE_AGGREGATE:
                    log.debug("Aggregate type data - header timer: {} size: {}", header.getTimer(), header.getSize());
                case TYPE_AUDIO_DATA:
                case TYPE_VIDEO_DATA:
                    // mark the event as from a live source
                    // log.trace("Marking message as originating from a Live source");
                    message.setSourceType(Constants.SOURCE_TYPE_LIVE);
                    // the client sends a few stream packets before stopping; we need to ignore them.
                    if (stream != null) {
                        EnsuresPacketExecutionOrder epeo = (EnsuresPacketExecutionOrder) conn.getAttribute(EnsuresPacketExecutionOrder.ATTRIBUTE_NAME);
                        if (epeo == null && stream != null) {
                            epeo = new EnsuresPacketExecutionOrder((IEventDispatcher) stream, conn);
                            conn.setAttribute(EnsuresPacketExecutionOrder.ATTRIBUTE_NAME, epeo);
                        }
                        epeo.addPacket(message);
                    }
                    break;
                case TYPE_FLEX_SHARED_OBJECT:
                case TYPE_SHARED_OBJECT:
                    onSharedObject(conn, channel, header, (SharedObjectMessage) message);
                    break;
                case TYPE_INVOKE:
                case TYPE_FLEX_MESSAGE:
                    onCommand(conn, channel, header, (Invoke) message);
                    IPendingServiceCall call = ((Invoke) message).getCall();
                    if (message.getHeader().getStreamId().intValue() != 0 && call.getServiceName() == null && StreamAction.PUBLISH.equals(call.getServiceMethodName())) {
                        if (stream != null) {
                            ((IEventDispatcher) stream).dispatchEvent(message);
                        }
                    }
                    break;
                case TYPE_NOTIFY:
                // like an invoke, but does not return anything and has a invoke / transaction id of 0
                case TYPE_FLEX_STREAM_SEND:
                    if (((Notify) message).getData() != null && stream != null) {
                        // Stream metadata
                        EnsuresPacketExecutionOrder epeo = (EnsuresPacketExecutionOrder) conn.getAttribute(EnsuresPacketExecutionOrder.ATTRIBUTE_NAME);
                        if (epeo == null) {
                            epeo = new EnsuresPacketExecutionOrder((IEventDispatcher) stream, conn);
                            conn.setAttribute(EnsuresPacketExecutionOrder.ATTRIBUTE_NAME, epeo);
                        }
                        epeo.addPacket(message);
                    } else {
                        onCommand(conn, channel, header, (Notify) message);
                    }
                    break;
                case TYPE_PING:
                    onPing(conn, channel, header, (Ping) message);
                    break;
                case TYPE_BYTES_READ:
                    onStreamBytesRead(conn, channel, header, (BytesRead) message);
                    break;
                case TYPE_CHUNK_SIZE:
                    onChunkSize(conn, channel, header, (ChunkSize) message);
                    break;
                case // onBWDone / peer bw
                Constants.TYPE_CLIENT_BANDWIDTH:
                    log.debug("Client bandwidth: {}", message);
                    onClientBandwidth(conn, channel, (ClientBW) message);
                    break;
                case // window ack size
                Constants.TYPE_SERVER_BANDWIDTH:
                    log.debug("Server bandwidth: {}", message);
                    onServerBandwidth(conn, channel, (ServerBW) message);
                    break;
                default:
                    log.debug("Unknown type: {}", header.getDataType());
            }
            if (message instanceof Unknown) {
                log.info("Message type unknown: {}", message);
            }
        } catch (Throwable t) {
            log.error("Exception", t);
        }
    }
}
Also used : Notify(org.red5.server.net.rtmp.event.Notify) IPendingServiceCall(org.red5.server.api.service.IPendingServiceCall) IRTMPEvent(org.red5.server.net.rtmp.event.IRTMPEvent) Invoke(org.red5.server.net.rtmp.event.Invoke) IClientStream(org.red5.server.api.stream.IClientStream) IEventDispatcher(org.red5.server.api.event.IEventDispatcher) Header(org.red5.server.net.rtmp.message.Header) Unknown(org.red5.server.net.rtmp.event.Unknown)

Aggregations

IEventDispatcher (org.red5.server.api.event.IEventDispatcher)3 IPendingServiceCall (org.red5.server.api.service.IPendingServiceCall)3 Notify (org.red5.server.net.rtmp.event.Notify)3 ObjectMap (org.red5.io.utils.ObjectMap)2 IEvent (org.red5.server.api.event.IEvent)2 IPendingServiceCallback (org.red5.server.api.service.IPendingServiceCallback)2 RTMPConnection (org.red5.server.net.rtmp.RTMPConnection)2 Test (org.junit.Test)1 IClientStream (org.red5.server.api.stream.IClientStream)1 IRTMPEvent (org.red5.server.net.rtmp.event.IRTMPEvent)1 Invoke (org.red5.server.net.rtmp.event.Invoke)1 Unknown (org.red5.server.net.rtmp.event.Unknown)1 Header (org.red5.server.net.rtmp.message.Header)1