forked from kaltura/mwEmbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kWidget.getSources.js
executable file
·174 lines (160 loc) · 5.21 KB
/
kWidget.getSources.js
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
/**
* Stand alone source grabber.
*/
if( ! window.kWidget ){
window.kWidget = {};
}
( function( kWidget ) {
// Add master exported function:
kWidget.getSources = function( settings ){
var sourceApi = new kWidget.api( { 'wid' : '_' + settings.partnerId } );
sourceApi.doRequest([
{
'contextDataParams' : {
'referrer' : document.URL,
'objectType' : 'KalturaEntryContextDataParams',
'flavorTags': 'all'
},
'service' : 'baseentry',
'entryId' : settings.entryId,
'action' : 'getContextData'
},
{
'service' : 'baseentry',
'action' : 'get',
'version' : '-1',
'entryId' : settings.entryId
}], function( result ){ // API result
var ks = sourceApi.ks;
var ipadAdaptiveFlavors = [];
var iphoneAdaptiveFlavors = [];
var deviceSources = [];
var protocol = location.protocol.substr(0, location.protocol.length-1);
// Set the service url based on protocol type
var serviceUrl;
if( protocol == 'https' ){
serviceUrl = 'https://www.kaltura.com';
} else {
serviceUrl = 'http://cdnbakmi.kaltura.com';
}
var baseUrl = serviceUrl + '/p/' + settings.partnerId +
'/sp/' + settings.partnerId + '00/playManifest';
for( var i in result[0]['flavorAssets'] ){
var asset = result[0]['flavorAssets'][i];
// Continue if clip is not ready (2)
if( asset.status != 2 ) {
continue;
}
// Setup a source object:
var source = {
'data-bitrate' : asset.bitrate * 8,
'data-width' : asset.width,
'data-height' : asset.height
};
var src = baseUrl + '/entryId/' + asset.entryId;
// Check if Apple http streaming is enabled and the tags include applembr ( single stream HLS )
if( asset.tags.indexOf('applembr') != -1 ) {
src += '/format/applehttp/protocol/'+ protocol + '/a.m3u8';
deviceSources.push({
'data-flavorid' : 'AppleMBR',
'type' : 'application/vnd.apple.mpegurl',
'src' : src
});
continue;
} else {
src += '/flavorId/' + asset.id + '/format/url/protocol/' + protocol;
}
// add source data if a web flavor:
if( asset.tags.toLowerCase().indexOf('web') != -1 ){
source['data-flavorid'] = asset.videoCodecId + ' ' + asset.height + 'P';
source['src'] = src + '/a.' + asset.fileExt;
source['type'] = 'video/h264';
}
// add the file extension:
if( asset.tags.toLowerCase().indexOf('ipad') != -1 ){
source['src'] = src + '/a.mp4';
source['data-flavorid'] = 'iPad';
source['type'] = 'video/h264';
}
// Check for iPhone src
if( asset.tags.toLowerCase().indexOf('iphone') != -1 ){
source['src'] = src + '/a.mp4';
source['data-flavorid'] = 'iPhone';
source['type'] = 'video/h264';
}
// Check for ogg source
if( asset.fileExt &&
(
asset.fileExt.toLowerCase() == 'ogg'
||
asset.fileExt.toLowerCase() == 'ogv'
||
( asset.containerFormat && asset.containerFormat.toLowerCase() == 'ogg' )
)
){
source['src'] = src + '/a.ogg';
source['data-flavorid'] = 'ogg';
source['type'] = 'video/ogg';
}
// Check for webm source
if( asset.fileExt == 'webm'
||
asset.tags.indexOf('webm') != -1
|| // Kaltura transcodes give: 'matroska'
( asset.containerFormat && asset.containerFormat.toLowerCase() == 'matroska' )
|| // some ingestion systems give "webm"
( asset.containerFormat && asset.containerFormat.toLowerCase() == 'webm' )
){
source['src'] = src + '/a.webm';
source['data-flavorid'] = 'webm';
source['type'] = 'video/webm';
}
// Check for 3gp source
if( asset.fileExt == '3gp' ){
source['src'] = src + '/a.3gp';
source['data-flavorid'] = '3gp'
source['type'] = 'video/3gp';
}
// Add the device sources
if( source['src'] ){
deviceSources.push( source );
}
// Check for adaptive compatible flavor:
if( asset.tags.toLowerCase().indexOf('ipadnew') != -1 ){
ipadAdaptiveFlavors.push( asset.id );
}
if( asset.tags.toLowerCase().indexOf('iphonenew') != -1 ){
iphoneAdaptiveFlavors.push( asset.id );
}
};
// Add the flavor list adaptive style urls ( multiple flavor HLS ):
// Create iPad flavor for Akamai HTTP
if( ipadAdaptiveFlavors.length != 0 ) {
deviceSources.push({
'data-flavorid' : 'iPadNew',
'type' : 'application/vnd.apple.mpegurl',
'src' : baseUrl + '/entryId/' + asset.entryId + '/flavorIds/' + ipadAdaptiveFlavors.join(',') + '/format/applehttp/protocol/' + protocol + '/a.m3u8'
});
}
// Create iPhone flavor for Akamai HTTP
if(iphoneAdaptiveFlavors.length != 0 ) {
deviceSources.push({
'data-flavorid' : 'iPhoneNew',
'type' : 'application/vnd.apple.mpegurl',
'src' : baseUrl + '/entryId/' + asset.entryId + '/flavorIds/' + iphoneAdaptiveFlavors.join(',') + '/format/applehttp/protocol/' + protocol + '/a.m3u8'
});
}
// callback with device sources, poster
if( settings.callback ){
settings.callback({
'poster': result[1]['thumbnailUrl'],
'duration': result[1]['duration'],
'name': result[1]['name'],
'entryId' : result[1]['id'],
'description': result[1]['description'],
'sources': deviceSources
});
}
});
};
} )( window.kWidget );