src/jlibrtp/RTPSession.java
author nikita@nikita-rack
Thu, 09 Apr 2009 20:26:58 +0200
changeset 99 8de21ac527ce
parent 13 e684f11070d5
permissions -rw-r--r--
revert pour refaire un push propre
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     1
/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     2
 * Java RTP Library (jlibrtp)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     3
 * Copyright (C) 2006 Arne Kepp
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     4
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     5
 * This library is free software; you can redistribute it and/or
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     7
 * License as published by the Free Software Foundation; either
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     8
 * version 2.1 of the License, or (at your option) any later version.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
     9
 *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    10
 * This library is distributed in the hope that it will be useful,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    13
 * Lesser General Public License for more details.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    14
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    16
 * License along with this library; if not, write to the Free Software
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    18
 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    19
package jlibrtp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    20
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    21
import java.net.DatagramSocket;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    22
import java.net.MulticastSocket;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    23
import java.net.DatagramPacket;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    24
import java.net.InetAddress;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    25
import java.net.InetSocketAddress;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    26
import java.util.Iterator;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    27
import java.util.concurrent.locks.*;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    28
import java.util.Random;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    29
import java.util.Enumeration;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    30
/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    31
 * The RTPSession object is the core of jlibrtp. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    32
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    33
 * One should be instantiated for every communication channel, i.e. if you send voice and video, you should create one for each.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    34
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    35
 * The instance holds a participant database, as well as other information about the session. When the application registers with the session, the necessary threads for receiving and processing RTP packets are spawned.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    36
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    37
 * RTP Packets are sent synchronously, all other operations are asynchronous.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    38
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    39
 * @author Arne Kepp
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    40
 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    41
public class RTPSession {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    42
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    43
	  * The debug level is final to avoid compilation of if-statements.</br>
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    44
	  * 0 provides no debugging information, 20 provides everything </br>
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    45
	  * Debug output is written to System.out</br>
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    46
	  * Debug level for RTP related things.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    47
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    48
	 final static public int rtpDebugLevel = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    49
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    50
	  * The debug level is final to avoid compilation of if-statements.</br>
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    51
	  * 0 provides no debugging information, 20 provides everything </br>
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    52
	  * Debug output is written to System.out</br>
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    53
	  * Debug level for RTCP related things.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    54
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    55
	 final static public int rtcpDebugLevel = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    56
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    57
	 /** RTP unicast socket */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    58
	 protected DatagramSocket rtpSock = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    59
	 /** RTP multicast socket */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    60
	 protected MulticastSocket rtpMCSock = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    61
	 /** RTP multicast group */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    62
	 protected InetAddress mcGroup = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    63
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    64
	 // Internal state
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    65
	 /** Whether this session is a multicast session or not */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    66
	 protected boolean mcSession = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    67
	 /** Current payload type, can be changed by application */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    68
	 protected int payloadType = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    69
	 /** SSRC of this session */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    70
	 protected long ssrc;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    71
	 /** The last timestamp when we sent something */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    72
	 protected long lastTimestamp = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    73
	 /** Current sequence number */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    74
	 protected int seqNum = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    75
	 /** Number of packets sent by this session */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    76
	 protected int sentPktCount = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    77
	 /** Number of octets sent by this session */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    78
	 protected int sentOctetCount = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    79
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    80
	 /** The random seed */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    81
	 protected Random random = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    82
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    83
	 /** Session bandwidth in BYTES per second */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    84
	 protected int bandwidth = 8000;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    85
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    86
	 /** By default we do not return packets from strangers in unicast mode */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    87
	 protected boolean naiveReception = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    88
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    89
	 /** Should the library attempt frame reconstruction? */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    90
	 protected boolean frameReconstruction = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    91
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    92
	 /** Maximum number of packets used for reordering */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    93
	 protected int pktBufBehavior = 3;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    94
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    95
	 /** Participant database */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    96
	 protected ParticipantDatabase partDb = new ParticipantDatabase(this); 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    97
	 /** Handle to application interface for RTP */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    98
	 protected RTPAppIntf appIntf = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    99
	 /** Handle to application interface for RTCP (optional) */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   100
	 protected RTCPAppIntf rtcpAppIntf = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   101
	 /** Handle to application interface for AVPF, RFC 4585 (optional) */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   102
	 protected RTCPAVPFIntf rtcpAVPFIntf = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   103
	 /** Handle to application interface for debugging */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   104
	 protected DebugAppIntf debugAppIntf = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   105
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   106
	 /** The RTCP session associated with this RTP Session */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   107
	 protected RTCPSession rtcpSession = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   108
	 /** The thread for receiving RTP packets */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   109
	 protected RTPReceiverThread recvThrd = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   110
	 /** The thread for invoking callbacks for RTP packets */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   111
	 protected AppCallerThread appCallerThrd = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   112
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   113
	 /** Lock to protect the packet buffers */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   114
	 final protected Lock pktBufLock = new ReentrantLock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   115
	 /** Condition variable, to tell the  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   116
	 final protected Condition pktBufDataReady = pktBufLock.newCondition();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   117
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   118
	 /** Enough is enough, set to true when you want to quit. */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   119
	 protected boolean endSession = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   120
	 /** Only one registered application, please */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   121
	 protected boolean registered = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   122
	 /** We're busy resolving a SSRC conflict, please try again later */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   123
	 protected boolean conflict = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   124
	 /** Number of conflicts observed, exessive number suggests loop in network */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   125
	 protected int conflictCount = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   126
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   127
	 /** SDES CNAME */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   128
	 protected String cname = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   129
	 /** SDES The participant's real name */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   130
	 public String name = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   131
	 /** SDES The participant's email */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   132
	 public String email = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   133
	 /** SDES The participant's phone number */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   134
	 public String phone = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   135
	 /** SDES The participant's location*/
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   136
	 public String loc = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   137
	 /** SDES The tool the participants is using */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   138
	 public String tool = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   139
	 /** SDES A note */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   140
	 public String note = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   141
	 /** SDES A priv string, loosely defined */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   142
	 public String priv = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   143
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   144
	 // RFC 4585 stuff. This should live on RTCPSession, but we need to have this
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   145
	 // infromation ready by the time the RTCP Session starts
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   146
	 // 0 = RFC 3550 , -1 = ACK , 1 = Immediate feedback, 2 = Early RTCP,  
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   147
	 protected int rtcpMode = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   148
	 protected int fbEarlyThreshold = -1;		// group size, immediate -> early transition point
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   149
	 protected int fbRegularThreshold = -1;	// group size, early -> regular transition point
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   150
	 protected int minInterval = 5000;		// minimum interval
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   151
	 protected int fbMaxDelay = 1000;			// how long the information is useful
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   152
	 // RTCP bandwidth
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   153
	 protected int rtcpBandwidth = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   154
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   155
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   156
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   157
	  * Returns an instance of a <b>unicast</b> RTP session. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   158
	  * Following this you should adjust any settings and then register your application.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   159
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   160
	  * The sockets should have external ip addresses, else your CNAME automatically
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   161
	  * generated CNAMe will be bad.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   162
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   163
	  * @param	rtpSocket UDP socket to receive RTP communication on
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   164
	  * @param	rtcpSocket UDP socket to receive RTCP communication on, null if none.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   165
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   166
	 public RTPSession(DatagramSocket rtpSocket, DatagramSocket rtcpSocket) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   167
		 mcSession = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   168
		 rtpSock = rtpSocket;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   169
		 this.generateCNAME();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   170
		 this.generateSsrc();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   171
		 this.rtcpSession = new RTCPSession(this,rtcpSocket);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   172
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   173
		 // The sockets are not always imediately available?
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   174
		 try { Thread.sleep(1); } catch (InterruptedException e) { System.out.println("RTPSession sleep failed"); }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   175
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   176
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   177
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   178
	  * Returns an instance of a <b>multicast</b> RTP session. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   179
	  * Following this you should register your application.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   180
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   181
	  * The sockets should have external ip addresses, else your CNAME automatically
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   182
	  * generated CNAMe will be bad.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   183
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   184
	  * @param	rtpSock a multicast socket to receive RTP communication on
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   185
	  * @param	rtcpSock a multicast socket to receive RTP communication on
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   186
	  * @param	multicastGroup the multicast group that we want to communicate with.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   187
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   188
	 public RTPSession(MulticastSocket rtpSock, MulticastSocket rtcpSock, InetAddress multicastGroup) throws Exception {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   189
		 mcSession = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   190
		 rtpMCSock =rtpSock;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   191
		 mcGroup = multicastGroup;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   192
		 rtpMCSock.joinGroup(mcGroup);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   193
		 rtcpSock.joinGroup(mcGroup);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   194
		 this.generateCNAME();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   195
		 this.generateSsrc();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   196
		 this.rtcpSession = new RTCPSession(this,rtcpSock,mcGroup);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   197
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   198
		 // The sockets are not always imediately available?
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   199
		 try { Thread.sleep(1); } catch (InterruptedException e) { System.out.println("RTPSession sleep failed"); }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   200
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   201
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   202
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   203
	  * Registers an application (RTPAppIntf) with the RTP session.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   204
	  * The session will call receiveData() on the supplied instance whenever data has been received.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   205
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   206
	  * Following this you should set the payload type and add participants to the session.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   207
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   208
	  * @param	rtpApp an object that implements the RTPAppIntf-interface
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   209
	  * @param	rtcpApp an object that implements the RTCPAppIntf-interface (optional)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   210
	  * @return	-1 if this RTPSession-instance already has an application registered.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   211
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   212
	 public int RTPSessionRegister(RTPAppIntf rtpApp, RTCPAppIntf rtcpApp, DebugAppIntf debugApp) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   213
		if(registered) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   214
			System.out.println("RTPSessionRegister(): Can\'t register another application!");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   215
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   216
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   217
			registered = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   218
			generateSeqNum();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   219
			if(RTPSession.rtpDebugLevel > 0) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   220
				System.out.println("-> RTPSessionRegister");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   221
			}  
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   222
			this.appIntf = rtpApp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   223
			this.rtcpAppIntf = rtcpApp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   224
			this.debugAppIntf = debugApp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   225
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   226
			recvThrd = new RTPReceiverThread(this);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   227
			appCallerThrd = new AppCallerThread(this, rtpApp);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   228
			recvThrd.start();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   229
		 	appCallerThrd.start();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   230
		 	rtcpSession.start();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   231
		 	return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   232
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   233
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   234
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   235
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   236
	  * Send data to all participants registered as receivers, using the current timeStamp,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   237
	  * dynamic sequence number and the current payload type specified for the session.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   238
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   239
	  * @param buf A buffer of bytes, less than 1496 bytes
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   240
	  * @return	null if there was a problem, {RTP Timestamp, Sequence number} otherwise
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   241
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   242
	 public long[] sendData(byte[] buf) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   243
		 byte[][] tmp = {buf}; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   244
		 long[][] ret = this.sendData(tmp, null, null, -1, null);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   245
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   246
		 if(ret != null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   247
			 return ret[0];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   248
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   249
		 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   250
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   251
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   252
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   253
	  * Send data to all participants registered as receivers, using the specified timeStamp,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   254
	  * sequence number and the current payload type specified for the session.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   255
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   256
	  * @param buf A buffer of bytes, less than 1496 bytes
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   257
	  * @param rtpTimestamp the RTP timestamp to be used in the packet
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   258
	  * @param seqNum the sequence number to be used in the packet
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   259
	  * @return null if there was a problem, {RTP Timestamp, Sequence number} otherwise
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   260
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   261
	 public long[] sendData(byte[] buf, long rtpTimestamp, long seqNum) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   262
		 byte[][] tmp = {buf};
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   263
		 long[][] ret = this.sendData(tmp, null, null, -1, null);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   264
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   265
		 if(ret != null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   266
			 return ret[0];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   267
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   268
		 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   269
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   270
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   271
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   272
	  * Send data to all participants registered as receivers, using the current timeStamp and
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   273
	  * payload type. The RTP timestamp will be the same for all the packets.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   274
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   275
	  * @param buffers A buffer of bytes, should not bed padded and less than 1500 bytes on most networks.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   276
	  * @param csrcArray an array with the SSRCs of contributing sources
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   277
	  * @param markers An array indicating what packets should be marked. Rarely anything but the first one
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   278
	  * @param rtpTimestamp The RTP timestamp to be applied to all packets
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   279
	  * @param seqNumbers An array with the sequence number associated with each byte[]
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   280
	  * @return	null if there was a problem sending the packets, 2-dim array with {RTP Timestamp, Sequence number}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   281
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   282
	 public long[][] sendData(byte[][] buffers, long[] csrcArray, boolean[] markers, long rtpTimestamp, long[] seqNumbers) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   283
		 if(RTPSession.rtpDebugLevel > 5) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   284
			 System.out.println("-> RTPSession.sendData(byte[])");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   285
		 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   286
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   287
		 // Same RTP timestamp for all
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   288
		 if(rtpTimestamp < 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   289
			 rtpTimestamp = System.currentTimeMillis();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   290
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   291
		 // Return values
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   292
		 long[][] ret = new long[buffers.length][2];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   293
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   294
		 for(int i=0; i<buffers.length; i++) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   295
			 byte[] buf = buffers[i];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   296
			 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   297
			 boolean marker = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   298
			 if(markers != null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   299
				  marker = markers[i];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   300
			 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   301
			 if(buf.length > 1500) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   302
				 System.out.println("RTPSession.sendData() called with buffer exceeding 1500 bytes ("+buf.length+")");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   303
			 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   304
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   305
			 // Get the return values
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   306
			 ret[i][0] = rtpTimestamp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   307
			 if(seqNumbers == null) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   308
				 ret[i][1] = getNextSeqNum();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   309
			 } else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   310
				 ret[i][1] = seqNumbers[i];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   311
			 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   312
			 // Create a new RTP Packet
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   313
			 RtpPkt pkt = new RtpPkt(rtpTimestamp,this.ssrc,(int) ret[i][1],this.payloadType,buf);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   314
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   315
			 if(csrcArray != null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   316
				 pkt.setCsrcs(csrcArray);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   317
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   318
			 pkt.setMarked(marker);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   319
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   320
			 // Creates a raw packet
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   321
			 byte[] pktBytes = pkt.encode();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   322
			 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   323
			 //System.out.println(Integer.toString(StaticProcs.bytesToUIntInt(pktBytes, 2)));
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   324
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   325
			 // Pre-flight check, are resolving an SSRC conflict?
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   326
			 if(this.conflict) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   327
				 System.out.println("RTPSession.sendData() called while trying to resolve conflict.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   328
				 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   329
			 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   330
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   331
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   332
			 if(this.mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   333
				 DatagramPacket packet = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   334
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   335
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   336
				 try {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   337
					 packet = new DatagramPacket(pktBytes,pktBytes.length,this.mcGroup,this.rtpMCSock.getPort());
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   338
				 } catch (Exception e) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   339
					 System.out.println("RTPSession.sendData() packet creation failed.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   340
					 e.printStackTrace();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   341
					 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   342
				 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   343
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   344
				 try {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   345
					 rtpMCSock.send(packet);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   346
					 //Debug
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   347
					 if(this.debugAppIntf != null) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   348
						 this.debugAppIntf.packetSent(1, (InetSocketAddress) packet.getSocketAddress(), 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   349
								 new String("Sent multicast RTP packet of size " + packet.getLength() + 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   350
										 " to " + packet.getSocketAddress().toString() + " via " 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   351
										 + rtpMCSock.getLocalSocketAddress().toString()));
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   352
					 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   353
				 } catch (Exception e) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   354
					 System.out.println("RTPSession.sendData() multicast failed.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   355
					 e.printStackTrace();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   356
					 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   357
				 }		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   358
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   359
			 } else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   360
				 // Loop over recipients
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   361
				 Iterator<Participant> iter = partDb.getUnicastReceivers();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   362
				 while(iter.hasNext()) {			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   363
					 InetSocketAddress receiver = iter.next().rtpAddress;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   364
					 DatagramPacket packet = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   365
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   366
					 if(RTPSession.rtpDebugLevel > 15) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   367
						 System.out.println("   Sending to " + receiver.toString());
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   368
					 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   369
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   370
					 try {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   371
						 packet = new DatagramPacket(pktBytes,pktBytes.length,receiver);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   372
					 } catch (Exception e) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   373
						 System.out.println("RTPSession.sendData() packet creation failed.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   374
						 e.printStackTrace();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   375
						 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   376
					 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   377
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   378
					 //Actually send the packet
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   379
					 try {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   380
						 rtpSock.send(packet);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   381
						 //Debug
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   382
						 if(this.debugAppIntf != null) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   383
							 this.debugAppIntf.packetSent(0, (InetSocketAddress) packet.getSocketAddress(), 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   384
									 new String("Sent unicast RTP packet of size " + packet.getLength() + 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   385
											 " to " + packet.getSocketAddress().toString() + " via " 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   386
											 + rtpSock.getLocalSocketAddress().toString()));
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   387
						 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   388
					 } catch (Exception e) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   389
						 System.out.println("RTPSession.sendData() unicast failed.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   390
						 e.printStackTrace();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   391
						 return null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   392
					 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   393
				 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   394
			 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   395
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   396
			 //Update our stats
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   397
			 this.sentPktCount++;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   398
			 this.sentOctetCount++;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   399
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   400
			 if(RTPSession.rtpDebugLevel > 5) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   401
				 System.out.println("<- RTPSession.sendData(byte[]) " + pkt.getSeqNumber());
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   402
			 }  
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   403
		 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   404
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   405
		 return ret;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   406
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   407
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   408
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   409
	  * Send RTCP App packet to receiver specified by ssrc
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   410
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   411
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   412
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   413
	  * Return values:
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   414
	  *  0 okay
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   415
	  * -1 no RTCP session established
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   416
	  * -2 name is not byte[4];
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   417
	  * -3 data is not byte[x], where x = 4*y for syme y
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   418
	  * -4 type is not a 5 bit unsigned integer
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   419
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   420
	  * Note that a return value of 0 does not guarantee delivery.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   421
	  * The participant must also exist in the participant database,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   422
	  * otherwise the message will eventually be deleted.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   423
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   424
	  * @param ssrc of the participant you want to reach
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   425
	  * @param type the RTCP App packet subtype, default 0
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   426
	  * @param name the ASCII (in byte[4]) representation
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   427
	  * @param data the data itself 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   428
	  * @return 0 if okay, negative value otherwise (see above)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   429
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   430
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   431
	 public int sendRTCPAppPacket(long ssrc, int type, byte[] name, byte[] data) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   432
		 if(this.rtcpSession == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   433
			 return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   434
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   435
		 if(name.length != 4)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   436
			 return -2;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   437
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   438
		 if(data.length % 4 != 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   439
			 return -3;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   440
		 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   441
		 if(type > 63 || type < 0 )
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   442
			 return -4;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   443
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   444
		RtcpPktAPP pkt = new RtcpPktAPP(ssrc, type, name, data);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   445
		this.rtcpSession.addToAppQueue(ssrc, pkt);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   446
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   447
		return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   448
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   449
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   450
	  * Add a participant object to the participant database.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   451
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   452
	  * If packets have already been received from this user, we will try to update the automatically inserted participant with the information provided here.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   453
	  *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   454
	  * @param p A participant.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   455
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   456
	public int addParticipant(Participant p) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   457
		//For now we make all participants added this way persistent
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   458
		p.unexpected = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   459
		return this.partDb.addParticipant(0, p);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   460
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   461
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   462
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   463
	  * Remove a participant from the database. All buffered packets will be destroyed.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   464
	  *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   465
	  * @param p A participant.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   466
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   467
	 public void removeParticipant(Participant p) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   468
		partDb.removeParticipant(p);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   469
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   470
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   471
	 public Iterator<Participant> getUnicastReceivers() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   472
		 return partDb.getUnicastReceivers();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   473
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   474
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   475
	 public Enumeration<Participant> getParticipants() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   476
		 return partDb.getParticipants();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   477
	 }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   478
	 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   479
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   480
	  * End the RTP Session. This will halt all threads and send bye-messages to other participants.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   481
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   482
	  * RTCP related threads may require several seconds to wake up and terminate.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   483
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   484
	public void endSession() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   485
		this.endSession = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   486
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   487
		// No more RTP packets, please
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   488
		if(this.mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   489
			this.rtpMCSock.close();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   490
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   491
			this.rtpSock.close();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   492
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   493
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   494
		// Signal the thread that pushes data to application
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   495
		this.pktBufLock.lock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   496
		try { this.pktBufDataReady.signalAll(); } finally {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   497
			this.pktBufLock.unlock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   498
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   499
		// Interrupt what may be sleeping
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   500
		this.rtcpSession.senderThrd.interrupt();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   501
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   502
		// Give things a chance to cool down.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   503
		try { Thread.sleep(50); } catch (Exception e){ };
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   504
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   505
		this.appCallerThrd.interrupt();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   506
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   507
		// Give things a chance to cool down.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   508
		try { Thread.sleep(50); } catch (Exception e){ };
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   509
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   510
		if(this.rtcpSession != null) {		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   511
			// No more RTP packets, please
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   512
			if(this.mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   513
				this.rtcpSession.rtcpMCSock.close();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   514
			} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   515
				this.rtcpSession.rtcpSock.close();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   516
			}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   517
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   518
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   519
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   520
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   521
	 /**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   522
	  * Check whether this session is ending.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   523
	  * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   524
	  * @return true if session and associated threads are terminating.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   525
	  */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   526
	boolean isEnding() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   527
		return this.endSession;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   528
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   529
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   530
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   531
	 * Overrides CNAME, used for outgoing RTCP packets.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   532
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   533
	 * @param cname a string, e.g. username@hostname. Must be unique for session.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   534
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   535
	public void CNAME(String cname) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   536
		this.cname = cname;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   537
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   538
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   539
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   540
	 * Get the current CNAME, used for outgoing SDES packets
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   541
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   542
	public String CNAME() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   543
		return this.cname;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   544
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   545
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   546
	public long getSsrc() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   547
		return this.ssrc;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   548
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   549
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   550
	private void generateCNAME() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   551
		String hostname;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   552
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   553
		if(this.mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   554
			hostname = this.rtpMCSock.getLocalAddress().getCanonicalHostName();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   555
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   556
			hostname = this.rtpSock.getLocalAddress().getCanonicalHostName();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   557
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   558
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   559
		//if(hostname.equals("0.0.0.0") && System.getenv("HOSTNAME") != null) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   560
		//	hostname = System.getenv("HOSTNAME");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   561
		//}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   562
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   563
		cname = System.getProperty("user.name") + "@" + hostname;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   564
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   565
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   566
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   567
	 * Change the RTP socket of the session. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   568
	 * Peers must be notified through SIP or other signalling protocol.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   569
	 * Only valid if this is a unicast session to begin with.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   570
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   571
	 * @param newSock integer for new port number, check it is free first.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   572
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   573
	public int updateRTPSock(DatagramSocket newSock) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   574
		if(!mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   575
			 rtpSock = newSock;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   576
			 return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   577
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   578
			System.out.println("Can't switch from multicast to unicast.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   579
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   580
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   581
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   582
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   583
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   584
	 * Change the RTCP socket of the session. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   585
	 * Peers must be notified through SIP or other signalling protocol.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   586
	 * Only valid if this is a unicast session to begin with.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   587
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   588
	 * @param newSock the new unicast socket for RTP communication.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   589
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   590
	public int updateRTCPSock(DatagramSocket newSock) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   591
		if(!mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   592
			this.rtcpSession.rtcpSock = newSock;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   593
			return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   594
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   595
			System.out.println("Can't switch from multicast to unicast.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   596
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   597
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   598
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   599
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   600
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   601
	 * Change the RTP multicast socket of the session. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   602
	 * Peers must be notified through SIP or other signalling protocol.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   603
	 * Only valid if this is a multicast session to begin with.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   604
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   605
	 * @param newSock the new multicast socket for RTP communication.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   606
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   607
	public int updateRTPSock(MulticastSocket newSock) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   608
		if(mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   609
			 this.rtpMCSock = newSock;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   610
			 return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   611
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   612
			System.out.println("Can't switch from unicast to multicast.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   613
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   614
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   615
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   616
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   617
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   618
	 * Change the RTCP multicast socket of the session. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   619
	 * Peers must be notified through SIP or other signalling protocol.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   620
	 * Only valid if this is a multicast session to begin with.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   621
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   622
	 * @param newSock the new multicast socket for RTCP communication.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   623
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   624
	public int updateRTCPSock(MulticastSocket newSock) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   625
		if(mcSession) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   626
			this.rtcpSession.rtcpMCSock = newSock;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   627
			return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   628
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   629
			System.out.println("Can't switch from unicast to multicast.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   630
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   631
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   632
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   633
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   634
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   635
	 * Update the payload type used for the session. It is represented as a 7 bit integer, whose meaning must be negotiated elsewhere (see IETF RFCs <a href="http://www.ietf.org/rfc/rfc3550.txt">3550</a> and <a href="http://www.ietf.org/rfc/rfc3550.txt">3551</a>)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   636
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   637
	 * @param payloadT an integer representing the payload type of any subsequent packets that are sent.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   638
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   639
	public int payloadType(int payloadT) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   640
		if(payloadT > 128 || payloadT < 0) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   641
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   642
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   643
			this.payloadType = payloadT;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   644
			return this.payloadType;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   645
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   646
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   647
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   648
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   649
	 * Get the payload type that is currently used for outgoing RTP packets.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   650
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   651
	 * @return payload type as integer
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   652
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   653
	public int payloadType() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   654
		return this.payloadType;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   655
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   656
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   657
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   658
	 * Should packets from unknown participants be returned to the application? This can be dangerous.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   659
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   660
	 * @param doAccept packets from participants not added by the application.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   661
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   662
	public void naivePktReception(boolean doAccept) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   663
		naiveReception = doAccept;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   664
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   665
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   666
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   667
	 * Are packets from unknown participants returned to the application?
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   668
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   669
	 * @return whether we accept packets from participants not added by the application.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   670
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   671
	public boolean naivePktReception() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   672
		return naiveReception;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   673
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   674
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   675
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   676
	 * Set the number of RTP packets that should be buffered when a packet is
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   677
	 * missing or received out of order. Setting this number high increases
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   678
	 * the chance of correctly reordering packets, but increases latency when
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   679
	 * a packet is dropped by the network.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   680
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   681
	 * Packets that arrive in order are not affected, they are passed straight
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   682
	 * to the application.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   683
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   684
	 * The maximum delay is numberofPackets * packet rate , where the packet rate
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   685
	 * depends on the codec and profile used by the sender.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   686
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   687
	 * Valid values:
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   688
	 *  >0 - The maximum number of packets (based on RTP Timestamp) that may accumulate
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   689
	 *  0 - All valid packets received in order will be given to the application
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   690
	 * -1 - All valid packets will be given to the application
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   691
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   692
	 * @param behavior the be
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   693
	 * @return the behavior set, unchanged in the case of a erroneous value
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   694
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   695
	public int packetBufferBehavior(int behavior) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   696
		if(behavior > -2) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   697
			this.pktBufBehavior = behavior; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   698
			// Signal the thread that pushes data to application
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   699
			this.pktBufLock.lock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   700
			try { this.pktBufDataReady.signalAll(); } finally {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   701
				this.pktBufLock.unlock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   702
			}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   703
			return this.pktBufBehavior;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   704
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   705
			return this.pktBufBehavior;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   706
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   707
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   708
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   709
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   710
	 * The number of RTP packets that should be buffered when a packet is
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   711
	 * missing or received out of order. A high number  increases the chance 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   712
	 * of correctly reordering packets, but increases latency when a packet is 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   713
	 * dropped by the network.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   714
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   715
	 * A negative value disables the buffering, out of order packets will simply be dropped.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   716
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   717
	 * @return the maximum number of packets that can accumulate before the first is returned
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   718
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   719
	public int packetBufferBehavior() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   720
		return this.pktBufBehavior;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   721
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   722
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   723
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   724
	 * Set whether the stack should operate in RFC 4585 mode.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   725
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   726
	 * This will automatically call adjustPacketBufferBehavior(-1),
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   727
	 * i.e. disable all RTP packet buffering in jlibrtp,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   728
	 * and disable frame reconstruction 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   729
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   730
	 * @param rtcpAVPFIntf the in
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   731
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   732
	public int registerAVPFIntf(RTCPAVPFIntf rtcpAVPFIntf, int maxDelay, int earlyThreshold, int regularThreshold ) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   733
		if(this.rtcpSession != null) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   734
			this.packetBufferBehavior(-1);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   735
			this.frameReconstruction = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   736
			this.rtcpAVPFIntf = rtcpAVPFIntf;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   737
			this.fbEarlyThreshold = earlyThreshold;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   738
			this.fbRegularThreshold = regularThreshold;	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   739
			return 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   740
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   741
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   742
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   743
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   744
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   745
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   746
	 * Unregisters the RTCP AVPF interface, thereby going from
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   747
	 * RFC 4585 mode to RFC 3550
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   748
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   749
	 * You still have to adjust packetBufferBehavior() and
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   750
	 * frameReconstruction.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   751
	 * 	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   752
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   753
	public void unregisterAVPFIntf() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   754
		this.fbEarlyThreshold = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   755
		this.fbRegularThreshold = -1;	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   756
		this.rtcpAVPFIntf = null;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   757
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   758
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   759
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   760
	 * Enable / disable frame reconstruction in the packet buffers.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   761
	 * This is only relevant if getPacketBufferBehavior > 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   762
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   763
	 * Default is true.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   764
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   765
	public void frameReconstruction(boolean toggle) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   766
		this.frameReconstruction = toggle;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   767
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   768
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   769
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   770
	 * Whether the packet buffer will attempt to reconstruct
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   771
	 * packet automatically.  
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   772
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   773
	 * @return the status
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   774
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   775
	public boolean frameReconstruction() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   776
		return this.frameReconstruction;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   777
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   778
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   779
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   780
	 * The bandwidth currently allocated to the session,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   781
	 * in bytes per second. The default is 8000.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   782
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   783
	 * This value is not enforced and currently only
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   784
	 * used to calculate the RTCP interval to ensure the
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   785
	 * control messages do not exceed 5% of the total bandwidth
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   786
	 * described here.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   787
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   788
	 * Since the actual value may change a conservative
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   789
	 * estimate should be used to avoid RTCP flooding.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   790
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   791
	 * see rtcpBandwidth(void)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   792
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   793
	 * @return current bandwidth setting
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   794
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   795
	public int sessionBandwidth() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   796
		return this.bandwidth;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   797
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   798
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   799
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   800
	 * Set the bandwidth of the session.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   801
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   802
	 * See sessionBandwidth(void) for details. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   803
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   804
	 * @param bandwidth the new value requested, in bytes per second
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   805
	 * @return the actual value set
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   806
 	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   807
	public int sessionBandwidth(int bandwidth) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   808
		if(bandwidth < 1) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   809
			this.bandwidth = 8000;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   810
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   811
			this.bandwidth = bandwidth;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   812
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   813
		return this.bandwidth;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   814
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   815
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   816
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   817
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   818
	 * RFC 3550 dictates that 5% of the total bandwidth,
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   819
	 * as set by sessionBandwidth, should be dedicated
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   820
	 * to RTCP traffic. This 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   821
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   822
	 * This should normally not be done, but is permissible in 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   823
	 * conjunction with feedback (RFC 4585) and possibly
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   824
	 * other profiles. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   825
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   826
	 * Also see sessionBandwidth(void)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   827
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   828
	 * @return current RTCP bandwidth setting, -1 means not in use
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   829
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   830
	public int rtcpBandwidth() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   831
		return this.rtcpBandwidth;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   832
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   833
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   834
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   835
	 * Set the RTCP bandwidth, see rtcpBandwidth(void) for details. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   836
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   837
	 * This function must be
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   838
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   839
	 * @param bandwidth the new value requested, in bytes per second or -1 to disable
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   840
	 * @return the actual value set
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   841
 	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   842
	public int rtcpBandwidth(int bandwidth) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   843
		if(bandwidth < -1) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   844
			this.rtcpBandwidth = -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   845
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   846
			this.rtcpBandwidth = bandwidth;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   847
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   848
		return this.rtcpBandwidth;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   849
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   850
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   851
	/********************************************* Feedback message stuff ***************************************/
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   852
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   853
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   854
	 * Adds a Picture Loss Indication to the feedback queue
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   855
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   856
	 * @param ssrcMediaSource
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   857
	 * @return 0 if packet was queued, -1 if no feedback support, 1 if redundant
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   858
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   859
	public int fbPictureLossIndication(long ssrcMediaSource) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   860
		int ret = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   861
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   862
		if(this.rtcpAVPFIntf == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   863
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   864
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   865
		RtcpPktPSFB pkt = new RtcpPktPSFB(this.ssrc, ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   866
		pkt.makePictureLossIndication();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   867
		ret = this.rtcpSession.addToFbQueue(ssrcMediaSource, pkt);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   868
		if(ret == 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   869
			this.rtcpSession.wakeSenderThread(ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   870
		return ret; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   871
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   872
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   873
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   874
	 * Adds a Slice Loss Indication to the feedback queue
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   875
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   876
	 * @param ssrcMediaSource
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   877
	 * @param sliFirst macroblock (MB) address of the first lost macroblock
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   878
	 * @param sliNumber number of lost macroblocks
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   879
	 * @param sliPictureId six least significant bits of the codec-specific identif
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   880
	 * @return 0 if packet was queued, -1 if no feedback support, 1 if redundant
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   881
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   882
	public int fbSlicLossIndication(long ssrcMediaSource, int[] sliFirst, int[] sliNumber, int[] sliPictureId) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   883
		int ret = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   884
		if(this.rtcpAVPFIntf == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   885
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   886
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   887
		RtcpPktPSFB pkt = new RtcpPktPSFB(this.ssrc, ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   888
		pkt.makeSliceLossIndication(sliFirst, sliNumber, sliPictureId);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   889
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   890
		ret = this.rtcpSession.addToFbQueue(ssrcMediaSource, pkt);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   891
		if(ret == 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   892
			this.rtcpSession.wakeSenderThread(ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   893
		return ret; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   894
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   895
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   896
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   897
	 * Adds a Reference Picture Selection Indication to the feedback queue
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   898
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   899
	 * @param ssrcMediaSource
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   900
	 * @param bitPadding number of padded bits at end of bitString
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   901
	 * @param payloadType RTP payload type for codec
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   902
	 * @param bitString RPSI information as natively defined by the video codec
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   903
	 * @return 0 if packet was queued, -1 if no feedback support, 1 if redundant
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   904
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   905
	public int fbRefPictureSelIndic(long ssrcMediaSource, int bitPadding, int payloadType, byte[] bitString) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   906
		int ret = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   907
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   908
		if(this.rtcpAVPFIntf == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   909
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   910
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   911
		RtcpPktPSFB pkt = new RtcpPktPSFB(this.ssrc, ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   912
		pkt.makeRefPictureSelIndic(bitPadding, payloadType, bitString);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   913
		ret = this.rtcpSession.addToFbQueue(ssrcMediaSource, pkt);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   914
		if(ret == 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   915
			this.rtcpSession.wakeSenderThread(ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   916
		return ret; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   917
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   918
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   919
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   920
	 * Adds a Picture Loss Indication to the feedback queue
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   921
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   922
	 * @param ssrcMediaSource
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   923
	 * @param bitString the original application message
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   924
	 * @return 0 if packet was queued, -1 if no feedback support, 1 if redundant
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   925
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   926
	public int fbAppLayerFeedback(long ssrcMediaSource, byte[] bitString) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   927
		int ret = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   928
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   929
		if(this.rtcpAVPFIntf == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   930
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   931
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   932
		RtcpPktPSFB pkt = new RtcpPktPSFB(this.ssrc, ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   933
		pkt.makeAppLayerFeedback(bitString);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   934
		ret = this.rtcpSession.addToFbQueue(ssrcMediaSource, pkt);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   935
		if(ret == 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   936
			this.rtcpSession.wakeSenderThread(ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   937
		return ret; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   938
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   939
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   940
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   941
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   942
	 * Adds a RTP Feedback packet to the feedback queue.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   943
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   944
	 * These are mostly used for NACKs.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   945
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   946
	 * @param ssrcMediaSource
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   947
	 * @param FMT the Feedback Message Subtype
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   948
	 * @param PID RTP sequence numbers of lost packets
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   949
	 * @param BLP bitmask of following lost packets, shared index with PID 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   950
	 * @return 0 if packet was queued, -1 if no feedback support, 1 if redundant
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   951
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   952
	public int fbPictureLossIndication(long ssrcMediaSource, int FMT, int[] PID, int[] BLP) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   953
		int ret = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   954
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   955
		if(this.rtcpAVPFIntf == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   956
			return -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   957
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   958
		RtcpPktRTPFB pkt = new RtcpPktRTPFB(this.ssrc, ssrcMediaSource, FMT, PID, BLP);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   959
		ret = this.rtcpSession.addToFbQueue(ssrcMediaSource, pkt);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   960
		if(ret == 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   961
			this.rtcpSession.wakeSenderThread(ssrcMediaSource);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   962
		return ret; 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   963
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   964
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   965
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   966
	 * Fetches the next sequence number for RTP packets.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   967
	 * @return the next sequence number 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   968
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   969
	private int getNextSeqNum() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   970
		seqNum++;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   971
		// 16 bit number
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   972
		if(seqNum > 65536) { 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   973
			seqNum = 0;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   974
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   975
		return seqNum;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   976
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   977
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   978
	/** 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   979
	 * Initializes a random variable
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   980
	 *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   981
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   982
	private void createRandom() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   983
		this.random = new Random(System.currentTimeMillis() + Thread.currentThread().getId() 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   984
				- Thread.currentThread().hashCode() + this.cname.hashCode());
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   985
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   986
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   987
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   988
	/** 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   989
	 * Generates a random sequence number
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   990
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   991
	private void generateSeqNum() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   992
		if(this.random == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   993
			createRandom();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   994
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   995
		seqNum = this.random.nextInt();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   996
		if(seqNum < 0)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   997
			seqNum = -seqNum;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   998
		while(seqNum > 65535) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   999
			seqNum = seqNum / 10;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1000
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1001
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1002
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1003
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1004
	 * Generates a random SSRC
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1005
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1006
	private void generateSsrc() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1007
		if(this.random == null)
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1008
			createRandom();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1009
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1010
		// Set an SSRC
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1011
		this.ssrc = this.random.nextInt();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1012
		if(this.ssrc < 0) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1013
			this.ssrc = this.ssrc * -1;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1014
		}	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1015
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1016
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1017
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1018
	 * Resolve an SSRC conflict.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1019
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1020
	 * Also increments the SSRC conflict counter, after 5 conflicts
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1021
	 * it is assumed there is a loop somewhere and the session will
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1022
	 * terminate. 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1023
	 *
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1024
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1025
	protected void resolveSsrcConflict() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1026
		System.out.println("!!!!!!! Beginning SSRC conflict resolution !!!!!!!!!");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1027
		this.conflictCount++;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1028
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1029
		if(this.conflictCount < 5) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1030
			//Don't send any more regular packets out until we have this sorted out.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1031
			this.conflict = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1032
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1033
			//Send byes
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1034
			rtcpSession.sendByes();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1035
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1036
			//Calculate the next delay
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1037
			rtcpSession.calculateDelay();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1038
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1039
			//Generate a new Ssrc for ourselves
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1040
			generateSsrc();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1041
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1042
			//Get the SDES packets out faster
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1043
			rtcpSession.initial = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1044
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1045
			this.conflict = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1046
			System.out.println("SSRC conflict resolution complete");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1047
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1048
		} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1049
			System.out.println("Too many conflicts. There is probably a loop in the network.");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1050
			this.endSession();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1051
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1052
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
  1053
}