flash-rtmp.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. 'use strict';
  2. exports.__esModule = true;
  3. /**
  4. * @file flash-rtmp.js
  5. * @module flash-rtmp
  6. */
  7. /**
  8. * Add RTMP properties to the {@link Flash} Tech.
  9. *
  10. * @param {Flash} Flash
  11. * The flash tech class.
  12. *
  13. * @mixin FlashRtmpDecorator
  14. */
  15. function FlashRtmpDecorator(Flash) {
  16. Flash.streamingFormats = {
  17. 'rtmp/mp4': 'MP4',
  18. 'rtmp/flv': 'FLV'
  19. };
  20. /**
  21. * Join connection and stream with an ampersand.
  22. *
  23. * @param {string} connection
  24. * The connection string.
  25. *
  26. * @param {string} stream
  27. * The stream string.
  28. */
  29. Flash.streamFromParts = function (connection, stream) {
  30. return connection + '&' + stream;
  31. };
  32. /**
  33. * The flash parts object that contains connection and stream info.
  34. *
  35. * @typedef {Object} Flash~PartsObject
  36. *
  37. * @property {string} connection
  38. * The connection string of a source, defaults to an empty string.
  39. *
  40. * @property {string} stream
  41. * The stream string of the source, defaults to an empty string.
  42. */
  43. /**
  44. * Convert a source url into a stream and connection parts.
  45. *
  46. * @param {string} src
  47. * the source url
  48. *
  49. * @return {Flash~PartsObject}
  50. * The parts object that contains a connection and a stream
  51. */
  52. Flash.streamToParts = function (src) {
  53. var parts = {
  54. connection: '',
  55. stream: ''
  56. };
  57. if (!src) {
  58. return parts;
  59. }
  60. // Look for the normal URL separator we expect, '&'.
  61. // If found, we split the URL into two pieces around the
  62. // first '&'.
  63. var connEnd = src.search(/&(?!\w+=)/);
  64. var streamBegin = void 0;
  65. if (connEnd !== -1) {
  66. streamBegin = connEnd + 1;
  67. } else {
  68. // If there's not a '&', we use the last '/' as the delimiter.
  69. connEnd = streamBegin = src.lastIndexOf('/') + 1;
  70. if (connEnd === 0) {
  71. // really, there's not a '/'?
  72. connEnd = streamBegin = src.length;
  73. }
  74. }
  75. parts.connection = src.substring(0, connEnd);
  76. parts.stream = src.substring(streamBegin, src.length);
  77. return parts;
  78. };
  79. /**
  80. * Check if the source type is a streaming type.
  81. *
  82. * @param {string} srcType
  83. * The mime type to check.
  84. *
  85. * @return {boolean}
  86. * - True if the source type is a streaming type.
  87. * - False if the source type is not a streaming type.
  88. */
  89. Flash.isStreamingType = function (srcType) {
  90. return srcType in Flash.streamingFormats;
  91. };
  92. // RTMP has four variations, any string starting
  93. // with one of these protocols should be valid
  94. /**
  95. * Regular expression used to check if the source is an rtmp source.
  96. *
  97. * @property {RegExp} Flash.RTMP_RE
  98. */
  99. Flash.RTMP_RE = /^rtmp[set]?:\/\//i;
  100. /**
  101. * Check if the source itself is a streaming type.
  102. *
  103. * @param {string} src
  104. * The url to the source.
  105. *
  106. * @return {boolean}
  107. * - True if the source url indicates that the source is streaming.
  108. * - False if the shource url indicates that the source url is not streaming.
  109. */
  110. Flash.isStreamingSrc = function (src) {
  111. return Flash.RTMP_RE.test(src);
  112. };
  113. /**
  114. * A source handler for RTMP urls
  115. * @type {Object}
  116. */
  117. Flash.rtmpSourceHandler = {};
  118. /**
  119. * Check if Flash can play the given mime type.
  120. *
  121. * @param {string} type
  122. * The mime type to check
  123. *
  124. * @return {string}
  125. * 'maybe', or '' (empty string)
  126. */
  127. Flash.rtmpSourceHandler.canPlayType = function (type) {
  128. if (Flash.isStreamingType(type)) {
  129. return 'maybe';
  130. }
  131. return '';
  132. };
  133. /**
  134. * Check if Flash can handle the source natively
  135. *
  136. * @param {Object} source
  137. * The source object
  138. *
  139. * @param {Object} [options]
  140. * The options passed to the tech
  141. *
  142. * @return {string}
  143. * 'maybe', or '' (empty string)
  144. */
  145. Flash.rtmpSourceHandler.canHandleSource = function (source, options) {
  146. var can = Flash.rtmpSourceHandler.canPlayType(source.type);
  147. if (can) {
  148. return can;
  149. }
  150. if (Flash.isStreamingSrc(source.src)) {
  151. return 'maybe';
  152. }
  153. return '';
  154. };
  155. /**
  156. * Pass the source to the flash object.
  157. *
  158. * @param {Object} source
  159. * The source object
  160. *
  161. * @param {Flash} tech
  162. * The instance of the Flash tech
  163. *
  164. * @param {Object} [options]
  165. * The options to pass to the source
  166. */
  167. Flash.rtmpSourceHandler.handleSource = function (source, tech, options) {
  168. var srcParts = Flash.streamToParts(source.src);
  169. tech.setRtmpConnection(srcParts.connection);
  170. tech.setRtmpStream(srcParts.stream);
  171. };
  172. // Register the native source handler
  173. Flash.registerSourceHandler(Flash.rtmpSourceHandler);
  174. return Flash;
  175. }
  176. exports['default'] = FlashRtmpDecorator;