src/com/beem/project/beem/jingle/RTPTransmitter.java
author jibe@jibe-desktop
Thu, 09 Apr 2009 20:02:26 +0200
changeset 98 4d6ff785605d
parent 53 ef115770432d
child 212 bbc0b169cdf0
permissions -rwxr-xr-x
toto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     1
package com.beem.project.beem.jingle;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     2
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     3
import java.io.IOException;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     4
import java.net.DatagramSocket;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     5
import java.net.ServerSocket;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     6
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     7
import org.jivesoftware.smackx.jingle.SmackLogger;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     8
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
     9
import jlibrtp.DataFrame;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    10
import jlibrtp.Participant;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    11
import jlibrtp.RTPAppIntf;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    12
import jlibrtp.RTPSession;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    13
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    14
public class RTPTransmitter implements Runnable, RTPAppIntf {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    15
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    16
	private static final SmackLogger LOGGER = SmackLogger
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    17
			.getLogger(SenderMediaManager.class);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    18
	private RTPSession rtpSession;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    19
	private boolean killme = false;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    20
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    21
	public RTPTransmitter(String remoteIP, int port) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    22
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    23
		DatagramSocket rtpSocket = null;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    24
		int rtpPort = 0;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    25
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    26
		try {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    27
			rtpPort = getFreePort();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    28
			rtpSocket = new DatagramSocket(rtpPort);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    29
		} catch (Exception e) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    30
			System.out.println("RTPSession failed to obtain port");
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    31
			return;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    32
		}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    33
		rtpSession = new RTPSession(rtpSocket, null);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    34
		rtpSession.naivePktReception(true);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    35
		rtpSession.RTPSessionRegister(this, null, null);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    36
		rtpSession.addParticipant(new Participant(remoteIP,rtpPort, 0));
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    37
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    38
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    39
	private void start() {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    40
		LOGGER.info("Debut envoi de donnees par RTPTransmitter");
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    41
		while (!killme) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    42
			rtpSession.sendData(null);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    43
		}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    44
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    45
		try {Thread.sleep(200);} catch (Exception e) {}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    46
		this.rtpSession.endSession();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    47
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    48
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    49
	@Override
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    50
	public void run() {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    51
		start();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    52
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    53
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    54
	@Override
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    55
	public int frameSize(int payloadType) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    56
		return 1;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    57
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    58
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    59
	@Override
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    60
	public void receiveData(DataFrame frame, Participant participant) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    61
		//On envoie uniquement
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    62
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    63
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    64
	@Override
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    65
	public void userEvent(int type, Participant[] participant) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    66
		//je sais pas ce que c'est
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    67
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    68
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    69
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    70
	protected int getFreePort() {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    71
		ServerSocket ss;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    72
		int freePort = 0;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    73
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    74
		for (int i = 0; i < 10; i++) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    75
			freePort = (int) (10000 + Math.round(Math.random() * 10000));
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    76
			freePort = freePort % 2 == 0 ? freePort : freePort + 1;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    77
			try {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    78
				ss = new ServerSocket(freePort);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    79
				freePort = ss.getLocalPort();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    80
				ss.close();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    81
				return freePort;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    82
			} catch (IOException e) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    83
				e.printStackTrace();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    84
			}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    85
		}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    86
		try {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    87
			ss = new ServerSocket(0);
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    88
			freePort = ss.getLocalPort();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    89
			ss.close();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    90
		} catch (IOException e) {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    91
			e.printStackTrace();
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    92
		}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    93
		return freePort;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    94
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    95
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    96
	public void stop() {
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    97
		this.killme = true;
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    98
	}
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
    99
ef115770432d un tres vieux commit pas fait sur du rtp.
nikita@nikita-rack
parents:
diff changeset
   100
}