src/jlibrtp/AppCallerThread.java
changeset 834 e8d6255306f8
parent 833 f5a5d9237d69
child 835 4e40f3481f23
equal deleted inserted replaced
833:f5a5d9237d69 834:e8d6255306f8
     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
       
    30  * waiting period 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
       
    44 	 *            the RTPSession with participants etc
       
    45 	 * @param rtpApp
       
    46 	 *            the interface to which data is given
       
    47 	 */
       
    48 	protected AppCallerThread(RTPSession session, RTPAppIntf rtpApp) {
       
    49 		rtpSession = session;
       
    50 		appl = rtpApp;
       
    51 		if (RTPSession.rtpDebugLevel > 1) {
       
    52 			System.out.println("<-> AppCallerThread created");
       
    53 		}
       
    54 	}
       
    55 
       
    56 	/**
       
    57 	 * The AppCallerThread will run in this loop until the RTPSession is
       
    58 	 * terminated.
       
    59 	 * 
       
    60 	 * Whenever an RTP packet is received it will loop over the participants to
       
    61 	 * check for packet buffers that have available frame.
       
    62 	 */
       
    63 	public void run() {
       
    64 		if (RTPSession.rtpDebugLevel > 3) {
       
    65 			System.out.println("-> AppCallerThread.run()");
       
    66 		}
       
    67 
       
    68 		while (rtpSession.endSession == false) {
       
    69 
       
    70 			rtpSession.pktBufLock.lock();
       
    71 			try {
       
    72 				if (RTPSession.rtpDebugLevel > 4) {
       
    73 					System.out.println("<-> AppCallerThread going to Sleep");
       
    74 				}
       
    75 
       
    76 				try {
       
    77 					rtpSession.pktBufDataReady.await();
       
    78 				} catch (Exception e) {
       
    79 					System.out.println("AppCallerThread:" + e.getMessage());
       
    80 				}
       
    81 
       
    82 				// Next loop over all participants and check whether they have
       
    83 				// anything for us.
       
    84 				Enumeration<Participant> enu = rtpSession.partDb
       
    85 				.getParticipants();
       
    86 
       
    87 				while (enu.hasMoreElements()) {
       
    88 					Participant p = enu.nextElement();
       
    89 
       
    90 					boolean done = false;
       
    91 					// System.out.println(p.ssrc + " " + !done +" " +
       
    92 					// p.rtpAddress
       
    93 					// + " " + rtpSession.naiveReception + " " + p.pktBuffer);
       
    94 					// System.out.println("done: " + done + "  p.unexpected: " +
       
    95 					// p.unexpected);
       
    96 					while (!done
       
    97 							&& (!p.unexpected || rtpSession.naiveReception)
       
    98 							&& p.pktBuffer != null && p.pktBuffer.length > 0) {
       
    99 
       
   100 						DataFrame aFrame = p.pktBuffer.popOldestFrame();
       
   101 						if (aFrame == null) {
       
   102 							done = true;
       
   103 						} else {
       
   104 							appl.receiveData(aFrame, p);
       
   105 						}
       
   106 					}
       
   107 				}
       
   108 
       
   109 			} finally {
       
   110 				rtpSession.pktBufLock.unlock();
       
   111 			}
       
   112 		}
       
   113 		if (RTPSession.rtpDebugLevel > 3) {
       
   114 			System.out.println("<- AppCallerThread.run() terminating");
       
   115 		}
       
   116 	}
       
   117 
       
   118 	public DataFrame getNextDataFrame() {
       
   119 		if (RTPSession.rtpDebugLevel > 3) {
       
   120 			System.out.println("-> AppCallerThread.run()");
       
   121 		}
       
   122 		//rtpSession.pktBufLock.lock();
       
   123 		try {
       
   124 			if (RTPSession.rtpDebugLevel > 4) {
       
   125 				System.out.println("<-> AppCallerThread going to Sleep");
       
   126 			}
       
   127 
       
   128 			/*try {
       
   129 				rtpSession.pktBufDataReady.await();
       
   130 			} catch (Exception e) {
       
   131 				System.out.println("AppCallerThread:" + e.getMessage());
       
   132 			}*/
       
   133 
       
   134 			// Next loop over all participants and check whether they have
       
   135 			// anything for us.
       
   136 			/*Enumeration<Participant> enu = rtpSession.partDb
       
   137 			.getParticipants();
       
   138 
       
   139 			while (enu.hasMoreElements()) {*/
       
   140 				Participant p = rtpSession.firstPart;
       
   141 
       
   142 				boolean done = false;
       
   143 				// System.out.println(p.ssrc + " " + !done +" " +
       
   144 				// p.rtpAddress
       
   145 				// + " " + rtpSession.naiveReception + " " + p.pktBuffer);
       
   146 				// System.out.println("done: " + done + "  p.unexpected: " +
       
   147 				// p.unexpected);
       
   148 				while (!done
       
   149 						&& (!p.unexpected || rtpSession.naiveReception)
       
   150 						&& p.pktBuffer != null && p.pktBuffer.length > 0) {
       
   151 
       
   152 					DataFrame aFrame = p.pktBuffer.popOldestFrame();
       
   153 					if (aFrame == null) {
       
   154 						done = true;
       
   155 					} else {
       
   156 						return aFrame;
       
   157 					}
       
   158 				}
       
   159 			//}
       
   160 
       
   161 		} finally {
       
   162 			//rtpSession.pktBufLock.unlock();
       
   163 		}
       
   164 		if (RTPSession.rtpDebugLevel > 3) {
       
   165 			System.out.println("<- AppCallerThread.run() terminating");
       
   166 		}
       
   167 		return null;
       
   168 	}
       
   169 
       
   170 }