SDPtoJingle.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. use Movim\Session;
  3. class SDPtoJingle
  4. {
  5. private $sdp;
  6. private $arr;
  7. private $jingle;
  8. private $content = null;
  9. private $transport = null;
  10. private $action;
  11. private $mid;
  12. private $mlineindex;
  13. // Move the global fingerprint into each medias
  14. private $global_fingerprint = [];
  15. private $fmtp_cache = [];
  16. private $rtcp_fb_cache = [];
  17. private $regex = [
  18. 'candidate' => "/^a=candidate:(\w{1,32}) (\d{1,5}) (udp|tcp) (\d{1,10}) ([a-zA-Z0-9:\.]{1,45}) (\d{1,5}) (typ) (host|srflx|prflx|relay)( (raddr) ([a-zA-Z0-9:\.]{1,45}) (rport) (\d{1,5}))?( (generation) (\d) (network) (\d) (id) ([a-zA-Z0-9]{1,45}))?/i", //à partir de generation les attr sont spécifiques à XMPP..autant l'enlever de la REGEX et les traiter à part? En théorie ils peuvent être dans n'importe quel ordre.
  19. 'sess_id' => "/^o=(\S+) (\d+)/i",
  20. 'group' => "/^a=group:(\S+) (.+)/i",
  21. 'rtpmap' => "/^a=rtpmap:(\d+) (([^\s\/]+)(\/(\d+)(\/([^\s\/]+))?)?)?/i",
  22. 'fmtp' => "/^a=fmtp:(\d+) (.+)/i",
  23. 'rtcp_fb' => "/^a=rtcp-fb:(\S+) (\S+)( (\S+))?/i",
  24. 'rtcp_fb_trr_int' => "/^a=rtcp-fb:(\d+) trr-int (\d+)/i",
  25. 'pwd' => "/^a=ice-pwd:(\S+)/i",
  26. 'ufrag' => "/^a=ice-ufrag:(\S+)/i",
  27. 'ptime' => "/^a=ptime:(\d+)/i",
  28. 'maxptime' => "/^a=maxptime:(\d+)/i",
  29. 'ssrc' => "/^a=ssrc:(\d+) (\w+)(:(\S+))?( (\w+))?/i",
  30. 'rtcp_mux' => "/^a=rtcp-mux/i",
  31. 'crypto' => "/^a=crypto:(\d{1,9}) (\w+) (\S+)( (\S+))?/i",
  32. 'zrtp_hash' => "/^a=zrtp-hash:(\S+) (\w+)/i",
  33. 'fingerprint' => "/^a=fingerprint:(\S+) (\S+)/i",
  34. 'setup' => "/^a=setup:(\S+)/i",
  35. 'extmap' => "/^a=extmap:([^\s\/]+)(\/([^\s\/]+))? (\S+)/i",
  36. 'sctpmap' => "/^a=sctpmap:(\d+) (\S+) (\d+)/i",
  37. 'mid' => "/^a=mid:(\S+)/i",
  38. 'bandwidth' => "/^b=(\w+):(\d+)/i",
  39. 'media' => "/^m=(audio|video|application|data)/i"
  40. ];
  41. function __construct($sdp, $initiator, $responder = false, $action = false, $mid = false, $mlineindex = false)
  42. {
  43. $this->sdp = $sdp;
  44. $this->arr = explode("\n", $this->sdp);
  45. if($mid) $this->mid = $mid;
  46. if($mlineindex) $this->mlineindex = $mlineindex;
  47. $this->jingle = new SimpleXMLElement('<jingle></jingle>');
  48. $this->jingle->addAttribute('xmlns', 'urn:xmpp:jingle:1');
  49. $this->jingle->addAttribute('initiator',$initiator);
  50. if($action)
  51. $this->jingle->addAttribute('action',$action);
  52. if($responder)
  53. $this->jingle->addAttribute('responder',$responder);
  54. $this->action = $action;
  55. }
  56. private function getSessionId()
  57. {
  58. $s = Session::start();
  59. if($sid = $s->get('jingleSid')){
  60. return $sid;
  61. }
  62. else{
  63. $o = $this->arr[1];
  64. $sid = explode(" ", $o);
  65. return substr(base_convert($sid[1], 30, 10), 0, 6);
  66. }
  67. }
  68. private function initContent($force = false)
  69. {
  70. if($this->content == null
  71. || $force) {
  72. $this->content = $this->jingle->addChild('content');
  73. $this->transport = $this->content->addChild('transport');
  74. $this->transport->addAttribute('xmlns', "urn:xmpp:jingle:transports:ice-udp:1");
  75. $this->content->addAttribute('creator', 'initiator'); // FIXME
  76. }
  77. }
  78. private function addFmtpParameters($payloadtype, $params)
  79. {
  80. foreach($params as $value) {
  81. $p = explode('=', trim($value));
  82. $parameter = $payloadtype->addChild('parameter');
  83. if(count($p) == 1) {
  84. $parameter->addAttribute('value', $p[0]);
  85. } else {
  86. $parameter->addAttribute('name', $p[0]);
  87. $parameter->addAttribute('value', $p[1]);
  88. }
  89. }
  90. }
  91. private function addRtcpFbParameters($payloadtype, $params)
  92. {
  93. foreach($params as $matches) {
  94. $rtcpfp = $payloadtype->addChild('rtcp-fb');
  95. $rtcpfp->addAttribute('xmlns', "urn:xmpp:jingle:apps:rtp:rtcp-fb:0");
  96. $rtcpfp->addAttribute('id', $matches[1]);
  97. $rtcpfp->addAttribute('type', $matches[2]);
  98. if(isset($matches[4]))
  99. $rtcpfp->addAttribute('subtype', $matches[4]);
  100. }
  101. }
  102. public function addName($name = false)
  103. {
  104. if($name) {
  105. $this->content->addAttribute('name', $name);
  106. } elseif($this->mid) {
  107. $this->content->addAttribute('name', $this->mid);
  108. }
  109. }
  110. function generate()
  111. {
  112. foreach($this->arr as $l) {
  113. foreach($this->regex as $key => $r) {
  114. if(preg_match($r, $l, $matches)) {
  115. switch($key) {
  116. case 'sess_id':
  117. $this->jingle->addAttribute('sid', $this->getSessionId());
  118. break;
  119. case 'media':
  120. $this->initContent(true);
  121. // The description node
  122. if($this->action != 'transport-info') {
  123. $description = $this->content->addChild('description');
  124. $description->addAttribute('xmlns', "urn:xmpp:jingle:apps:rtp:1");
  125. $description->addAttribute('media', $matches[1]);
  126. }
  127. if(!empty($this->global_fingerprint)) {
  128. $fingerprint = $this->transport->addChild('fingerprint', $this->global_fingerprint['fingerprint']);
  129. //$this->transport->addAttribute('pwd', $this->global_fingerprint['pwd']);
  130. //$this->transport->addAttribute('ufrag', $this->global_fingerprint['ufrag']);
  131. $fingerprint->addAttribute('xmlns', "urn:xmpp:jingle:apps:dtls:0");
  132. $fingerprint->addAttribute('hash', $this->global_fingerprint['hash']);
  133. }
  134. break;
  135. case 'mid':
  136. $this->addName($matches[1]);
  137. break;
  138. case 'bandwidth':
  139. $bandwidth = $description->addChild('bandwidth');
  140. $bandwidth->addAttribute('type', $matches[1]);
  141. $bandwidth->addAttribute('value', $matches[2]);
  142. break;
  143. case 'rtpmap':
  144. $payloadtype = $description->addChild('payload-type');
  145. $payloadtype->addAttribute('id', $matches[1]);
  146. $payloadtype->addAttribute('name', $matches[3]);
  147. if(isset($matches[4]))
  148. $payloadtype->addAttribute('clockrate', $matches[5]);
  149. if(isset($matches[7]))
  150. $payloadtype->addAttribute('channels', $matches[7]);
  151. if(isset($this->fmtp_cache[$matches[1]])) {
  152. $this->addFmtpParameters($payloadtype, $this->fmtp_cache[$matches[1]]);
  153. unset($this->fmtp_cache[$matches[1]]);
  154. }
  155. if(isset($this->rtcp_fb_cache[$matches[1]])) {
  156. $this->addRtcpFbParameters($payloadtype, $this->rtcp_fb_cache[$matches[1]]);
  157. unset($this->rtcp_fb_cache[$matches[1]]);
  158. }
  159. break;
  160. // http://xmpp.org/extensions/xep-0167.html#format
  161. case 'fmtp':
  162. // If fmtp is added just after the correspondant rtpmap
  163. $params = explode(';', $matches[2]);
  164. if(isset($payloadtype)
  165. && $matches[1] == $payloadtype->attributes()->id) {
  166. $this->addFmtpParameters($payloadtype, $params);
  167. // If not we cache it
  168. } else {
  169. $this->fmtp_cache[$matches[1]] = $params;
  170. }
  171. break;
  172. // http://xmpp.org/extensions/xep-0293.html
  173. case 'rtcp_fb':
  174. if($matches[1] == '*') {
  175. $this->addRtcpFbParameters($description, [$matches]);
  176. } else {
  177. if(isset($payloadtype)
  178. && $matches[1] == $payloadtype->attributes()->id) {
  179. $this->addRtcpFbParameters($payloadtype, [$matches]);
  180. } else {
  181. if(!isset($this->rtcp_fb_cache[$matches[1]])) {
  182. $this->rtcp_fb_cache[$matches[1]] = [];
  183. }
  184. array_push($this->rtcp_fb_cache[$matches[1]], $matches);
  185. }
  186. }
  187. break;
  188. case 'rtcp_fb_trr_int':
  189. $rtcpfp = $payloadtype->addChild('rtcp-fb-trr-int');
  190. $rtcpfp->addAttribute('xmlns', "urn:xmpp:jingle:apps:rtp:rtcp-fb:0");
  191. $rtcpfp->addAttribute('id', $matches[1]);
  192. $rtcpfp->addAttribute('value', $matches[2]);
  193. break;
  194. // http://xmpp.org/extensions/xep-0167.html#srtp
  195. case 'crypto':
  196. $encryption = $description->addChild('encryption');
  197. $crypto = $encryption->addChild('crypto');
  198. $crypto->addAttribute('crypto-suite', $matches[2]);
  199. $crypto->addAttribute('key-params', $matches[3]);
  200. $crypto->addAttribute('tag', $matches[1]);
  201. if(isset($matches[5]))
  202. $crypto->addAttribute('session-params', $matches[5]);
  203. break;
  204. // http://xmpp.org/extensions/xep-0262.html
  205. case 'zrtp_hash':
  206. $zrtphash = $encryption->addChild('zrtp-hash', $matches[2]);
  207. $zrtphash->addAttribute('xmlns', "urn:xmpp:jingle:apps:rtp:zrtp:1");
  208. $zrtphash->addAttribute('version', $matches[1]);
  209. break;
  210. case 'rtcp_mux':
  211. $description->addChild('rtcp-mux');
  212. break;
  213. // http://xmpp.org/extensions/xep-0294.html
  214. case 'extmap':
  215. $rtphdrext = $description->addChild('rtp-hdrext');
  216. $rtphdrext->addAttribute('xmlns', "urn:xmpp:jingle:apps:rtp:rtp-hdrext:0");
  217. $rtphdrext->addAttribute('id', $matches[1]);
  218. $rtphdrext->addAttribute('uri', $matches[4]);
  219. if(isset($matches[3]) && $matches[3] != '')
  220. $rtphdrext->addAttribute('senders', $matches[3]);
  221. break;
  222. // http://xmpp.org/extensions/xep-0339.html
  223. case 'ssrc':
  224. if(!$description->source) {
  225. $ssrc = $description->addChild('source');
  226. $ssrc->addAttribute('xmlns', "urn:xmpp:jingle:apps:rtp:ssma:0");
  227. $ssrc->addAttribute('id', $matches[1]);
  228. }
  229. $param = $ssrc->addChild('parameter');
  230. $param->addAttribute('name', $matches[2]);
  231. $param->addAttribute('value', $matches[4]);
  232. break;
  233. case 'ptime':
  234. $description->addAttribute('ptime', $matches[1]);
  235. break;
  236. case 'maxptime':
  237. $description->addAttribute('maxptime', $matches[1]);
  238. break;
  239. // http://xmpp.org/extensions/xep-0338.html
  240. case 'group':
  241. $group = $this->jingle->addChild('group');
  242. $group->addAttribute('xmlns', "urn:xmpp:jingle:apps:grouping:0");
  243. $group->addAttribute('semantics', $matches[1]);
  244. $params = explode(' ', $matches[2]);
  245. foreach($params as $value) {
  246. $content = $group->addChild('content');
  247. $content->addAttribute('name', trim($value));
  248. }
  249. break;
  250. // http://xmpp.org/extensions/xep-0320.html
  251. case 'fingerprint':
  252. if($this->content == null) {
  253. $this->global_fingerprint['fingerprint'] = $matches[2];
  254. $this->global_fingerprint['hash'] = $matches[1];
  255. } else {
  256. $fingerprint = $this->transport->addChild('fingerprint', $matches[2]);
  257. $fingerprint->addAttribute('xmlns', "urn:xmpp:jingle:apps:dtls:0");
  258. $fingerprint->addAttribute('hash', $matches[1]);
  259. }
  260. break;
  261. // http://xmpp.org/extensions/inbox/jingle-dtls.html
  262. case 'sctpmap':
  263. $sctpmap = $this->transport->addChild('sctpmap');
  264. $sctpmap->addAttribute('xmlns', "urn:xmpp:jingle:transports:dtls-sctp:1");
  265. $sctpmap->addAttribute('number', $matches[1]);
  266. $sctpmap->addAttribute('protocol', $matches[2]);
  267. $sctpmap->addAttribute('streams', $matches[3]);
  268. break;
  269. // http://xmpp.org/extensions/xep-0320.html
  270. case 'setup':
  271. if($this->content != null) {
  272. $fingerprint->addAttribute('setup', $matches[1]);
  273. }
  274. break;
  275. case 'pwd':
  276. $this->transport->addAttribute('pwd', $matches[1]);
  277. break;
  278. case 'ufrag':
  279. $this->transport->addAttribute('ufrag', $matches[1]);
  280. break;
  281. case 'candidate':
  282. $this->initContent();
  283. $this->addName();
  284. $generation = $network = $id = $networkid = false;
  285. if($key = array_search("generation", $matches))
  286. $generation = $matches[($key+1)];
  287. if($key = array_search("network", $matches))
  288. $network = $matches[($key+1)];
  289. if($key = array_search("id", $matches))
  290. $id = $matches[($key+1)];
  291. if($key = array_search("network-id", $matches))
  292. $networkid = $matches[($key+1)];
  293. if(isset($matches[11]) && isset($matches[13])) {
  294. $reladdr = $matches[11];
  295. $relport = $matches[13];
  296. } else {
  297. $reladdr = $relport = null;
  298. }
  299. $candidate = $this->transport->addChild('candidate');
  300. $candidate->addAttribute('component' , $matches[2]);
  301. $candidate->addAttribute('foundation', $matches[1]);
  302. if($generation)
  303. $candidate->addAttribute('generation', $generation);
  304. if($id)
  305. $candidate->addAttribute('id' , $id);
  306. if($network)
  307. $candidate->addAttribute('network' , $network);
  308. if($networkid)
  309. $candidate->addAttribute('network-id', $networkid);
  310. $candidate->addAttribute('ip' , $matches[5]);
  311. $candidate->addAttribute('port' , $matches[6]);
  312. $candidate->addAttribute('priority' , $matches[4]);
  313. $candidate->addAttribute('protocol' , $matches[3]);
  314. $candidate->addAttribute('type' , $matches[8]);
  315. if($reladdr) {
  316. $candidate->addAttribute('rel-addr' , $reladdr);
  317. $candidate->addAttribute('rel-port' , $relport);
  318. }
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. return dom_import_simplexml($this->jingle);
  325. }
  326. }