Example usage for io.netty.handler.codec.rtsp RtspMethods TEARDOWN

List of usage examples for io.netty.handler.codec.rtsp RtspMethods TEARDOWN

Introduction

In this page you can find the example usage for io.netty.handler.codec.rtsp RtspMethods TEARDOWN.

Prototype

HttpMethod TEARDOWN

To view the source code for io.netty.handler.codec.rtsp RtspMethods TEARDOWN.

Click Source Link

Document

The TEARDOWN request stops the stream delivery for the given URI, freeing the resources associated with it.

Usage

From source file:sas.systems.imflux.functionaltest.session.SimpleRtspSessionFunctionalTest.java

License:Apache License

/**
 * Create N {@link SimpleRtspSession}s. Afterwards send data 
 * from each session to the others. Use the automated RTSP handling functionality.
 * /*from   w  w  w  .  j a  va 2  s .com*/
 * @throws Exception
 */
@Test
public void testDeliveryToAllParticipantsWithAutoRtspHandling() throws Exception {
    this.sessions = new SimpleRtspSession[N];
    final AtomicInteger[] counters = new AtomicInteger[N];
    final CountDownLatch latch = new CountDownLatch(N);
    final String optionsString = "OPTIONS, SETUP, TEARDOWN";

    // create N sessions and initialize them
    for (int i = 0; i < N; i++) {
        final String sessionId = "session" + i;
        final RtpParticipant localParticipant = RtpParticipant.createReceiver(new RtpParticipantInfo(i),
                "127.0.0.1", 10000 + (i * 2), 20001 + (i * 2));
        final SocketAddress localAddress = new InetSocketAddress("127.0.0.1", 30000 + i);
        this.sessions[i] = new SimpleRtspSession(sessionId, localParticipant, localAddress);
        this.sessions[i].setUseNio(true);
        this.sessions[i].setAutomatedRtspHandling(true);
        this.sessions[i].setOptionsString(optionsString);
        assertTrue(this.sessions[i].init());

        final AtomicInteger counter = new AtomicInteger();
        counters[i] = counter;

        this.sessions[i].addRequestListener(new RtspRequestListener() {
            @Override
            public void teardownRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void setupRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void setParameterRequestReceived(HttpRequest message, RtspParticipant participant) {
            }

            @Override
            public void redirectRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void recordRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void playRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void pauseRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void optionsRequestReceived(HttpRequest message, RtspParticipant participant) {
                fail("automated RTSP handling is set to true, so this method should not be invoked!");
            }

            @Override
            public void getParameterRequestReceived(HttpRequest message, RtspParticipant participant) {
            }

            @Override
            public void describeRequestReceived(HttpRequest message, RtspParticipant participant) {
            }

            @Override
            public void announceRequestReceived(HttpRequest request, RtspParticipant participant) {
            }
        });

        final SimpleRtspSession session = this.sessions[i];
        this.sessions[i].addResponseListener(new RtspResponseListener() {
            @Override
            public void responseReceived(HttpResponse message, RtspParticipant participant) {
                final SocketAddress remote = participant
                        .getRemoteAddress(); /*new InetSocketAddress("127.0.0.1", 30001);*/
                final String cSeqString = message.headers().get(RtspHeaders.Names.CSEQ);
                int cseq = 0;
                try {
                    cseq = Integer.valueOf(cSeqString);
                } catch (Exception e) {
                    fail("Sequence number was not correctly set!");
                }
                System.out.println("\t" + sessionId + " received response from " + remote + "\tstatus: "
                        + message.getStatus().code() + "\t cSeq: " + cSeqString);

                if (cseq == 0) {
                    // check PUBLIC header
                    final String options = message.headers().get(RtspHeaders.Names.PUBLIC);
                    assertNotNull(options);
                    assertEquals(optionsString, options);
                }
                if (cseq == 1) { // response of SETUP
                    // check session header
                    final String sessionId = message.headers().get(RtspHeaders.Names.SESSION);
                    assertNotNull(sessionId);

                    // check transport header
                    final String transport = message.headers().get(RtspHeaders.Names.TRANSPORT);
                    assertNotNull(transport);
                    // TODO: further checking if possible

                    // send TEARDOWN request
                    final HttpRequest teardownRequest = new DefaultHttpRequest(RtspVersions.RTSP_1_0,
                            RtspMethods.TEARDOWN, "rtsp://localhost/path/to/resource");
                    teardownRequest.headers().add(RtspHeaders.Names.CSEQ, cseq + 1);
                    teardownRequest.headers().add(RtspHeaders.Names.SESSION, sessionId);
                    session.sendRequest(teardownRequest, remote);
                    // these both do not work...why exactly?
                    //                      session.sendRequest(teardownRequest, participant.getChannel());
                    //                    participant.sendMessage(teardownRequest);
                }
                if (cseq == 2) { // response of TEARDOWN
                    if (message.getStatus().equals(RtspResponseStatuses.OK)
                            && counter.incrementAndGet() == (N - 1)) {
                        latch.countDown();
                    }
                }
            }
        });
    }

    // send data
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i == j)
                continue;

            final SocketAddress remoteAddress = new InetSocketAddress("127.0.0.1", 30000 + j);
            final String uri = "rtsp://localhost:" + (30000 + j) + "/path/to/resource";
            int cseq = 0;

            // send OPTIONS request
            final HttpRequest optionsRequest = new DefaultHttpRequest(RtspVersions.RTSP_1_0,
                    RtspMethods.OPTIONS, uri);
            optionsRequest.headers().add(RtspHeaders.Names.CSEQ, cseq++);
            assertTrue(this.sessions[i].sendRequest(optionsRequest,
                    new InetSocketAddress("127.0.0.1", 30000 + j)));

            // send SETUP request
            final RtpParticipant localRtp = this.sessions[i].getLocalRtpParticipant();
            final int localRtpPort = ((InetSocketAddress) localRtp.getDataDestination()).getPort();
            final int localRtcpPort = ((InetSocketAddress) localRtp.getControlDestination()).getPort();
            final HttpRequest setupRequest = new DefaultHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.SETUP,
                    uri);
            setupRequest.headers().add(RtspHeaders.Names.CSEQ, cseq++);
            setupRequest.headers().add(RtspHeaders.Names.TRANSPORT,
                    "RTP/AVP;unicast;client_port=" + localRtpPort + "-" + localRtcpPort);
            assertTrue(this.sessions[i].sendRequest(setupRequest, remoteAddress));

        }
        // wait for 50 ms to prevent too high load on a single session
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            fail(e.toString());
        }
    }
    System.out.println("Wait for latch.....");

    // wait for the Threads to finish and check counters
    if (!latch.await(2000L, TimeUnit.MILLISECONDS))
        System.out.println("! Latch timed out !");
    System.out.println(".. latch finished!\n");
    for (byte i = 0; i < N; i++) {
        assertEquals((N - 1), counters[i].get());
    }
}

From source file:sas.systems.imflux.session.rtsp.SimpleRtspSession.java

License:Apache License

/**
 * {@inheritDoc}/*  w w  w  . j av  a  2  s  . c o m*/
 */
@Override
public void requestReceived(Channel channel, HttpRequest request) {
    if (!request.getDecoderResult().isSuccess())
        return;

    LOG.debug("RTSP request received: {}", request);

    if (request.getMethod().equals(RtspMethods.OPTIONS)) {
        handleOptionsRequest(channel, request);
    }
    if (request.getMethod().equals(RtspMethods.DESCRIBE)) {
        if (this.requestListener.isEmpty()) {
            LOG.warn(
                    "No requestListener registered, sending NOT_IMPLEMENTED as response of a DESCRIBE request!");
            sendNotImplemented(channel, request);
        }
        // forward message (resource description is application specific)
        for (RtspRequestListener listener : this.requestListener) {
            listener.describeRequestReceived(request, RtspParticipant.newInstance(channel));
        }
    }
    if (request.getMethod().equals(RtspMethods.ANNOUNCE)) {
        if (this.requestListener.isEmpty()) {
            LOG.warn(
                    "No requestListener registered, sending NOT_IMPLEMENTED as response of an ANNOUNCE request!");
            sendNotImplemented(channel, request);
        }
        // forward message (resource description is again application specific)
        for (RtspRequestListener listener : this.requestListener) {
            listener.announceRequestReceived(request, RtspParticipant.newInstance(channel));
        }
    }
    if (request.getMethod().equals(RtspMethods.SETUP)) {
        handleSetupRequest(channel, request);
    }
    if (request.getMethod().equals(RtspMethods.PLAY)) {
        if (!automatedRtspHandling) {
            // forward message
            for (RtspRequestListener listener : this.requestListener) {
                listener.playRequestReceived(request, RtspParticipant.newInstance(channel));
            }
        } else {
            sendNotImplemented(channel, request);
        }
    }
    if (request.getMethod().equals(RtspMethods.PAUSE)) {
        if (!automatedRtspHandling) {
            // forward message
            for (RtspRequestListener listener : this.requestListener) {
                listener.pauseRequestReceived(request, RtspParticipant.newInstance(channel));
            }
        } else {
            sendNotImplemented(channel, request);
        }
    }
    if (request.getMethod().equals(RtspMethods.TEARDOWN)) {
        handleTeardownRequest(channel, request);
    }
    if (request.getMethod().equals(RtspMethods.GET_PARAMETER)) {
        if (this.requestListener.isEmpty()) {
            // RTSP assumes an empty body if content-length header is missing
            if (!request.headers().contains(RtspHeaders.Names.CONTENT_LENGTH)) {
                // assume this is a ping 
                HttpResponse response = new DefaultHttpResponse(rtspVersion, RtspResponseStatuses.OK);
                response.headers().add(RtspHeaders.Names.CSEQ, request.headers().get(RtspHeaders.Names.CSEQ));
                sendResponse(response, channel);
            }
            LOG.warn(
                    "No requestListener registered, sending NOT_IMPLEMENTED as response of a GET_PARAMETER request!");
            sendNotImplemented(channel, request);
        }
        // forward message (GET_PARAMETER is application specific)
        for (RtspRequestListener listener : this.requestListener) {
            listener.getParameterRequestReceived(request, RtspParticipant.newInstance(channel));
        }
    }
    if (request.getMethod().equals(RtspMethods.SET_PARAMETER)) {
        if (this.requestListener.isEmpty()) {
            LOG.warn(
                    "No requestListener registered, sending NOT_IMPLEMENTED as response of a SET_PARAMETER request!");
            sendNotImplemented(channel, request);
        }
        // forward message (SET_PARAMETER is application specific)
        for (RtspRequestListener listener : this.requestListener) {
            listener.setParameterRequestReceived(request, RtspParticipant.newInstance(channel));
        }
    }
    if (request.getMethod().equals(RtspMethods.REDIRECT)) {
        if (!automatedRtspHandling) {
            // forward message 
            for (RtspRequestListener listener : this.requestListener) {
                listener.redirectRequestReceived(request, RtspParticipant.newInstance(channel));
            }
        } else {
            sendNotImplemented(channel, request);
        }
    }
    if (request.getMethod().equals(RtspMethods.RECORD)) {
        if (!automatedRtspHandling) {
            // forward message 
            for (RtspRequestListener listener : this.requestListener) {
                listener.recordRequestReceived(request, RtspParticipant.newInstance(channel));
            }
        } else {
            sendNotImplemented(channel, request);
        }
    }
}