All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.red5.server.api.stream.support.StreamUtils Maven / Gradle / Ivy

Go to download

Ant Media Server supports RTMP, RTSP, MP4, HLS, WebRTC, Adaptive Streaming, etc.

There is a newer version: 2.10.0
Show newest version
/*
 * RED5 Open Source Media Server - https://github.com/Red5/
 * 
 * Copyright 2006-2016 by respective authors (see below). All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.red5.server.api.stream.support;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.red5.server.api.scope.IScope;
import org.red5.server.api.stream.IServerStream;
import org.red5.server.stream.ServerStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Stream helper methods.
 * 
 * @author The Red5 Project
 * @author Steven Gong ([email protected])
 * @author Paul Gregoire ([email protected])
 */
public abstract class StreamUtils {

    private static final Logger logger = LoggerFactory.getLogger(StreamUtils.class);

    /* Map to hold reference to the instanced server streams */
    private static volatile ConcurrentMap serverStreamMap = new ConcurrentHashMap(16, 0.75f, 4);

    /**
     * Creates server stream
     * 
     * @param scope
     *            Scope of stream
     * @param name
     *            Name of stream
     * @return IServerStream object
     */
    public static IServerStream createServerStream(IScope scope, String name) {
        logger.debug("Creating server stream: {} scope: {}", name, scope);
        ServerStream stream = new ServerStream();
        stream.setScope(scope);
        stream.setName(name);
        stream.setPublishedName(name);
        //save to the list for later lookups
        String key = scope.getName() + '/' + name;
        serverStreamMap.put(key, stream);
        return stream;
    }

    /**
     * Looks up a server stream in the stream map. Null will be returned if the stream is not found.
     *
     * @param scope
     *            Scope of stream
     * @param name
     *            Name of stream
     * @return IServerStream object
     */
    public static IServerStream getServerStream(IScope scope, String name) {
        logger.debug("Looking up server stream: {} scope: {}", name, scope);
        String key = scope.getName() + '/' + name;
        if (serverStreamMap.containsKey(key)) {
            return serverStreamMap.get(key);
        } else {
            logger.warn("Server stream not found with key: {}", key);
            return null;
        }
    }

    /**
     * Puts a server stream in the stream map
     *
     * @param scope
     *            Scope of stream
     * @param name
     *            Name of stream
     * @param stream
     *            ServerStream object
     */
    public static void putServerStream(IScope scope, String name, IServerStream stream) {
        logger.debug("Putting server stream in the map - name: {} scope: {} stream: {}", new Object[] { name, scope, stream });
        String key = scope.getName() + '/' + name;
        if (!serverStreamMap.containsKey(key)) {
            serverStreamMap.put(key, stream);
        } else {
            logger.warn("Server stream already exists in the map with key: {}", key);
        }
    }

    /**
     * Removes a server stream from the stream map
     *
     * @param scope
     *            Scope of stream
     * @param name
     *            Name of stream
     */
    public static void removeServerStream(IScope scope, String name) {
        logger.debug("Removing server stream from the map - name: {} scope: {}", name, scope);
        String key = scope.getName() + '/' + name;
        if (serverStreamMap.containsKey(key)) {
            serverStreamMap.remove(key);
        } else {
            logger.warn("Server stream did not exist in the map with key: {}", key);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy