BxStrmEngineOvenMediaEngine.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Stream Stream
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxStrmEngineOvenMediaEngine extends BxDol
  12. {
  13. public function getName()
  14. {
  15. return 'OvenMediaEngine';
  16. }
  17. public function isSreamFromBrowser()
  18. {
  19. return true;
  20. }
  21. public function getViewersNum($sStreamKey)
  22. {
  23. $a = $this->getStreamStats($sStreamKey);
  24. if (!$a)
  25. return false;
  26. return $a['totalConnections'];
  27. }
  28. public function getStreamStats($sStreamKey)
  29. {
  30. $sApp = getParam('bx_stream_app');
  31. if (!$sApp)
  32. return false;
  33. return $this->_apiRequest("/stats/current/vhosts/default/apps/{$sApp}/streams/{$sStreamKey}");
  34. }
  35. public function startRecording($iRecordingId, $sStreamKey)
  36. {
  37. $sApp = getParam('bx_stream_app');
  38. $sSourceName = getParam('bx_stream_server_ome_recordings_source');
  39. if (!$sApp)
  40. return false;
  41. if (!$sSourceName)
  42. $sSourceName = $sStreamKey;
  43. else
  44. $sSourceName = str_replace('{key}', $sStreamKey, $sSourceName);
  45. return $this->_apiRequest("/vhosts/default/apps/{$sApp}:startRecord", 'post-json', [
  46. "id" => "{$iRecordingId}",
  47. "stream" => [
  48. "name" => $sSourceName,
  49. ],
  50. ]);
  51. }
  52. public function stopRecording($iRecordingId)
  53. {
  54. $sApp = getParam('bx_stream_app');
  55. if (!$sApp)
  56. return false;
  57. return $this->_apiRequest("/vhosts/default/apps/{$sApp}:stopRecord", 'post-json', [
  58. "id" => "{$iRecordingId}",
  59. ]);
  60. }
  61. public function getRecordings($iRecordingId)
  62. {
  63. $sApp = getParam('bx_stream_app');
  64. if (!$sApp)
  65. return false;
  66. return $this->_apiRequest("/vhosts/default/apps/{$sApp}:records", 'post-json', [
  67. "id" => "{$iRecordingId}",
  68. ]);
  69. }
  70. public function processRecordings ($iRecordingId, $aContentInfo, $oModule, $iTries = 0)
  71. {
  72. $CNF = &$oModule->_oConfig->CNF;
  73. $bDeleteRecording = false;
  74. $oStorage = BxDolStorage::getObjectInstance('bx_stream_recordings');
  75. if ($oStorage) {
  76. $sFileUrl = getParam('bx_stream_recordings_url') . $aContentInfo['key'] . '_' . $iRecordingId . '.ts';
  77. $oModule->_oDb->updateRecording($iRecordingId, ['status' => 'processing']);
  78. $iFileId = $oStorage->storeFileFromUrl($sFileUrl, true, $aContentInfo[$CNF['FIELD_AUTHOR']], $aContentInfo['id']);
  79. if ($iFileId) {
  80. $bDeleteRecording = true;
  81. }
  82. else {
  83. bx_log('bx_stream', "Store recording from URL(" . $sFileUrl . ") failed for content id({$aContentInfo['id']}): " . $oStorage->getErrorString(), BX_LOG_ERR);
  84. }
  85. }
  86. else {
  87. bx_log('bx_stream', "Store recording failed because 'bx_stream_recordings' storage doesn't exist", BX_LOG_ERR);
  88. $bDeleteRecording = true;
  89. }
  90. if ($bDeleteRecording || ($iTries + 1) > 10) {
  91. $oModule->_oDb->deleteRecording($iRecordingId);
  92. }
  93. else {
  94. $oModule->_oDb->updateRecording($iRecordingId, ['tries' => $iTries + 1, 'status' => 'pending']);
  95. }
  96. }
  97. public function getRtmpSettings($sStreamKey)
  98. {
  99. $sHost = getParam('bx_stream_server_host');
  100. $sApp = getParam('bx_stream_app');
  101. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  102. if (!$sHost || !$sApp)
  103. return false;
  104. $sBaseUrl = "rtmp://$sHost:1935/$sApp";
  105. if (!$sPolicySecret)
  106. return ['server' => $sBaseUrl, 'key' => $sStreamKey];
  107. return ['server' => $sBaseUrl, 'key' => str_replace($sBaseUrl . '/', '', $this->_signUrl($sBaseUrl . '/' . $sStreamKey, $sPolicySecret))];
  108. }
  109. public function getRtmpIngestUrl($sStreamKey)
  110. {
  111. if (!($a = $this->getRtmpSettings($sStreamKey)))
  112. return false;
  113. list ($sBaseUrl, $sStreamKey) = $a;
  114. return $a['server'] . '/' . $a['key'];
  115. $sHost = getParam('bx_stream_server_host');
  116. $sApp = getParam('bx_stream_app');
  117. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  118. if (!$sHost || !$sApp)
  119. return false;
  120. $sBaseUrl = "rtmp://$sHost:1935/$sApp/$sStreamKey";
  121. if (!$sPolicySecret)
  122. return $sBaseUrl;
  123. return $this->_signUrl($sBaseUrl, $sPolicySecret);
  124. }
  125. public function getWebrtcIngestUrl($sStreamKey)
  126. {
  127. $sHost = getParam('bx_stream_server_host');
  128. $sApp = getParam('bx_stream_app');
  129. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  130. $sBaseUrl = "wss://$sHost:3334/$sApp/$sStreamKey?direction=send";
  131. if (!$sPolicySecret)
  132. return $sBaseUrl;
  133. return $this->_signUrl($sBaseUrl, $sPolicySecret);
  134. }
  135. protected function _signUrl($sBaseUrl, $sPolicySecret)
  136. {
  137. $sSignatureQueryKeyName = 'signature';
  138. $sPolicyQueryKeyName = 'policy';
  139. $sPolicy = json_encode(['url_expire' => 2556104400000]);//(time() + 50*365*24*60*60)*1000]);
  140. // 1. Perform base64url() for POLICY (RFC 4648 5.)
  141. $sPolicyBase64 = $this->_base64URLEncode($sPolicy);
  142. // 2. Generates an URL such as "ws://ome_host:3333/app/stream?policy=${POLICY_BASE64}"
  143. $sPolicyUrl = bx_append_url_params($sBaseUrl, [$sPolicyQueryKeyName => $sPolicyBase64]);
  144. // 3. Perform sha1(base64url()) for SIGNATURE (RFC 4648 5.)
  145. $sSha1 = hash_hmac('sha1', $sPolicyUrl, $sPolicySecret, true);
  146. $sSignature = $this->_base64URLEncode($sSha1);
  147. // 4. Create the whole URL
  148. $sWholeUrl = bx_append_url_params($sPolicyUrl, [$sSignatureQueryKeyName => $sSignature]);
  149. return $sWholeUrl;
  150. }
  151. protected function _base64URLEncode($s)
  152. {
  153. return trim(strtr(base64_encode($s), '+/', '-_'), '=');
  154. }
  155. protected function _apiRequest($sUri, $sMethod = 'get', $aBody = [])
  156. {
  157. $sApiProtocol = getParam('bx_stream_server_ome_api_protocol');
  158. $sHost = getParam('bx_stream_server_host');
  159. $iApiPort = (int)getParam('bx_stream_server_ome_api_port');
  160. $sApiKey = getParam('bx_stream_server_ome_api_key');
  161. if (!$sApiProtocol || !$sHost || !$iApiPort || !$sApiKey)
  162. return false;
  163. $sUrl = $sApiProtocol . "://{$sHost}:$iApiPort/v1{$sUri}";
  164. $s = bx_file_get_contents($sUrl, $aBody, $sMethod, ['Authorization: Basic ' . $sApiKey]);
  165. bx_log('bx_stream_ome_api', $sUrl . "\n" . $s, BX_LOG_DEBUG);
  166. if (!$s)
  167. return false;
  168. $a = @json_decode($s, true);
  169. if (null === $a)
  170. return false;
  171. if (!isset($a['response']))
  172. return false;
  173. return $a['response'];
  174. }
  175. }
  176. /** @} */