Example usage for io.netty.handler.codec.rtsp RtspResponseStatuses METHOD_NOT_VALID

List of usage examples for io.netty.handler.codec.rtsp RtspResponseStatuses METHOD_NOT_VALID

Introduction

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

Prototype

HttpResponseStatus METHOD_NOT_VALID

To view the source code for io.netty.handler.codec.rtsp RtspResponseStatuses METHOD_NOT_VALID.

Click Source Link

Document

455 Method Not Valid in This State

Usage

From source file:org.mobicents.media.server.ctrl.rtsp.SetupAction.java

License:Open Source License

public FullHttpResponse call() throws Exception {
    FullHttpResponse response = null;// w  ww.  ja  va  2s .c  o  m

    //determine session
    RtspSession session = getSession(this.request.headers().get(RtspHeaders.Names.SESSION));
    if (session == null) {
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.SESSION_NOT_FOUND);
        response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }

    String filePath = null;
    String trackID = null;

    URI uri = new URI(this.request.getUri());
    String path = uri.getPath();

    int pos = path.indexOf("/trackID");
    if (pos > 0) {
        filePath = path.substring(0, pos);
        trackID = path.substring(pos + 1);
    } else {
        filePath = path;
    }

    File f = new File(filePath);
    if (f.isDirectory() || !f.exists()) {
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.NOT_FOUND);
        response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }

    int remotePort = this.getRemotePort();
    InetSocketAddress remoteAddress = new InetSocketAddress(remoteHost, remotePort);

    if (session.getState() == SessionState.PLAYING || session.getState() == SessionState.RECORDING) {
        // We don't support changing the Transport while state is PLAYING or RECORDING
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.METHOD_NOT_VALID);
        response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }

    Endpoint endpoint = (Endpoint) session.getAttribute("endpoint");
    if (endpoint == null) {
        try {
            endpoint = rtspController.lookup(ENDPOINT_NAME);
            session.setAttribute("endpoint", endpoint);
        } catch (ResourceUnavailableException e) {
            logger.warn("There is no free endpoint: " + ENDPOINT_NAME);
            response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0,
                    RtspResponseStatuses.SERVICE_UNAVAILABLE);
            response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
            response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
            return response;
        }
    }

    Connection connection = (Connection) session.getAttribute("connection");
    if (connection == null) {
        try {
            connection = endpoint.createConnection(ConnectionType.RTP, false);

            connection.setMode(mode);
            session.setAttribute("connection", connection);
        } catch (Exception e) {
            logger.error(e);
            response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0,
                    RtspResponseStatuses.SERVICE_UNAVAILABLE);
            response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
            response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
            return response;
        }
    }

    int ssrc = 268435456 + (int) (Math.random() * (Integer.MAX_VALUE - 268435456));

    Player player = null;//(Player) endpoint.getComponent("player");
    player.setURL(f.getAbsolutePath());
    //        player.setSSRC(trackID, ssrc);

    List<String> trackIds = (List<String>) session.getAttribute("trackIds");
    if (trackIds == null) {
        trackIds = new ArrayList<String>();
        session.setAttribute("trackIds", trackIds);
    }
    trackIds.add(trackID);

    // connection.setOtherParty(trackID, remoteAddress);

    int port = 0;//endpoint.getLocalPort(trackID);
    String source = null;//endpoint.getLocalAddress(trackID);
    String lastModified = formatter.format(new Date(f.lastModified()));
    String date = formatter.format(new Date());

    String transport = "RTP/AVP/UDP;unicast;source=" + source + ";" + this.clientPort + ";server_port=" + port
            + "-" + port + ";ssrc=" + Integer.toHexString(ssrc);
    response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
    response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
    response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
    response.headers().add(RtspHeaders.Names.SESSION, session.getId());
    response.headers().add(RtspHeaders.Names.TRANSPORT, transport);
    response.headers().add(HttpHeaders.Names.LAST_MODIFIED, lastModified);
    //TODO CACHE_CONTROL must come from settings. Let user decide how they want CACHE_CONTROL
    response.headers().add(HttpHeaders.Names.CACHE_CONTROL, "must-revalidate");
    response.headers().add(HttpHeaders.Names.DATE, date);
    //TODO EXPIRES must come from settings. Also depends on CACHE_CONTRL
    response.headers().add(HttpHeaders.Names.EXPIRES, date);
    session.setState(SessionState.READY);
    //ConnectionActivity connectionActivity = session.addConnection(connection);

    return response;
}

From source file:org.mobicents.media.server.rtsp.action.SetupAction.java

License:Open Source License

public FullHttpResponse call() throws Exception {
    FullHttpResponse response = null;//w ww  .  j  a v  a 2s. c  o m

    //determine session
    RtspSession session = getSession(this.request.headers().get(RtspHeaders.Names.SESSION));
    if (session == null) {
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.SESSION_NOT_FOUND);
        response.headers().add(HttpHeaders.Names.SERVER, RtspProvider.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }

    String filePath = null;
    String trackID = null;

    URI uri = new URI(this.request.getUri());
    String path = uri.getPath();

    int pos = path.indexOf("/trackID");
    if (pos > 0) {
        filePath = path.substring(0, pos);
        trackID = path.substring(pos + 1);
    } else {
        filePath = path;
    }

    File f = new File(filePath);
    if (f.isDirectory() || !f.exists()) {
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.NOT_FOUND);
        response.headers().add(HttpHeaders.Names.SERVER, RtspProvider.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }

    int remotePort = this.getRemotePort();
    InetSocketAddress remoteAddress = new InetSocketAddress(remoteHost, remotePort);

    if (session.getState() == SessionState.PLAYING || session.getState() == SessionState.RECORDING) {
        // We don't support changing the Transport while state is PLAYING or RECORDING
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.METHOD_NOT_VALID);
        response.headers().add(HttpHeaders.Names.SERVER, RtspProvider.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }

    Endpoint endpoint = (Endpoint) session.getAttribute("endpoint");
    /**if (endpoint == null) {
    try {
        endpoint = rtspController.lookup(ENDPOINT_NAME);
        session.setAttribute("endpoint", endpoint);
    } catch (ResourceUnavailableException e) {
        logger.warn("There is no free endpoint: " + ENDPOINT_NAME);
        response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.SERVICE_UNAVAILABLE);
        response.headers().add(HttpHeaders.Names.SERVER, RtspController.SERVER);
        response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
        return response;
    }
    }*/

    Connection connection = (Connection) session.getAttribute("connection");
    if (connection == null) {
        try {
            connection = endpoint.createConnection(ConnectionType.RTP, false);

            connection.setMode(mode);
            session.setAttribute("connection", connection);
        } catch (Exception e) {
            logger.error(e);
            response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0,
                    RtspResponseStatuses.SERVICE_UNAVAILABLE);
            response.headers().add(HttpHeaders.Names.SERVER, RtspProvider.SERVER);
            response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
            return response;
        }
    }

    int ssrc = 268435456 + (int) (Math.random() * (Integer.MAX_VALUE - 268435456));

    Player player = null;//(Player) endpoint.getComponent("player");
    player.setURL(f.getAbsolutePath());
    //        player.setSSRC(trackID, ssrc);

    List<String> trackIds = (List<String>) session.getAttribute("trackIds");
    if (trackIds == null) {
        trackIds = new ArrayList<String>();
        session.setAttribute("trackIds", trackIds);
    }
    trackIds.add(trackID);

    // connection.setOtherParty(trackID, remoteAddress);

    int port = 0;//endpoint.getLocalPort(trackID);
    String source = null;//endpoint.getLocalAddress(trackID);
    String lastModified = formatter.format(new Date(f.lastModified()));
    String date = formatter.format(new Date());

    String transport = "RTP/AVP/UDP;unicast;source=" + source + ";" + this.clientPort + ";server_port=" + port
            + "-" + port + ";ssrc=" + Integer.toHexString(ssrc);
    response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
    response.headers().add(HttpHeaders.Names.SERVER, RtspProvider.SERVER);
    response.headers().add(RtspHeaders.Names.CSEQ, this.request.headers().get(RtspHeaders.Names.CSEQ));
    response.headers().add(RtspHeaders.Names.SESSION, session.getId());
    response.headers().add(RtspHeaders.Names.TRANSPORT, transport);
    response.headers().add(HttpHeaders.Names.LAST_MODIFIED, lastModified);
    //TODO CACHE_CONTROL must come from settings. Let user decide how they want CACHE_CONTROL
    response.headers().add(HttpHeaders.Names.CACHE_CONTROL, "must-revalidate");
    response.headers().add(HttpHeaders.Names.DATE, date);
    //TODO EXPIRES must come from settings. Also depends on CACHE_CONTRL
    response.headers().add(HttpHeaders.Names.EXPIRES, date);
    session.setState(SessionState.READY);
    //ConnectionActivity connectionActivity = session.addConnection(connection);

    return response;
}