/** Crea Ftes new form PanelFilm */ public PanelFilm(URL mediaUrl) throws IOException { initComponents(); setLayout(new BorderLayout()); Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true); drawFilm = new JPanel(); try { mediaPlayer = Manager.createRealizedPlayer(mediaUrl); System.out.println(Manager.createRealizedPlayer(mediaUrl).toString()); video = mediaPlayer.getVisualComponent(); System.out.println(mediaPlayer.getVisualComponent()); controls = mediaPlayer.getControlPanelComponent(); if (video != null) { drawFilm.add(video, BorderLayout.CENTER); } if (controls != null) { drawFilm.add(controls, BorderLayout.SOUTH); } this.setVisible(false); drawFilm.setVisible(true); mediaPlayer.start(); } catch (NoPlayerException noPlayerException) { noPlayerException.printStackTrace(); } catch (CannotRealizeException cannotRealizeException) { cannotRealizeException.printStackTrace(); } }
public MediaPanel(URL mediaURL) { setLayout(new BorderLayout()); // use a BorderLayout // Use lightweight components for Swing compatibility Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true); try { // create a player to play the media specified in the URL Player mediaPlayer = Manager.createRealizedPlayer(mediaURL); // get the components for the video and the playback controls Component video = mediaPlayer.getVisualComponent(); Component controls = mediaPlayer.getControlPanelComponent(); if (video != null) add(video, BorderLayout.CENTER); // add video component if (controls != null) add(controls, BorderLayout.SOUTH); // add controls mediaPlayer.start(); // start playing the media clip } // end try catch (NoPlayerException noPlayerException) { System.err.println("No media player found"); } // end catch catch (CannotRealizeException cannotRealizeException) { System.err.println("Could not realize media player"); } // end catch catch (IOException iOException) { System.err.println("Error reading from the source"); } // end catch } // end MediaPanel constructor
// Constructor public MediaPlayer(URL mediauUrl) { setLayout(new BorderLayout()); // use a BorderLayout // Use lightweight components for Swing compatibility Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true); try { // create a player to play the media specified in the URL Player mediaPlayer = Manager.createRealizedPlayer(mediauUrl); // get the components for the video and the playback controls Component video = mediaPlayer.getVisualComponent(); Component control = mediaPlayer.getControlPanelComponent(); if (video != null) { add(video, BorderLayout.CENTER); // place the video component in the panel } if (control != null) { add(control, BorderLayout.SOUTH); // place the control component in the panel } mediaPlayer.start(); // start playing the media clip } catch (NoPlayerException noPlayerException) { System.err.println("No media player found: " + noPlayerException); } catch (CannotRealizeException cannotRealizeException) { System.err.println("Could not realize media player: " + cannotRealizeException); } catch (IOException iOException) { System.err.println("Error reading from the source: " + iOException); } }
public void startProcessing() { try { processor = Manager.createProcessor(getMainCamSource()); } catch (IOException e) { // JOptionPane.showMessageDialog(parent, // "IO Exception creating processor: " + e.getMessage(), "Error", // JOptionPane.WARNING_MESSAGE); return; } catch (NoProcessorException e) { // JOptionPane.showMessageDialog(parent, // "Exception creating processor: " + e.getMessage(), "Error", // JOptionPane.WARNING_MESSAGE); return; } CamStateHelper playhelper = new CamStateHelper(processor); if (!playhelper.configure(10000)) { JOptionPane.showMessageDialog( parent, "cannot configure processor", "Error", JOptionPane.WARNING_MESSAGE); return; } processor.setContentDescriptor(null); if (!playhelper.realize(10000)) { JOptionPane.showMessageDialog( parent, "cannot realize processor", "Error", JOptionPane.WARNING_MESSAGE); return; } // In order for or your clones to start, you must start the original source processor.start(); setProcessing(true); }
public void setMainSource() { setProcessing(false); VideoFormat vidformat = new VideoFormat(VideoFormat.RGB); Vector devices = CaptureDeviceManager.getDeviceList(vidformat); CaptureDeviceInfo di = null; if (devices.size() > 0) di = (CaptureDeviceInfo) devices.elementAt(0); else { JOptionPane.showMessageDialog( parent, "Your camera is not connected", "No webcam found", JOptionPane.WARNING_MESSAGE); return; } try { ml = di.getLocator(); setMainCamSource(Manager.createDataSource(ml)); } catch (Exception e) { JOptionPane.showMessageDialog( parent, "Exception locating media: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE); return; } }
// Load decodes the track and creates a Realized Player public void load(String filename) { Decoder p1 = new Decoder(); try { p1.decode(filename, track.getPath()); player = Manager.createRealizedPlayer(track.toURL()); } catch (Exception e) { e.printStackTrace(); } }
/** * Initializes the decoder. The file will be opened, decoding will start, and the first frame of * the movie will be sent to the handlers. * * @param mediaFile - the URL of the media file to be loaded */ public void init(String mediaFile) { try { java.net.URL url = new java.net.URL(mediaFile); processor = Manager.createProcessor(url); processor.addControllerListener(this); processor.configure(); } catch (Exception e) { // Debug.trace("Movie player exception "+e); } }
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. return in; }
public Capture_video() { try { // List out available Devices to Capture Video. Vector<CaptureDeviceInfo> list = CaptureDeviceManager.getDeviceList(format); System.out.println(CaptureDeviceManager.getDeviceList(null).toString()); // Iterating list for (CaptureDeviceInfo temp : list) { // Checking whether the current device supports VfW // VfW = Video for Windows if (temp.getName().startsWith("vfw:")) { System.out.println("Found : " + temp.getName().substring(4)); // Selecting the very first device that supports VfW cam = temp; System.out.println("Selected : " + cam.getName().substring(4)); break; } } System.out.println("Put it on work!..."); // Getting the MediaLocator for Selected device. // MediaLocator describes the location of media content locator = cam.getLocator(); if (locator != null) { // Create a Player for Media Located by MediaLocator player = Manager.createRealizedPlayer(locator); if (player != null) { // Starting the player player.start(); // Creating a Frame to display Video f.setTitle("Test Webcam"); f.setLayout(new BorderLayout()); // Adding the Visual Component to display Video captured by Player // from URL provided by MediaLocator f.add(player.getVisualComponent(), BorderLayout.CENTER); f.pack(); f.setAlwaysOnTop(true); f.setVisible(true); } } } catch (Exception e) { e.printStackTrace(); } }
/** {@inheritDoc} */ @Override public DataSource createDataSource(MediaLocator mediaLocator) throws DataSourceCreationException { DataSource dataSource = null; try { dataSource = Manager.createDataSource(mediaLocator); } catch (NoDataSourceException ex) { throw new DataSourceCreationException("Couldn't create DataSource", ex); } catch (IOException ex) { throw new DataSourceCreationException("IOException creating DataSource", ex); } return dataSource; }
/** * Restarts the recording for a specific SSRC. * * @param ssrc the SSRC for which to restart recording. RTP packet of the new recording). */ private void resetRecording(long ssrc, long timestamp) { ReceiveStreamDesc receiveStream = findReceiveStream(ssrc); // we only restart audio recordings if (receiveStream != null && receiveStream.format instanceof AudioFormat) { String newFilename = getNextFilename(path + "/" + ssrc, AUDIO_FILENAME_SUFFIX); // flush the buffer contained in the MP3 encoder String s = "trying to flush ssrc=" + ssrc; Processor p = receiveStream.processor; if (p != null) { s += " p!=null"; for (TrackControl tc : p.getTrackControls()) { Object o = tc.getControl(FlushableControl.class.getName()); if (o != null) ((FlushableControl) o).flush(); } } if (logger.isInfoEnabled()) { logger.info("Restarting recording for SSRC=" + ssrc + ". New filename: " + newFilename); } receiveStream.dataSink.close(); receiveStream.dataSink = null; // flush the FMJ jitter buffer // DataSource ds = receiveStream.receiveStream.getDataSource(); // if (ds instanceof net.sf.fmj.media.protocol.rtp.DataSource) // ((net.sf.fmj.media.protocol.rtp.DataSource)ds).flush(); receiveStream.filename = newFilename; try { receiveStream.dataSink = Manager.createDataSink( receiveStream.dataSource, new MediaLocator("file:" + newFilename)); } catch (NoDataSinkException ndse) { logger.warn("Could not reset recording for SSRC=" + ssrc + ": " + ndse); removeReceiveStream(receiveStream, false); } try { receiveStream.dataSink.open(); receiveStream.dataSink.start(); } catch (IOException ioe) { logger.warn("Could not reset recording for SSRC=" + ssrc + ": " + ioe); removeReceiveStream(receiveStream, false); } audioRecordingStarted(ssrc, timestamp); } }
private void initWebcam() { Vector videoDevices = CaptureDeviceManager.getDeviceList(new VideoFormat(null)); if (videoDevices.size() > 0) { webcam = (CaptureDeviceInfo) videoDevices.get(0); Format[] formats = webcam.getFormats(); Format selectedFormat = null; try { for (Format f : formats) { if (f.toString().contains("640") && f.toString().contains("480")) { selectedFormat = f; break; } } } catch (Exception e) { logger.error( "Failed to get required webcam resolution (640x480). Taking default format: " + e.getMessage()); selectedFormat = formats[0]; } try { webcamPlayer = Manager.createRealizedPlayer(webcam.getLocator()); FormatControl fc = (FormatControl) webcamPlayer.getControl("javax.media.control.FormatControl"); System.out.println("Selected webcam format: " + selectedFormat.toString()); fc.setFormat(selectedFormat); webcamPlayer.start(); webcamAvailable = true; Timer timer = new Timer(); timer.schedule( new TimerTask() { @Override public void run() { frameGrabber = (FrameGrabbingControl) webcamPlayer.getControl("javax.media.control.FrameGrabbingControl"); } }, 2500); } catch (Exception e) { logger.error("Failed to get webcam feed: " + e.getMessage()); } } else { logger.error("No webcam available"); } }
/** * Creates a video stream of the interactions with the given component at the specified frame rate * and saves it as the given file. */ public static Processor recordVideo(Component component, int fps, File dest) { if (component == null) return null; Dimension size = component.getSize(); Point location = component.getLocationOnScreen(); String loc = MessageFormat.format( "screen://{0},{1},{2},{3}/{4}", new Object[] { new Integer(location.x), new Integer(location.y), new Integer(size.width - size.width % 8), new Integer(size.height - size.height % 8), new Integer(fps) }); MediaLocator mSource = new MediaLocator(loc); Processor p = null; try { DataSource data = Manager.createDataSource(mSource); p = Manager.createProcessor(mSource); waitForState(p, Processor.Configured); p.setContentDescriptor(new FileTypeDescriptor("video.x_msvideo")); p.getTrackControls()[0].setFormat(new VideoFormat(VideoFormat.MJPG)); waitForState(p, Processor.Realized); DataSource data2 = p.getDataOutput(); String path = dest.getParent(); String name = dest.getName(); if (!name.toLowerCase().endsWith(".avi")) name += ".avi"; MediaLocator mediaDest = new MediaLocator(new File(path, name).toURL()); DataSink dataSink = Manager.createDataSink(data2, mediaDest); dataSink.open(); dataSink.start(); p.start(); } catch (Exception ex) { ex.printStackTrace(); p = null; } return p; }
public void player_begin(URL url) { if (h4JmfPlugin.playMP3 != null) { logger.severe("playMP3!=null"); h4JmfPlugin.cnsl.append("playMP3!=null"); return; } if (url == null) { h4JmfPlugin.cnsl.append("url==null"); return; } MediaLocator mediaLocator = new MediaLocator(url); try { // final JPanel jpnl_this=this; DataSource ds = Manager.createDataSource(mediaLocator); // cnsl.append("ds="+ds); h4JmfPlugin.playMP3 = Manager.createPlayer(ds); /** * ********************************************************** ControllerListener moved to * outer class addControllerListener done in h4JmfPlugin ********************************** */ h4JmfPlugin.player_begin(); } catch (Exception e) { logger.severe(e.getMessage()); h4JmfPlugin.cnsl.append(e); return; } // h4JmfPlugin.playMP3.realize(); // logger.info("after realize()"); // but possible [JMF thread: com.sun.media.PlaybackEngine@1ac13d7[ // com.sun.media.PlaybackEngine@1ac13d7 ] ( realizeThread)] [error] PlaybackEngine@1ac13d7 ] ( // realizeThread): Unable to handle format: mpeglayer3, 16000.0 Hz, 16-bit, Mono, // LittleEndian, Signed, 2000.0 frame rate, FrameSize=16384 bits // running tshvr under hedwig :11:17:08 PM [JMF thread: // com.sun.media.content.unknown.Handler@8c7be5 ( prefetchThread)] [error] Handler@8c7be5 ( // prefetchThread): Error: Unable to prefetch com.sun.media.PlaybackEngine@6d3b92 } // player_begin
/** Finds a camera and sets it up */ void fetchDeviceDataSource() { CaptureDeviceInfo CapDevice = (CaptureDeviceInfo) camCapDevice.elementAt(formatIdx); Format CapFormat = (Format) camCapFormat.elementAt(formatIdx); MediaLocator loc = CapDevice.getLocator(); try { dataSource = Manager.createDataSource(loc); } catch (Exception e) { } try { // ensures 30 fps or as otherwise preferred FormatControl formCont = ((CaptureDevice) dataSource).getFormatControls()[0]; VideoFormat formatVideoNew = new VideoFormat(null, null, -1, null, (float) cameraFPS); formCont.setFormat(CapFormat.intersects(formatVideoNew)); } catch (Exception e) { } }
public void update(ReceiveStreamEvent e) { if (e instanceof NewReceiveStreamEvent) { ReceiveStream rs = ((NewReceiveStreamEvent) e).getReceiveStream(); DataSource ds = rs.getDataSource(); try { try { player = Manager.createPlayer(ds); } catch (IOException e1) { e1.printStackTrace(); } } catch (NoPlayerException e1) { e1.printStackTrace(); } player.addControllerListener(this); player.start(); } else if (e instanceof ByeEvent) { disconnect(); } }
public JMFMovie(File file) { try { player = Manager.createPlayer(file.toURI().toURL()); framePositioningControl = (FramePositioningControl) player.getControl(FramePositioningControl.class.toString()); frameGrabbingControl = (FrameGrabbingControl) player.getControl(FrameGrabbingControl.class.toString()); imageConverter = new BufferToImage( (VideoFormat) ((FormatControl) player.getControl(FormatControl.class.toString())).getFormat()); player.realize(); } catch (NoPlayerException e) { throw new RuntimeException(e); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
/** @param args */ public static void main(String[] args) { String url = "rtp://192.168.1.1:22224/audio/16"; MediaLocator mrl = new MediaLocator(url); // Create a player for this rtp session Player player = null; try { player = Manager.createPlayer(mrl); } catch (NoPlayerException e) { e.printStackTrace(); System.exit(-1); } catch (MalformedURLException e) { e.printStackTrace(); System.exit(-1); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } if (player != null) { System.out.println("Player created."); player.realize(); // wait for realizing while (player.getState() != Controller.Realized) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Starting player"); player.start(); } else { System.err.println("Player doesn't created."); System.exit(-1); } System.out.println("Exiting."); }
/** Create the DataSink. */ DataSink createDataSink(Processor p, MediaLocator outML) { DataSource ds; if ((ds = p.getDataOutput()) == null) { System.err.println( "Something is really wrong: the processor does not have an output DataSource"); return null; } DataSink dsink; try { System.err.println("- Create DataSink for: " + outML); dsink = Manager.createDataSink(ds, outML); dsink.open(); } catch (Exception e) { System.err.println("Cannot create the DataSink: " + e); return null; } return dsink; }
public static void main(String args[]) throws Exception { // Take the path of the audio file from command line File f = new File("C:\\Users\\Larry\\AppData\\Local\\Temp\\jgoogle_tts-588881786930740869.mp3"); // Create a Player object that realizes the audio final Player p = Manager.createRealizedPlayer(f.toURI().toURL()); // Start the music p.start(); // Create a Scanner object for taking input from cmd Scanner s = new Scanner(System.in); // Read a line and store it in st String st = s.nextLine(); // If user types 's', stop the audio if (st.equals("s")) { p.stop(); } }
public void capture(String device) { CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice(device); System.out.println("-----------------------------------------------------------"); System.out.println("CaptureDevice Name is " + deviceInfo.getName()); System.out.println("-----------------------------------------------------------"); System.out.println("CaptureDevice is " + deviceInfo); System.out.println("-----------------------------------------------------------"); Format[] formatsSupported = deviceInfo.getFormats(); System.out.println("Supports " + formatsSupported.length + " formats"); for (int findex = 0; findex < formatsSupported.length; findex++) { System.out.println("Unique encoding name is " + formatsSupported[findex].getEncoding()); System.out.println("Format attributes are " + formatsSupported[findex]); } System.out.println("-----------------------------------------------------------"); System.out.println("Media Locator is " + deviceInfo.getLocator()); System.out.println("***********************************************************"); try { player = Manager.createPlayer(deviceInfo.getLocator()); } catch (java.io.IOException e) { System.out.println("IOException"); } catch (javax.media.NoPlayerException npe) { System.out.println("NoPlayerException"); } player.addControllerListener(this); System.out.println("About to call start on player"); player.realize(); waitForState(player.Realized); addVisualElements(); setVisible(true); player.start(); System.out.println("Called start on player"); waitForState(player.Started); System.out.println("Player started"); }
// Creates an RTP transmit data sink. This is the easiest way to create // an RTP transmitter. The other way is to use the RTPSessionManager API. // Using an RTP session manager gives you more control if you wish to // fine tune your transmission and set other parameters. private String createTransmitter() { // Create a media locator for the RTP data sink. // For example: // rtp://129.130.131.132:42050/video String rtpURL = "rtp://" + ipAddress + ":" + port + "/video"; MediaLocator outputLocator = new MediaLocator(rtpURL); // Create a data sink, open it and start transmission. It will wait // for the processor to start sending data. So we need to start the // output data source of the processor. We also need to start the // processor itself, which is done after this method returns. try { rtptransmitter = Manager.createDataSink(dataOutput, outputLocator); rtptransmitter.open(); rtptransmitter.start(); dataOutput.start(); } catch (MediaException me) { return "Couldn't create RTP data sink"; } catch (IOException ioe) { return "Couldn't create RTP data sink"; } return null; }
protected void initPlayer(URL url) { try { // clear all previous player and associated controls if (mediaPlayer != null) { removeAll(); mediaPlayer.stop(); mediaPlayer.deallocate(); mediaPlayer = null; gain = null; } sendEvent("loadstart"); mediaPlayer = Manager.createPlayer(url); mediaPlayer.addControllerListener( new ControllerListener() { @Override public void controllerUpdate(ControllerEvent e) { if (e instanceof RealizeCompleteEvent) { Component video = mediaPlayer.getVisualComponent(); if (video != null) { add(video, BorderLayout.CENTER); } Component controls = null; String showControls = getParameter("controls"); if (showControls != null && showControls.equals("true")) { controls = mediaPlayer.getControlPanelComponent(); } if (controls != null) { add(controls, BorderLayout.SOUTH); } gain = mediaPlayer.getGainControl(); gain.addGainChangeListener( new GainChangeListener() { @Override public void gainChange(GainChangeEvent e) { sendEvent("volumechange"); } }); validate(); } else if (e instanceof PrefetchCompleteEvent) { sendEvent("loadeddata"); sendEvent("durationchange"); } else if (e instanceof DurationUpdateEvent) { sendEvent("durationchange"); } else if (e instanceof MediaTimeSetEvent) { sendEvent("timeupdate"); } else if (e instanceof RateChangeEvent) { sendEvent("ratechange"); } else if (e instanceof StopTimeChangeEvent) { // Do something? } else if (e instanceof StartEvent) { playerStatus = Status.PLAYING; sendEvent("play"); sendEvent("playing"); } else if (e instanceof EndOfMediaEvent) { playerStatus = Status.ENDED; sendEvent("pause"); sendEvent("ended"); } else if (e instanceof ControllerClosedEvent) { playerStatus = Status.ENDED; sendEvent("pause"); sendEvent("ended"); } else if (e instanceof RestartingEvent) { // Do something? } else if (e instanceof StopAtTimeEvent) { playerStatus = Status.PAUSED; sendEvent("pause"); } else if (e instanceof StopByRequestEvent) { playerStatus = Status.PAUSED; sendEvent("pause"); } else if (e instanceof StopEvent) { playerStatus = Status.PAUSED; sendEvent("pause"); } } }); playerStatus = Status.INITIALIZED; } catch (NoPlayerException ex) { System.err.println("No media player found: " + ex.getMessage()); } catch (IOException ex) { System.err.println("Error reading from the source: " + ex.getMessage()); } }
public static void main(String[] args) { // ---------------- CUT HERE START ----------------- // Format formats[] = new Format[2]; formats[0] = new AudioFormat(AudioFormat.IMA4); formats[1] = new VideoFormat(VideoFormat.CINEPAK); FileTypeDescriptor outputType = new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME); Processor p = null; try { p = Manager.createRealizedProcessor(new ProcessorModel(formats, outputType)); } catch (IOException e) { System.exit(-1); } catch (NoProcessorException e) { System.exit(-1); } catch (CannotRealizeException e) { System.exit(-1); } // get the output of the processor DataSource source = p.getDataOutput(); // create a File protocol MediaLocator with the location of the file to // which bits are to be written MediaLocator dest = new MediaLocator("file://foo.mov"); // 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); filewriter.open(); } catch (NoDataSinkException e) { System.exit(-1); } catch (IOException e) { System.exit(-1); } catch (SecurityException e) { System.exit(-1); } // now start the filewriter and processor try { filewriter.start(); } catch (IOException e) { System.exit(-1); } p.start(); // stop and close the processor when done capturing... // close the datasink when EndOfStream event is received... // ----------------- CUT HERE END ---------------- // try { Thread.currentThread().sleep(4000); } catch (InterruptedException ie) { } p.stop(); p.close(); try { Thread.currentThread().sleep(1000); } catch (InterruptedException ie) { } filewriter.close(); try { Thread.currentThread().sleep(4000); } catch (InterruptedException ie) { } System.exit(0); }
/** * Implements {@link ReceiveStreamListener#update(ReceiveStreamEvent)}. * * <p>{@link #rtpManager} will use this to notify us of <tt>ReceiveStreamEvent</tt>s. */ @Override public void update(ReceiveStreamEvent event) { if (event == null) return; ReceiveStream receiveStream = event.getReceiveStream(); if (event instanceof NewReceiveStreamEvent) { if (receiveStream == null) { logger.warn("NewReceiveStreamEvent: null"); return; } final long ssrc = getReceiveStreamSSRC(receiveStream); ReceiveStreamDesc receiveStreamDesc = findReceiveStream(ssrc); if (receiveStreamDesc != null) { String s = "NewReceiveStreamEvent for an existing SSRC. "; if (receiveStream != receiveStreamDesc.receiveStream) s += "(but different ReceiveStream object)"; logger.warn(s); return; } else receiveStreamDesc = new ReceiveStreamDesc(receiveStream); if (logger.isInfoEnabled()) logger.info("New ReceiveStream, ssrc=" + ssrc); // Find the format of the ReceiveStream DataSource dataSource = receiveStream.getDataSource(); if (dataSource instanceof PushBufferDataSource) { Format format = null; PushBufferDataSource pbds = (PushBufferDataSource) dataSource; for (PushBufferStream pbs : pbds.getStreams()) { if ((format = pbs.getFormat()) != null) break; } if (format == null) { logger.error("Failed to handle new ReceiveStream: " + "Failed to determine format"); return; } receiveStreamDesc.format = format; } else { logger.error("Failed to handle new ReceiveStream: " + "Unsupported DataSource"); return; } int rtpClockRate = -1; if (receiveStreamDesc.format instanceof AudioFormat) rtpClockRate = (int) ((AudioFormat) receiveStreamDesc.format).getSampleRate(); else if (receiveStreamDesc.format instanceof VideoFormat) rtpClockRate = 90000; getSynchronizer().setRtpClockRate(ssrc, rtpClockRate); // create a Processor and configure it Processor processor = null; try { processor = Manager.createProcessor(receiveStream.getDataSource()); } catch (NoProcessorException npe) { logger.error("Failed to create Processor: ", npe); return; } catch (IOException ioe) { logger.error("Failed to create Processor: ", ioe); return; } if (logger.isInfoEnabled()) logger.info("Created processor for SSRC=" + ssrc); processor.addControllerListener(this); receiveStreamDesc.processor = processor; final int streamCount; synchronized (receiveStreams) { receiveStreams.add(receiveStreamDesc); streamCount = receiveStreams.size(); } /* * XXX TODO IRBABOON * This is a terrible hack which works around a failure to realize() * some of the Processor-s for audio streams, when multiple streams * start nearly simultaneously. The cause of the problem is currently * unknown (and synchronizing all FMJ calls in RecorderRtpImpl * does not help). * XXX TODO NOOBABRI */ if (receiveStreamDesc.format instanceof AudioFormat) { final Processor p = processor; new Thread() { @Override public void run() { // delay configuring the processors for the different // audio streams to decrease the probability that they // run together. try { int ms = 450 * (streamCount - 1); logger.warn( "Sleeping for " + ms + "ms before" + " configuring processor for SSRC=" + ssrc + " " + System.currentTimeMillis()); Thread.sleep(ms); } catch (Exception e) { } p.configure(); } }.run(); } else { processor.configure(); } } else if (event instanceof TimeoutEvent) { if (receiveStream == null) { // TODO: we might want to get the list of ReceiveStream-s from // rtpManager and compare it to our list, to see if we should // remove a stream. logger.warn("TimeoutEvent: null."); return; } // FMJ silently creates new ReceiveStream instances, so we have to // recognize them by the SSRC. ReceiveStreamDesc receiveStreamDesc = findReceiveStream(getReceiveStreamSSRC(receiveStream)); if (receiveStreamDesc != null) { if (logger.isInfoEnabled()) { logger.info("ReceiveStream timeout, ssrc=" + receiveStreamDesc.ssrc); } removeReceiveStream(receiveStreamDesc, true); } } else if (event != null && logger.isInfoEnabled()) { logger.info("Unhandled ReceiveStreamEvent (" + event.getClass().getName() + "): " + event); } }
public static void main(String[] args) { if (args.length != 2) { System.out.println("Usage: rtpaudio <targetIP> <targetPort>"); System.exit(0); } try { RegistryDefaults.setDefaultFlags(RegistryDefaults.FMJ); // create a clean registry RegistryDefaults.unRegisterAll(RegistryDefaults.ALL); RegistryDefaults.registerAll(RegistryDefaults.FMJ); // remove all capture devices Vector deviceList = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); for (int i = 0; i < deviceList.size(); i++) { CaptureDeviceInfo cdi = (CaptureDeviceInfo) deviceList.elementAt(i); CaptureDeviceManager.removeDevice(cdi); } // update capture device list new net.sf.fmj.media.cdp.javasound.CaptureDevicePlugger().addCaptureDevices(); PlugInManager.commit(); deviceList = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); if ((null == deviceList) || (deviceList.size() == 0)) { System.out.println("### ERROR found no audio capture device"); System.exit(0); } // enumerate all codec Vector codecList = PlugInManager.getPlugInList(null, null, PlugInManager.CODEC); System.out.println("found " + codecList.size() + " codec"); for (int i = 0; i < codecList.size(); i++) { String aCodecClass = (String) codecList.elementAt(i); System.out.println("# " + (i + 1) + " " + aCodecClass); } // fetch first available audio capture device deviceList = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); CaptureDeviceInfo captureDeviceInfo = (CaptureDeviceInfo) deviceList.elementAt(0); System.out.println("### using " + captureDeviceInfo.getName()); System.out.println("### locator " + captureDeviceInfo.getLocator()); javax.media.protocol.DataSource dataSource = javax.media.Manager.createDataSource( new javax.media.MediaLocator(captureDeviceInfo.getLocator().toString())); // javax.media.protocol.DataSource dataSource = // javax.media.Manager.createDataSource(new // javax.media.MediaLocator("javasound://")); System.out.println("### created datasource " + dataSource.getClass().getName()); javax.media.control.FormatControl[] formatControls = ((javax.media.protocol.CaptureDevice) dataSource).getFormatControls(); System.out.println("got format control " + formatControls[0].getClass().getName()); System.out.println("current format is " + formatControls[0].getFormat()); // set audio capture format javax.media.Format[] formats = formatControls[0].getSupportedFormats(); for (int i = 0; i < formats.length; i++) { javax.media.format.AudioFormat af = (javax.media.format.AudioFormat) formats[i]; if ((af.getChannels() == 1) && (af.getSampleSizeInBits() == 16)) { if (af.getSampleRate() == Format.NOT_SPECIFIED) { javax.media.format.AudioFormat newAudioFormat = new javax.media.format.AudioFormat( af.getEncoding(), 8000.0f, javax.media.Format.NOT_SPECIFIED, javax.media.Format.NOT_SPECIFIED); // javax.media.format.AudioFormat newAudioFormat = new // javax.media.format.AudioFormat(af.getEncoding(), // 44100.0f, javax.media.Format.NOT_SPECIFIED, // javax.media.Format.NOT_SPECIFIED); formatControls[0].setFormat(newAudioFormat.intersects(af)); break; } } } System.out.println("current format is now " + formatControls[0].getFormat()); FrameProcessingControl fpc = null; // adujst recording buffer ( to adjust latency ) dataSource.stop(); Object[] controls = dataSource.getControls(); for (int i = 0; i < controls.length; i++) { String className = controls[i].getClass().getName(); if (-1 != className.indexOf("JavaSoundBufferControl")) { javax.media.control.BufferControl bc = (javax.media.control.BufferControl) controls[i]; System.out.println( "### current javasound buffer length is " + bc.getBufferLength() + " ms"); bc.setBufferLength(40); System.out.println( "### current javasound buffer length is " + bc.getBufferLength() + " ms"); } else if (-1 != className.indexOf("JitterBufferControl")) { javax.media.control.BufferControl bc = (javax.media.control.BufferControl) controls[i]; System.out.println("### current jitter buffer length is " + bc.getBufferLength() + " ms"); bc.setBufferLength(80); System.out.println("### current jitter buffer length is " + bc.getBufferLength() + " ms"); } else if (-1 != className.indexOf("FPC")) { fpc = (FrameProcessingControl) controls[i]; System.out.println("### found bitrate control " + fpc.getClass()); } } dataSource.start(); // create processor javax.media.Processor processor = javax.media.Manager.createProcessor(dataSource); System.out.println("### created processor " + processor.getClass().getName()); processor.configure(); for (int idx = 0; idx < 100; idx++) { if (processor.getState() == Processor.Configured) { break; } Thread.sleep(100); } System.out.println("### processor state " + processor.getState()); processor.setContentDescriptor( new javax.media.protocol.ContentDescriptor(ContentDescriptor.RAW_RTP)); javax.media.control.TrackControl[] tracks = processor.getTrackControls(); // /tracks[0].setFormat(new // javax.media.format.AudioFormat(javax.media.format.AudioFormat.ULAW_RTP, // 8000, 8, 1)); tracks[0].setFormat( new javax.media.format.AudioFormat(javax.media.format.AudioFormat.GSM_RTP, 8000, 8, 1)); processor.realize(); for (int idx = 0; idx < 100; idx++) { if (processor.getState() == Controller.Realized) { break; } Thread.sleep(100); } System.out.println("### processor state " + processor.getState()); javax.media.protocol.DataSource dataOutput = processor.getDataOutput(); System.out.println("### processor data output " + dataOutput.getClass().getName()); // BitRateControl BitRateControl bitrateControl = null; Object[] controls2 = dataOutput.getControls(); for (int i = 0; i < controls2.length; i++) { if (controls2[i] instanceof BitRateControl) { bitrateControl = (BitRateControl) controls2[i]; System.out.println("### found bitrate control " + bitrateControl.getClass()); break; } } // PacketSizeControl Object[] controls3 = processor.getControls(); for (int i = 0; i < controls3.length; i++) { if (controls3[i] instanceof PacketSizeControl) { PacketSizeControl psc = (PacketSizeControl) controls3[i]; System.out.println("### current packetsize is " + psc.getPacketSize() + " bytes"); psc.setPacketSize(66); System.out.println("### current packetsize is " + psc.getPacketSize() + " bytes"); break; } } // enumerate all controls of the processor Object[] pcontrols = processor.getControls(); for (int i = 0; i < pcontrols.length; i++) { System.out.println("processor control " + i + " " + pcontrols[i]); } javax.media.rtp.RTPManager rtpManager = javax.media.rtp.RTPManager.newInstance(); javax.media.rtp.SessionAddress local = new javax.media.rtp.SessionAddress( InetAddress.getLocalHost(), Integer.valueOf(args[1]).intValue()); javax.media.rtp.SessionAddress target = new javax.media.rtp.SessionAddress( InetAddress.getByName(args[0]), Integer.valueOf(args[1]).intValue()); rtpManager.initialize(local); rtpManager.addTarget(target); javax.media.rtp.SendStream sendStream = rtpManager.createSendStream(dataOutput, 0); sendStream.start(); processor.start(); Thread.sleep(1000); System.out.println("\n>>>>>> TRANSMITTING ULAW/RTP AUDIO NOW"); while (2 > 1) { Thread.sleep(1000); if (null != bitrateControl) { TransmissionStats stats = sendStream.getSourceTransmissionStats(); System.out.println( "rtp audio send: bitrate=" + bitrateControl.getBitRate() + " (pdu=" + stats.getPDUTransmitted() + " bytes=" + stats.getBytesTransmitted() + " overrun=" + fpc.getFramesDropped() + ")"); } } } catch (Exception ex) { ex.printStackTrace(); } System.exit(0); }
/** * 播放歌曲 * * @param songInfo */ private void playInfoMusic(SongInfo msongInfo) { this.songInfo = msongInfo; if (songInfo == null) { SongMessage msg = new SongMessage(); msg.setType(SongMessage.SERVICEERRORMUSIC); msg.setErrorMessage(SongMessage.ERRORMESSAGEPLAYSONGNULL); ObserverManage.getObserver().setMessage(msg); return; } File songFile = new File(songInfo.getFilePath()); if (!songFile.exists()) { logger.error("播放文件不存在!"); // 下一首 SongMessage songMessage = new SongMessage(); songMessage.setType(SongMessage.NEXTMUSIC); ObserverManage.getObserver().setMessage(songMessage); return; } try { if (mediaPlayer == null) { // mediaPlayer = new MediaPlayer(); // // mediaPlayer.setDataSource(songInfo.getFilePath()); // // // // float playedRate = (float) songInfo.getPlayProgress() // // / songInfo.getDuration(); // // mediaPlayer.seekTo(playedRate); // mediaPlayer.setMediaLocator(new MediaLocator(songInfo // .getFilePath())); File file = new File(songInfo.getFilePath()); mediaPlayer = Manager.createRealizedPlayer(new MediaLocator(file.toURL())); initVolume(); mediaPlayer.prefetch(); // 获取媒体数据 double totalTime = mediaPlayer.getDuration().getSeconds(); double rate = songInfo.getPlayProgress() * 1.00 / songInfo.getDuration(); mediaPlayer.setMediaTime(new Time(totalTime * rate)); mediaPlayer.prefetch(); mediaPlayer.start(); } } catch (Exception e) { logger.error("不能播放此文件:" + songInfo.getFilePath()); e.printStackTrace(); SongMessage songMessage = new SongMessage(); songMessage.setType(SongMessage.NEXTMUSIC); ObserverManage.getObserver().setMessage(songMessage); } if (mediaPlayer != null) { mediaPlayer.addControllerListener( new ControllerListener() { @Override public void controllerUpdate(ControllerEvent e) { // 当媒体播放结束时 if (e instanceof EndOfMediaEvent) { mediaPlayer.setMediaTime(new Time(0)); mediaPlayer = null; SongMessage songMessage = new SongMessage(); songMessage.setType(SongMessage.NEXTMUSIC); ObserverManage.getObserver().setMessage(songMessage); } } }); // mediaPlayer.setOnCompletionListener(new OnCompletionListener() { // // @Override // public void onProgressChanged() { // // float playedRate = mediaPlayer.getPlayedRate(); // // long playProgress = (long) (songInfo.getDuration() * playedRate); // // if (songInfo != null) { // songInfo.setPlayProgress(playProgress); // SongMessage msg = new SongMessage(); // msg.setSongInfo(songInfo); // msg.setType(SongMessage.SERVICEPLAYINGMUSIC); // ObserverManage.getObserver().setMessage(msg); // } // } // // @Override // public void onCompletion() { // // SongMessage songMessage = new SongMessage(); // songMessage.setType(SongMessage.NEXTMUSIC); // ObserverManage.getObserver().setMessage(songMessage); // } // }); } if (lrcThread == null) { lrcThread = new Thread(new LrcRunable()); lrcThread.start(); } }
/** * Implements {@link ControllerListener#controllerUpdate(ControllerEvent)}. Handles events from * the <tt>Processor</tt>s that this instance uses to transcode media. * * @param ev the event to handle. */ public void controllerUpdate(ControllerEvent ev) { if (ev == null || ev.getSourceController() == null) { return; } Processor processor = (Processor) ev.getSourceController(); ReceiveStreamDesc desc = findReceiveStream(processor); if (desc == null) { logger.warn("Event from an orphaned processor, ignoring: " + ev); return; } if (ev instanceof ConfigureCompleteEvent) { if (logger.isInfoEnabled()) { logger.info( "Configured processor for ReceiveStream ssrc=" + desc.ssrc + " (" + desc.format + ")" + " " + System.currentTimeMillis()); } boolean audio = desc.format instanceof AudioFormat; if (audio) { ContentDescriptor cd = processor.setContentDescriptor(AUDIO_CONTENT_DESCRIPTOR); if (!AUDIO_CONTENT_DESCRIPTOR.equals(cd)) { logger.error( "Failed to set the Processor content " + "descriptor to " + AUDIO_CONTENT_DESCRIPTOR + ". Actual result: " + cd); removeReceiveStream(desc, false); return; } } for (TrackControl track : processor.getTrackControls()) { Format trackFormat = track.getFormat(); if (audio) { final long ssrc = desc.ssrc; SilenceEffect silenceEffect; if (Constants.OPUS_RTP.equals(desc.format.getEncoding())) { silenceEffect = new SilenceEffect(48000); } else { // We haven't tested that the RTP timestamps survive // the journey through the chain when codecs other than // opus are in use, so for the moment we rely on FMJ's // timestamps for non-opus formats. silenceEffect = new SilenceEffect(); } silenceEffect.setListener( new SilenceEffect.Listener() { boolean first = true; @Override public void onSilenceNotInserted(long timestamp) { if (first) { first = false; // send event only audioRecordingStarted(ssrc, timestamp); } else { // change file and send event resetRecording(ssrc, timestamp); } } }); desc.silenceEffect = silenceEffect; AudioLevelEffect audioLevelEffect = new AudioLevelEffect(); audioLevelEffect.setAudioLevelListener( new SimpleAudioLevelListener() { @Override public void audioLevelChanged(int level) { activeSpeakerDetector.levelChanged(ssrc, level); } }); try { // We add an effect, which will insert "silence" in // place of lost packets. track.setCodecChain(new Codec[] {silenceEffect, audioLevelEffect}); } catch (UnsupportedPlugInException upie) { logger.warn("Failed to insert silence effect: " + upie); // But do go on, a recording without extra silence is // better than nothing ;) } } else { // transcode vp8/rtp to vp8 (i.e. depacketize vp8) if (trackFormat.matches(vp8RtpFormat)) track.setFormat(vp8Format); else { logger.error("Unsupported track format: " + trackFormat + " for ssrc=" + desc.ssrc); // we currently only support vp8 removeReceiveStream(desc, false); return; } } } processor.realize(); } else if (ev instanceof RealizeCompleteEvent) { desc.dataSource = processor.getDataOutput(); long ssrc = desc.ssrc; boolean audio = desc.format instanceof AudioFormat; String suffix = audio ? AUDIO_FILENAME_SUFFIX : VIDEO_FILENAME_SUFFIX; // XXX '\' on windows? String filename = getNextFilename(path + "/" + ssrc, suffix); desc.filename = filename; DataSink dataSink; if (audio) { try { dataSink = Manager.createDataSink(desc.dataSource, new MediaLocator("file:" + filename)); } catch (NoDataSinkException ndse) { logger.error("Could not create DataSink: " + ndse); removeReceiveStream(desc, false); return; } } else { dataSink = new WebmDataSink(filename, desc.dataSource); } if (logger.isInfoEnabled()) logger.info( "Created DataSink (" + dataSink + ") for SSRC=" + ssrc + ". Output filename: " + filename); try { dataSink.open(); } catch (IOException e) { logger.error("Failed to open DataSink (" + dataSink + ") for" + " SSRC=" + ssrc + ": " + e); removeReceiveStream(desc, false); return; } if (!audio) { final WebmDataSink webmDataSink = (WebmDataSink) dataSink; webmDataSink.setSsrc(ssrc); webmDataSink.setEventHandler(eventHandler); webmDataSink.setKeyFrameControl( new KeyFrameControlAdapter() { @Override public boolean requestKeyFrame(boolean urgent) { return requestFIR(webmDataSink); } }); } try { dataSink.start(); } catch (IOException e) { logger.error( "Failed to start DataSink (" + dataSink + ") for" + " SSRC=" + ssrc + ". " + e); removeReceiveStream(desc, false); return; } if (logger.isInfoEnabled()) logger.info("Started DataSink for SSRC=" + ssrc); desc.dataSink = dataSink; processor.start(); } else if (logger.isDebugEnabled()) { logger.debug( "Unhandled ControllerEvent from the Processor for ssrc=" + desc.ssrc + ": " + ev); } }
public void actionPerformed(ActionEvent e) { if (e.getSource() == play_time) { dure.setText(toString(lecteur.getMediaTime()) + " - " + toString(lecteur.getDuration())); deplacement.setValue( (int) (lecteur.getMediaTime().getSeconds() / lecteur.getDuration().getSeconds() * 100)); if (lecteur.getMediaTime().getSeconds() == lecteur.getDuration().getSeconds()) { lecteur.stop(); lecteur.close(); lecteur = null; deplacement.setValue(0); dure.setText("00:00 - " + toString(lecteur.getDuration())); play_time.stop(); } } if (e.getSource() == open) { fc = new JFileChooser(); fc.setAcceptAllFileFilterUsed(false); fc.showOpenDialog(this); if (fc.getDialogType() == JFileChooser.APPROVE_OPTION) { fichier = fc.getSelectedFile(); name.setText(fichier.getName().substring(0, (int) fichier.getName().length() - 4)); } } if (e.getSource() == play) { try { if (fichier != null) { if (lecteur == null) { lecteur = Manager.createPlayer(fichier.toURL()); lecteur.start(); play_time.start(); } if (enPause == true) { lecteur.start(); play_time.start(); enPause = false; } } } catch (Exception ex) { ex.printStackTrace(); } } if (e.getSource() == pause) { if (enPause == false && lecteur != null) { lecteur.stop(); play_time.stop(); enPause = true; } } if (e.getSource() == stop) { if (enPause == false && lecteur != null) { lecteur.stop(); lecteur.close(); lecteur = null; play_time.stop(); dure.setText("00:00 - 00:00"); deplacement.setValue(0); } } }
protected void initJMF() { Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true); }