BxStrmEngineOvenMediaEngine.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $iFileId = $oStorage->storeFileFromUrl($sFileUrl, true, $aContentInfo[$CNF['FIELD_AUTHOR']], $aContentInfo['id']);
  78. if ($iFileId) {
  79. $bDeleteRecording = true;
  80. }
  81. else {
  82. bx_log('bx_stream', "Store recording from URL(" . getParam('bx_stream_recordings_url') . $a['filePath'] . ") failed for content id($iContentId): " . $oStorage->getErrorString());
  83. }
  84. }
  85. else {
  86. bx_log('bx_stream', "Store recording failed because 'bx_stream_recordings' storage doesn't exist");
  87. $bDeleteRecording = true;
  88. }
  89. if ($bDeleteRecording || ($iTries + 1) > 10) {
  90. $oModule->_oDb->deleteRecording($iRecordingId);
  91. }
  92. else {
  93. $oModule->_oDb->updateRecording($iRecordingId, ['tries' => $iTries + 1]);
  94. }
  95. }
  96. public function getRtmpSettings($sStreamKey)
  97. {
  98. $sHost = getParam('bx_stream_server_host');
  99. $sApp = getParam('bx_stream_app');
  100. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  101. if (!$sHost || !$sApp)
  102. return false;
  103. $sBaseUrl = "rtmp://$sHost:1935/$sApp";
  104. if (!$sPolicySecret)
  105. return ['server' => $sBaseUrl, 'key' => $sStreamKey];
  106. return ['server' => $sBaseUrl, 'key' => str_replace($sBaseUrl . '/', '', $this->_signUrl($sBaseUrl . '/' . $sStreamKey, $sPolicySecret))];
  107. }
  108. public function getRtmpIngestUrl($sStreamKey)
  109. {
  110. if (!($a = $this->getRtmpSettings($sStreamKey)))
  111. return false;
  112. list ($sBaseUrl, $sStreamKey) = $a;
  113. return $a['server'] . '/' . $a['key'];
  114. $sHost = getParam('bx_stream_server_host');
  115. $sApp = getParam('bx_stream_app');
  116. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  117. if (!$sHost || !$sApp)
  118. return false;
  119. $sBaseUrl = "rtmp://$sHost:1935/$sApp/$sStreamKey";
  120. if (!$sPolicySecret)
  121. return $sBaseUrl;
  122. return $this->_signUrl($sBaseUrl, $sPolicySecret);
  123. }
  124. public function getWebrtcIngestUrl($sStreamKey)
  125. {
  126. $sHost = getParam('bx_stream_server_host');
  127. $sApp = getParam('bx_stream_app');
  128. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  129. $sBaseUrl = "wss://$sHost:3334/$sApp/$sStreamKey?direction=send";
  130. if (!$sPolicySecret)
  131. return $sBaseUrl;
  132. return $this->_signUrl($sBaseUrl, $sPolicySecret);
  133. }
  134. protected function _signUrl($sBaseUrl, $sPolicySecret)
  135. {
  136. $sSignatureQueryKeyName = 'signature';
  137. $sPolicyQueryKeyName = 'policy';
  138. $sPolicy = json_encode(['url_expire' => 2556104400000]);//(time() + 50*365*24*60*60)*1000]);
  139. // 1. Perform base64url() for POLICY (RFC 4648 5.)
  140. $sPolicyBase64 = $this->_base64URLEncode($sPolicy);
  141. // 2. Generates an URL such as "ws://ome_host:3333/app/stream?policy=${POLICY_BASE64}"
  142. $sPolicyUrl = bx_append_url_params($sBaseUrl, [$sPolicyQueryKeyName => $sPolicyBase64]);
  143. // 3. Perform sha1(base64url()) for SIGNATURE (RFC 4648 5.)
  144. $sSha1 = hash_hmac('sha1', $sPolicyUrl, $sPolicySecret, true);
  145. $sSignature = $this->_base64URLEncode($sSha1);
  146. // 4. Create the whole URL
  147. $sWholeUrl = bx_append_url_params($sPolicyUrl, [$sSignatureQueryKeyName => $sSignature]);
  148. return $sWholeUrl;
  149. }
  150. protected function _base64URLEncode($s)
  151. {
  152. return trim(strtr(base64_encode($s), '+/', '-_'), '=');
  153. }
  154. protected function _apiRequest($sUri, $sMethod = 'get', $aBody = [])
  155. {
  156. $sApiProtocol = getParam('bx_stream_server_ome_api_protocol');
  157. $sHost = getParam('bx_stream_server_host');
  158. $iApiPort = (int)getParam('bx_stream_server_ome_api_port');
  159. $sApiKey = getParam('bx_stream_server_ome_api_key');
  160. if (!$sApiProtocol || !$sHost || !$iApiPort || !$sApiKey)
  161. return false;
  162. $sUrl = "http://{$sHost}:$iApiPort/v1{$sUri}";
  163. $s = bx_file_get_contents($sUrl, $aBody, $sMethod, ['Authorization: Basic ' . $sApiKey]);
  164. bx_log('bx_stream_ome_api', $sUrl . "\n" . $s);
  165. if (!$s)
  166. return false;
  167. $a = @json_decode($s, true);
  168. if (null === $a)
  169. return false;
  170. if (!isset($a['response']))
  171. return false;
  172. return $a['response'];
  173. }
  174. }
  175. /** @} */