Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xiphias3.client;
- import javax.media.DataSink;
- import javax.media.MediaLocator;
- import javax.media.IncompatibleSourceException;
- import javax.media.Buffer;
- import javax.media.protocol.*;
- import javax.media.datasink.DataSinkListener;
- import java.io.IOException;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.DatagramPacket;
- import java.net.SocketAddress;
- /*
- * Author: Xiphias3
- * Date: Oct 19, 2010
- * Time: 8:54:54 AM
- */
- public class ULAWUDPDataSink implements DataSink, BufferTransferHandler
- {
- private MediaLocator m_outputLocation;
- private DatagramSocket m_socket;
- private int m_nPort;
- private PushBufferDataSource m_source;
- private SocketAddress m_targetAddress;
- private Buffer m_buffer;
- public ULAWUDPDataSink(int nPort) {
- m_nPort = nPort;
- m_buffer = new Buffer();
- }
- public boolean isConnected() {
- return ((m_socket != null) && (m_socket.isConnected()) && (!m_socket.isClosed()));
- }
- public void setOutputLocator(MediaLocator mediaLocator) {
- m_outputLocation = mediaLocator;
- }
- public MediaLocator getOutputLocator() {
- return m_outputLocation;
- }
- public void start() throws IOException {
- try {
- if (m_source != null)
- m_source.start();
- }
- catch (Exception x) {
- throw new IOException(x.getMessage());
- }
- }
- public void transferData(PushBufferStream pushBufferStream) {
- try {
- pushBufferStream.read(m_buffer);
- if ((!m_buffer.isDiscard()) && (!m_buffer.isEOM())) {
- byte[] abData = new ULAWPacket(m_buffer).toByteAray();
- DatagramPacket packet = new DatagramPacket(abData, 0, abData.length, m_targetAddress);
- m_socket.send(packet);
- }
- }
- catch (IOException iox) {
- iox.printStackTrace();
- close();
- }
- }
- public void stop() throws IOException {
- try {
- if (m_source != null)
- m_source.stop();
- }
- catch (Exception x) {
- throw new IOException(x.getMessage());
- }
- }
- public void open() throws IOException, SecurityException {
- if (m_socket == null) {
- try {
- String szAddress = m_outputLocation.toExternalForm();
- String szHost = szAddress.substring(0, szAddress.indexOf(':'));
- int nPort = Integer.parseInt(szAddress.substring(szAddress.lastIndexOf(':') + 1));
- m_socket = new DatagramSocket(m_nPort);
- m_socket.connect(InetAddress.getByName(szHost), nPort);
- m_targetAddress = m_socket.getRemoteSocketAddress();
- m_socket.send(new DatagramPacket(new byte[0], 0, 0));
- //System.out.println("DataSink connected");
- }
- catch (Exception x) {
- m_socket = null;
- throw new IOException(x.getMessage());
- }
- }
- }
- public void close() {
- if (m_socket != null) {
- m_socket.disconnect();
- m_socket.close();
- }
- }
- public String getContentType() {
- return ContentDescriptor.RAW;
- }
- public void addDataSinkListener(DataSinkListener dataSinkListener) {
- }
- public void removeDataSinkListener(DataSinkListener dataSinkListener) {
- }
- public synchronized void setSource(DataSource dataSource) throws IOException, IncompatibleSourceException {
- if (dataSource == null) {
- m_source = null;
- return;
- }
- m_source = (PushBufferDataSource) dataSource;
- m_source.getStreams()[0].setTransferHandler(this);
- }
- public Object[] getControls() {
- return new Object[0];
- }
- public Object getControl(String s) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement