Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.00 KB | None | 0 0
  1. package xiphias3.client;
  2.  
  3. import javax.media.DataSink;
  4. import javax.media.MediaLocator;
  5. import javax.media.IncompatibleSourceException;
  6. import javax.media.Buffer;
  7. import javax.media.protocol.*;
  8. import javax.media.datasink.DataSinkListener;
  9. import java.io.IOException;
  10. import java.net.DatagramSocket;
  11. import java.net.InetAddress;
  12. import java.net.DatagramPacket;
  13. import java.net.SocketAddress;
  14.  
  15. /*
  16.  * Author: Xiphias3
  17.  * Date:   Oct 19, 2010
  18.  * Time:   8:54:54 AM
  19.  */
  20.  
  21. public class ULAWUDPDataSink implements DataSink, BufferTransferHandler
  22. {
  23.     private MediaLocator         m_outputLocation;
  24.     private DatagramSocket       m_socket;
  25.     private int                  m_nPort;
  26.     private PushBufferDataSource m_source;
  27.     private SocketAddress        m_targetAddress;
  28.     private Buffer               m_buffer;
  29.  
  30.     public ULAWUDPDataSink(int nPort) {
  31.         m_nPort = nPort;
  32.         m_buffer = new Buffer();
  33.     }
  34.  
  35.     public boolean isConnected() {
  36.         return ((m_socket != null) && (m_socket.isConnected()) && (!m_socket.isClosed()));
  37.     }
  38.  
  39.     public void setOutputLocator(MediaLocator mediaLocator) {
  40.         m_outputLocation = mediaLocator;
  41.     }
  42.  
  43.     public MediaLocator getOutputLocator() {
  44.         return m_outputLocation;
  45.     }
  46.  
  47.     public void start() throws IOException {
  48.         try {
  49.             if (m_source != null)
  50.                 m_source.start();
  51.         }
  52.         catch (Exception x) {
  53.             throw new IOException(x.getMessage());
  54.         }
  55.     }
  56.  
  57.     public void transferData(PushBufferStream pushBufferStream) {
  58.         try {
  59.             pushBufferStream.read(m_buffer);
  60.             if ((!m_buffer.isDiscard()) && (!m_buffer.isEOM())) {
  61.                 byte[] abData = new ULAWPacket(m_buffer).toByteAray();
  62.                 DatagramPacket packet = new DatagramPacket(abData, 0, abData.length, m_targetAddress);
  63.                 m_socket.send(packet);
  64.             }
  65.         }
  66.         catch (IOException iox) {
  67.             iox.printStackTrace();
  68.             close();
  69.         }
  70.     }
  71.  
  72.     public void stop() throws IOException {
  73.         try {
  74.             if (m_source != null)
  75.                 m_source.stop();
  76.         }
  77.         catch (Exception x) {
  78.             throw new IOException(x.getMessage());
  79.         }
  80.     }
  81.  
  82.     public void open() throws IOException, SecurityException {
  83.         if (m_socket == null) {
  84.             try {
  85.                 String szAddress = m_outputLocation.toExternalForm();
  86.                 String szHost = szAddress.substring(0, szAddress.indexOf(':'));
  87.                 int nPort = Integer.parseInt(szAddress.substring(szAddress.lastIndexOf(':') + 1));
  88.                 m_socket = new DatagramSocket(m_nPort);
  89.                 m_socket.connect(InetAddress.getByName(szHost), nPort);
  90.                 m_targetAddress = m_socket.getRemoteSocketAddress();
  91.                 m_socket.send(new DatagramPacket(new byte[0], 0, 0));
  92.                 //System.out.println("DataSink connected");
  93.             }
  94.             catch (Exception x) {
  95.                 m_socket = null;
  96.                 throw new IOException(x.getMessage());
  97.             }
  98.         }
  99.     }
  100.  
  101.     public void close() {
  102.         if (m_socket != null) {
  103.             m_socket.disconnect();
  104.             m_socket.close();
  105.         }
  106.     }
  107.  
  108.     public String getContentType() {
  109.         return ContentDescriptor.RAW;
  110.     }
  111.  
  112.     public void addDataSinkListener(DataSinkListener dataSinkListener) {
  113.  
  114.     }
  115.  
  116.     public void removeDataSinkListener(DataSinkListener dataSinkListener) {
  117.  
  118.     }
  119.  
  120.     public synchronized void setSource(DataSource dataSource) throws IOException, IncompatibleSourceException {
  121.         if (dataSource == null) {
  122.             m_source = null;
  123.             return;
  124.         }
  125.         m_source = (PushBufferDataSource) dataSource;
  126.         m_source.getStreams()[0].setTransferHandler(this);
  127.     }
  128.  
  129.     public Object[] getControls() {
  130.         return new Object[0];
  131.     }
  132.  
  133.     public Object getControl(String s) {
  134.         return null;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement