VideoTestController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Http;
  6. use Jenssegers\Agent\Agent;
  7. use OpenTok\MediaMode;
  8. use OpenTok\OpenTok;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Str;
  11. use OpenTok\ArchiveMode;
  12. use OpenTok\OutputMode;
  13. class VideoTestController extends Controller
  14. {
  15. public function index(){
  16. $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
  17. $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
  18. $otSessionId = $otSession->getSessionId();
  19. $otToken = $opentok->generateToken($otSessionId);
  20. $step = 'webcam-test';
  21. $agent = new Agent();
  22. $allow = !$agent->isPhone() && !$agent->isTablet();
  23. return view('app.video.video-test', compact('otSessionId', 'otToken', 'step', 'allow'));
  24. }
  25. // get video url from archive id
  26. public function getArchive($archiveId)
  27. {
  28. $opentok = new OpenTok(config('app.opentokApiKey'), config('app.opentokApiSecret'));
  29. $archive = $opentok->getArchive($archiveId);
  30. while ($archive->url == null) {
  31. $archive = $opentok->getArchive($archiveId);
  32. }
  33. return response()->json(['video' => $archive->url]);
  34. }
  35. public function startRecording(Request $request, $sessionId)
  36. {
  37. $opentok = new OpenTok(config('app.opentokApiKey'), config('app.opentokApiSecret'));
  38. $archiveOptions = array(
  39. 'name' => 'Recording Data',
  40. 'hasAudio' => true,
  41. 'hasVideo' => true,
  42. 'outputMode' => OutputMode::COMPOSED
  43. );
  44. $archive = $opentok->startArchive($sessionId, $archiveOptions);
  45. $archiveId = $archive->id;
  46. session()->put('archive_id', $archiveId);
  47. return response()->json($archive);
  48. }
  49. public function stopRecording($archiveId)
  50. {
  51. $opentok = new OpenTok(config('app.opentokApiKey'), config('app.opentokApiSecret'));
  52. $archive = $opentok->stopArchive($archiveId);
  53. return response()->json($archive);
  54. }
  55. public function deleteArchive($archiveId)
  56. {
  57. $opentok = new OpenTok(config('app.opentokApiKey'), config('app.opentokApiSecret'));
  58. $opentok->deleteArchive($archiveId);
  59. return response()->json(['response' => 'archive deleted successfully']);
  60. }
  61. public function saveArchive(Request $request)
  62. {
  63. $opentok = new OpenTok(config('app.opentokApiKey'), config('app.opentokApiSecret'));
  64. $archiveId = $request->input('archiveID');
  65. $archive = $opentok->getArchive($archiveId);
  66. //If failed to get the recorded data URL, call the API again
  67. while ($archive->url == null) {
  68. $archive = $opentok->getArchive($archiveId);
  69. }
  70. $contents = file_get_contents($archive->url);
  71. $fileName = Str::uuid() . '_' . Carbon::now()->format('d-m-y_h-i-s-a') . '.mp4';
  72. $result = Storage::disk('local')->put($fileName, $contents);
  73. if(!$result){
  74. return $this->fail('Unable to save file.');
  75. }
  76. $response = $this->postFileToJava($request, $fileName, $request->input('ep') ? $request->input('ep') : null);
  77. if(!@$response['success']){
  78. return $this->fail($response['message']);
  79. }
  80. return $response;
  81. }
  82. private function postFileToJava($request, $fileName, $ep = null){
  83. $requestToPost = Http::asMultipart();
  84. try {
  85. $videoFilePath = storage_path('app/'.$fileName);
  86. $videoFile = fopen(config('app.storageDir') . '/' .$videoFilePath, 'r');
  87. $requestToPost->attach($ep ? 'file' : 'messageVideo', $videoFile, $fileName);
  88. } catch (\Exception $e) {
  89. error_log($e->getMessage() . " VIDEO PATH: " . $fileName);
  90. }
  91. $backendUrl = config('stag.backendUrl') . ($ep ? $ep : '/internalMessage/create');
  92. $inputs = [
  93. ['name'=>'toProUid', 'contents' => $request->get('toProUid')],
  94. ['name'=>'regardingClientUid', 'contents' => $request->get('regardingClientUid')],
  95. ['name'=>'regardingCompanyUid', 'contents' => $request->get('regardingCompanyUid')],
  96. ['name'=>'contentText', 'contents' => $request->get('contentText')],
  97. ['name'=> 'sessionKey', 'contents' => $this->performer->session_key],
  98. ];
  99. if($ep) {
  100. $inputs = [
  101. ['name'=>'purpose', 'contents' => 'webcam'],
  102. ['name'=> 'sessionKey', 'contents' => $this->performer->session_key],
  103. ];
  104. }
  105. $response = $requestToPost->post($backendUrl, $inputs)->json();
  106. return $response;
  107. }
  108. public function videoDownload($uid, Request $request)
  109. {
  110. //TODO
  111. return Storage::download('video-file.mp4');
  112. }
  113. public function uploadVideo(Request $request)
  114. {
  115. if ($request->hasFile('hcp_intro')) {
  116. $video = $request->file('hcp_intro');
  117. $fileName = time() . '.' . $video->getClientOriginalExtension();
  118. $path = $video->store(""); //, uniqid() . '.' . $request->file('hcp_intro')->getClientOriginalExtension());
  119. }
  120. return redirect()->back();
  121. }
  122. }