src/jlibrtp/AppCallerThread.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.util.Enumeration;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    22
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    23
/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    24
 * The purpose of this thread is to check whether there are packets ready from 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    25
 * any participants.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    26
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    27
 * It should sleep when not in use, and be woken up by a condition variable.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    28
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    29
 * Optionally, if we do jitter-control, the condition variable should have a max waiting period 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    30
 * equal to how often we need to push data.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    31
 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    32
 * @author Arne Kepp
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    33
 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    34
public class AppCallerThread extends Thread {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    35
	/**  The parent RTP Session */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    36
	RTPSession rtpSession;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    37
	/**  The applications interface, where the callback methods are called */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    38
	RTPAppIntf appl;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    39
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    40
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    41
	 * Instatiates the AppCallerThread
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    42
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    43
	 * @param session the RTPSession with participants etc
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    44
	 * @param rtpApp the interface to which data is given
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    45
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    46
	protected AppCallerThread(RTPSession session, RTPAppIntf rtpApp) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    47
		rtpSession = session;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    48
		appl = rtpApp;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    49
		if(RTPSession.rtpDebugLevel > 1) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    50
			System.out.println("<-> AppCallerThread created");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    51
		}  
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    52
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    53
	
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    54
	/**
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    55
	 * The AppCallerThread will run in this loop until the RTPSession
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    56
	 * is terminated.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    57
	 * 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    58
	 * Whenever an RTP packet is received it will loop over the
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    59
	 * participants to check for packet buffers that have available
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    60
	 * frame.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    61
	 */
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    62
	public void run() {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    63
		if(RTPSession.rtpDebugLevel > 3) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    64
			System.out.println("-> AppCallerThread.run()");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    65
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    66
		
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    67
		while(rtpSession.endSession == false) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    68
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    69
			rtpSession.pktBufLock.lock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    70
		    try {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    71
				if(RTPSession.rtpDebugLevel > 4) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    72
					System.out.println("<-> AppCallerThread going to Sleep");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    73
				}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    74
				
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    75
				try { rtpSession.pktBufDataReady.await(); } 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    76
					catch (Exception e) { System.out.println("AppCallerThread:" + e.getMessage());}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    77
					
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    78
		    	// Next loop over all participants and check whether they have anything for us.
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    79
				Enumeration<Participant> enu = rtpSession.partDb.getParticipants();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    80
				
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    81
				while(enu.hasMoreElements()) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    82
					Participant p = enu.nextElement(); 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    83
					
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    84
					boolean done = false;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    85
					//System.out.println(p.ssrc + " " + !done +" " + p.rtpAddress 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    86
					//		+ " " + rtpSession.naiveReception + " " + p.pktBuffer);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    87
					//System.out.println("done: " + done + "  p.unexpected: " + p.unexpected);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    88
					while(!done && (!p.unexpected || rtpSession.naiveReception) 
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    89
							&& p.pktBuffer != null && p.pktBuffer.length > 0) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    90
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    91
						DataFrame aFrame = p.pktBuffer.popOldestFrame();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    92
						if(aFrame == null) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    93
							done = true;
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    94
						} else {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    95
							appl.receiveData(aFrame, p);
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    96
						}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    97
					}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    98
				}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
    99
		    
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   100
		     } finally {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   101
		       rtpSession.pktBufLock.unlock();
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   102
		     }
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   103
			
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   104
		}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   105
		if(RTPSession.rtpDebugLevel > 3) {
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   106
			System.out.println("<- AppCallerThread.run() terminating");
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   107
		}  
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   108
	}
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   109
e684f11070d5 ajout de jlibrtp
nikita@nikita-rack
parents:
diff changeset
   110
}