BxStrmEngineOvenMediaEngine.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 isSreamFromBrowser()
  14. {
  15. return true;
  16. }
  17. public function getViewersNum($sStreamKey)
  18. {
  19. $a = $this->getStreamStats($sStreamKey);
  20. if (!$a)
  21. return false;
  22. return $a['totalConnections'];
  23. }
  24. public function getStreamStats($sStreamKey)
  25. {
  26. $sApiProtocol = getParam('bx_stream_server_ome_api_protocol');
  27. $sHost = getParam('bx_stream_server_host');
  28. $iApiPort = (int)getParam('bx_stream_server_ome_api_port');
  29. $sApp = getParam('bx_stream_app');
  30. $sApiKey = getParam('bx_stream_server_ome_api_key');
  31. if (!$sApiProtocol || !$sHost || !$iApiPort || !$sApp || !$sApiKey)
  32. return false;
  33. $sUrl = "http://{$sHost}:$iApiPort/v1/stats/current/vhosts/default/apps/{$sApp}/streams/{$sStreamKey}";
  34. $s = bx_file_get_contents($sUrl, [], 'get', ['Authorization: Basic ' . $sApiKey]);
  35. bx_log('bx_stream_ome_api', $sUrl . "\n" . $s);
  36. if (!$s)
  37. return false;
  38. $a = @json_decode($s, true);
  39. if (null === $a)
  40. return false;
  41. if (!isset($a['response']))
  42. return false;
  43. return $a['response'];
  44. }
  45. public function getRtmpSettings($sStreamKey)
  46. {
  47. $sHost = getParam('bx_stream_server_host');
  48. $sApp = getParam('bx_stream_app');
  49. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  50. if (!$sHost || !$sApp)
  51. return false;
  52. $sBaseUrl = "rtmp://$sHost:1935/$sApp";
  53. if (!$sPolicySecret)
  54. return ['server' => $sBaseUrl, 'key' => $sStreamKey];
  55. return ['server' => $sBaseUrl, 'key' => str_replace($sBaseUrl . '/', '', $this->_signUrl($sBaseUrl . '/' . $sStreamKey, $sPolicySecret))];
  56. }
  57. public function getRtmpIngestUrl($sStreamKey)
  58. {
  59. if (!($a = $this->getRtmpSettings($sStreamKey)))
  60. return false;
  61. list ($sBaseUrl, $sStreamKey) = $a;
  62. return $a['server'] . '/' . $a['key'];
  63. $sHost = getParam('bx_stream_server_host');
  64. $sApp = getParam('bx_stream_app');
  65. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  66. if (!$sHost || !$sApp)
  67. return false;
  68. $sBaseUrl = "rtmp://$sHost:1935/$sApp/$sStreamKey";
  69. if (!$sPolicySecret)
  70. return $sBaseUrl;
  71. return $this->_signUrl($sBaseUrl, $sPolicySecret);
  72. }
  73. public function getWebrtcIngestUrl($sStreamKey)
  74. {
  75. $sHost = getParam('bx_stream_server_host');
  76. $sApp = getParam('bx_stream_app');
  77. $sPolicySecret = getParam('bx_stream_server_ome_policy_secret');
  78. $sBaseUrl = "wss://$sHost:3334/$sApp/$sStreamKey?direction=send";
  79. if (!$sPolicySecret)
  80. return $sBaseUrl;
  81. return $this->_signUrl($sBaseUrl, $sPolicySecret);
  82. }
  83. protected function _signUrl($sBaseUrl, $sPolicySecret)
  84. {
  85. $sSignatureQueryKeyName = 'signature';
  86. $sPolicyQueryKeyName = 'policy';
  87. $sPolicy = json_encode(['url_expire' => 2556104400000]);//(time() + 50*365*24*60*60)*1000]);
  88. // 1. Perform base64url() for POLICY (RFC 4648 5.)
  89. $sPolicyBase64 = $this->_base64URLEncode($sPolicy);
  90. // 2. Generates an URL such as "ws://ome_host:3333/app/stream?policy=${POLICY_BASE64}"
  91. $sPolicyUrl = bx_append_url_params($sBaseUrl, [$sPolicyQueryKeyName => $sPolicyBase64]);
  92. // 3. Perform sha1(base64url()) for SIGNATURE (RFC 4648 5.)
  93. $sSha1 = hash_hmac('sha1', $sPolicyUrl, $sPolicySecret, true);
  94. $sSignature = $this->_base64URLEncode($sSha1);
  95. // 4. Create the whole URL
  96. $sWholeUrl = bx_append_url_params($sPolicyUrl, [$sSignatureQueryKeyName => $sSignature]);
  97. return $sWholeUrl;
  98. }
  99. protected function _base64URLEncode($s)
  100. {
  101. return trim(strtr(base64_encode($s), '+/', '-_'), '=');
  102. }
  103. }
  104. /** @} */