Ejemplo n.º 1
0
  @Test
  public void testFindProtocolHandler() {
    IRTMPEventIOHandler ioHandler =
        new IRTMPEventIOHandler() {
          public Red5Message read() {
            return null;
          }

          public void write(Red5Message msg) {}
        };
    IRTMPEventIOHandler oldHandler = null;
    IURLProtocolHandler handler = null;
    int retval = -1;
    URLProtocolManager manager = null;
    ;
    Red5HandlerFactory factory = null;
    IURLProtocolHandlerFactory oldFactory = null;
    FfmpegIOHandle ffmpegHandle = new FfmpegIOHandle();

    manager = URLProtocolManager.getManager();
    assertTrue("could not find the protocol manager", manager != null);

    factory = new Red5HandlerFactory();
    oldFactory = manager.registerFactory(mRed5URLProtocol, factory);
    assertTrue("no previously registered factory", oldFactory == null);

    ISimpleMediaFile file = new SimpleMediaFile();
    file.setURL(mRed5URLProtocol + ":input");
    oldHandler = factory.registerStream(ioHandler, file);
    assertTrue("a buffer already registered", oldHandler == null);

    String goodUrl = mRed5URLProtocol + ":" + "input";
    String badUrl = mRed5URLProtocol + ":" + "shouldNotExist";

    handler = factory.getHandler(mRed5URLProtocol, goodUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Did not find handler for registered stream", handler != null);

    handler = factory.getHandler(mRed5URLProtocol, badUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Found unexpected handler for stream", handler == null);

    // Now, test that we can successfully trick Ffmpeg into opening the
    // file.
    retval = Helper.url_open(ffmpegHandle, goodUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Ffmpeg could not find the stream: " + goodUrl, retval >= 0);

    retval = Helper.url_open(ffmpegHandle, badUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Ffmpeg found the unexpected stream: " + badUrl, retval == -1);

    // and make sure we can't open the bad URL.

    // now delete the stream
    oldHandler = factory.deleteStream("input");
    assertTrue("did not get a buffer back when deleting a stream", oldHandler != null);
    assertTrue("did not get readbuffer back", oldHandler == ioHandler);

    handler = factory.getHandler(mRed5URLProtocol, goodUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("found unexpected handler for delete stream", handler == null);
  }
Ejemplo n.º 2
0
  /**
   * 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();
  }