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

VOIP program

Development Platform:

Java

  1. /*
  2.  * TCPPushSourceStream.java
  3.  *
  4.  * Created on November 19, 2003, 9:51 AM
  5.  */
  6. package gov.nist.applet.phone.media.protocol.transport;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.net.Socket;
  10. import javax.media.protocol.SourceTransferHandler;
  11. import javax.media.protocol.PushSourceStream;
  12. import javax.media.protocol.ContentDescriptor;
  13. //import gov.nist.applet.phone.ua.PushToTalkStatus;
  14. /**
  15.  * This class is an implementation of the PushSourceStream for
  16.  * The TCP transport protocol underlying to RTP. 
  17.  * @author  DERUELLE Jean
  18.  */
  19. public class TCPPushSourceStream extends Thread implements PushSourceStream{
  20.     private Socket socket;    
  21.     private InputStream in;    
  22.     private boolean done = false;
  23.     private boolean dataRead = false;    
  24.     private SourceTransferHandler sth = null;    
  25.     private boolean ctrl;
  26.     /** 
  27.      * Creates a new instance of TCPPushSourceStream
  28.      * @param socket - the socket from which we will get the input stream.
  29.      * @param ctrl - used to know if we are reading the rtcp packets     
  30.      */
  31.     public TCPPushSourceStream(Socket socket,boolean ctrl) {
  32.         this.socket = socket;                     
  33.         this.ctrl=ctrl;
  34.         try{
  35.             //if(ctrl)
  36.                 //in=socket.getInputStream();
  37.             //else
  38.                 in=socket.getInputStream();
  39.                 //in=new MyInputStream(socket.getInputStream());
  40.         }
  41.         catch(IOException ioe){
  42.             ioe.printStackTrace();
  43.         }                
  44.     }
  45.     /**
  46.      * Read some data from the input stream.
  47.      * @param buffer - the buffer into which the data is read.
  48.      * @param offset - the start offset in array buffer at which the data is written.
  49.      * @param len - the maximum number of bytes to read.
  50.      * @return the total number of bytes read into the buffer,
  51.      * or -1 if there is no more data because the end of the stream has been reached.
  52.      */
  53.     public int read(byte buffer[], int offset, int length) {              
  54.         int byteRead=0;        
  55.         
  56.         try 
  57.         {
  58.             //if(ctrl)
  59.             //    byteRead=in.read(buffer,offset,length);
  60.             //else{
  61.                 //if(!PushToTalkStatus.pushToTalk)
  62.                     byteRead=in.read(buffer,offset,length);
  63.                 /*else{
  64.                     if(PushToTalkStatus.talking)
  65.                         if(!ctrl)
  66.                             return 0;
  67.                         else
  68.                             byteRead=in.read(buffer,offset,length);
  69.                     else
  70.                         byteRead=in.read(buffer,offset,length);            
  71.                 }*/
  72.             //}
  73.         }
  74.         catch (IOException e) {
  75.             return -1;
  76.         }
  77.         synchronized (this) {
  78.             dataRead = true;
  79.             notify();
  80.         }
  81.         return byteRead;
  82.     }
  83.     /**
  84.      * Start this thread
  85.      */
  86.     public synchronized void start() {
  87.         super.start();
  88.         if (sth != null) {
  89.             dataRead = true;
  90.             notify();
  91.         }
  92.     }
  93.     /**
  94.      * Kill this thread
  95.      */
  96.     public synchronized void kill() {
  97.         done = true;
  98.         notify();
  99.     }
  100.     /**
  101.      * Determine the size of the buffer needed for the data transfer. 
  102.      * This method is provided so that a transfer handler can determine how much data, 
  103.      * at a minimum, will be available to transfer from the source. 
  104.      * Overflow and data loss is likely to occur if this much data isn't read at transfer time.
  105.      * @return The size of the data transfer.
  106.      */
  107.     public int getMinimumTransferSize() {
  108.         return 2 * 1024; // twice the MTU size, just to be safe.
  109.     }
  110.     
  111.     /**
  112.      * Register an object to service data transfers to this stream.
  113.      *
  114.      * If a handler is already registered when setTransferHandler is called, 
  115.      * the handler is replaced; there can only be one handler at a time.
  116.      * @param sth - The handler to transfer data to.
  117.      */
  118.     public synchronized void setTransferHandler(SourceTransferHandler sth) {
  119.         this.sth = sth;
  120.         dataRead = true;
  121.         notify();
  122.     }
  123.     
  124.     /**
  125.      * Get the current content type for this stream.
  126.      * @return The current ContentDescriptor for this stream.
  127.      */
  128.     // Not applicable.
  129.     public ContentDescriptor getContentDescriptor() {
  130.         return null;
  131.     }
  132.     /**
  133.      * Get the size, in bytes, of the content on this stream. 
  134.      * LENGTH_UNKNOWN is returned if the length is not known.
  135.      * @return The content length in bytes.
  136.      */
  137.     // Not applicable.
  138.     public long getContentLength() {
  139.         return LENGTH_UNKNOWN;
  140.     }
  141.     /**
  142.      * Find out if the end of the stream has been reached.
  143.      * @return Returns true if there is no more data.
  144.      */
  145.     // Not applicable.
  146.     public boolean endOfStream() {
  147.         return false;
  148.     }
  149.     /**
  150.      * Obtain the collection of objects that control the object that implements this interface.
  151.      * If no controls are supported, a zero length array is returned.
  152.      * @return the collection of object controls
  153.      */
  154.     // Not applicable.
  155.     public Object[] getControls() {
  156.         return new Object[0];
  157.     }
  158.     /**
  159.      * Obtain the object that implements the specified Class or Interface  The full class or interface name must be used.
  160.      * If the control is not supported then null is returned.
  161.      * @return the object that implements the control, or null.
  162.      */
  163.     // Not applicable.
  164.     public Object getControl(String type) {
  165.         return null;
  166.     }
  167.     /**
  168.      * Loop and notify the transfer handler of new data.
  169.      */
  170.     public void run() {
  171.         while (!done) {
  172.             synchronized (this) {
  173.                 while (!dataRead && !done) {
  174.                     try {
  175.                         wait();
  176.                     } catch (InterruptedException e) { }
  177.                 }
  178.                 dataRead = false;
  179.             }
  180.             if (sth != null && !done) {
  181.                 sth.transferData(this);
  182.             }
  183.         }
  184.     }    
  185.     
  186.     class MyInputStream  extends InputStream {        
  187.         private int ptr;
  188.         private int maxBufferRead;
  189.         private  byte[] buffer;
  190.         private  byte[] sockBuffer;
  191.         private boolean bufferFull;
  192.         private InputStream in;
  193.         private static final int MAX_BUFFER_SIZE=131072;
  194.         
  195.         public MyInputStream (InputStream in) {
  196.             super();
  197.             ptr =0;
  198.             maxBufferRead=0;
  199.             bufferFull=false;
  200.             buffer=new byte[MAX_BUFFER_SIZE];
  201.             sockBuffer=new byte[MAX_BUFFER_SIZE];
  202.             this.in = in;
  203.         }
  204.         public int read() throws IOException {    
  205.             //if(!PushToTalkStatus.pushToTalk){
  206.                 byte content[]=new byte[1];
  207.                 int nbBytesRead=in.read(content);                
  208.                 if(nbBytesRead==-1)
  209.                     return -1;
  210.                 else
  211.                     return (int)content[0];
  212.             /*}
  213.             else{                                
  214.                 while(!bufferFull){
  215.                     int nbBytesRead=in.read(sockBuffer);   
  216.                     //System.out.println("nbBytesRead: "+nbBytesRead+" ctrl"+ctrl);
  217.                     if(nbBytesRead==-1)
  218.                         return -1;            
  219.                     if(nbBytesRead<MAX_BUFFER_SIZE){
  220.                         //System.out.println("ptr+nbBytesRead: "+ptr+" ctrl"+ctrl);
  221.                         if(ptr+nbBytesRead<MAX_BUFFER_SIZE){
  222.                             System.arraycopy(sockBuffer, 0, buffer, ptr, nbBytesRead);
  223.                             ptr+=nbBytesRead;                   
  224.                         }
  225.                         else{                            
  226.                             maxBufferRead=ptr;
  227.                             ptr=0;
  228.                             System.out.println("buffer fulled: size read: "+maxBufferRead);                    
  229.                             System.out.println("ptr on : "+ptr);
  230.                             bufferFull=true;
  231.                         }
  232.                     }
  233.                 }
  234.                 //We read the buffer                
  235.                 int byteVal= (int) buffer[ptr++];
  236.                 System.out.println("ptr on : "+ptr);
  237.                 //The buffer has been read we have to fill it up again
  238.                 if(ptr>=maxBufferRead){
  239.                     ptr=0;
  240.                     bufferFull=false;
  241.                     maxBufferRead=0;
  242.                     System.out.println("buffer emptied");
  243.                 }
  244.                 return byteVal;
  245.             }*/
  246.         }
  247.     }
  248.     
  249. }