/
videojs.comscore.coffee
281 lines (230 loc) · 7.49 KB
/
videojs.comscore.coffee
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
((vjs) ->
#------------------------------------------------------------ private
###
This keymap defines how to map the internal keys (left-hand side) to the
user's key name (right-hand side).
###
isArray = (obj) -> toString.call(obj) is "[object Array]"
isNumber = (value) -> !isNaN parseInt(value, 10)
# supplied during the `$ grunt-init videojs` process
extend = (obj) -> #, arg1, arg2, ...
arg = undefined
i = undefined
k = undefined
i = 1
while i < arguments.length
arg = arguments[i]
for k of arg
obj[k] = arg[k] if arg.hasOwnProperty(k)
i++
obj
keymap =
ad: 'ad'
premium: 'premium'
ugc: 'ugc'
live: 'live'
duration: 'duration'
index: 'index'
id: 'id'
name: 'name'
publisher: 'publisher'
show: 'show'
url: 'url'
classification: 'classificaiton' # only use this if you absolutely know what this is supposed to be
# page 13 (http://cl.ly/UGkP)
classificationTypes =
video:
shortform:
premium: 'vc11'
ugc: 'vc21'
longform:
premium: 'vc12'
ugc: 'vc22'
live:
premium: 'vc13'
ugc: 'vc23'
audio: 'ac00'
default: 'vc00'
ad:
preroll: 'va11'
midroll: 'va12'
postroll: 'va13'
live: 'va21'
audio: 'aa00'
default: 'va00'
#------------------------------------------------------------
#------------------------------------------------------------ Clip
class Clip
premium = false
ugc = false
live = false
ns_st_ad: false
ns_st_cl: null
ns_st_cn: null
ns_st_ci: null
ns_st_ep: null
ns_st_pu: null
ns_st_pr: null
ns_st_cu: null
ns_st_ct: null
constructor: (index, metadata) ->
@ad metadata[keymap.ad]
@premium metadata[keymap.premium]
@ugc metadata[keymap.ugc]
@duration metadata[keymap.duration]
@index index
@id metadata[keymap.id]
@name metadata[keymap.name]
@publisher metadata[keymap.publisher]
@show metadata[keymap.show]
@url metadata[keymap.url]
@classification metadata[keymap.classification]
# getters/setters (could be more DRY, but I'm leaving it this way for clarity)
# -------------------------
ad: (args...) ->
flag = args?[0]
if flag?.toString() then @ns_st_ad = flag
return @ns_st_ad
ugc: (args...) ->
flag = args?[0]
if flag?.toString() then ugc = flag
return ugc
premium: (args...) ->
flag = args?[0]
if flag?.toString() then premium = flag
return premium
live: (args...) ->
flag = args?[0]
if flag?.toString() then live = flag
return live
duration: (length, inSeconds) ->
if inSeconds then length = length * 1000
if length then @ns_st_cl = Math.round(length)
return @ns_st_cl || 0
index: (index) ->
if index then @ns_st_cn = index
return @ns_st_cn
id: (id) ->
if id then @ns_st_ci = id
return @ns_st_ci
name: (name) ->
if name then @ns_st_ep = name
return @ns_st_ep
publisher: (name) ->
if name then @ns_st_pu = name
return @ns_st_pu
show: (name) ->
if name then @ns_st_pr = name
return @ns_st_pr
url: (url) ->
if url then @ns_st_cu = url
return @ns_st_cu
classification: (classification) ->
if classification then @ns_st_ct = classification
# long form is defined as 10 minutes or greater
isLongForm = => @duration()/1000 >= 600
if @ad() # if it's an ad
# for now, if it's an ad we're just gonna call it a preroll all the time
@ns_st_ct = classificationTypes.ad.preroll
else # if it's content
if @live() # live
if @premium()
@ns_st_ct = classificationTypes.video.live.premium
if @ugc()
@ns_st_ct = classificationTypes.video.live.ugc
else if isLongForm() # video
if @premium()
@ns_st_ct = classificationTypes.video.longform.premium
if @ugc()
@ns_st_ct = classificationTypes.video.longform.ugc
else
if @premium()
@ns_st_ct = classificationTypes.video.shortform.premium
if @ugc()
@ns_st_ct = classificationTypes.video.shortform.ugc
unless @ns_st_ct then @ns_st_ct = classificationTypes.video.default
return @ns_st_ct
#------------------------------------------------------------
#------------------------------------------------------------ plugin
comscore = (id, playlist, keymapOverride) ->
throw new Error 'The first argument should be your comScore ID' unless isNumber(id)
throw new Error 'The second argument should be an array (can be empty)' unless isArray(playlist)
events =
BUFFER: ns_.StreamSense.PlayerEvents.BUFFER
END: ns_.StreamSense.PlayerEvents.END
PLAY: ns_.StreamSense.PlayerEvents.PLAY
PAUSE: ns_.StreamSense.PlayerEvents.PAUSE
player = @ # save a reference to the player instance
tracker = new ns_.StreamSense {}, "http://b.scorecardresearch.com/p?c1=2&c2=#{id}"
currentClip = null
clips = []
keymap = extend {}, keymap, keymapOverride if keymapOverride
currentPosition = 0
stalled = false
stallCounter = 0
initialize = ->
clips = makeClips(playlist)
tracker.setPlaylist clips if clips.length > 0
makeClips = (playlist) ->
return playlist.map (metadata, index) -> new Clip(index, metadata)
getClipByUrl = (url) ->
for clip in clips
return clip if url is clip.url()
getCurrentClip = -> getClipByUrl(player.currentSrc())
getCurrentTime = -> Math.round(player.currentTime() * 1000)
getClips = (index) ->
if index >= 0 then return clips[index]
return clips
updateLoadedClip = ->
currentClip = getCurrentClip()
currentClip.url player.currentSrc()
currentClip.duration player.duration(), true
tracker.setClip currentClip
checkIfStalled = ->
definitivelyStalled = !stalled and stallCounter++ > 3
if definitivelyStalled and currentPosition is getCurrentTime()
stalled = true
stallCounter = 0
buffer(currentPosition)
return true
else if stalled and currentPosition isnt getCurrentTime()
play(currentPosition)
stalled = false
stallCounter = 0
return false
return false
play = (position) ->
tracker.notify events.PLAY, {}, position
pause = (position) ->
tracker.notify events.PAUSE, {}, position
end = (duration) ->
tracker.notify events.END, {}, duration
buffer = (position) ->
tracker.notify events.BUFFER, {}, position
progress = ->
checkIfStalled()
currentPosition = getCurrentTime()
# listeners
# -------------------------
player.on 'durationchange', -> updateLoadedClip()
player.on 'play', -> play currentPosition
player.on 'ended', -> end currentClip.duration()
player.on 'pause', -> pause currentPosition
player.on 'progress', progress
# exposing the public api
# -------------------------
player.comscore =
play: play
pause: pause
end: end
buffer: buffer
getClips: getClips
getCurrentClip: getCurrentClip
updateLoadedClip: updateLoadedClip
classificationTypes: classificationTypes
initialize()
return player.comscore
#------------------------------------------------------------ end plugin
# register the plugin with video.js
vjs.plugin "comscore", comscore
)(window.videojs)