Java Code Examples for org.red5.server.api.IConnection#getScope()

The following examples show how to use org.red5.server.api.IConnection#getScope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Associate connection with client
 * 
 * @param conn
 *            Connection object
 */
protected void register(IConnection conn) {
    if (log.isDebugEnabled()) {
        if (conn == null) {
            log.debug("Register null connection, client id: {}", id);
        } else {
            log.debug("Register connection ({}:{}) client id: {}", conn.getRemoteAddress(), conn.getRemotePort(), id);
        }
    }
    if (conn != null) {
        IScope scope = conn.getScope();
        if (scope != null) {
            log.debug("Registering for scope: {}", scope);
            connections.add(conn);
        } else {
            log.warn("Clients scope is null. Id: {}", id);
        }
    } else {
        log.warn("Clients connection is null. Id: {}", id);
    }
}
 
Example 2
Source File: ServiceUtils.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke a method on all connections to the current scope and handle result.
 * 
 * @param method
 *            name of the method to invoke
 * @param params
 *            parameters to pass to the method
 * @param callback
 *            object to notify when result is received
 */
public static void invokeOnAllConnections(String method, Object[] params, IPendingServiceCallback callback) {
    IConnection conn = Red5.getConnectionLocal();
    if (conn != null) {
        log.debug("Connection for invoke on all: {}", conn);
        IScope scope = conn.getScope();
        log.debug("Scope for invoke on all: {}", scope);
        invokeOnAllScopeConnections(scope, method, params, callback);
    } else {
        log.warn("Connection was null (thread local), scope cannot be located and cannot execute invoke request");
    }
}
 
Example 3
Source File: ServiceUtils.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * Notify a method on all connections to the current scope.
 * 
 * @param method
 *            name of the method to notify
 * @param params
 *            parameters to pass to the method
 */
public static void notifyOnAllConnections(String method, Object[] params) {
    IConnection conn = Red5.getConnectionLocal();
    if (conn != null) {
        log.debug("Connection for notify on all: {}", conn);
        IScope scope = conn.getScope();
        log.debug("Scope for notify on all: {}", scope);
        notifyOnAllScopeConnections(scope, method, params);
    } else {
        log.warn("Connection was null (thread local), scope cannot be located and cannot execute notify request");
    }
}
 
Example 4
Source File: MultiThreadedApplicationAdapter.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * Returns connection result for given scope and parameters. Whether the scope is room or app level scope, this method distinguishes it
 * and acts accordingly. You override {@link ApplicationAdapter#appConnect(IConnection, Object[])} or
 * {@link ApplicationAdapter#roomConnect(IConnection, Object[])} in your application to make it act the way you want.
 * 
 * @param conn
 *            Connection object
 * @return <code>true</code> if connect is successful, <code>false</code> otherwise
 */
public boolean connect(IConnection conn) {
    // ensure the log is not null at this point
    if (log == null) {
        log = Red5LoggerFactory.getLogger(this.getClass());
    }
    // get the scope from the connection
    IScope scope = conn.getScope();
    log.debug("connect: {} > {}", conn, scope);
    return connect(conn, scope, null);
}
 
Example 5
Source File: Application.java    From red5-examples with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void appDisconnect(IConnection conn) {
    log.info("oflaDemo appDisconnect");
    if (appScope == conn.getScope() && serverStream != null) {
        serverStream.close();
    }
    super.appDisconnect(conn);
}
 
Example 6
Source File: StreamService.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void play(String name, int start, int length, boolean flushPlaylist) {
    log.debug("Play called - name: {} start: {} length: {} flush playlist: {}", new Object[] { name, start, length, flushPlaylist });
    IConnection conn = Red5.getConnectionLocal();
    if (conn instanceof IStreamCapableConnection) {
        IScope scope = conn.getScope();
        IStreamCapableConnection streamConn = (IStreamCapableConnection) conn;
        Number streamId = conn.getStreamId();
        if (StringUtils.isEmpty(name)) {
            log.warn("The stream name may not be empty");
            sendNSFailed(streamConn, StatusCodes.NS_FAILED, "The stream name may not be empty.", name, streamId);
            return;
        }
        IStreamSecurityService security = (IStreamSecurityService) ScopeUtils.getScopeService(scope, IStreamSecurityService.class);
        if (security != null) {
            Set<IStreamPlaybackSecurity> handlers = security.getStreamPlaybackSecurity();
            for (IStreamPlaybackSecurity handler : handlers) {
                if (!handler.isPlaybackAllowed(scope, name, start, length, flushPlaylist)) {
                    log.warn("You are not allowed to play stream {}", name);
                    sendNSFailed(streamConn, StatusCodes.NS_FAILED, "You are not allowed to play the stream.", name, streamId);
                    return;
                }
            }
        }
        boolean created = false;
        IClientStream stream = streamConn.getStreamById(streamId);
        if (stream == null) {
            if (log.isTraceEnabled()) {
                log.trace("Stream not found for stream id: {} streams: {}", streamId, streamConn.getStreamsMap());
            }
            try {
                // if our current stream id is less than or equal to 0, reserve a new id
                if (streamId.doubleValue() <= 0.0d) {
                    streamId = streamConn.reserveStreamId();
                }
                // instance a new stream for the stream id
                stream = streamConn.newPlaylistSubscriberStream(streamId);
                if (stream != null) {
                    if (log.isTraceEnabled()) {
                        log.trace("Created stream: {} for stream id: {}", stream, streamId);
                    }
                    stream.setBroadcastStreamPublishName(name);
                    stream.start();
                    created = true;
                } else {
                    log.warn("Stream was null for id: {}", streamId);
                    // throw the ex so the ns fail will go out
                    throw new Exception("Stream creation failed for name: " + name + " id: " + streamId);
                }
            } catch (Exception e) {
                log.warn("Unable to start playing stream: {}", name, e);
                sendNSFailed(streamConn, StatusCodes.NS_FAILED, "Unable to start playing stream", name, streamId);
                return;
            }
        }
        if (stream instanceof ISubscriberStream) {
            ISubscriberStream subscriberStream = (ISubscriberStream) stream;
            IPlayItem item = simplePlayback.get() ? SimplePlayItem.build(name, start, length) : DynamicPlayItem.build(name, start, length);
            if (subscriberStream instanceof IPlaylistSubscriberStream) {
                IPlaylistSubscriberStream playlistStream = (IPlaylistSubscriberStream) subscriberStream;
                if (flushPlaylist) {
                    playlistStream.removeAllItems();
                }
                playlistStream.addItem(item);
            } else if (subscriberStream instanceof ISingleItemSubscriberStream) {
                ISingleItemSubscriberStream singleStream = (ISingleItemSubscriberStream) subscriberStream;
                singleStream.setPlayItem(item);
            } else {
                // not supported by this stream service
                log.warn("Stream instance type: {} is not supported", subscriberStream.getClass().getName());
                return;
            }
            try {
                subscriberStream.play();
            } catch (IOException err) {
                if (created) {
                    stream.close();
                    streamConn.deleteStreamById(streamId);
                }
                log.warn("Unable to play stream " + name, err);
                sendNSFailed(streamConn, StatusCodes.NS_FAILED, err.getMessage(), name, streamId);
            }
        }
    } else {
        log.debug("Connection was not stream capable");
    }
}
 
Example 7
Source File: Scope.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/**
 * Connect to scope with parameters. To successfully connect to scope it must have handler that will accept this connection with given
 * set of parameters. Client associated with connection is added to scope clients set, connection is registered as scope event listener.
 * 
 * @param conn
 *            Connection object
 * @param params
 *            Parameters passed with connection
 * @return true on success, false otherwise
 */
public boolean connect(IConnection conn, Object[] params) {
    log.debug("Connect - scope: {} connection: {}", this, conn);
    if (enabled) {
        if (hasParent() && !parent.connect(conn, params)) {
            log.debug("Connection to parent failed");
            return false;
        }
        if (hasHandler() && !getHandler().connect(conn, this, params)) {
            log.debug("Connection to handler failed");
            return false;
        }
        if (!conn.isConnected()) {
            log.debug("Connection is not connected");
            // timeout while connecting client
            return false;
        }
        final IClient client = conn.getClient();
        // we would not get this far if there is no handler
        if (hasHandler() && !getHandler().join(client, this)) {
            return false;
        }
        // checking the connection again? why?
        if (!conn.isConnected()) {
            // timeout while connecting client
            return false;
        }
        // add the client and event listener
        if (clients.add(client) && addEventListener(conn)) {
            log.debug("Added client");
            // increment conn stats
            connectionStats.increment();
            // get connected scope
            IScope connScope = conn.getScope();
            log.trace("Connection scope: {}", connScope);
            if (this.equals(connScope)) {
                final IServer server = getServer();
                if (server instanceof Server) {
                    ((Server) server).notifyConnected(conn);
                }
            }
            return true;
        }
    } else {
        log.debug("Connection failed, scope is disabled");
    }
    return false;
}
 
Example 8
Source File: Application.java    From red5-examples with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean appConnect(IConnection conn, Object[] params) {
    log.info("oflaDemo appConnect");
    IScope appScope = conn.getScope();
    log.debug("App connect called for scope: {}", appScope.getName());
    // getting client parameters
    Map<String, Object> properties = conn.getConnectParams();
    if (log.isDebugEnabled()) {
        for (Map.Entry<String, Object> e : properties.entrySet()) {
            log.debug("Connection property: {} = {}", e.getKey(), e.getValue());
        }
    }

    // Trigger calling of "onBWDone", required for some FLV players	
    // commenting out the bandwidth code as it is replaced by the mina filters
    //measureBandwidth(conn);
    //		if (conn instanceof IStreamCapableConnection) {
    //			IStreamCapableConnection streamConn = (IStreamCapableConnection) conn;
    //			SimpleConnectionBWConfig bwConfig = new SimpleConnectionBWConfig();
    //			bwConfig.getChannelBandwidth()[IBandwidthConfigure.OVERALL_CHANNEL] =
    //				1024 * 1024;
    //			bwConfig.getChannelInitialBurst()[IBandwidthConfigure.OVERALL_CHANNEL] =
    //				128 * 1024;
    //			streamConn.setBandwidthConfigure(bwConfig);
    //		}

    //		if (appScope == conn.getScope()) {
    //			serverStream = StreamUtils.createServerStream(appScope, "live0");
    //			SimplePlayItem item = new SimplePlayItem();
    //			item.setStart(0);
    //			item.setLength(10000);
    //			item.setName("on2_flash8_w_audio");
    //			serverStream.addItem(item);
    //			item = new SimplePlayItem();
    //			item.setStart(20000);
    //			item.setLength(10000);
    //			item.setName("on2_flash8_w_audio");
    //			serverStream.addItem(item);
    //			serverStream.start();
    //			try {
    //				serverStream.saveAs("aaa", false);
    //				serverStream.saveAs("bbb", false);
    //			} catch (Exception e) {}
    //		}

    return super.appConnect(conn, params);
}