/** {@inheritDoc} */
 public boolean unregisterBroadcastStream(IScope scope, String name, IBroadcastStream bs) {
   if (log.isDebugEnabled()) {
     log.debug("Unregistering - name: {} stream: {} scope: {}", new Object[] {name, bs, scope});
     ((Scope) scope).dump();
   }
   IBroadcastScope broadcastScope = scope.getBroadcastScope(name);
   if (bs != null) {
     log.debug("Unsubscribing scope {} from provider {}", broadcastScope, bs.getProvider());
     broadcastScope.unsubscribe(bs.getProvider());
   }
   // if the scope has no listeners try to remove it
   if (!((BasicScope) broadcastScope).hasEventListeners()) {
     if (log.isDebugEnabled()) {
       log.debug("Scope has no event listeners attempting removal");
     }
     scope.removeChildScope(broadcastScope);
   }
   // verify that scope was removed
   return scope.getBasicScope(ScopeType.BROADCAST, name) == null;
 }
 /** {@inheritDoc} */
 public void publish(Boolean dontStop) {
   if (!dontStop) {
     IConnection conn = Red5.getConnectionLocal();
     if (conn instanceof IStreamCapableConnection) {
       IStreamCapableConnection streamConn = (IStreamCapableConnection) conn;
       int streamId = conn.getStreamId();
       IClientStream stream = streamConn.getStreamById(streamId);
       if (stream instanceof IBroadcastStream) {
         IBroadcastStream bs = (IBroadcastStream) stream;
         if (bs.getPublishedName() != null) {
           IBroadcastScope bsScope = getBroadcastScope(conn.getScope(), bs.getPublishedName());
           if (bsScope != null) {
             bsScope.unsubscribe(bs.getProvider());
             if (conn instanceof BaseConnection) {
               ((BaseConnection) conn).unregisterBasicScope(bsScope);
             }
           }
           bs.close();
           streamConn.deleteStreamById(streamId);
         }
       }
     }
   }
 }
 /** {@inheritDoc} */
 public boolean registerBroadcastStream(IScope scope, String name, IBroadcastStream bs) {
   if (log.isDebugEnabled()) {
     log.debug("Registering - name: {} stream: {} scope: {}", new Object[] {name, bs, scope});
     ((Scope) scope).dump();
   }
   IBroadcastScope broadcastScope = scope.getBroadcastScope(name);
   if (broadcastScope == null) {
     log.debug("Creating a new scope");
     broadcastScope = new BroadcastScope(scope, name);
     if (scope.addChildScope(broadcastScope)) {
       log.debug("Broadcast scope added");
     } else {
       log.warn("Broadcast scope was not added to {}", scope);
     }
   }
   // set the client broadcast stream if we have a broadcast scope
   if (broadcastScope != null && bs instanceof IClientBroadcastStream) {
     broadcastScope.setClientBroadcastStream((IClientBroadcastStream) bs);
   }
   if (log.isDebugEnabled()) {
     log.debug("Subscribing scope {} to provider {}", broadcastScope, bs.getProvider());
   }
   return broadcastScope.subscribe(bs.getProvider(), null);
 }
 /** {@inheritDoc} */
 public void publish(String name, String mode) {
   Map<String, String> params = null;
   if (name != null && name.contains("?")) {
     // read and utilize the query string values
     params = new HashMap<String, String>();
     String tmp = name;
     // check if we start with '?' or not
     if (name.charAt(0) != '?') {
       tmp = name.split("\\?")[1];
     } else if (name.charAt(0) == '?') {
       tmp = name.substring(1);
     }
     // now break up into key/value blocks
     String[] kvs = tmp.split("&");
     // take each key/value block and break into its key value parts
     for (String kv : kvs) {
       String[] split = kv.split("=");
       params.put(split[0], split[1]);
     }
     // grab the streams name
     name = name.substring(0, name.indexOf("?"));
   }
   log.debug("publish called with name {} and mode {}", name, mode);
   IConnection conn = Red5.getConnectionLocal();
   if (conn instanceof IStreamCapableConnection) {
     IScope scope = conn.getScope();
     IStreamCapableConnection streamConn = (IStreamCapableConnection) conn;
     int streamId = conn.getStreamId();
     if (StringUtils.isEmpty(name)) {
       sendNSFailed(
           streamConn, StatusCodes.NS_FAILED, "The stream name may not be empty.", name, streamId);
       log.error("The stream name may not be empty.");
       return;
     }
     IStreamSecurityService security =
         (IStreamSecurityService) ScopeUtils.getScopeService(scope, IStreamSecurityService.class);
     if (security != null) {
       Set<IStreamPublishSecurity> handlers = security.getStreamPublishSecurity();
       for (IStreamPublishSecurity handler : handlers) {
         if (!handler.isPublishAllowed(scope, name, mode)) {
           sendNSFailed(
               streamConn,
               StatusCodes.NS_PUBLISH_BADNAME,
               "You are not allowed to publish the stream.",
               name,
               streamId);
           log.error("You are not allowed to publish the stream {}", name);
           return;
         }
       }
     }
     IBroadcastScope bsScope = getBroadcastScope(scope, name);
     if (bsScope != null && !bsScope.getProviders().isEmpty()) {
       // another stream with that name is already published
       sendNSFailed(streamConn, StatusCodes.NS_PUBLISH_BADNAME, name, name, streamId);
       log.error("Bad name {}", name);
       return;
     }
     IClientStream stream = streamConn.getStreamById(streamId);
     if (stream != null && !(stream instanceof IClientBroadcastStream)) {
       log.error(
           "Stream not found or is not instance of IClientBroadcastStream, name: {}, streamId: {}",
           name,
           streamId);
       return;
     }
     boolean created = false;
     if (stream == null) {
       stream = streamConn.newBroadcastStream(streamId);
       created = true;
     }
     IClientBroadcastStream bs = (IClientBroadcastStream) stream;
     try {
       // set publish name
       bs.setPublishedName(name);
       // set stream parameters if they exist
       if (params != null) {
         bs.setParameters(params);
       }
       IContext context = conn.getScope().getContext();
       IProviderService providerService =
           (IProviderService) context.getBean(IProviderService.BEAN_NAME);
       // TODO handle registration failure
       if (providerService.registerBroadcastStream(conn.getScope(), name, bs)) {
         bsScope = getBroadcastScope(conn.getScope(), name);
         bsScope.setClientBroadcastStream(bs);
         if (conn instanceof BaseConnection) {
           ((BaseConnection) conn).registerBasicScope(bsScope);
         }
       }
       log.debug("Mode: {}", mode);
       if (IClientStream.MODE_RECORD.equals(mode)) {
         bs.start();
         bs.saveAs(name, false);
       } else if (IClientStream.MODE_APPEND.equals(mode)) {
         bs.start();
         bs.saveAs(name, true);
       } else {
         bs.start();
       }
       bs.startPublishing();
     } catch (IOException e) {
       log.warn("Stream I/O exception", e);
       sendNSFailed(
           streamConn,
           StatusCodes.NS_RECORD_NOACCESS,
           "The file could not be created/written to.",
           name,
           streamId);
       bs.close();
       if (created) {
         streamConn.deleteStreamById(streamId);
       }
     } catch (Exception e) {
       log.warn("Exception on publish", e);
     }
   }
 }