src/jlibrtp/AppCallerThread.java
changeset 214 2bf440c54ca5
parent 213 9bdff6cbd120
child 215 5db64229be69
equal deleted inserted replaced
213:9bdff6cbd120 214:2bf440c54ca5
     1 /**
       
     2  * Java RTP Library (jlibrtp)
       
     3  * Copyright (C) 2006 Arne Kepp
       
     4  * 
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2.1 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Lesser General Public License for more details.
       
    14  * 
       
    15  * You should have received a copy of the GNU Lesser General Public
       
    16  * License along with this library; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
       
    18  */
       
    19 package jlibrtp;
       
    20 
       
    21 import java.util.Enumeration;
       
    22 
       
    23 /**
       
    24  * The purpose of this thread is to check whether there are packets ready from 
       
    25  * any participants.
       
    26  * 
       
    27  * It should sleep when not in use, and be woken up by a condition variable.
       
    28  * 
       
    29  * Optionally, if we do jitter-control, the condition variable should have a max waiting period 
       
    30  * equal to how often we need to push data.
       
    31  * 
       
    32  * @author Arne Kepp
       
    33  */
       
    34 public class AppCallerThread extends Thread {
       
    35 	/**  The parent RTP Session */
       
    36 	RTPSession rtpSession;
       
    37 	/**  The applications interface, where the callback methods are called */
       
    38 	RTPAppIntf appl;
       
    39 	
       
    40 	/**
       
    41 	 * Instatiates the AppCallerThread
       
    42 	 * 
       
    43 	 * @param session the RTPSession with participants etc
       
    44 	 * @param rtpApp the interface to which data is given
       
    45 	 */
       
    46 	protected AppCallerThread(RTPSession session, RTPAppIntf rtpApp) {
       
    47 		rtpSession = session;
       
    48 		appl = rtpApp;
       
    49 		if(RTPSession.rtpDebugLevel > 1) {
       
    50 			System.out.println("<-> AppCallerThread created");
       
    51 		}  
       
    52 	}
       
    53 	
       
    54 	/**
       
    55 	 * The AppCallerThread will run in this loop until the RTPSession
       
    56 	 * is terminated.
       
    57 	 * 
       
    58 	 * Whenever an RTP packet is received it will loop over the
       
    59 	 * participants to check for packet buffers that have available
       
    60 	 * frame.
       
    61 	 */
       
    62 	public void run() {
       
    63 		if(RTPSession.rtpDebugLevel > 3) {
       
    64 			System.out.println("-> AppCallerThread.run()");
       
    65 		}
       
    66 		
       
    67 		while(rtpSession.endSession == false) {
       
    68 			
       
    69 			rtpSession.pktBufLock.lock();
       
    70 		    try {
       
    71 				if(RTPSession.rtpDebugLevel > 4) {
       
    72 					System.out.println("<-> AppCallerThread going to Sleep");
       
    73 				}
       
    74 				
       
    75 				try { rtpSession.pktBufDataReady.await(); } 
       
    76 					catch (Exception e) { System.out.println("AppCallerThread:" + e.getMessage());}
       
    77 					
       
    78 		    	// Next loop over all participants and check whether they have anything for us.
       
    79 				Enumeration<Participant> enu = rtpSession.partDb.getParticipants();
       
    80 				
       
    81 				while(enu.hasMoreElements()) {
       
    82 					Participant p = enu.nextElement(); 
       
    83 					
       
    84 					boolean done = false;
       
    85 					//System.out.println(p.ssrc + " " + !done +" " + p.rtpAddress 
       
    86 					//		+ " " + rtpSession.naiveReception + " " + p.pktBuffer);
       
    87 					//System.out.println("done: " + done + "  p.unexpected: " + p.unexpected);
       
    88 					while(!done && (!p.unexpected || rtpSession.naiveReception) 
       
    89 							&& p.pktBuffer != null && p.pktBuffer.length > 0) {
       
    90 
       
    91 						DataFrame aFrame = p.pktBuffer.popOldestFrame();
       
    92 						if(aFrame == null) {
       
    93 							done = true;
       
    94 						} else {
       
    95 							appl.receiveData(aFrame, p);
       
    96 						}
       
    97 					}
       
    98 				}
       
    99 		    
       
   100 		     } finally {
       
   101 		       rtpSession.pktBufLock.unlock();
       
   102 		     }
       
   103 			
       
   104 		}
       
   105 		if(RTPSession.rtpDebugLevel > 3) {
       
   106 			System.out.println("<- AppCallerThread.run() terminating");
       
   107 		}  
       
   108 	}
       
   109 
       
   110 }