PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/resources/diameter-gq/ra/src/main/java/org/mobicents/slee/resource/diameter/gq/SDPConverter.java

http://mobicents.googlecode.com/
Java | 801 lines | 672 code | 74 blank | 55 comment | 274 complexity | a56158de4647c6d917d1899bf9ef9610 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.slee.resource.diameter.gq;
  23. import gov.nist.javax.sdp.fields.AttributeField;
  24. import gov.nist.javax.sdp.fields.BandwidthField;
  25. import java.util.Vector;
  26. import javax.sdp.Media;
  27. import javax.sdp.MediaDescription;
  28. import javax.sdp.SdpException;
  29. import javax.sdp.SdpFactory;
  30. import javax.sdp.SdpParseException;
  31. import javax.sdp.SessionDescription;
  32. import sun.net.util.IPAddressUtil;
  33. import net.java.slee.resource.diameter.base.events.avp.IPFilterRule;
  34. import net.java.slee.resource.diameter.gq.GqAvpFactory;
  35. import net.java.slee.resource.diameter.gq.GqProvider;
  36. import net.java.slee.resource.diameter.gq.events.avp.FlowStatus;
  37. import net.java.slee.resource.diameter.gq.events.avp.FlowUsage;
  38. import net.java.slee.resource.diameter.gq.events.avp.MediaComponentDescription;
  39. import net.java.slee.resource.diameter.gq.events.avp.MediaSubComponent;
  40. import net.java.slee.resource.diameter.gq.events.avp.MediaType;
  41. /**
  42. * Converts SDP to Gq' Media-Component-Description AVP.<br>
  43. * <br>
  44. * From the Diameter Gq' Reference Point Protocol Details (ETSI TS 183.017 V3.2.1) specification Annex B
  45. *
  46. * @author <a href="mailto:webdev@web-ukraine.info"> Yulian Oifa </a>
  47. */
  48. public class SDPConverter {
  49. /**
  50. * Returns the value of the Media-Component-Description[] AVP, of type Grouped.
  51. */
  52. public static MediaComponentDescription[] convertSDP(GqProvider gqProvider, String sourceSDP, String destinationSDP) throws SdpException,
  53. SdpParseException {
  54. SdpFactory sdpFactory = SdpFactory.getInstance();
  55. SessionDescription sourceSessionDescription = sdpFactory.createSessionDescription(sourceSDP);
  56. SessionDescription destinationSessionDescription = sdpFactory.createSessionDescription(destinationSDP);
  57. Vector<MediaDescription> sourceMediaDescriptions = sourceSessionDescription.getMediaDescriptions(false);
  58. Vector<MediaDescription> destinationMediaDescriptions = destinationSessionDescription.getMediaDescriptions(false);
  59. Vector<AttributeField> attributes;
  60. Vector<BandwidthField> bandwidth;
  61. Vector<MediaComponentDescription> mediaComponentDescriptions = new Vector<MediaComponentDescription>();
  62. Vector<MediaSubComponent[]> mediaSubComponents = new Vector<MediaSubComponent[]>();
  63. GqAvpFactory avpFactory = gqProvider.getGqAvpFactory();
  64. MediaComponentDescription currDescription = null;
  65. Media currMedia;
  66. String mediaType;
  67. String codecLine;
  68. String mode;
  69. if (sourceMediaDescriptions == null)
  70. throw new SdpParseException(0, 0, "no source media found");
  71. if (destinationMediaDescriptions == null)
  72. throw new SdpParseException(0, 0, "no destination media found");
  73. String proto;
  74. int dataPort;
  75. int rtcpPort;
  76. int ports;
  77. int step;
  78. String dataAddress;
  79. String rtcpAddress;
  80. MediaSubComponent currSubComponent;
  81. IPFilterRule currFlowDescription;
  82. String[] rtcpValues;
  83. for (int i = 0; i < sourceMediaDescriptions.size(); i++) {
  84. attributes = sourceMediaDescriptions.get(i).getAttributes(false);
  85. bandwidth = sourceMediaDescriptions.get(i).getBandwidths(false);
  86. currMedia = sourceMediaDescriptions.get(i).getMedia();
  87. currDescription = avpFactory.createMediaComponentDescription();
  88. mediaType = currMedia.getMediaType().toLowerCase();
  89. if (mediaType.equals("audio"))
  90. currDescription.setMediaType(MediaType.AUDIO);
  91. else if (mediaType.equals("video"))
  92. currDescription.setMediaType(MediaType.VIDEO);
  93. else if (mediaType.equals("control"))
  94. currDescription.setMediaType(MediaType.CONTROL);
  95. else if (mediaType.equals("application"))
  96. currDescription.setMediaType(MediaType.APPLICATION);
  97. else if (mediaType.equals("message"))
  98. currDescription.setMediaType(MediaType.MESSAGE);
  99. else if (mediaType.equals("data"))
  100. currDescription.setMediaType(MediaType.DATA);
  101. else if (mediaType.equals("text"))
  102. currDescription.setMediaType(MediaType.TEXT);
  103. else
  104. currDescription.setMediaType(MediaType.OTHER);
  105. codecLine = "downlink" + System.getProperty("line.separator");
  106. codecLine = codecLine + "offer" + System.getProperty("line.separator");
  107. codecLine = codecLine + currMedia.toString() + System.getProperty("line.separator");
  108. mode = "";
  109. dataPort = currMedia.getMediaPort();
  110. rtcpPort = currMedia.getMediaPort() + 1;
  111. if (sourceMediaDescriptions.get(i).getConnection() != null)
  112. rtcpAddress = sourceMediaDescriptions.get(i).getConnection().getAddress();
  113. else
  114. rtcpAddress = sourceSessionDescription.getConnection().getAddress();
  115. dataAddress = rtcpAddress;
  116. proto = currMedia.getProtocol().toLowerCase();
  117. ports = 1;
  118. step = 2;
  119. if (currMedia.getPortCount() > 0)
  120. ports = currMedia.getPortCount();
  121. if (attributes != null)
  122. for (int j = 0; j < attributes.size(); j++)
  123. if (attributes.get(j).getName().equals("recvonly"))
  124. mode = "recvonly";
  125. else if (attributes.get(j).getName().equals("sendonly"))
  126. mode = "sendonly";
  127. else if (attributes.get(j).getName().equals("sendrecv"))
  128. mode = "sendrecv";
  129. else if (attributes.get(j).getName().equals("inactive"))
  130. mode = "inactive";
  131. else if (attributes.get(j).getName().equals("rtcp")) {
  132. rtcpValues = attributes.get(j).getValue().split(" ");
  133. rtcpPort = Integer.parseInt(rtcpValues[0]);
  134. if (rtcpPort != dataPort + 1)
  135. step = 1;
  136. if (rtcpValues.length == 3)
  137. rtcpAddress = rtcpValues[2];
  138. }
  139. else
  140. codecLine += attributes.get(j).toString() + System.getProperty("line.separator");
  141. if (proto.equals("rtp/avp")) {
  142. mediaSubComponents.add(new MediaSubComponent[ports * 2]);
  143. // 2 flows
  144. if (!mode.equals("recvonly")) {
  145. for (int j = 0; j < ports; j++) {
  146. currSubComponent = avpFactory.createMediaSubComponent();
  147. currSubComponent.setFlowNumber(1 + j * ports);
  148. currFlowDescription = new IPFilterRule("permit in 17 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  149. currSubComponent.setFlowDescription(currFlowDescription);
  150. mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
  151. }
  152. }
  153. for (int j = 0; j < ports; j++) {
  154. currSubComponent = avpFactory.createMediaSubComponent();
  155. currSubComponent.setFlowNumber(2 + j * ports);
  156. currSubComponent.setFlowUsage(FlowUsage.RTCP);
  157. currFlowDescription = new IPFilterRule("permit in 17 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
  158. currSubComponent.setFlowDescription(currFlowDescription);
  159. mediaSubComponents.get(i)[1 + j * ports] = currSubComponent;
  160. }
  161. }
  162. else if (proto.equals("udp")) {
  163. // 1 flow
  164. if (!mode.equals("recvonly")) {
  165. for (int j = 0; j < ports; j++) {
  166. currSubComponent = avpFactory.createMediaSubComponent();
  167. currSubComponent.setFlowNumber(1 + j * ports);
  168. currFlowDescription = new IPFilterRule("permit in 17 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  169. currSubComponent.setFlowDescription(currFlowDescription);
  170. mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
  171. }
  172. }
  173. }
  174. else if (proto.equals("rtp/avp-tcp") || proto.equals("tcp/rtp/avp")) {
  175. // 2 flows
  176. if (!mode.equals("recvonly")) {
  177. for (int j = 0; j < ports; j++) {
  178. currSubComponent = avpFactory.createMediaSubComponent();
  179. currSubComponent.setFlowNumber(1 + j * ports);
  180. currFlowDescription = new IPFilterRule("permit in 6 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  181. currSubComponent.setFlowDescription(currFlowDescription);
  182. mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
  183. }
  184. }
  185. for (int j = 0; j < ports; j++) {
  186. currSubComponent = avpFactory.createMediaSubComponent();
  187. currSubComponent.setFlowNumber(2 + j * ports);
  188. currSubComponent.setFlowUsage(FlowUsage.RTCP);
  189. currFlowDescription = new IPFilterRule("permit in 6 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
  190. currSubComponent.setFlowDescription(currFlowDescription);
  191. mediaSubComponents.get(i)[1 + j * ports] = currSubComponent;
  192. }
  193. }
  194. else if (proto.equals("tcp")) {
  195. // 1 flow
  196. if (!mode.equals("recvonly")) {
  197. for (int j = 0; j < ports; j++) {
  198. currSubComponent = avpFactory.createMediaSubComponent();
  199. currSubComponent.setFlowNumber(1 + j * ports);
  200. currFlowDescription = new IPFilterRule("permit in 6 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  201. currSubComponent.setFlowDescription(currFlowDescription);
  202. mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
  203. }
  204. }
  205. }
  206. else if (proto.equals("rtp/avp-dccp") || proto.equals("dccp/rtp/avp")) {
  207. // 2 flows
  208. if (!mode.equals("recvonly")) {
  209. for (int j = 0; j < ports; j++) {
  210. currSubComponent = avpFactory.createMediaSubComponent();
  211. currSubComponent.setFlowNumber(1 + j * ports);
  212. currFlowDescription = new IPFilterRule("permit in 33 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  213. currSubComponent.setFlowDescription(currFlowDescription);
  214. mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
  215. }
  216. }
  217. for (int j = 0; j < ports; j++) {
  218. currSubComponent = avpFactory.createMediaSubComponent();
  219. currSubComponent.setFlowNumber(2 + j * ports);
  220. currSubComponent.setFlowUsage(FlowUsage.RTCP);
  221. currFlowDescription = new IPFilterRule("permit in 33 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
  222. currSubComponent.setFlowDescription(currFlowDescription);
  223. mediaSubComponents.get(i)[1 + j * ports] = currSubComponent;
  224. }
  225. }
  226. else if (proto.equals("dccp")) {
  227. // 1 flow
  228. if (!mode.equals("recvonly")) {
  229. for (int j = 0; j < ports; j++) {
  230. currSubComponent = avpFactory.createMediaSubComponent();
  231. currSubComponent.setFlowNumber(1 + j * ports);
  232. currFlowDescription = new IPFilterRule("permit in 33 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  233. currSubComponent.setFlowDescription(currFlowDescription);
  234. mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
  235. }
  236. }
  237. }
  238. else
  239. throw new SdpParseException(0, 0, "protocol not supported " + proto);
  240. if (bandwidth != null)
  241. for (int j = 0; j < bandwidth.size(); j++)
  242. if (bandwidth.get(j).getType().equals("AS"))
  243. currDescription.setMaxRequestedBandwidthDL(bandwidth.get(j).getValue());
  244. else if (bandwidth.get(j).getType().equals("RR"))
  245. currDescription.setRRBandwidth(bandwidth.get(j).getValue());
  246. else if (bandwidth.get(j).getType().equals("RS"))
  247. currDescription.setRSBandwidth(bandwidth.get(j).getValue());
  248. else
  249. codecLine += bandwidth.get(j).toString() + System.getProperty("line.separator");
  250. currDescription.setCodecData(codecLine.getBytes());
  251. if (currMedia.getMediaPort() == 0)
  252. currDescription.setFlowStatus(FlowStatus.REMOVED);
  253. else if (mode.equals("recvonly"))
  254. currDescription.setFlowStatus(FlowStatus.ENABLED_DOWNLINK);
  255. else if (mode.equals("sendonly"))
  256. currDescription.setFlowStatus(FlowStatus.ENABLED_UPLINK);
  257. else if (mode.equals("inactive"))
  258. currDescription.setFlowStatus(FlowStatus.DISABLED);
  259. else
  260. currDescription.setFlowStatus(FlowStatus.ENABLED);
  261. currDescription.setMediaComponentNumber(i + 1);
  262. mediaComponentDescriptions.add(currDescription);
  263. }
  264. Boolean found = false;
  265. for (int i = 0; i < destinationMediaDescriptions.size(); i++) {
  266. found = false;
  267. currMedia = destinationMediaDescriptions.get(i).getMedia();
  268. mediaType = currMedia.getMediaType().toLowerCase();
  269. attributes = destinationMediaDescriptions.get(i).getAttributes(false);
  270. bandwidth = destinationMediaDescriptions.get(i).getBandwidths(false);
  271. codecLine = "uplink" + System.getProperty("line.separator");
  272. codecLine = codecLine + "answer" + System.getProperty("line.separator");
  273. codecLine = codecLine + currMedia.toString() + System.getProperty("line.separator");
  274. for (int j = 0; j < mediaComponentDescriptions.size(); j++)
  275. if (mediaComponentDescriptions.get(j).getMediaType().toString().toLowerCase().equals(mediaType)) {
  276. currDescription = mediaComponentDescriptions.get(j);
  277. found = true;
  278. break;
  279. }
  280. if (found) {
  281. dataPort = currMedia.getMediaPort();
  282. rtcpPort = currMedia.getMediaPort() + 1;
  283. if (destinationMediaDescriptions.get(i).getConnection() != null)
  284. rtcpAddress = destinationMediaDescriptions.get(i).getConnection().getAddress();
  285. else
  286. rtcpAddress = destinationSessionDescription.getConnection().getAddress();
  287. dataAddress = rtcpAddress;
  288. proto = currMedia.getProtocol().toLowerCase();
  289. ports = 1;
  290. step = 2;
  291. if (currMedia.getPortCount() > 0)
  292. ports = currMedia.getPortCount();
  293. mode = "";
  294. if (attributes != null)
  295. for (int j = 0; j < attributes.size(); j++)
  296. if (attributes.get(j).getName().equals("recvonly"))
  297. mode = "recvonly";
  298. else if (attributes.get(j).getName().equals("sendonly"))
  299. mode = "sendonly";
  300. else if (attributes.get(j).getName().equals("sendrecv"))
  301. mode = "sendrecv";
  302. else if (attributes.get(j).getName().equals("inactive"))
  303. mode = "inactive";
  304. else if (attributes.get(j).getName().equals("rtcp")) {
  305. rtcpValues = attributes.get(j).getValue().split(" ");
  306. rtcpPort = Integer.parseInt(rtcpValues[0]);
  307. if (rtcpPort != dataPort + 1)
  308. step = 1;
  309. if (rtcpValues.length == 3)
  310. rtcpAddress = rtcpValues[2];
  311. }
  312. else
  313. codecLine += attributes.get(j).toString() + System.getProperty("line.separator");
  314. if (proto.equals("rtp/avp")) {
  315. // 2 flows
  316. if (!mode.equals("sendonly")) {
  317. for (int j = 0; j < ports; j++) {
  318. currFlowDescription = new IPFilterRule("permit out 17 from any to " + dataAddress + " " + (dataPort + j * step));
  319. mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
  320. }
  321. }
  322. for (int j = 0; j < ports; j++) {
  323. currFlowDescription = new IPFilterRule("permit out 17 from any to " + rtcpAddress + " " + (rtcpPort + j * step));
  324. mediaSubComponents.get(i)[1 + j * ports].setFlowDescription(currFlowDescription);
  325. }
  326. }
  327. else if (proto.equals("udp")) {
  328. // 1 flow
  329. if (!mode.equals("sendonly")) {
  330. for (int j = 0; j < ports; j++) {
  331. currFlowDescription = new IPFilterRule("permit out 17 from any to " + dataAddress + " " + (dataPort + j * step));
  332. mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
  333. }
  334. }
  335. }
  336. else if (proto.equals("rtp/avp-tcp") || proto.equals("tcp/rtp/avp")) {
  337. // 2 flows
  338. if (!mode.equals("sendonly")) {
  339. for (int j = 0; j < ports; j++) {
  340. currFlowDescription = new IPFilterRule("permit out 6 from any to " + dataAddress + " " + (dataPort + j * step));
  341. mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
  342. }
  343. }
  344. for (int j = 0; j < ports; j++) {
  345. currFlowDescription = new IPFilterRule("permit out 6 from any to " + rtcpAddress + " " + (rtcpPort + j * step));
  346. mediaSubComponents.get(i)[1 + j * ports].setFlowDescription(currFlowDescription);
  347. }
  348. }
  349. else if (proto.equals("tcp")) {
  350. // 1 flow
  351. if (!mode.equals("sendonly")) {
  352. for (int j = 0; j < ports; j++) {
  353. currFlowDescription = new IPFilterRule("permit out 6 from any to " + dataAddress + " " + (dataPort + j * step));
  354. mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
  355. }
  356. }
  357. }
  358. else if (proto.equals("rtp/avp-dccp") || proto.equals("dccp/rtp/avp")) {
  359. // 2 flows
  360. if (!mode.equals("sendonly")) {
  361. for (int j = 0; j < ports; j++) {
  362. currFlowDescription = new IPFilterRule("permit out 33 from any to " + dataAddress + " " + (dataPort + j * step));
  363. mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
  364. }
  365. }
  366. for (int j = 0; j < ports; j++) {
  367. currFlowDescription = new IPFilterRule("permit out 33 from any to " + rtcpAddress + " " + (rtcpPort + j * step));
  368. mediaSubComponents.get(i)[1 + j * ports].setFlowDescription(currFlowDescription);
  369. }
  370. }
  371. else if (proto.equals("dccp")) {
  372. // 1 flow
  373. if (!mode.equals("sendonly")) {
  374. for (int j = 0; j < ports; j++) {
  375. currFlowDescription = new IPFilterRule("permit out 33 from any to " + dataAddress + " " + (dataPort + j * step));
  376. mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
  377. }
  378. }
  379. }
  380. else
  381. throw new SdpParseException(0, 0, "protocol not supported " + proto);
  382. if (bandwidth != null)
  383. for (int j = 0; j < bandwidth.size(); j++)
  384. if (bandwidth.get(j).getType().equals("AS"))
  385. currDescription.setMaxRequestedBandwidthUL(bandwidth.get(j).getValue());
  386. else if (bandwidth.get(j).getType().equals("RR")) {
  387. // do nothing
  388. }
  389. else if (bandwidth.get(j).getType().equals("RS")) {
  390. // do nothing
  391. }
  392. else
  393. codecLine += bandwidth.get(j).toString() + System.getProperty("line.separator");
  394. currDescription.setMediaSubComponents(mediaSubComponents.get(i));
  395. currDescription.setCodecData(codecLine.getBytes());
  396. }
  397. }
  398. return mediaComponentDescriptions.toArray(new MediaComponentDescription[mediaComponentDescriptions.size()]);
  399. }
  400. /**
  401. * Returns the value of the Media-Component-Description[] AVP, of type Grouped.
  402. */
  403. public static MediaComponentDescription[] convertSDP(GqProvider gqProvider, String sourceSDP) throws SdpException, SdpParseException {
  404. SdpFactory sdpFactory = SdpFactory.getInstance();
  405. SessionDescription sourceSessionDescription = sdpFactory.createSessionDescription(sourceSDP);
  406. Vector<MediaDescription> sourceMediaDescriptions = sourceSessionDescription.getMediaDescriptions(false);
  407. Vector<AttributeField> attributes;
  408. Vector<BandwidthField> bandwidth;
  409. Vector<MediaComponentDescription> mediaComponentDescriptions = new Vector<MediaComponentDescription>();
  410. GqAvpFactory avpFactory = gqProvider.getGqAvpFactory();
  411. MediaComponentDescription currDescription = null;
  412. Media currMedia;
  413. String mediaType;
  414. String codecLine;
  415. String mode;
  416. if (sourceMediaDescriptions == null)
  417. throw new SdpParseException(0, 0, "no source media found");
  418. String proto;
  419. int dataPort;
  420. int rtcpPort;
  421. int ports;
  422. int step;
  423. String dataAddress;
  424. String rtcpAddress;
  425. MediaSubComponent currSubComponent;
  426. IPFilterRule currFlowDescription;
  427. String[] rtcpValues;
  428. for (int i = 0; i < sourceMediaDescriptions.size(); i++) {
  429. attributes = sourceMediaDescriptions.get(i).getAttributes(false);
  430. bandwidth = sourceMediaDescriptions.get(i).getBandwidths(false);
  431. currMedia = sourceMediaDescriptions.get(i).getMedia();
  432. currDescription = avpFactory.createMediaComponentDescription();
  433. mediaType = currMedia.getMediaType().toLowerCase();
  434. if (mediaType.equals("audio"))
  435. currDescription.setMediaType(MediaType.AUDIO);
  436. else if (mediaType.equals("video"))
  437. currDescription.setMediaType(MediaType.VIDEO);
  438. else if (mediaType.equals("control"))
  439. currDescription.setMediaType(MediaType.CONTROL);
  440. else if (mediaType.equals("application"))
  441. currDescription.setMediaType(MediaType.APPLICATION);
  442. else if (mediaType.equals("message"))
  443. currDescription.setMediaType(MediaType.MESSAGE);
  444. else if (mediaType.equals("data"))
  445. currDescription.setMediaType(MediaType.DATA);
  446. else if (mediaType.equals("text"))
  447. currDescription.setMediaType(MediaType.TEXT);
  448. else
  449. currDescription.setMediaType(MediaType.OTHER);
  450. codecLine = "downlink" + System.getProperty("line.separator");
  451. codecLine = codecLine + "offer" + System.getProperty("line.separator");
  452. codecLine = codecLine + currMedia.toString() + System.getProperty("line.separator");
  453. mode = "";
  454. dataPort = currMedia.getMediaPort();
  455. rtcpPort = currMedia.getMediaPort() + 1;
  456. if (sourceMediaDescriptions.get(i).getConnection() != null)
  457. rtcpAddress = sourceMediaDescriptions.get(i).getConnection().getAddress();
  458. else
  459. rtcpAddress = sourceSessionDescription.getConnection().getAddress();
  460. dataAddress = rtcpAddress;
  461. proto = currMedia.getProtocol().toLowerCase();
  462. ports = 1;
  463. step = 2;
  464. if (currMedia.getPortCount() > 0)
  465. ports = currMedia.getPortCount();
  466. if (attributes != null)
  467. for (int j = 0; j < attributes.size(); j++)
  468. if (attributes.get(j).getName().equals("recvonly"))
  469. mode = "recvonly";
  470. else if (attributes.get(j).getName().equals("sendonly"))
  471. mode = "sendonly";
  472. else if (attributes.get(j).getName().equals("sendrecv"))
  473. mode = "sendrecv";
  474. else if (attributes.get(j).getName().equals("inactive"))
  475. mode = "inactive";
  476. else if (attributes.get(j).getName().equals("rtcp")) {
  477. rtcpValues = attributes.get(j).getValue().split(" ");
  478. rtcpPort = Integer.parseInt(rtcpValues[0]);
  479. if (rtcpPort != dataPort + 1)
  480. step = 1;
  481. if (rtcpValues.length == 3)
  482. rtcpAddress = rtcpValues[2];
  483. }
  484. else
  485. codecLine += attributes.get(j).toString() + System.getProperty("line.separator");
  486. if (proto.equals("rtp/avp")) {
  487. // 2 flows
  488. if (!mode.equals("recvonly")) {
  489. for (int j = 0; j < ports; j++) {
  490. currSubComponent = avpFactory.createMediaSubComponent();
  491. currSubComponent.setFlowNumber(1 + j * step);
  492. currFlowDescription = new IPFilterRule("permit in 17 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  493. currSubComponent.setFlowDescription(currFlowDescription);
  494. currDescription.setMediaSubComponent(currSubComponent);
  495. }
  496. }
  497. for (int j = 0; j < ports; j++) {
  498. currSubComponent = avpFactory.createMediaSubComponent();
  499. currSubComponent.setFlowNumber(2 + j * step);
  500. currSubComponent.setFlowUsage(FlowUsage.RTCP);
  501. currFlowDescription = new IPFilterRule("permit in 17 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
  502. currSubComponent.setFlowDescription(currFlowDescription);
  503. currDescription.setMediaSubComponent(currSubComponent);
  504. }
  505. }
  506. else if (proto.equals("udp")) {
  507. // 1 flow
  508. if (!mode.equals("recvonly")) {
  509. for (int j = 0; j < ports; j++) {
  510. currSubComponent = avpFactory.createMediaSubComponent();
  511. currSubComponent.setFlowNumber(1 + j * step);
  512. currFlowDescription = new IPFilterRule("permit in 17 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  513. currSubComponent.setFlowDescription(currFlowDescription);
  514. currDescription.setMediaSubComponent(currSubComponent);
  515. }
  516. }
  517. }
  518. else if (proto.equals("rtp/avp-tcp") || proto.equals("tcp/rtp/avp")) {
  519. // 2 flows
  520. if (!mode.equals("recvonly")) {
  521. for (int j = 0; j < ports; j++) {
  522. currSubComponent = avpFactory.createMediaSubComponent();
  523. currSubComponent.setFlowNumber(1 + j * step);
  524. currFlowDescription = new IPFilterRule("permit in 6 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  525. currSubComponent.setFlowDescription(currFlowDescription);
  526. currDescription.setMediaSubComponent(currSubComponent);
  527. }
  528. }
  529. for (int j = 0; j < ports; j++) {
  530. currSubComponent = avpFactory.createMediaSubComponent();
  531. currSubComponent.setFlowNumber(2 + j * step);
  532. currSubComponent.setFlowUsage(FlowUsage.RTCP);
  533. currFlowDescription = new IPFilterRule("permit in 6 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
  534. currSubComponent.setFlowDescription(currFlowDescription);
  535. currDescription.setMediaSubComponent(currSubComponent);
  536. }
  537. }
  538. else if (proto.equals("tcp")) {
  539. // 1 flow
  540. if (!mode.equals("recvonly")) {
  541. for (int j = 0; j < ports; j++) {
  542. currSubComponent = avpFactory.createMediaSubComponent();
  543. currSubComponent.setFlowNumber(1 + j * step);
  544. currFlowDescription = new IPFilterRule("permit in 6 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  545. currSubComponent.setFlowDescription(currFlowDescription);
  546. currDescription.setMediaSubComponent(currSubComponent);
  547. }
  548. }
  549. }
  550. else if (proto.equals("rtp/avp-dccp") || proto.equals("dccp/rtp/avp")) {
  551. // 2 flows
  552. if (!mode.equals("recvonly")) {
  553. for (int j = 0; j < ports; j++) {
  554. currSubComponent = avpFactory.createMediaSubComponent();
  555. currSubComponent.setFlowNumber(1 + j * step);
  556. currFlowDescription = new IPFilterRule("permit in 33 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  557. currSubComponent.setFlowDescription(currFlowDescription);
  558. currDescription.setMediaSubComponent(currSubComponent);
  559. }
  560. }
  561. for (int j = 0; j < ports; j++) {
  562. currSubComponent = avpFactory.createMediaSubComponent();
  563. currSubComponent.setFlowNumber(2 + j * step);
  564. currSubComponent.setFlowUsage(FlowUsage.RTCP);
  565. currFlowDescription = new IPFilterRule("permit in 33 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
  566. currSubComponent.setFlowDescription(currFlowDescription);
  567. currDescription.setMediaSubComponent(currSubComponent);
  568. }
  569. }
  570. else if (proto.equals("dccp")) {
  571. // 1 flow
  572. if (!mode.equals("recvonly")) {
  573. for (int j = 0; j < ports; j++) {
  574. currSubComponent = avpFactory.createMediaSubComponent();
  575. currSubComponent.setFlowNumber(1 + j * step);
  576. currFlowDescription = new IPFilterRule("permit in 33 from " + dataAddress + " " + (dataPort + j * step) + " to any");
  577. currSubComponent.setFlowDescription(currFlowDescription);
  578. currDescription.setMediaSubComponent(currSubComponent);
  579. }
  580. }
  581. }
  582. else
  583. throw new SdpParseException(0, 0, "protocol not supported " + proto);
  584. if (bandwidth != null)
  585. for (int j = 0; j < bandwidth.size(); j++)
  586. if (bandwidth.get(j).getType().equals("AS"))
  587. currDescription.setMaxRequestedBandwidthDL(bandwidth.get(j).getValue());
  588. else if (bandwidth.get(j).getType().equals("RR"))
  589. currDescription.setRRBandwidth(bandwidth.get(j).getValue());
  590. else if (bandwidth.get(j).getType().equals("RS"))
  591. currDescription.setRSBandwidth(bandwidth.get(j).getValue());
  592. else
  593. codecLine += bandwidth.get(j).toString() + System.getProperty("line.separator");
  594. currDescription.setCodecData(codecLine.getBytes());
  595. if (currMedia.getMediaPort() == 0)
  596. currDescription.setFlowStatus(FlowStatus.REMOVED);
  597. else if (mode.equals("recvonly"))
  598. currDescription.setFlowStatus(FlowStatus.ENABLED_DOWNLINK);
  599. else if (mode.equals("sendonly"))
  600. currDescription.setFlowStatus(FlowStatus.ENABLED_UPLINK);
  601. else if (mode.equals("inactive"))
  602. currDescription.setFlowStatus(FlowStatus.DISABLED);
  603. else
  604. currDescription.setFlowStatus(FlowStatus.ENABLED);
  605. currDescription.setMediaComponentNumber(i + 1);
  606. mediaComponentDescriptions.add(currDescription);
  607. }
  608. return mediaComponentDescriptions.toArray(new MediaComponentDescription[mediaComponentDescriptions.size()]);
  609. }
  610. public static SessionDescription convertMediaComponents(MediaComponentDescription[] data, Boolean source) {
  611. String result = new String();
  612. MediaSubComponent[] currSubComponents;
  613. IPFilterRule[] currRules;
  614. Boolean rtcpFound, dataFound;
  615. String originAddress = new String();
  616. for (int i = 0; i < data.length; i++) {
  617. if (source && data[i].getCodecData().length > 0)
  618. result += data[i].getCodecData()[0] + System.getProperty("line.separator");
  619. else if (!source && data[i].getCodecData().length > 1)
  620. result += data[i].getCodecData()[1] + System.getProperty("line.separator");
  621. if (data[i].hasFlowStatus()) {
  622. switch (data[i].getFlowStatus().getValue()) {
  623. case FlowStatus._ENABLED_UPLINK:
  624. if (source)
  625. result += "a=sendonly" + System.getProperty("line.separator");
  626. else
  627. result += "a=recvonly" + System.getProperty("line.separator");
  628. break;
  629. case FlowStatus._ENABLED_DOWNLINK:
  630. if (source)
  631. result += "a=recvonly" + System.getProperty("line.separator");
  632. else
  633. result += "a=sendonly" + System.getProperty("line.separator");
  634. break;
  635. case FlowStatus._ENABLED:
  636. result += "a=sendrecv" + System.getProperty("line.separator");
  637. break;
  638. case FlowStatus._DISABLED:
  639. result += "a=inactive" + System.getProperty("line.separator");
  640. break;
  641. }
  642. }
  643. if (data[i].hasMaxRequestedBandwidthDL() && source)
  644. result += "b=AS:" + data[i].getMaxRequestedBandwidthDL() + System.getProperty("line.separator");
  645. if (data[i].hasMaxRequestedBandwidthUL() && !source)
  646. result += "b=AS:" + data[i].getMaxRequestedBandwidthUL() + System.getProperty("line.separator");
  647. if (data[i].hasRRBandwidth())
  648. result += "b=RR:" + data[i].getRRBandwidth() + System.getProperty("line.separator");
  649. if (data[i].hasRSBandwidth())
  650. result += "b=RS:" + data[i].getRSBandwidth() + System.getProperty("line.separator");
  651. currSubComponents = data[i].getMediaSubComponents();
  652. rtcpFound = false;
  653. dataFound = false;
  654. for (int j = 0; j < currSubComponents.length; j++) {
  655. if (currSubComponents[j].hasFlowUsage() && currSubComponents[j].getFlowUsage().getValue() == FlowUsage._RTCP && !rtcpFound) {
  656. currRules = currSubComponents[j].getFlowDescriptions();
  657. for (int k = 0; k < currRules.length; k++)
  658. if (currRules[k].getDirection() == IPFilterRule.DIR_IN && source) {
  659. if (currRules[k].getSourcePorts().length > 0 && IPAddressUtil.isIPv4LiteralAddress(currRules[k].getSourceIp())) {
  660. result += "a=rtcp:" + currRules[k].getSourcePorts()[0][0] + " IN IP4 " + currRules[k].getSourceIp()
  661. + System.getProperty("line.separator");
  662. rtcpFound = true;
  663. }
  664. else if (currRules[k].getSourcePorts().length > 0 && IPAddressUtil.isIPv6LiteralAddress(currRules[k].getSourceIp())) {
  665. result += "a=rtcp:" + currRules[k].getSourcePorts()[0][0] + " IN IP6 " + currRules[k].getSourceIp()
  666. + System.getProperty("line.separator");
  667. rtcpFound = true;
  668. }
  669. }
  670. else if (currRules[k].getDirection() == IPFilterRule.DIR_OUT && !source) {
  671. if (currRules[k].getDestPorts().length > 0 && IPAddressUtil.isIPv4LiteralAddress(currRules[k].getDestIp())) {
  672. result += "a=rtcp:" + currRules[k].getDestPorts()[0][0] + " IN IP4 " + currRules[k].getDestIp()
  673. + System.getProperty("line.separator");
  674. rtcpFound = true;
  675. }
  676. else if (currRules[k].getDestPorts().length > 0 && IPAddressUtil.isIPv6LiteralAddress(currRules[k].getDestIp())) {
  677. result += "a=rtcp:" + currRules[k].getDestPorts()[0][0] + " IN IP6 " + currRules[k].getDestIp()
  678. + System.getProperty("line.separator");
  679. rtcpFound = true;
  680. }
  681. }
  682. }
  683. else if (!dataFound) {
  684. currRules = currSubComponents[j].getFlowDescriptions();
  685. for (int k = 0; k < currRules.length; k++)
  686. if (currRules[k].getDirection() == IPFilterRule.DIR_IN && source) {
  687. if (IPAddressUtil.isIPv4LiteralAddress(currRules[k].getSourceIp())) {
  688. originAddress = "IN IP4 " + currRules[k].getSourceIp();
  689. result += "c=IN IP4 " + currRules[k].getSourceIp() + System.getProperty("line.separator");
  690. dataFound = true;
  691. }
  692. else if (IPAddressUtil.isIPv6LiteralAddress(currRules[k].getSourceIp())) {
  693. originAddress = "IN IP6 " + currRules[k].getSourceIp();
  694. result += "c=IN IP6 " + currRules[k].getSourceIp() + System.getProperty("line.separator");
  695. dataFound = true;
  696. }
  697. }
  698. else if (currRules[k].getDirection() == IPFilterRule.DIR_OUT && !source) {
  699. if (IPAddressUtil.isIPv4LiteralAddress(currRules[k].getDestIp())) {
  700. originAddress = "IN IP4 " + currRules[k].getDestIp();
  701. result += "c=IN IP4 " + currRules[k].getDestIp() + System.getProperty("line.separator");
  702. dataFound = true;
  703. }
  704. else if (IPAddressUtil.isIPv6LiteralAddress(currRules[k].getSourceIp())) {
  705. originAddress = "IN IP6 " + currRules[k].getDestIp();
  706. result += "c=IN IP6 " + currRules[k].getDestIp() + System.getProperty("line.separator");
  707. dataFound = true;
  708. }
  709. }
  710. }
  711. }
  712. }
  713. SdpFactory sdpFactory = SdpFactory.getInstance();
  714. try {
  715. // those values are not transferred in gq and are required
  716. result = "o=- 1 1 " + originAddress + System.getProperty("line.separator") + result;
  717. result = "t=0 0" + System.getProperty("line.separator") + result;
  718. result = "s=Diameter GQ Parser" + System.getProperty("line.separator") + result;
  719. result = "v=0" + System.getProperty("line.separator") + result;
  720. SessionDescription resultDescription = sdpFactory.createSessionDescription(result);
  721. return resultDescription;
  722. }
  723. catch (Exception ex) {
  724. return null;
  725. }
  726. }
  727. }