Skip to content

Commit 449b615

Browse files
committed
feat: enable player-source config inheritance
1 parent 12eb8dd commit 449b615

5 files changed

Lines changed: 162 additions & 103 deletions

File tree

src/plugins/cloudinary/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class CloudinaryContext {
122122
options.sourceTransformation = options.sourceTransformation || this.sourceTransformation();
123123
options.sourceTypes = options.sourceTypes || this.sourceTypes();
124124
options.poster = options.poster || posterOptionsForCurrent();
125-
options.queryParams = Object.assign(options.queryParams || {}, options.usageReport ? { _s: `vp-${VERSION}` } : {});
125+
options.queryParams = Object.assign(options.queryParams || {}, options.allowUsageReport ? { _s: `vp-${VERSION}` } : {});
126126

127127
if (options.sourceTypes.indexOf('audio') > -1) {
128128
builtSrc = new AudioSource(publicId, options);

src/plugins/cloudinary/models/video-source/video-source.js

Lines changed: 103 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { sliceAndUnsetProperties } from 'utils/slicing';
21
import { objectToQuerystring } from 'utils/querystring';
32
import castArray from 'lodash/castArray';
43
import { SOURCE_TYPE } from 'utils/consts';
4+
import { SOURCE_PARAMS } from 'video-player.const';
55
import {
66
CONTAINER_MIME_TYPES,
77
ADAPTIVE_SOURCETYPES,
@@ -37,58 +37,21 @@ class VideoSource extends BaseSource {
3737
options.poster = Object.assign({ publicId }, DEFAULT_POSTER_PARAMS);
3838
}
3939

40-
const {
41-
poster,
42-
sourceTypes,
43-
sourceTransformation,
44-
info,
45-
recommendations,
46-
textTracks,
47-
withCredentials,
48-
interactionAreas,
49-
chapters,
50-
visualSearch
51-
} = sliceAndUnsetProperties(
52-
options,
53-
'poster',
54-
'sourceTypes',
55-
'sourceTransformation',
56-
'info',
57-
'recommendations',
58-
'textTracks',
59-
'withCredentials',
60-
'interactionAreas',
61-
'chapters',
62-
'visualSearch'
63-
);
64-
6540
super(publicId, options);
6641

67-
this._sourceTypes = null;
68-
this._recommendations = null;
69-
this._textTracks = null;
70-
this._poster = null;
71-
this._info = null;
72-
this._sourceTransformation = null;
73-
this._interactionAreas = null;
74-
this._chapters = null;
75-
this._visualSearch = null;
7642
this._type = SOURCE_TYPE.VIDEO;
7743
this.isRawUrl = _isRawUrl;
7844
this.isLiveStream = options.type === 'live';
79-
this._rawTransformation = options.raw_transformation;
80-
this.withCredentials = !!withCredentials;
45+
this.withCredentials = !!options.withCredentials;
8146
this.getInitOptions = () => initOptions;
8247

83-
this.poster(poster, { type: options.type });
84-
this.sourceTypes(sourceTypes);
85-
this.sourceTransformation(sourceTransformation);
86-
this.info(info);
87-
this.interactionAreas(interactionAreas);
88-
this.chapters(chapters);
89-
this.visualSearch(visualSearch);
90-
this.recommendations(recommendations);
91-
this.textTracks(textTracks);
48+
// Set extracted parameters using their respective methods
49+
SOURCE_PARAMS.forEach(param => {
50+
if (options[param] !== undefined && this[param]) {
51+
this[param](options[param]);
52+
}
53+
});
54+
9255
this.objectId = generateId();
9356
}
9457

@@ -171,7 +134,9 @@ class VideoSource extends BaseSource {
171134
return this;
172135
}
173136

174-
poster(publicId, options = {}) {
137+
poster(publicId) {
138+
let options = { type: this.getInitOptions().type };
139+
175140
if (!publicId) {
176141
return this._poster;
177142
}
@@ -228,7 +193,7 @@ class VideoSource extends BaseSource {
228193
const [type, codecTrans] = formatToMimeTypeAndTransformation(sourceType);
229194

230195
// If user's transformation include video_codec then don't add another video codec to transformation
231-
if (codecTrans && !(hasCodec(opts.transformation) || hasCodec(this._rawTransformation))) {
196+
if (codecTrans && !(hasCodec(opts.transformation) || hasCodec(this.raw_transformation()))) {
232197
opts.transformation = mergeTransformations(opts.transformation, codecTrans);
233198
}
234199

@@ -284,6 +249,96 @@ class VideoSource extends BaseSource {
284249
getInteractionAreas() {
285250
return this._interactionAreas;
286251
}
252+
253+
// Methods for additional SOURCE_PARAMS
254+
adaptiveStreaming(value) {
255+
if (value === undefined) {
256+
return this._adaptiveStreaming;
257+
}
258+
this._adaptiveStreaming = value;
259+
return this;
260+
}
261+
262+
allowUsageReport(value) {
263+
if (value === undefined) {
264+
return this._allowUsageReport;
265+
}
266+
this._allowUsageReport = value;
267+
return this;
268+
}
269+
270+
autoShowRecommendations(value) {
271+
if (value === undefined) {
272+
return this._autoShowRecommendations;
273+
}
274+
this._autoShowRecommendations = value;
275+
return this;
276+
}
277+
278+
queryParams(params) {
279+
if (params === undefined) {
280+
return this._queryParams;
281+
}
282+
this._queryParams = params;
283+
return this;
284+
}
285+
286+
raw_transformation(value) {
287+
if (value === undefined) {
288+
return this._raw_transformation;
289+
}
290+
this._raw_transformation = value;
291+
return this;
292+
}
293+
294+
shoppable(value) {
295+
if (value === undefined) {
296+
return this._shoppable;
297+
}
298+
this._shoppable = value;
299+
return this;
300+
}
301+
302+
// Additional setter methods for remaining SOURCE_PARAMS
303+
source(value) {
304+
if (value === undefined) {
305+
return this._source;
306+
}
307+
this._source = value;
308+
return this;
309+
}
310+
311+
transformation(value) {
312+
if (value === undefined) {
313+
return this._transformation;
314+
}
315+
this._transformation = value;
316+
return this;
317+
}
318+
319+
cloudinaryConfig(value) {
320+
if (value === undefined) {
321+
return this._cloudinaryConfig;
322+
}
323+
this._cloudinaryConfig = value;
324+
return this;
325+
}
326+
327+
type(value) {
328+
if (value === undefined) {
329+
return this._typeValue;
330+
}
331+
this._typeValue = value;
332+
return this;
333+
}
334+
335+
posterOptions(value) {
336+
if (value === undefined) {
337+
return this._posterOptions;
338+
}
339+
this._posterOptions = value;
340+
return this;
341+
}
287342
}
288343

289344
export default VideoSource;

src/video-player.const.js

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,70 @@
1-
export const CLOUDINARY_PARAMS = [
1+
// Parameters that can be passed to source configuration (inherited by source method)
2+
export const SOURCE_PARAMS = [
3+
'adaptiveStreaming',
4+
'allowUsageReport',
5+
'autoShowRecommendations',
6+
'chapters',
27
'cloudinaryConfig',
3-
'transformation',
4-
'sourceTypes',
5-
'sourceTransformation',
8+
'info',
9+
'interactionAreas',
10+
'poster',
611
'posterOptions',
7-
'autoShowRecommendations',
8-
'fontFace',
9-
'secure'
12+
'publicId',
13+
'queryParams',
14+
'raw_transformation',
15+
'recommendations',
16+
'shoppable',
17+
'source',
18+
'sourceTransformation',
19+
'sourceTypes',
20+
'textTracks',
21+
'transformation',
22+
'type',
23+
'visualSearch',
24+
'withCredentials'
1025
];
1126

12-
export const PLAYER_PARAMS = CLOUDINARY_PARAMS.concat([
27+
// All parameters that can be passed to player constructor
28+
export const PLAYER_PARAMS = SOURCE_PARAMS.concat([
1329
'_internalAnalyticsMetadata',
14-
'debug',
15-
'publicId',
16-
'source',
17-
'autoplayMode',
18-
'playedEventPercents',
19-
'playedEventTimes',
30+
'ads',
31+
'aiHighlightsGraph',
2032
'analytics',
33+
'autoplayMode',
34+
'chaptersButton',
2135
'cloudinaryAnalytics',
22-
'allowUsageReport',
23-
'fluid',
24-
'ima',
25-
'playlistWidget',
26-
'hideContextMenu',
2736
'colors',
37+
'debug',
38+
'fetchErrorUsingGet',
2839
'floatingWhenNotVisible',
29-
'ads',
30-
'showJumpControls',
31-
'chaptersButton',
40+
'fluid',
41+
'fontFace',
42+
'hideContextMenu',
43+
'ima',
3244
'pictureInPictureToggle',
33-
'textTracks',
45+
'playedEventPercents',
46+
'playedEventTimes',
47+
'playlistWidget',
3448
'qualitySelector',
35-
'fetchErrorUsingGet',
36-
'withCredentials',
3749
'seekThumbnails',
38-
'aiHighlightsGraph',
39-
'chapters',
40-
'queryParams',
41-
'type',
42-
'visualSearch',
43-
'adaptiveStreaming'
50+
'showJumpControls'
4451
]);
4552

53+
// We support both camelCase and snake_case for cloudinary SDK params
4654
export const CLOUDINARY_CONFIG_PARAM = [
47-
'cloud_name',
48-
'secure',
49-
'private_cdn',
50-
'secure_distribution',
55+
'api_secret',
56+
'auth_token',
5157
'cdn_subdomain',
52-
'secure_cdn_subdomain',
58+
'cloud_name',
5359
'cname',
60+
'private_cdn',
61+
'secure',
62+
'secure_cdn_subdomain',
63+
'secure_distribution',
5464
'shorten',
5565
'sign_url',
56-
'api_secret',
5766
'url_suffix',
58-
'use_root_path',
59-
'auth_token'
67+
'use_root_path'
6068
];
6169

6270
export const FLUID_CLASS_NAME = 'cld-fluid';

src/video-player.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { PLAYER_EVENT, SOURCE_TYPE } from './utils/consts';
2424
import { getAnalyticsFromPlayerOptions } from './utils/get-analytics-player-options';
2525
import { extendCloudinaryConfig, normalizeOptions, isRawUrl, ERROR_CODE } from './plugins/cloudinary/common';
2626
import { isVideoInReadyState, checkIfVideoIsAvailable } from './utils/video-retry';
27+
import { SOURCE_PARAMS } from './video-player.const';
2728

2829
const INTERNAL_ANALYTICS_URL = 'https://analytics-api-s.cloudinary.com';
2930

@@ -487,10 +488,13 @@ class VideoPlayer extends Utils.mixin(Eventable) {
487488
this._setExtendedEvents();
488489

489490
// Load first video (mainly to support video tag 'source' and 'public-id' attributes)
490-
const source = this.playerOptions.source || this.playerOptions.publicId;
491+
// Source parameters are set to playerOptions.cloudinary
492+
const source = this.playerOptions.cloudinary.source || this.playerOptions.cloudinary.publicId;
491493

492494
if (source) {
493-
this.source(source, this.playerOptions);
495+
const sourceOptions = Object.assign({}, this.playerOptions.cloudinary);
496+
497+
this.source(source, sourceOptions);
494498
}
495499
}
496500

@@ -564,22 +568,14 @@ class VideoPlayer extends Utils.mixin(Eventable) {
564568
return this.videojs.cloudinary.source(publicId, options);
565569
}
566570

567-
if (this.playerOptions.adaptiveStreaming) {
568-
options.adaptiveStreaming = this.playerOptions.adaptiveStreaming;
569-
}
571+
// Inherit source parameters from player options (source options take precedence)
572+
const inherited = pick(this.playerOptions, SOURCE_PARAMS);
573+
options = { ...inherited, ...options };
570574

571575
if (options.shoppable && this.videojs.shoppable) {
572576
this.videojs.shoppable(this.videojs, options);
573577
}
574578

575-
if (this.playerOptions.allowUsageReport) {
576-
options.usageReport = true;
577-
}
578-
579-
if (this.playerOptions.withCredentials) {
580-
options.withCredentials = true;
581-
}
582-
583579
this._resetReloadVideo();
584580
this._resetReTryVideoState();
585581

src/video-player.utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import videojs from 'video.js';
22
import Utils from './utils';
33
import defaults from './config/defaults';
44
import {
5-
CLOUDINARY_PARAMS,
65
PLAYER_PARAMS,
6+
SOURCE_PARAMS,
77
FLUID_CLASS_NAME,
88
AUTO_PLAY_MODE
99
} from './video-player.const';
@@ -90,7 +90,7 @@ export const extractOptions = (elem, options) => {
9090
const playerOptions = Utils.sliceAndUnsetProperties(options, ...PLAYER_PARAMS);
9191

9292
// Cloudinary plugin specific options
93-
playerOptions.cloudinary = Utils.sliceAndUnsetProperties(playerOptions, ...CLOUDINARY_PARAMS);
93+
playerOptions.cloudinary = Utils.sliceAndUnsetProperties(playerOptions, ...SOURCE_PARAMS);
9494

9595
// Allow explicitly passing options to videojs using the `videojs` namespace, in order
9696
// to avoid param name conflicts:

0 commit comments

Comments
 (0)