-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathvideo-source.js
More file actions
263 lines (212 loc) · 8.02 KB
/
video-source.js
File metadata and controls
263 lines (212 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { appendQueryParams } from '~/utils/querystring';
import castArray from 'lodash/castArray';
import { SOURCE_TYPE } from '~/utils/consts';
import { SOURCE_PARAMS } from '../../../../video-player.const';
import {
CONTAINER_MIME_TYPES,
ADAPTIVE_SOURCETYPES,
DEFAULT_POSTER_PARAMS,
DEFAULT_VIDEO_PARAMS,
VIDEO_SUFFIX_REMOVAL_PATTERN
} from './video-source.const';
import {
formatToMimeTypeAndTransformation,
hasCodec,
normalizeFormat
} from './video-source.utils';
import { normalizeOptions, isSrcEqual, isRawUrl, mergeTransformations, getCloudinaryUrlPrefix } from '../../common';
import { utf8ToBase64 } from '~/utils/utf8Base64';
import Transformation from '@cloudinary/url-gen/backwards/transformation';
import BaseSource from '../base-source';
import ImageSource from '../image-source';
let objectId = 0;
const generateId = () => objectId++;
class VideoSource extends BaseSource {
constructor(_publicId, initOptions = {}) {
const _isRawUrl = isRawUrl(_publicId);
let { publicId, options } = normalizeOptions(_publicId, initOptions);
if (!_isRawUrl) {
publicId = publicId.replace(VIDEO_SUFFIX_REMOVAL_PATTERN, '');
}
options = Object.assign({}, DEFAULT_VIDEO_PARAMS, options);
if (options.resourceType) {
options.resource_type = options.resourceType;
}
if (options.poster === undefined) {
options.poster = Object.assign({ publicId }, DEFAULT_POSTER_PARAMS);
}
super(publicId, options);
this._type = SOURCE_TYPE.VIDEO;
this.isRawUrl = _isRawUrl;
this.isLiveStream = options.type === 'live';
this.withCredentials = !!options.withCredentials;
this.getInitOptions = () => initOptions;
// Get properties that need simple getter/setter methods (exclude special cases)
const EXCLUDED_PROPERTIES = [
'poster', // Has complex logic
'withCredentials', // Direct property
'publicId', // BaseSource method
'cloudinaryConfig', // BaseSource method
'transformation', // BaseSource method
'queryParams', // BaseSource method
'type', // BaseSource handles getType()
'info' // Custom override method
];
const SIMPLE_PROPERTIES = SOURCE_PARAMS.filter(param => !EXCLUDED_PROPERTIES.includes(param));
// Create simple getter/setter methods
this._createGetterSetters(SIMPLE_PROPERTIES);
// Set initial values from options
SIMPLE_PROPERTIES.forEach(prop => {
if (options[prop] !== undefined) {
this[prop](options[prop]);
}
});
// Initialize poster
this.poster(options.poster);
this.objectId = generateId();
}
// Helper method to create simple getter/setter methods
_createGetterSetters(properties) {
properties.forEach(prop => {
const privateKey = `_${prop}`;
this[prop] = function (value) {
if (value === undefined) {
// Provide sensible defaults for specific properties
if (prop === 'sourceTypes' && this[privateKey] === undefined) {
return ['auto'];
}
if (prop === 'sourceTransformation' && this[privateKey] === undefined) {
return {};
}
return this[privateKey];
}
this[privateKey] = value;
return this;
};
});
}
poster(publicId) {
let options = { type: this.getInitOptions().type };
if (!publicId) {
return this._poster;
}
if (publicId instanceof ImageSource) {
this._poster = publicId;
return this;
}
({ publicId, options } = normalizeOptions(publicId, options, { tolerateMissingId: true }));
if (!publicId && this.isRawUrl) {
return null;
}
if (this.isLiveStream) {
return null;
}
if (!publicId) {
publicId = this.publicId();
options = Object.assign({}, options, DEFAULT_POSTER_PARAMS);
}
options.cloudinaryConfig = options.cloudinaryConfig || this.cloudinaryConfig();
options.resource_type = this.resourceType() || options.resource_type;
if (!isRawUrl(publicId)) {
options.queryParams = this.queryParams();
}
if (publicId === true) {
const urlPrefix = getCloudinaryUrlPrefix(options.cloudinaryConfig);
const deliveryType = this.getInitOptions().type || 'upload';
const base64PublicId = utf8ToBase64(this.publicId());
let appletUrl = `${urlPrefix}/_applet_/video_service/elements/${deliveryType}/${base64PublicId}/poster`;
const transformation = this.getInitOptions().posterOptions?.transformation;
if (transformation) {
const transformationString = new Transformation(transformation).toString();
appletUrl += `?tx=${transformationString}`;
}
this._poster = new ImageSource(appletUrl, {
cloudinaryConfig: options.cloudinaryConfig,
queryParams: options.queryParams
});
return this;
}
this._poster = new ImageSource(publicId, options);
return this;
}
contains(source) {
const sources = this.generateSources();
return sources.some(_source => isSrcEqual(_source, source));
}
generateSources() {
if (this.isRawUrl) {
const type = this.sourceTypes()[0] === 'auto' ? null : this.sourceTypes()[0];
return [this.generateRawSource(this.publicId(), type)];
}
const srcs = this.sourceTypes().map(sourceType => {
const srcTransformation = this.sourceTransformation()[sourceType] || this.transformation();
const format = normalizeFormat(sourceType);
const isAdaptive = ADAPTIVE_SOURCETYPES.includes(format);
const opts = {};
if (srcTransformation) {
opts.transformation = castArray(srcTransformation);
}
Object.assign(opts, { format });
const [type, codecTrans] = formatToMimeTypeAndTransformation(sourceType);
// If user's transformation include video_codec then don't add another video codec to transformation
if (codecTrans && !(hasCodec(opts.transformation) || hasCodec(this.rawTransformation()))) {
opts.transformation = mergeTransformations(opts.transformation, codecTrans);
}
if (opts.format === 'auto') {
delete opts.format;
opts.transformation = mergeTransformations(opts.transformation, {
fetch_format: 'auto:video'
});
}
if (isAdaptive) {
// Search for streaming_profile anywhere in the transformation
const hasStreamingProfile = JSON.stringify(opts.transformation || {}).includes('"streaming_profile":');
if (!hasStreamingProfile && !this.isLiveStream) {
opts.transformation = mergeTransformations(opts.transformation, {
streaming_profile: 'auto'
});
}
}
// dr (dynamic range) is not yet exposed by @cloudinary/url-gen, so we use raw_transformation
if (this.hdr() === true && window.matchMedia && window.matchMedia('(dynamic-range: high)').matches) {
opts.transformation = mergeTransformations(opts.transformation, {
fetch_format: 'mp4',
video_codec: 'h265',
raw_transformation: 'dr_high'
});
}
const src = appendQueryParams(this.config().url(this.publicId(), opts), this.queryParams());
return {
type,
src,
cldSrc: this,
isAdaptive: isAdaptive,
withCredentials: this.withCredentials
};
});
return srcs;
}
generateRawSource(url, type) {
type = type || url.split('.').pop();
const isAdaptive = ADAPTIVE_SOURCETYPES.includes(type);
if (CONTAINER_MIME_TYPES[type]) {
type = CONTAINER_MIME_TYPES[type];
} else {
type = type ? `video/${type}` : null;
}
return { type, src: url, cldSrc: this, isAdaptive, withCredentials: this.withCredentials };
}
info(value) {
if (value !== undefined) {
this._info = value;
return this;
}
const info = this._info || this.getInitOptions().info;
return {
title: this.title() || info?.title || '',
subtitle: this.description() || info?.subtitle || '',
description: this.description() || info?.subtitle || '',
};
}
}
export default VideoSource;