UNPKG

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