RawLiveDataSource.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 5k
Category:

VOIP program

Development Platform:

Java

  1. /*
  2.  * RawLiveDataSource.java
  3.  *
  4.  * Created on March 20, 2003, 8:56 AM
  5.  */
  6. package gov.nist.media.protocol.live;
  7. import javax.media.Time;
  8. import javax.media.protocol.*;
  9. import java.io.IOException;
  10. /**
  11.  * A default data-source created directly from an array of byte
  12.  * This Data source allow one to play or play back a buffer filled by RAW 
  13.  * audio data
  14.  * 
  15.  * @author Jean Deruelle <jean.deruelle@nist.gov>
  16.  *
  17.  * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
  18.  */
  19. public class RawLiveDataSource extends PushBufferDataSource {
  20.     protected Object [] controls = new Object[0]; 
  21.     protected boolean started = false;    
  22.     protected boolean connected = false;
  23.     protected Time duration = DURATION_UNKNOWN;
  24.     protected RawLiveStream [] streams = null;
  25.     protected RawLiveStream stream = null;
  26.     
  27.     /**
  28.      * Constructs a new instance of RawLiveDataSource
  29.      *
  30.      */
  31.     public RawLiveDataSource() {
  32.     }
  33.     
  34. /**
  35.  * Get the current content type for this stream.
  36.  *
  37.  * @return The current <CODE>ContentDescriptor</CODE> for this stream.
  38.  */
  39.     public String getContentType() {
  40. if (!connected){
  41.      System.err.println("Error: DataSource not connected");
  42.         return null;
  43.     }
  44.     return ContentDescriptor.RAW;
  45.     }
  46. /**
  47.  * The <CODE>connect</CODE> method initiates communication with the source.
  48.  *
  49.  * @exception IOException Thrown if there are IO problems
  50.  * when <CODE>connect</CODE> is called.
  51.  */
  52.     public void connect() throws IOException {
  53.  if (connected)
  54.             return;
  55.  connected = true;
  56.     }
  57.     
  58. /**
  59.  * The <CODE>disconnect</CODE> method frees resources used to maintain a
  60.  * connection to the source.
  61.  * If no resources are in use, <CODE>disconnect</CODE> is ignored.
  62.  */
  63.     public void disconnect() {
  64. try {
  65.             if (started)
  66.                 stop();
  67.         } catch (IOException e) {}
  68. connected = false;
  69.     }
  70. /**
  71.  * Initiate data-transfer. The <CODE>start</CODE> method must be
  72.  * called before data is available.
  73.  *(You must call <CODE>connect</CODE> before calling <CODE>start</CODE>.)
  74.  *
  75.  * @exception IOException Thrown if there are IO problems with the source
  76.  * when <CODE>start</CODE> is called.
  77.  */
  78.     public void start() throws IOException {
  79. // we need to throw error if connect() has not been called
  80.         if (!connected)
  81.             throw new java.lang.Error("DataSource must be connected before it can be started");
  82.         if (started)
  83.             return;
  84. started = true;
  85. stream.start(true);
  86.     }
  87. /**
  88.  * Stop the data-transfer.
  89.  * If the source has not been connected and started,
  90.  * <CODE>stop</CODE> does nothing.
  91.  */
  92.     public void stop() throws IOException {
  93. if ((!connected) || (!started))
  94.     return;
  95. started = false;
  96. stream.start(false);
  97.     }
  98. /**
  99.  * Obtain the collection of objects that
  100.  * control the object that implements this interface.
  101.  * <p>
  102.  *
  103.  * No controls are supported.
  104.  * A zero length array is returned.
  105.  *
  106.  * @return A zero length array
  107.  */
  108.     public Object [] getControls() {
  109. return controls;
  110.     }
  111. /**
  112.  * Obtain the object that implements the specified
  113.  * <code>Class</code> or <code>Interface</code>
  114.  * The full class or interface name must be used.
  115.  * <p>
  116.  *
  117.  * The control is not supported.
  118.  * <code>null</code> is returned.
  119.  *
  120.  * @return <code>null</code>.
  121.  */
  122.     public Object getControl(String controlType) {
  123.        try {
  124.           Class  cls = Class.forName(controlType);
  125.           Object cs[] = getControls();
  126.           for (int i = 0; i < cs.length; i++) {
  127.              if (cls.isInstance(cs[i]))
  128.                 return cs[i];
  129.           }
  130.           return null;
  131.        } catch (Exception e) {   // no such controlType or such control
  132.          return null;
  133.        }
  134.     }
  135. /**
  136.  * Set the buffer of this <code>DataSource</code>
  137.  * @param buffer
  138.  */
  139. public void setBuffer(byte[] buffer){
  140. if (streams == null) {
  141. streams = new RawLiveStream[1];
  142. stream = streams[0] = new RawLiveStream();
  143. }
  144. stream.data=buffer;
  145. }
  146. /**
  147.  * @see javax.media.Duration#getDuration()
  148.  */
  149.     public Time getDuration() {
  150. return duration;
  151.     }
  152. /**
  153.  * Get the collection of streams that this source
  154.  * manages. The collection of streams is entirely
  155.  * content dependent. The  MIME type of this
  156.  * <CODE>DataSource</CODE> provides the only indication of
  157.  * what streams can be available on this connection.
  158.  *
  159.  * @return The collection of streams for this source.
  160.  */
  161.     public PushBufferStream [] getStreams() {
  162. if (streams == null) {
  163.     streams = new RawLiveStream[1];
  164.     stream = streams[0] = new RawLiveStream();
  165. }
  166. return streams;
  167.     }
  168.     
  169. }