/**
  * Stop transcoding a stream.
  *
  * @param aStream The stream to stop transcoding.
  * @param aScope The application scope.
  */
 public synchronized void stopTranscodingStream(IBroadcastStream aStream, IScope aScope) {
   System.out.println(
       "stopTranscodingStream(" + aStream.getPublishedName() + "," + aScope.getName() + ")");
   String inputName = aStream.getPublishedName();
   Transcoder transcoder = mTranscoders.get(inputName);
   if (transcoder != null) {
     transcoder.stop();
   }
   BroadcastStream outputStream = mOutputStreams.get(inputName);
   if (outputStream != null) {
     outputStream.stop();
   }
 }
  /**
   * A hook to record a sample stream. A file is written in webapps/sip/streams/
   *
   * @param stream
   */
  private void recordStream(IBroadcastStream stream) {
    IConnection conn = Red5.getConnectionLocal();
    String streamName = stream.getPublishedName();

    try {
      ClientBroadcastStream cstream =
          (ClientBroadcastStream)
              this.getBroadcastStream(conn.getScope(), stream.getPublishedName());
      cstream.saveAs(streamName, false);
    } catch (Exception e) {
      System.out.println("ERROR while recording stream " + e.getMessage());
      e.printStackTrace();
    }
  }
  /**
   * Starts transcoding this stream. This method is a no-op if this stream is already a stream copy
   * created by this transcoder.
   *
   * @param aStream The stream to copy.
   * @param aScope The application scope.
   */
  public synchronized void startTranscodingStream(IBroadcastStream aStream, IScope aScope) {
    System.out.println(
        "startTranscodingStream(" + aStream.getPublishedName() + "," + aScope.getName() + ")");
    if (aStream.getPublishedName().startsWith(getStreamPrefix())) {
      System.out.println("Not making a copy of a copy: " + aStream.getPublishedName());
      return;
    }
    System.out.println("Making transcoded version of: " + aStream.getPublishedName());

    /*
     * Now, we need to set up the output stream we want to broadcast to.
     * Turns out aaffmpeg-red5 provides one of those.
     */
    String outputName = getStreamPrefix() + aStream.getPublishedName();
    BroadcastStream outputStream = new BroadcastStream(outputName);
    outputStream.setPublishedName(outputName);
    outputStream.setScope(aScope);

    IContext context = aScope.getContext();

    IProviderService providerService =
        (IProviderService) context.getBean(IProviderService.BEAN_NAME);
    if (providerService.registerBroadcastStream(aScope, outputName, outputStream)) {
      IBroadcastScope bsScope =
          (BroadcastScope) providerService.getLiveProviderInput(aScope, outputName, true);

      bsScope.setAttribute(IBroadcastScope.STREAM_ATTRIBUTE, outputStream);
    } else {
      System.out.println("Got a fatal error; could not register broadcast stream");
      throw new RuntimeException("fooey!");
    }
    mOutputStreams.put(aStream.getPublishedName(), outputStream);
    outputStream.start();

    /** Now let's give aaffmpeg-red5 some information about what we want to transcode as. */
    ISimpleMediaFile outputStreamInfo = new SimpleMediaFile();
    outputStreamInfo.setHasAudio(true);
    outputStreamInfo.setAudioBitRate(32000);
    outputStreamInfo.setAudioChannels(1);
    outputStreamInfo.setAudioSampleRate(22050);
    outputStreamInfo.setAudioCodec(ICodec.ID.CODEC_ID_MP3);
    outputStreamInfo.setHasVideo(true);
    // Unfortunately the Trans-coder needs to know the width and height
    // you want to output as; even if you don't know yet.
    outputStreamInfo.setVideoWidth(640);
    outputStreamInfo.setVideoHeight(480);
    outputStreamInfo.setVideoBitRate(320000);
    outputStreamInfo.setVideoCodec(ICodec.ID.CODEC_ID_FLV1);
    outputStreamInfo.setVideoGlobalQuality(0);

    /** And finally, let's create out transcoder */
    Transcoder transcoder =
        new Transcoder(aStream, outputStream, outputStreamInfo, null, null, mVideoPictureListener);
    Thread transcoderThread = new Thread(transcoder);
    transcoderThread.setDaemon(true);
    mTranscoders.put(aStream.getPublishedName(), transcoder);
    System.out.println("Starting transcoding thread for: " + aStream.getPublishedName());
    transcoderThread.start();
  }
  @Override
  public void streamPublishStart(IBroadcastStream stream) {
    String clientId = Red5.getConnectionLocal().getClient().getId();
    String userid = getUserId();
    String username = getUsername();

    log.debug(
        "{} has started publishing stream [{}]",
        username + "[uid=" + userid + "][clientid=" + clientId + "]",
        stream.getPublishedName());
    System.out.println("streamPublishStart: " + stream.getPublishedName());
    IConnection conn = Red5.getConnectionLocal();
    String peerId = (String) conn.getAttribute("VOICE_CONF_PEER");
    if (peerId != null) {
      super.streamPublishStart(stream);
      sipPeerManager.startTalkStream(peerId, clientId, stream, conn.getScope());
      //	    	recordStream(stream);
    }
  }
 /** {@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);
 }
 public void streamBroadcastStart(IBroadcastStream stream) {
   loginfo("Red5SIP Stream broadcast start: " + stream.getPublishedName());
 }
 public void streamBroadcastClose(IBroadcastStream stream) {
   loginfo("Red5SIP Stream broadcast close: " + stream.getPublishedName());
 }
 public void streamPublishStart(IBroadcastStream stream) {
   loginfo("Red5SIP Stream publish start: " + stream.getPublishedName());
   IConnection current = Red5.getConnectionLocal();
 }