Package javax.media

Examples of javax.media.Processor


  
   private InputStream getInputStream(String urlStr, Format outputFormat, ContentDescriptor outputContentDescriptor) throws Exception
   {
     final ProcessorModel processorModel = new ProcessorModel(new MediaLocator(urlStr), outputFormat == null ? null : new Format[]{outputFormat}, outputContentDescriptor);
    
    final Processor processor = Manager.createRealizedProcessor(processorModel);
 
    final DataSource ds = processor.getDataOutput();

    final DataSink[] streamDataSinkHolder = new DataSink[] {null};
    // connect the data output of the processor to a StreamDataSink, which will make the data available to PipedInputStream, which we return.
    final PipedInputStream in = new PipedInputStream() {

      // override close to clean up everything when the media has been served.
      @Override
      public void close() throws IOException
      {
        super.close();
        logger.fine("Closed input stream");
        logger.fine("Stopping processor");
        processor.stop();
        logger.fine("Closing processor");
        processor.close();
        logger.fine("Deallocating processor");
        processor.deallocate();
        if (streamDataSinkHolder[0] != null)
        {  logger.fine("Closing StreamDataSink");
          streamDataSinkHolder[0].close();
        }
      }
     
    };
    final PipedOutputStream out = new PipedOutputStream(in);
    final DataSink streamDataSink = new StreamDataSink(out);
    streamDataSinkHolder[0] = streamDataSink;
   
    streamDataSink.setSource(ds);
    streamDataSink.open();
    streamDataSink.start();
   
    logger.info("Starting processor");
    processor.start();
   
    // TODO: if there is an error, make sure we clean up.
    // for example, if the client breaks the connection.
    // we need a controller listener to listen for errors.
View Full Code Here


  }
  System.err.println("!!! " + format + " didn't match any stream ");
    }

    boolean createInputDataSource(String mediaFile) {
  Processor proc = null;
  try {
      MediaLocator ml = new MediaLocator(mediaFile);
      proc = Manager.createProcessor(ml);
      if (proc != null) {
    boolean reachedState = waitForState(proc, Processor.Configured);
    if (!reachedState)
        return false;
       
    TrackControl [] tracks = proc.getTrackControls();
    // get the formats, check the type and set the new
    // formats. Realize the proc and get the datasource
    // Set the local variables with datasource/stream/format
    // information
    for (int i = 0; i < tracks.length; i++) {
        Format format = tracks[i].getFormat();
        Format [] supported = tracks[i].getSupportedFormats();
        if (format instanceof AudioFormat) {
            for (int j = 0; j < supported.length; j++) {
                if (supported[j].matches(preferredAudioFormat)) {
                    preferredAudioFormat = (AudioFormat) supported[j];
                    break;
                }
            }
      tracks[i].setFormat(preferredAudioFormat);
        } else if (format instanceof VideoFormat) {
      Dimension size = ((VideoFormat)format).getSize();
      if (MiniME.getSaveType() == 1) {
         
          preferredVideoFormat = new RGBFormat(size,
                 size.width * size.height * 3,
                 Format.byteArray,
                 ((VideoFormat)format).getFrameRate(),
                 24,
                 1, 2, 3);
      } else {
          preferredVideoFormat = new RGBFormat(size,
                 size.width * size.height * 3,
                 Format.byteArray,
                 ((VideoFormat)format).getFrameRate(),
                 24,
                 3, 2, 1,
                 3, size.width * 3,
                 RGBFormat.TRUE,
                 Format.NOT_SPECIFIED);
      }
      for (int j = 0; j < supported.length; j++) {
          if (supported[j].matches(preferredVideoFormat)) {
              preferredVideoFormat = (RGBFormat) supported[j];
              break;
          }
      }
      tracks[i].setFormat(preferredVideoFormat);
        } else {
      System.err.println("Bad track in processor for " + mediaFile);
        }
    }
    proc.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW));
    reachedState = waitForState(proc, Controller.Realized);
    if (!reachedState)
        return false;
    DataSource ds = proc.getDataOutput();
    if (ds == null)
        return false;
    inputSourceNext = ds;
    ds.connect();
    inputStreamsNext = ((PushBufferDataSource)ds).getStreams();
View Full Code Here

      }
    }

    // Create a processor for this capturedevice & exit if we
    // cannot create it
    Processor processor = null;
    try
    {
      processor = Manager.createProcessor(new MediaLocator(urlStr));
    } catch (IOException e)
    {
      e.printStackTrace();
      System.exit(-1);
    } catch (NoProcessorException e)
    {
      e.printStackTrace();
      System.exit(-1);
    }

    // configure the processor
    processor.configure();

    while (processor.getState() != Processor.Configured)
    {
      try
      {
        Thread.sleep(100);
      } catch (InterruptedException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    processor.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW_RTP));

    TrackControl track[] = processor.getTrackControls();

    boolean encodingOk = false;

    // Go through the tracks and try to program one of them to
    // output gsm data.

    for (int i = 0; i < track.length; i++)
    {
      if (!encodingOk && track[i] instanceof FormatControl)
      {
        if (((FormatControl) track[i]).setFormat(format) == null)
        {

          track[i].setEnabled(false);
        } else
        {
          encodingOk = true;
        }
      } else
      {
        // we could not set this track to gsm, so disable it
        track[i].setEnabled(false);
      }
    }

    // At this point, we have determined where we can send out
    // gsm data or not.
    // realize the processor
    if (encodingOk)
    {
      if (!new net.sf.fmj.ejmf.toolkit.util.StateWaiter(processor).blockingRealize())
      {
        System.err.println("Failed to realize");
        return;
      }
   
//      while (processor.getState() != Processor.Realized)
//      {
//        try
//        {
//          Thread.sleep(100);
//        } catch (InterruptedException e)
//        {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//        }
//      }
      // get the output datasource of the processor and exit
      // if we fail
      DataSource ds = null;

      try
      {
        ds = processor.getDataOutput();
      } catch (NotRealizedError e)
      {
        e.printStackTrace();
        System.exit(-1);
      }

      // hand this datasource to manager for creating an RTP
      // datasink our RTP datasink will multicast the audio
      try
      {
        String url = "rtp://192.168.1.4:8000/audio/16";

        MediaLocator m = new MediaLocator(url);

        DataSink d = Manager.createDataSink(ds, m);
        d.open();
        d.start();
       
        System.out.println("Starting processor");
        processor.start();
        Thread.sleep(30000);
      } catch (Exception e)
      {
        e.printStackTrace();
        System.exit(-1);
View Full Code Here

            new Dimension(160, 120));
  buttonGo.setEnabled(false);
  try {
      sgds.connect();
      sgds.setProgressListener(this);
      Processor p = Manager.createProcessor(sgds);
      boolean success = waitForState(p, Processor.Configured);

      if (!success) {
    System.err.println("Error configuring output processor");
    buttonGo.setEnabled(true);
    return;
      }

      p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME));
      success = waitForState(p, Processor.Realized);
      if (!success) {
    System.err.println("Could not realize output processor");
    buttonGo.setEnabled(true);
      }
      DataSource ds = p.getDataOutput();
      doSave(p, ds);
  } catch (Exception ex) {
      buttonGo.setEnabled(true);
      System.err.println("Exception creating processor: " + ex);
  }
View Full Code Here

    String srcUrl = "file:///home/ken/Dev/LTI/fmj/samplemedia/gulp2.wav";
    String destUrl = "file:///home/ken/Desktop/foo.wav";
    String outputType = FileTypeDescriptor.WAVE;
   
//     CaptureDeviceInfo di = null;
      Processor p = null;
      StateHelper sh = null;
//      Vector deviceList = CaptureDeviceManager.getDeviceList(new
//               AudioFormat(AudioFormat.LINEAR, 44100, 16, 2));
//      if (deviceList.size() > 0)
//          di = (CaptureDeviceInfo)deviceList.firstElement();
//      else
//          // Exit if we can't find a device that does linear,
//            // 44100Hz, 16 bit,
//          // stereo audio.
//          System.exit(-1);
      try {
          p = Manager.createProcessor(new MediaLocator(srcUrl));
          System.out.println("Processor: " + p);
          sh = new StateHelper(p);
      } catch (IOException e) {
        e.printStackTrace();
          System.exit(-1);
      } catch (NoProcessorException e) {
        e.printStackTrace();
          System.exit(-1);
      }
      // Configure the processor
      if (!sh.configure(100000))
      {   System.err.println("Failed to configure within 100 secs");
        System.exit(-1);
      }
      // Set the output content type and realize the processor
      p.setContentDescriptor(new
                    FileTypeDescriptor(outputType));
      if (!sh.realize(100000))
      {  
       System.err.println("Failed to realize within 100 secs");
        System.exit(-1);
      }
      // get the output of the processor
      DataSource source = p.getDataOutput();
      System.out.println("p.getDataOutput()=" + source);
     System.out.println("source.getContentType()=" + source.getContentType());
      // create a File protocol MediaLocator with the location of the
      // file to which the data is to be written
      MediaLocator dest = new MediaLocator(destUrl);
      // create a datasink to do the file writing & open the sink to
      // make sure we can write to it.
      DataSink filewriter = null;
      try {
          filewriter = Manager.createDataSink(source, dest);
          System.out.println("DataSink: " + filewriter);
         System.out.println("DataSink content type: " + filewriter.getContentType());
          filewriter.open();
      } catch (NoDataSinkException e) {
        e.printStackTrace();
          System.exit(-1);
      } catch (IOException e) {
       e.printStackTrace();
          System.exit(-1);
      } catch (SecurityException e) {
       e.printStackTrace();
          System.exit(-1);
      }

      // if the Processor implements StreamWriterControl, we can
      // call setStreamSizeLimit
      // to set a limit on the size of the file that is written.
      StreamWriterControl swc = (StreamWriterControl)
          p.getControl("javax.media.control.StreamWriterControl");
      //set limit to 5MB
      if (swc != null)
          swc.setStreamSizeLimit(5000000);
  
      // now start the filewriter and processor
View Full Code Here

              waitFileSync.notifyAll();
            }
          }
        }
      };
    Processor processor = null;
    DataSink dataSink = null;
    try {
      processor = Manager.createProcessor(dataSource);
      processor.addControllerListener(controllerListener);
      processor.configure();
      // Put the Processor into configured state so we can set
      // some processing options on the processor.
      if (!waitForState(processor, Processor.Configured)) {
        throw new IOException("Failed to configure the processor.");
      }

      // Set the output content descriptor to QuickTime.
      processor.setContentDescriptor(new ContentDescriptor(FileTypeDescriptor.QUICKTIME));

      // Query for the processor for supported formats.
      // Then set it on the processor.
      TrackControl trackControls[] = processor.getTrackControls();
      Format format[] = trackControls [0].getSupportedFormats();
      if (format == null || format.length <= 0) {
        throw new IOException("The mux does not support the input format: " + trackControls [0].getFormat());
      }

      trackControls [0].setFormat(format [0]);

      // We are done with programming the processor. Let's just realize it.
      processor.realize();
      if (!waitForState(processor, Controller.Realized)) {
        throw new IOException("Failed to realize the processor.");
      }

      // Now, we'll need to create a DataSink.
      // Caution: do not use file.toURI().toURL() with JMF
      dataSink = Manager.createDataSink(processor.getDataOutput(),
          new MediaLocator(file.toURL()));
      dataSink.open();
      dataSink.addDataSinkListener(dataSinkListener);
      this.fileDone = false;

      // Start the actual transcoding
      processor.start();
      dataSink.start();

      // Wait for EndOfStream event.
      synchronized (this.waitFileSync) {
        while (!this.fileDone) {
          this.waitFileSync.wait();
        }
      }
     
      if (this.fileError != null) {
        throw new IOException(this.fileError);
      }
    } catch (NoProcessorException ex) {
      IOException ex2 = new IOException(ex.getMessage());
      ex2.initCause(ex);
      throw ex2;
    } catch (NoDataSinkException ex) {
      IOException ex2 = new IOException("Failed to create a DataSink for the given output MediaLocator");
      ex2.initCause(ex);
      throw ex2;
    } catch (InterruptedException ex) {
      if (dataSink != null) {
        dataSink.stop();
      }
      throw new InterruptedIOException("Video creation interrupted");
    } finally {
      if (dataSink != null) {
        dataSink.close();
        dataSink.removeDataSinkListener(dataSinkListener);
      }
      if (processor != null) {
        processor.close();
        processor.removeControllerListener(controllerListener);
      }
    }
  }
View Full Code Here

        } else {
            Image image = _jmfImageToken.asAWTImage();
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            ImageDataSource imageDataSource = new ImageDataSource(width, height);
            Processor processor;

            try {
                processor = Manager.createProcessor(imageDataSource);
            } catch (Exception ex) {
                throw new IllegalActionException(this, ex,
                        "Can't create processor");
            }

            processor.addControllerListener(this);

            processor.configure();

            if (!_waitForState(processor, Processor.Configured)) {
                throw new IllegalActionException(
                        "Failed to configure processor.");
            }

            if (_fileType == _QUICKTIME) {
                processor.setContentDescriptor(new ContentDescriptor(
                        FileTypeDescriptor.QUICKTIME));
            } else if (_fileType == _AVI) {
                processor.setContentDescriptor(new ContentDescriptor(
                        FileTypeDescriptor.MSVIDEO));
            } else if (_fileType == _MPEG) {
                processor.setContentDescriptor(new ContentDescriptor(
                        FileTypeDescriptor.MPEG));
            } else {
                throw new InternalErrorException("type = " + _fileType
                        + ", which is not one of " + _QUICKTIME
                        + "(QUICKTIME), " + _AVI + "(AVI) or " + _MPEG
                        + "(MPEG).");
            }

            TrackControl[] trackControl = processor.getTrackControls();
            Format[] format = trackControl[0].getSupportedFormats();

            if ((format == null) || (format.length <= 0)) {
                throw new IllegalActionException("Cannot support input format");
            }

            trackControl[0].setFormat(format[0]);

            processor.realize();

            if (!_waitForState(processor, Controller.Realized)) {
                throw new IllegalActionException("Failed to realize processor");
            }

            DataSource dataSource = processor.getDataOutput();

            if (dataSource == null) {
                throw new IllegalActionException("Processor does not have "
                        + "output DataSource");
            }

            if (_debugging) {
                _debug(_fileRoot);
            }

            DataSink dataSink;

            try {
                dataSink = Manager.createDataSink(dataSource, mediaLocator);
                dataSink.open();
            } catch (Exception ex) {
                throw new IllegalActionException(this, ex,
                        "Couldn't create the data sink");
            }

            dataSink.addDataSinkListener(this);

            try {
                processor.start();
                dataSink.start();
            } catch (IOException ex) {
                throw new IllegalActionException(this, ex,
                        "Could not start processor and datasink");
            }

            if (!_waitForFileDone()) {
                throw new IllegalActionException("Could not write the file");
            }

            try {
                dataSink.close();
            } catch (Exception ex) {
                throw new IllegalActionException(this, ex,
                        "can't close data sink");
            }

            processor.stop();
            processor.removeControllerListener(this);
            dataSink.removeDataSinkListener(this);
            processor.close();
            dataSink.close();
        }
    }
View Full Code Here

  public boolean doIt(int width, int height, int frameRate, Vector inFiles,
      MediaLocator outML) {
    ImageDataSource ids = new ImageDataSource(width, height, frameRate,
        inFiles);

    Processor p;

    try {
      //System.err
      //    .println("- create processor for the image datasource ...");
      p = Manager.createProcessor(ids);
    } catch (Exception e) {
      System.err
          .println("Yikes!  Cannot create a processor from the data source.");
      return false;
    }

    p.addControllerListener(this);

    // Put the Processor into configured state so we can set
    // some processing options on the processor.
    p.configure();
    if (!waitForState(p, p.Configured)) {
      System.err.println("Failed to configure the processor.");
      return false;
    }

    // Set the output content descriptor to QuickTime.
    p.setContentDescriptor(new ContentDescriptor(
        FileTypeDescriptor.QUICKTIME));

    // Query for the processor for supported formats.
    // Then set it on the processor.
    TrackControl tcs[] = p.getTrackControls();
    Format f[] = tcs[0].getSupportedFormats();
    if (f == null || f.length <= 0) {
      System.err.println("The mux does not support the input format: "
          + tcs[0].getFormat());
      return false;
    }

    tcs[0].setFormat(f[0]);

    //System.err.println("Setting the track format to: " + f[0]);

    // We are done with programming the processor. Let's just
    // realize it.
    p.realize();
    if (!waitForState(p, p.Realized)) {
      System.err.println("Failed to realize the processor.");
      return false;
    }

    // Now, we'll need to create a DataSink.
    DataSink dsink;
    if ((dsink = createDataSink(p, outML)) == null) {
      System.err
          .println("Failed to create a DataSink for the given output MediaLocator: "
              + outML);
      return false;
    }

    dsink.addDataSinkListener(this);
    fileDone = false;

    //System.out.println("Generating the video : "+outML.getURL().toString());

    // OK, we can now start the actual transcoding.
    try {
      p.start();
      dsink.start();
    } catch (IOException e) {
      System.err.println("IO error during processing");
      return false;
    }

    // Wait for EndOfStream event.
    waitForFileDone();

    // Cleanup.
    try {
      dsink.close();
    } catch (Exception e) {
    }
    p.removeControllerListener(this);

    //System.out.println("Video creation completed!!!!!");
    return true;
  }
View Full Code Here

TOP

Related Classes of javax.media.Processor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.