BxStrmEngineOvenMediaEngine.php 3.6 KB

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