Search in sources :

Example 1 with DataSource

use of javax.media.protocol.DataSource in project Smack by igniterealtime.

the class AudioChannel method createProcessor.

private String createProcessor() {
    if (locator == null)
        return "Locator is null";
    DataSource ds;
    try {
        ds = javax.media.Manager.createDataSource(locator);
    } catch (Exception e) {
        // Try JavaSound Locator as a last resort
        try {
            ds = javax.media.Manager.createDataSource(new MediaLocator("javasound://"));
        } catch (Exception ee) {
            return "Couldn't create DataSource";
        }
    }
    // Try to create a processor to handle the input jmf locator
    try {
        processor = javax.media.Manager.createProcessor(ds);
    } catch (NoProcessorException npe) {
        LOGGER.log(Level.WARNING, "exception", npe);
        return "Couldn't create processor";
    } catch (IOException ioe) {
        LOGGER.log(Level.WARNING, "exception", ioe);
        return "IOException creating processor";
    }
    // Wait for it to configure
    boolean result = waitForState(processor, Processor.Configured);
    if (!result) {
        return "Couldn't configure processor";
    }
    // Get the tracks from the processor
    TrackControl[] tracks = processor.getTrackControls();
    // Do we have at least one track?
    if (tracks == null || tracks.length < 1) {
        return "Couldn't find tracks in processor";
    }
    // Set the output content descriptor to RAW_RTP
    // This will limit the supported formats reported from
    // Track.getSupportedFormats to only valid RTP formats.
    ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
    processor.setContentDescriptor(cd);
    Format[] supported;
    Format chosen = null;
    boolean atLeastOneTrack = false;
    // Program the tracks.
    for (int i = 0; i < tracks.length; i++) {
        if (tracks[i].isEnabled()) {
            supported = tracks[i].getSupportedFormats();
            if (supported.length > 0) {
                for (Format format : supported) {
                    if (format instanceof AudioFormat) {
                        if (this.format.matches(format))
                            chosen = format;
                    }
                }
                if (chosen != null) {
                    tracks[i].setFormat(chosen);
                    LOGGER.severe("Track " + i + " is set to transmit as: " + chosen);
                    if (tracks[i].getFormat() instanceof AudioFormat) {
                        int packetRate = 20;
                        PacketSizeControl pktCtrl = (PacketSizeControl) processor.getControl(PacketSizeControl.class.getName());
                        if (pktCtrl != null) {
                            try {
                                pktCtrl.setPacketSize(getPacketSize(tracks[i].getFormat(), packetRate));
                            } catch (IllegalArgumentException e) {
                                pktCtrl.setPacketSize(80);
                            // Do nothing
                            }
                        }
                        if (tracks[i].getFormat().getEncoding().equals(AudioFormat.ULAW_RTP)) {
                            Codec[] codec = new Codec[3];
                            codec[0] = new com.ibm.media.codec.audio.rc.RCModule();
                            codec[1] = new com.ibm.media.codec.audio.ulaw.JavaEncoder();
                            codec[2] = new com.sun.media.codec.audio.ulaw.Packetizer();
                            ((com.sun.media.codec.audio.ulaw.Packetizer) codec[2]).setPacketSize(160);
                            try {
                                tracks[i].setCodecChain(codec);
                            } catch (UnsupportedPlugInException e) {
                                LOGGER.log(Level.WARNING, "exception", e);
                            }
                        }
                    }
                    atLeastOneTrack = true;
                } else
                    tracks[i].setEnabled(false);
            } else
                tracks[i].setEnabled(false);
        }
    }
    if (!atLeastOneTrack)
        return "Couldn't set any of the tracks to a valid RTP format";
    result = waitForState(processor, Controller.Realized);
    if (!result)
        return "Couldn't realize processor";
    // Get the output data source of the processor
    dataOutput = processor.getDataOutput();
    return null;
}
Also used : TrackControl(javax.media.control.TrackControl) NoProcessorException(javax.media.NoProcessorException) Codec(javax.media.Codec) MediaLocator(javax.media.MediaLocator) Format(javax.media.Format) AudioFormat(javax.media.format.AudioFormat) PacketSizeControl(javax.media.control.PacketSizeControl) AudioFormat(javax.media.format.AudioFormat) IOException(java.io.IOException) NoProcessorException(javax.media.NoProcessorException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidSessionAddressException(javax.media.rtp.InvalidSessionAddressException) UnsupportedPlugInException(javax.media.UnsupportedPlugInException) PushBufferDataSource(javax.media.protocol.PushBufferDataSource) DataSource(javax.media.protocol.DataSource) ContentDescriptor(javax.media.protocol.ContentDescriptor) UnsupportedPlugInException(javax.media.UnsupportedPlugInException)

Example 2 with DataSource

use of javax.media.protocol.DataSource in project Smack by igniterealtime.

the class AudioReceiver method update.

/**
 * ReceiveStreamListener.
 */
@Override
public synchronized void update(ReceiveStreamEvent evt) {
    // could be null.
    Participant participant = evt.getParticipant();
    // could be null.
    ReceiveStream stream = evt.getReceiveStream();
    if (evt instanceof RemotePayloadChangeEvent) {
        LOGGER.severe("  - Received an RTP PayloadChangeEvent.");
        LOGGER.severe("Sorry, cannot handle payload change.");
    } else if (evt instanceof NewReceiveStreamEvent) {
        try {
            stream = evt.getReceiveStream();
            DataSource ds = stream.getDataSource();
            // Find out the formats.
            RTPControl ctl = (RTPControl) ds.getControl("javax.jmf.rtp.RTPControl");
            if (ctl != null) {
                LOGGER.severe("  - Received new RTP stream: " + ctl.getFormat());
            } else
                LOGGER.severe("  - Received new RTP stream");
            if (participant == null)
                LOGGER.severe("      The sender of this stream had yet to be identified.");
            else {
                LOGGER.severe("      The stream comes from: " + participant.getCNAME());
            }
            // create a player by passing datasource to the Media Manager
            Player p = javax.media.Manager.createPlayer(ds);
            if (p == null)
                return;
            p.addControllerListener(this);
            p.realize();
            jingleMediaSession.mediaReceived(participant != null ? participant.getCNAME() : "");
            // Notify initialize() that a new stream had arrived.
            synchronized (dataSync) {
                dataReceived = true;
                dataSync.notifyAll();
            }
        } catch (Exception e) {
            LOGGER.severe("NewReceiveStreamEvent exception " + e.getMessage());
            return;
        }
    } else if (evt instanceof StreamMappedEvent) {
        if (stream != null && stream.getDataSource() != null) {
            DataSource ds = stream.getDataSource();
            // Find out the formats.
            RTPControl ctl = (RTPControl) ds.getControl("javax.jmf.rtp.RTPControl");
            LOGGER.severe("  - The previously unidentified stream ");
            if (ctl != null)
                LOGGER.severe("      " + ctl.getFormat());
            LOGGER.severe("      had now been identified as sent by: " + participant.getCNAME());
        }
    } else if (evt instanceof ByeEvent) {
        LOGGER.severe("  - Got \"bye\" from: " + participant.getCNAME());
    }
}
Also used : ByeEvent(javax.media.rtp.event.ByeEvent) Player(javax.media.Player) Participant(javax.media.rtp.Participant) NewReceiveStreamEvent(javax.media.rtp.event.NewReceiveStreamEvent) ReceiveStream(javax.media.rtp.ReceiveStream) RTPControl(javax.media.rtp.RTPControl) RemotePayloadChangeEvent(javax.media.rtp.event.RemotePayloadChangeEvent) StreamMappedEvent(javax.media.rtp.event.StreamMappedEvent) DataSource(javax.media.protocol.DataSource)

Example 3 with DataSource

use of javax.media.protocol.DataSource in project Spark by igniterealtime.

the class AudioReceiver method update.

/**
 * ReceiveStreamListener
 */
public synchronized void update(ReceiveStreamEvent evt) {
    // could be null.
    Participant participant = evt.getParticipant();
    // could be null.
    ReceiveStream stream = evt.getReceiveStream();
    if (evt instanceof RemotePayloadChangeEvent) {
        System.err.println("  - Received an RTP PayloadChangeEvent.");
        System.err.println("Sorry, cannot handle payload change.");
    // System.exit(0);
    } else if (evt instanceof NewReceiveStreamEvent) {
        try {
            stream = ((NewReceiveStreamEvent) evt).getReceiveStream();
            DataSource ds = stream.getDataSource();
            // Find out the formats.
            RTPControl ctl = (RTPControl) ds.getControl("javax.jmf.rtp.RTPControl");
            if (ctl != null) {
                System.err.println("  - Recevied new RTP stream: " + ctl.getFormat());
            } else
                System.err.println("  - Recevied new RTP stream");
            if (participant == null)
                System.err.println("      The sender of this stream had yet to be identified.");
            else {
                System.err.println("      The stream comes from: " + participant.getCNAME());
            }
            // create a player by passing datasource to the Media Manager
            Player p = javax.media.Manager.createPlayer(ds);
            if (p == null)
                return;
            p.addControllerListener(this);
            p.realize();
            // Notify intialize() that a new stream had arrived.
            synchronized (dataSync) {
                dataReceived = true;
                dataSync.notifyAll();
            }
        } catch (Exception e) {
            System.err.println("NewReceiveStreamEvent exception " + e.getMessage());
            return;
        }
    } else if (evt instanceof StreamMappedEvent) {
        if (stream != null && stream.getDataSource() != null) {
            DataSource ds = stream.getDataSource();
            // Find out the formats.
            RTPControl ctl = (RTPControl) ds.getControl("javax.jmf.rtp.RTPControl");
            System.err.println("  - The previously unidentified stream ");
            if (ctl != null)
                System.err.println("      " + ctl.getFormat());
            System.err.println("      had now been identified as sent by: " + participant.getCNAME());
        }
    } else if (evt instanceof ByeEvent) {
        System.err.println("  - Got \"bye\" from: " + participant.getCNAME());
    }
}
Also used : DataSource(javax.media.protocol.DataSource)

Example 4 with DataSource

use of javax.media.protocol.DataSource in project Spark by igniterealtime.

the class VideoReceiver method update.

/**
 * ReceiveStreamListener
 */
public synchronized void update(ReceiveStreamEvent evt) {
    // could be null.
    Participant participant = evt.getParticipant();
    // could be null.
    ReceiveStream stream = evt.getReceiveStream();
    System.out.println(evt);
    if (evt instanceof RemotePayloadChangeEvent) {
        System.err.println("  - Received an RTP PayloadChangeEvent.");
        System.err.println("Sorry, cannot handle payload change.");
    // System.exit(0);
    } else if (evt instanceof NewReceiveStreamEvent) {
        try {
            stream = ((NewReceiveStreamEvent) evt).getReceiveStream();
            DataSource ds = stream.getDataSource();
            System.out.println("DataSource:" + ds);
            // Find out the formats.
            RTPControl ctl = (RTPControl) ds.getControl("javax.jmf.rtp.RTPControl");
            if (ctl != null) {
                System.err.println("  - Recevied new RTP stream: " + ctl.getFormat());
            } else
                System.err.println("  - Recevied new RTP stream");
            if (participant == null)
                System.err.println("      The sender of this stream had yet to be identified.");
            else {
                System.err.println("      The stream comes from: " + participant.getCNAME());
            }
            // create a player by passing datasource to the Media Manager
            Player p = javax.media.Manager.createPlayer(ds);
            if (p == null)
                return;
            p.addControllerListener(this);
            p.realize();
            // Notify intialize() that a new stream had arrived.
            synchronized (dataSync) {
                dataReceived = true;
                dataSync.notifyAll();
            }
            System.out.println("Start2");
        } catch (Exception e) {
            System.err.println("NewReceiveStreamEvent exception " + e.getMessage());
            return;
        }
    } else if (evt instanceof StreamMappedEvent) {
        if (stream != null && stream.getDataSource() != null) {
            DataSource ds = stream.getDataSource();
            // Find out the formats.
            RTPControl ctl = (RTPControl) ds.getControl("javax.jmf.rtp.RTPControl");
            System.err.println("  - The previously unidentified stream ");
            if (ctl != null)
                System.err.println("      " + ctl.getFormat());
            System.err.println("      had now been identified as sent by: " + participant.getCNAME());
        }
    } else if (evt instanceof ByeEvent) {
        System.err.println("  - Got \"bye\" from: " + participant.getCNAME());
    }
}
Also used : ByeEvent(javax.media.rtp.event.ByeEvent) Player(javax.media.Player) Participant(javax.media.rtp.Participant) NewReceiveStreamEvent(javax.media.rtp.event.NewReceiveStreamEvent) ReceiveStream(javax.media.rtp.ReceiveStream) RTPControl(javax.media.rtp.RTPControl) RemotePayloadChangeEvent(javax.media.rtp.event.RemotePayloadChangeEvent) StreamMappedEvent(javax.media.rtp.event.StreamMappedEvent) DataSource(javax.media.protocol.DataSource)

Aggregations

DataSource (javax.media.protocol.DataSource)4 Player (javax.media.Player)2 Participant (javax.media.rtp.Participant)2 RTPControl (javax.media.rtp.RTPControl)2 ReceiveStream (javax.media.rtp.ReceiveStream)2 ByeEvent (javax.media.rtp.event.ByeEvent)2 NewReceiveStreamEvent (javax.media.rtp.event.NewReceiveStreamEvent)2 RemotePayloadChangeEvent (javax.media.rtp.event.RemotePayloadChangeEvent)2 StreamMappedEvent (javax.media.rtp.event.StreamMappedEvent)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Codec (javax.media.Codec)1 Format (javax.media.Format)1 MediaLocator (javax.media.MediaLocator)1 NoProcessorException (javax.media.NoProcessorException)1 UnsupportedPlugInException (javax.media.UnsupportedPlugInException)1 PacketSizeControl (javax.media.control.PacketSizeControl)1 TrackControl (javax.media.control.TrackControl)1 AudioFormat (javax.media.format.AudioFormat)1 ContentDescriptor (javax.media.protocol.ContentDescriptor)1