| 1 | package cz.vutbr.feec.session.rtprtcp; |
| 2 | |
| 3 | import java.net.DatagramSocket; |
| 4 | import java.net.InetAddress; |
| 5 | import java.net.SocketException; |
| 6 | import java.net.UnknownHostException; |
| 7 | |
| 8 | import org.apache.log4j.Logger; |
| 9 | |
| 10 | import cz.vutbr.feec.session.rtprtcp.internal.MemberTableChangeListener; |
| 11 | import cz.vutbr.feec.session.rtprtcp.internal.PacketSentOrRetreiveListener; |
| 12 | import cz.vutbr.feec.session.rtprtcp.internal.Session3550; |
| 13 | import cz.vutbr.feec.session.rtprtcp.internal.SessionType; |
| 14 | |
| 15 | import net.java.dev.jssm.MulticastSSM; |
| 16 | import net.java.dev.jssm.Socket; |
| 17 | |
| 18 | /** |
| 19 | * The Class AbstractSession. |
| 20 | * |
| 21 | * @brief Session for audio or video streams. |
| 22 | * @author burgetrm |
| 23 | */ |
| 24 | abstract public class AbstractSession extends Thread { |
| 25 | |
| 26 | /** The payload type. */ |
| 27 | private int payloadType; |
| 28 | |
| 29 | /** The cfg. */ |
| 30 | protected Config cfg; |
| 31 | |
| 32 | /** The session. */ |
| 33 | protected Session3550 session = null; |
| 34 | |
| 35 | /** The debug. */ |
| 36 | protected boolean debug = false; |
| 37 | |
| 38 | /** The logger. */ |
| 39 | private static Logger logger = Logger.getLogger(AbstractSession.class.getName()); |
| 40 | |
| 41 | private String name; |
| 42 | |
| 43 | /** |
| 44 | * Initialize common interfaces for sender and receiver. |
| 45 | * |
| 46 | * @param multicastRTPSocket the multicast RTP socket |
| 47 | * @param conf the cfg |
| 48 | */ |
| 49 | public AbstractSession(Config conf, Socket multicastRTPSocket, String name, int payloadType) { |
| 50 | this.payloadType = payloadType; |
| 51 | this.name = name; |
| 52 | this.cfg = conf; |
| 53 | // sender or receiver? |
| 54 | if (conf.getMemberType().equals(SessionType.SENDER)) { |
| 55 | createSenderSession(multicastRTPSocket); |
| 56 | } else if(conf.getMemberType().equals(SessionType.RECEIVER)){ |
| 57 | createReceiverSession(multicastRTPSocket, null); |
| 58 | } else if(conf.getMemberType().equals(SessionType.FEEDBACK_TARGET)){ |
| 59 | createFeedbackTargetSession(multicastRTPSocket, null); |
| 60 | } else { |
| 61 | throw new RuntimeException("Uknown member type"); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Initialize common interfaces for sender and receiver. |
| 68 | * |
| 69 | * @param conf the cfg |
| 70 | */ |
| 71 | public AbstractSession(Config conf, String name, int payloadType) { |
| 72 | this.payloadType = payloadType; |
| 73 | this.cfg = conf; |
| 74 | this.name = name; |
| 75 | // sender or receiver? |
| 76 | if (conf.getMemberType().equals(SessionType.SENDER)) { |
| 77 | assert false: "SENDER Session cannot be instantied like this."; |
| 78 | } else if(conf.getMemberType().equals(SessionType.RECEIVER)){ |
| 79 | createReceiverSession(null, null); |
| 80 | } else if(conf.getMemberType().equals(SessionType.FEEDBACK_TARGET)){ |
| 81 | |
| 82 | } else { |
| 83 | throw new RuntimeException("Uknown member type"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Creates the receiver session. |
| 89 | * |
| 90 | * @param multicastRTPSocket the multicast RTP socket |
| 91 | * @param multicastRTCPSocket the multicast RTCP socket |
| 92 | */ |
| 93 | protected final void createReceiverSession(Socket multicastRTPSocket, Socket multicastRTCPSocket) { |
| 94 | logger.debug("Creating receiver session"); |
| 95 | assert cfg.getMemberType() == SessionType.RECEIVER; |
| 96 | |
| 97 | initRecvOrFT(multicastRTPSocket, multicastRTCPSocket, cfg.getMemberType()); |
| 98 | } |
| 99 | |
| 100 | private void initRecvOrFT(Socket multicastRTPSocket, Socket multicastRTCPSocket, SessionType type) { |
| 101 | // prijem multicastovych dat RTP - vytfvori jen v pripade, nebyl-li jiz vytvoren |
| 102 | if(multicastRTPSocket == null) { |
| 103 | logger.debug("Bind("+getSSMPort()+")"); |
| 104 | multicastRTPSocket = new MulticastSSM(getSSMPort()); |
| 105 | multicastRTPSocket.setSoTimeout(500); |
| 106 | logger.debug("JOIN SSM GROUP (S,G) = ("+cfg.getSSMSourceIpAddress()+", "+cfg.getSSMGroupIpAddress()+")"); |
| 107 | multicastRTPSocket.join(cfg.getSSMSourceIpAddress(), cfg.getSSMGroupIpAddress()); |
| 108 | } |
| 109 | // prijem multicastovych dat RTCP - vytfvori jen v pripade, nebyl-li jiz vytvoren |
| 110 | if(multicastRTCPSocket == null) { |
| 111 | logger.debug("Bind("+getFeedbackInPort()+")"); |
| 112 | multicastRTCPSocket = new MulticastSSM(getFeedbackInPort()); |
| 113 | multicastRTCPSocket.setSoTimeout(500); |
| 114 | multicastRTCPSocket.join(cfg.getSSMSourceIpAddress(), cfg.getSSMGroupIpAddress()); |
| 115 | } |
| 116 | |
| 117 | DatagramSocket ucRtcpSocket; |
| 118 | |
| 119 | try { |
| 120 | ucRtcpSocket = new DatagramSocket(); |
| 121 | ucRtcpSocket.setSoTimeout(500); |
| 122 | } catch (SocketException e) { |
| 123 | e.printStackTrace(); |
| 124 | ucRtcpSocket = null; |
| 125 | System.exit(-1); |
| 126 | } |
| 127 | |
| 128 | // create session |
| 129 | |
| 130 | session = new Session3550(multicastRTPSocket, multicastRTCPSocket, ucRtcpSocket, type, |
| 131 | getBandwidth(), getSSMPort(), getFeedbackOutPort(), getFeedbackAddress(), name); |
| 132 | session.setPayloadType(getPayloadType()); // ne z abstraktni tridy - tent to bere ze session !!! |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Creates the sender session. |
| 137 | * |
| 138 | * @param multicastSocket the multicast socket |
| 139 | */ |
| 140 | protected final void createSenderSession(Socket multicastSocket) { |
| 141 | logger.debug("Creating sender session"); |
| 142 | |
| 143 | DatagramSocket ucRtcpSocket; |
| 144 | int ssmSendToPort = getSSMPort(); |
| 145 | try { |
| 146 | logger.debug("RTCP AUDIO IN:"+cfg.getFeedbackInPortAudio() + |
| 147 | "RTCP AUDIO OUT:"+cfg.getFeedbackOutPortAudio() + |
| 148 | // "RTCP VIDEO IN:"+cfg.getFeedbackInPortVideo() + |
| 149 | // "RTCP VIDEO OUT:"+cfg.getFeedbackOutPortVideo() + |
| 150 | "RTCP getFeedbackInPort()" + getFeedbackInPort() + |
| 151 | "SSM SEND TO PORT:" + ssmSendToPort |
| 152 | ); |
| 153 | ucRtcpSocket = new DatagramSocket(getFeedbackInPort()); |
| 154 | ucRtcpSocket.setSoTimeout(500); |
| 155 | } catch (SocketException e) { |
| 156 | e.printStackTrace(); |
| 157 | System.err.println(); |
| 158 | ucRtcpSocket = null; |
| 159 | } |
| 160 | |
| 161 | // create session |
| 162 | |
| 163 | logger.debug("CREATING SENDER SESSION - SendTo("+ssmSendToPort+","+multicastSocket+")"); |
| 164 | assert cfg.getMemberType() == SessionType.SENDER; |
| 165 | session = new Session3550( multicastSocket, ucRtcpSocket, cfg.getMemberType(), |
| 166 | getBandwidth(), ssmSendToPort, getFeedbackOutPort(), null, name); |
| 167 | session.setPayloadType(payloadType); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Creates the sender session. |
| 172 | * |
| 173 | * @param multicastSocket the multicast socket |
| 174 | */ |
| 175 | protected final void createFeedbackTargetSession(Socket multicastRTPSocket, Socket multicastRTCPSocket) { |
| 176 | logger.debug("CREATING FEEDBACKTARGET SESSION: AUDIO IN:"+cfg.getFeedbackInPortAudio() + |
| 177 | " AUDIO OUT:"+cfg.getFeedbackOutPortAudio() + |
| 178 | // " VIDEO IN:"+cfg.getFeedbackInPortVideo() + |
| 179 | // " VIDEO OUT:"+cfg.getFeedbackOutPortVideo() + |
| 180 | " getFeedbackInPort()" + getFeedbackInPort() + |
| 181 | "SSM SEND TO PORT:" + getSSMPort() |
| 182 | ); |
| 183 | // TODO: Je potreba v session 3550 implementovat chovani protokolu. |
| 184 | assert cfg.getMemberType() == SessionType.FEEDBACK_TARGET; |
| 185 | initRecvOrFT(multicastRTPSocket, multicastRTCPSocket, cfg.getMemberType()); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get UDP/IP port for feedback. |
| 190 | * |
| 191 | * @return number of port for audio or video according to concrete class |
| 192 | */ |
| 193 | abstract public int getFeedbackInPort(); |
| 194 | |
| 195 | /** |
| 196 | * Gets the feedback out port. |
| 197 | * |
| 198 | * @return the feedback out port |
| 199 | */ |
| 200 | abstract public int getFeedbackOutPort(); |
| 201 | |
| 202 | /** |
| 203 | * Get address for feedback. |
| 204 | * |
| 205 | * @return number of port for audio or video according to concrete class |
| 206 | */ |
| 207 | public InetAddress getFeedbackAddress() { |
| 208 | InetAddress adr = null; |
| 209 | try { |
| 210 | adr = InetAddress.getByName(cfg.getFeedbackIpAddress()); |
| 211 | } catch (UnknownHostException e) { |
| 212 | e.printStackTrace(); |
| 213 | System.exit(-1); |
| 214 | } |
| 215 | return adr; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Gets the bandwidth. |
| 220 | * |
| 221 | * @return reserved bandwidth for this session. |
| 222 | */ |
| 223 | abstract public int getBandwidth(); |
| 224 | |
| 225 | /** |
| 226 | * Get port for Source-Specific multicast. |
| 227 | * |
| 228 | * @return number of port for audio or video according to concrete class |
| 229 | */ |
| 230 | abstract public int getSSMPort(); |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Sets the members listener. |
| 235 | * |
| 236 | * @param listener the members listener |
| 237 | */ |
| 238 | public void setMembersListener(MemberTableChangeListener listener) { |
| 239 | session.addMemberTableChangeListener(listener); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Sets the packet listener. |
| 244 | * |
| 245 | * @param listener the packet listener |
| 246 | */ |
| 247 | public void setPacketListener(PacketSentOrRetreiveListener listener) { |
| 248 | session.addPacketSentOrRetreiveListener(listener); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Gets the session. |
| 253 | * |
| 254 | * @return the session |
| 255 | */ |
| 256 | public Session3550 getSession() { |
| 257 | return session; |
| 258 | } |
| 259 | |
| 260 | public int getPayloadType() { |
| 261 | return payloadType; |
| 262 | } |
| 263 | } |