pro-call.blade.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. @extends('layouts.meeting')
  2. @section('content')
  3. <div id="proCallComponent">
  4. <div class="d-flex align-items-center justify-content-center py-3 border-bottom">
  5. <span class="mr-3">
  6. {{ $pro->name_display }} | PRO
  7. </span>
  8. <button class="btn btn-sm btn-primary px-4 font-weight-bold"
  9. v-on:click.prevent="nextPatient()"
  10. :disabled="client || checkingForNextPatient || started">Next Patient</button>
  11. <span v-if="patientInQueue && !started" class="patient-in-q-alert text-warning text-sm ml-2 small">
  12. <i class="fa fa-circle"></i>
  13. </span>
  14. <span v-if="!patientInQueue && !started" class="text-success text-sm ml-2 small">
  15. <i class="fa fa-circle"></i>
  16. </span>
  17. </div>
  18. <div v-if="!started && noNextPatient" class="bg-light rounded text-center py-1 font-weight-bold text-sm my-3 mx-3">@{{ noNextPatient }}</div>
  19. <div class="" v-show="videoActive">
  20. <div class="py-3 text-center" v-if="started">
  21. <h6 class="text-black font-weight-bold m-0">Call in progress: @{{ timeDisplay() }}</h6>
  22. </div>
  23. <div class="main-view mx-auto">
  24. <div id="self-view" class="full-view" data-name="{{ $pro->name_display }}" data-type="PRO"></div>
  25. <div class="thumbs">
  26. </div>
  27. <button class="btn btn-danger rounded-circle hang-up"
  28. v-if="started"
  29. title="Leave Call"
  30. v-on:click.prevent="hangUp()">
  31. <i class="fa fa-phone"></i>
  32. </button>
  33. </div>
  34. </div>
  35. </div>
  36. <script>
  37. new Vue({
  38. el: '#proCallComponent',
  39. delimiters: ['@{{', '}}'],
  40. data: {
  41. time: 0,
  42. startTime: 0,
  43. started: false,
  44. client: false,
  45. pro: false,
  46. selfName: '',
  47. selfToken: '',
  48. clientUid: '',
  49. otSessionId: '',
  50. checkingForNextPatient: false,
  51. noNextPatient: false,
  52. otSession: false,
  53. selfUserType: 'PRO',
  54. patientInQueue: false,
  55. videoActive: false
  56. },
  57. methods: {
  58. pollForNextPatient: function() {
  59. if(!this.started) {
  60. this.nextPatient(true);
  61. }
  62. },
  63. nextPatient: function(_pollOnly = false) {
  64. var self = this;
  65. if(!_pollOnly) this.checkingForNextPatient = true;
  66. $.post('/api/client/getNextClientForVideoVisit', {}, function(_data) {
  67. if(_pollOnly) {
  68. self.patientInQueue = _data.success;
  69. }
  70. else {
  71. self.checkingForNextPatient = false;
  72. if(!_data.success) {
  73. self.noNextPatient = _data.message;
  74. window.setTimeout(function() {
  75. self.noNextPatient = false;
  76. }, 2000);
  77. }
  78. else {
  79. // get ot session key from client record
  80. self.client = true;
  81. self.clientUid = _data.data;
  82. self.videoActive = true;
  83. self.startOpenTokSession();
  84. }
  85. }
  86. }, 'json');
  87. },
  88. startOpenTokSession: function() {
  89. var self = this;
  90. self.getOpenTokSessionId(function() {
  91. self.selfName = '{{ $pro->name_display }}';
  92. $.post('/api/openTok/getClientToken', {
  93. opentokSessionId: self.otSessionId,
  94. data: JSON.stringify({
  95. uid: '{{ $pro->uid }}',
  96. name: self.selfName,
  97. type: 'PRO'
  98. })
  99. }, function (_data) {
  100. console.log(_data);
  101. self.selfToken = _data.data;
  102. self.initOpenTok();
  103. });
  104. });
  105. },
  106. timeDisplay: function() {
  107. var seconds = this.time / 1000,
  108. minutes = parseInt(seconds / 60, 10);
  109. seconds = parseInt(seconds % 60, 10);
  110. return minutes + " min, " + seconds + " sec";
  111. },
  112. hangUp: function() {
  113. if(this.otSession) {
  114. try {
  115. this.otSession.disconnect();
  116. }
  117. catch (e) {
  118. console.log('Was already disconnected.');
  119. }
  120. this.otSession = false;
  121. this.otSessionId = '';
  122. this.started = false;
  123. this.startTime = false;
  124. this.videoActive = false;
  125. // this.client = false;
  126. }
  127. },
  128. initOpenTok: function() {
  129. /* fake video feed (temp) */
  130. const randomColour = () => {
  131. return Math.round(Math.random() * 255);
  132. };
  133. const canvas = document.createElement('canvas');
  134. canvas.width = 640;
  135. canvas.height = 480;
  136. const ctx = canvas.getContext('2d');
  137. var pos = 100;
  138. window.setInterval(function() {
  139. ctx.clearRect(0, 0, canvas.width, canvas.height);
  140. ctx.font = "20px Georgia";
  141. ctx.fillStyle = `rgb(220, 220, 220)`;
  142. ctx.fillText("Video feed from {{ $pro->name_display }}", 20, pos);
  143. pos += 5;
  144. if(pos > canvas.height) pos = 100;
  145. }, 1000);
  146. var self = this;
  147. var apiKey = '<?= env('TOKBOX_API_KEY', '46678902') ?>';
  148. var sessionId = this.otSessionId;
  149. var token = this.selfToken;
  150. // destroy if existing
  151. // self.hangUp();
  152. self.otSession = OT.initSession(apiKey, sessionId);
  153. // peer connected
  154. self.otSession.on('streamCreated', function streamCreated(event) {
  155. console.log('streamCreated', arguments);
  156. var subscriberOptions = {
  157. insertMode: 'append',
  158. width: '100%',
  159. height: '100%'
  160. };
  161. var connectionData = JSON.parse(event.stream.connection.data);
  162. // add a div for remove view
  163. var remoteViewID = 'remote-view-' + event.stream.id;
  164. var remoteElem = $('<div id="' + remoteViewID + '" class="remote-view thumb-view" ' +
  165. 'data-stream="' + event.stream.id + '" ' +
  166. 'data-connection-data="' + event.stream.connection.data + '" ' +
  167. 'data-name="' + connectionData.name + '" ' +
  168. 'data-type="' + connectionData.type + '"></div>');
  169. remoteElem.appendTo('.thumbs');
  170. self.otSession.subscribe(event.stream, remoteViewID, subscriberOptions, self.handleOpenTokError);
  171. if (connectionData.type === 'CLIENT') {
  172. self.client = true;
  173. }
  174. if(!self.startTime) {
  175. self.startTime = new Date().getTime();
  176. window.setInterval(function() {
  177. self.time = new Date().getTime() - self.startTime;
  178. }, 1000);
  179. self.started = true;
  180. }
  181. self.activateParty(event.stream.id);
  182. });
  183. // peer disconnected
  184. self.otSession.on("streamDestroyed", function(event) {
  185. onPeerDisconnection(event, event.stream.connection.data);
  186. });
  187. // self.otSession.on("connectionDestroyed", function(event) {
  188. // debugger;
  189. // console.log('connectionDestroyed from ' + event.connection.data);
  190. // onPeerDisconnection(event, event.connection.data);
  191. // });
  192. self.otSession.on("connectionCreated", function(event) {
  193. console.log('connectionCreated');
  194. console.log(event);
  195. });
  196. function onPeerDisconnection(event, data) {
  197. if(event.stream && $('.full-view[data-stream="' + event.stream.id + '"]').length) {
  198. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):visible');
  199. if(allThumbs.length) {
  200. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  201. if($(this).attr('data-stream') !== event.stream.id) {
  202. self.activateParty($(this).attr('data-stream'));
  203. return false;
  204. }
  205. });
  206. }
  207. else {
  208. self.hangUp();
  209. }
  210. }
  211. if(event.stream) {
  212. var remoteViewElem = $('[data-stream="' + event.stream.id + '"]');
  213. if(remoteViewElem.length) {
  214. remoteViewElem.attr('data-stream', '');
  215. remoteViewElem.attr('data-connection-data', '');
  216. remoteViewElem.attr('data-type', '');
  217. remoteViewElem.attr('data-name', '');
  218. }
  219. remoteViewElem.addClass('disconnected-view')
  220. }
  221. var connectionData = JSON.parse(data);
  222. if(connectionData.type === 'CLIENT') {
  223. self.client = false;
  224. }
  225. // if no other parties in call, hang up
  226. if(!$('[data-stream]:not([data-stream=""])').length) {
  227. self.hangUp();
  228. }
  229. }
  230. // self connected
  231. self.otSession.on("sessionConnected", function(event) {
  232. self.joinMeetingAsPro(self.selfUserType);
  233. });
  234. // self disconnected
  235. self.otSession.on('sessionDisconnected', function sessionDisconnected(event) {
  236. console.log('You were disconnected from the session.', event.reason);
  237. });
  238. // initialize the publisher
  239. var publisherOptions = {
  240. videoSource: canvas.captureStream(1).getVideoTracks()[0], // TODO: Comment this line to use webcam
  241. insertMode: 'append',
  242. width: '100%',
  243. height: '100%',
  244. };
  245. var publisher = OT.initPublisher('self-view', publisherOptions, self.handleOpenTokError);
  246. publisher.on('streamCreated', function(event) {
  247. var selfView = $('#self-view');
  248. selfView.attr('data-stream', event.stream.id);
  249. selfView.attr('data-connection-data', event.stream.connection.data);
  250. });
  251. publisher.on('streamCreated', function(event) {
  252. console.log('publisher->streamCreated');
  253. var selfView = $('#self-view');
  254. selfView.attr('data-stream', event.stream.id);
  255. selfView.attr('data-connection-data', event.stream.connection.data);
  256. selfView.attr('data-type', 'PRO');
  257. self.activateParty('self');
  258. $('#self-view').show();
  259. });
  260. publisher.on('streamDestroyed', function(event) {
  261. event.preventDefault();
  262. console.log('publisher->streamDestroyed');
  263. $('#self-view').hide();
  264. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):visible');
  265. if(allThumbs.length) {
  266. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  267. if($(this).attr('data-stream') !== $('#self-view').attr('data-stream')) {
  268. self.activateParty($(this).attr('data-stream'));
  269. return false;
  270. }
  271. });
  272. }
  273. else {
  274. self.hangUp();
  275. }
  276. });
  277. // Connect to the session
  278. self.otSession.connect(token, function callback(error) {
  279. if (error) {
  280. self.handleOpenTokError(error);
  281. } else {
  282. // If the connection is successful, publish the publisher to the session
  283. self.otSession.publish(publisher, self.handleOpenTokError);
  284. }
  285. });
  286. },
  287. handleOpenTokError: function(e) {
  288. },
  289. getOpenTokSessionId: function(_done) {
  290. var self = this;
  291. $.get('/pro/get-opentok-session-key/' + self.clientUid, function(_data) {
  292. self.otSessionId = _data.data;
  293. console.log(_data);
  294. _done();
  295. }, 'json');
  296. },
  297. joinMeetingAsPro: function(_type) {
  298. var self = this;
  299. $.ajax({
  300. type: 'post',
  301. url: '/api/clientVideoVisit/joinVideoVisitAsPro',
  302. headers: {
  303. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  304. },
  305. data: {uid: self.clientUid},
  306. dataType: 'json'
  307. })
  308. .done(function (_data) {
  309. console.log(_data);
  310. })
  311. .fail(function (_data) {
  312. console.warn(_data);
  313. alert(_data.message);
  314. });
  315. },
  316. activateParty: function(_stream = 'self') {
  317. var current = $('.full-view');
  318. if(current.attr('data-stream') === _stream) return;
  319. current.removeClass('full-view').addClass('thumb-view');
  320. if(current.attr('data-type') === 'CLIENT') {
  321. current.prependTo('.thumbs');
  322. }
  323. else {
  324. current.appendTo('.thumbs');
  325. }
  326. if(_stream === 'self') {
  327. $('#self-view')
  328. .removeClass('thumb-view')
  329. .removeClass('disconnected-view')
  330. .addClass('full-view')
  331. .prependTo('.main-view');
  332. }
  333. else {
  334. $('div[data-stream="' + _stream + '"]')
  335. .removeClass('thumb-view')
  336. .removeClass('disconnected-view')
  337. .addClass('full-view')
  338. .prependTo('.main-view');
  339. }
  340. }
  341. },
  342. mounted: function() {
  343. var self = this;
  344. $(document).on('click', '.thumbs>div[data-stream]', function() {
  345. self.activateParty($(this).attr('data-stream'));
  346. return false;
  347. });
  348. // poll for new patients and alert
  349. window.setInterval(function() {
  350. self.pollForNextPatient();
  351. }, 5000);
  352. window.onbeforeunload = function() {
  353. if(self.started) {
  354. return "A call is in progress";
  355. }
  356. };
  357. @if(isset($client))
  358. self.client = true;
  359. self.clientUid = '{{ $client->uid }}';
  360. self.videoActive = true;
  361. self.startOpenTokSession();
  362. @endif
  363. }
  364. });
  365. </script>
  366. @endsection