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