title: How to add live stream source in Red5 IServerStream?
body: <h2>The problem</h2>
  
  <p>I have created and started Red5 server-side stream in my custom application based on oflaDemo in this way:</p>
  
  <pre><code>serverStream = StreamUtils.createServerStream( app , "mystream" );
  
  // repeats /streams/prometheus.flv from oflaDemo several times (20sec):
  serverStream.addItem( SimplePlayItem.build( "prometheus" , 0 , 20000 ) );
  serverStream.addItem( SimplePlayItem.build( "prometheus" , 0 , 20000 ) );
  
  // now I want to add "camsource" stream, wich is published from Flash client:
  serverStream.addItem( SimplePlayItem.build( "camsource" , 0 , 20000 ) );
  ...
  serverStream.play();
  </code></pre>
  
  <p>prometheus.flv plays 2x nicely, but when times comes for "camsource" the result is:</p>
  
  <p><code>[Red5_Scheduler_Worker-3] org.red5.server.stream.ProviderService - Exception attempting to lookup file: ServletContext resource [/streams/camsource.flv] cannot be resolved to URL because it does not exist</code></p>
  
  <p><strong>How can I create IServerStream that re-publishes user stream posted from flash client?</strong></p>
  
  <p>I can both see flv stream and cam stream by <code>NetStream</code> <code>.play(...)</code> in Flash, but I cannot create IServerStream programatically to publish from live source.</p>
  
  <p>The <code>comsource</code> publishing and <code>serverStream</code> are in the same Scope.</p>
  
  <h2>My current working path</h2>
  
  <p>Due to <a href="https://groups.google.com/d/msg/red5interest/3EhPcbTIY7c/AfnG4ddU2ZYJ" rel="nofollow">This response on Red5 group</a>, I have created my own ServerStream, in which I can push redirected/fake packets manually (I am not sure if this is done correctly):</p>
  
  <pre><code>public static class LiveTVServerStream extends ServerStream {
  
      public void manuallyPushPacket( IStreamPacket packet ) {
          for ( IStreamListener listener : getStreamListeners() ) {
              listener.packetReceived( this , packet );
          }
      }
  }
  </code></pre>
  
  <p>Then I create such stream and register it in <code>appStart()</code>:</p>
  
  <pre><code>liveTVServerStream = new LiveTVServerStream();
  liveTVServerStream.setScope( appScope );
  liveTVServerStream.setName( "livetv" );
  liveTVServerStream.setPublishedName( "livetv" );
  // Add single starting video because ServerStream requires at least 1 item
  liveTVServerStream.addItem( SimplePlayItem.build( "prometheus" , 0 , 20000 ) );
  liveTVServerStream.start();
  
  IContext context = scope.getContext();
  IProviderService providerService = (IProviderService) context.getBean( IProviderService.BEAN_NAME );
  providerService.registerBroadcastStream( scope , "livetv" , liveTVServerStream);
  </code></pre>
  
  <p>Then, if my "news reporter" (video source from webcam by Flash) starts broadcasting, I redirect packets to <code>liveTVServerStream</code>:</p>
  
  <pre><code>public void streamPublishStart( final IBroadcastStream stream ) {
     if ( !stream.getPublishedName().equals( "newsreporter" ) ) {
        return;
     }
  
  logInfo( "News reporter connected - redirecting packets" );
      stream.addStreamListener( new IStreamListener() {
          volatile int dispatchedPacketCount = 0;
          volatile int ignoredPacketCount = 0;
  
          @Override
          public void packetReceived( IBroadcastStream newsReporterStream , IStreamPacket packet ) {
              if ( packet instanceof IRTMPEvent ) {
                  dispatchedPacketCount++;
                  if ( (dispatchedPacketCount % 50) == 0 ) {
                      logInfo( "Dispatched packets: " + dispatchedPacketCount + " ignored packets: " + ignoredPacketCount );
                  }
  
                  // delta-time calculations
                  long t1 = newsReporterStream.getCreationTime();
                  long t2 = liveTVServerStream.getCreationTime();
                  long delta = t2 - t1;
                  int t3 = ((IRTMPEvent) packet).getTimestamp();
                  t3 += delta;
                  ((IRTMPEvent) packet).setTimestamp( t3 );
  
                  liveTVServerStream.manuallyPushPacket( packet );
              } else {
                  ignoredPacketCount++;
              }
  
          }
      } );
  </code></pre>
  
  <h2>Current results</h2>
  
  <p>The log of Red5 console can be found <a href="http://pastebin.com/dw2msY4Y" rel="nofollow">here</a>.</p>
  
  <p>"News reporter" flash works ok - sending video from webcam. But when I subscribe to watch "livetv" stream, there is no video. Flash parts works - tested it with <code>ServerStream</code> containing several .flv files in queue and Flash connected &amp; streamed properly, like live TV.</p>