forked from wonga00/astronaut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoStream.js
107 lines (84 loc) · 2.15 KB
/
videoStream.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
/*
streaming video ids
ex. videoStream.start(callback);
receives a video, timestamp at regular intervals
*/
var _ = require('lodash')
, youtube = require('./youtube')
, videoTable = require('./videotable');
var VIDEO_INTERVAL = 8000;
var sendVideoTimer = 0;
var cursor = -1;
var currentVid = {};
var positions = []; // array of positions of the video list to cycle through
var videos = [];
var videoCallback;
// ad config
// these are the interstitual videos
// var ads = ["Ip2ZGND1I9Q"];
var adIndex = 0;
var ads = [];
var lastAdTime = new Date();
var AD_INTERVAL = 60000 * 3; //time in between ads in milliseconds
function shouldSendAd() {
var now = new Date();
return (now - lastAdTime) > AD_INTERVAL;
}
function getNextAd() {
var vid = ads[adIndex];
adIndex = (adIndex + 1) % ads.length;
lastAdTime = new Date();
// we have to match the same format as a normal video
return {
id: vid,
viewCount: 1000,
uploaded: '2015-01-01T15:29:36Z'
}
}
function sendVideo() {
// sends the video at the cursor and increments the cursor
videoTable.readVideos(function(_videos) {
videos = _videos;
if (!videos) {
console.log('no videos');
return;
}
// first ensure our position list matches the video list
cursor += 1;
if ((positions.length != videos.length) || (cursor >= videos.length)) {
positions = _.shuffle(_.range(videos.length));
cursor = 0;
}
var position = positions[cursor];
console.log(
'num videos:', videos.length,
'cursor:', cursor,
'position:', position);
var data = {
video: videos[position],
time: (new Date())/1000,
offset: 0
};
currentVid = data;
if (videoCallback) {
videoCallback(currentVid);
}
});
}
function start(sendVideoCallback) {
videoCallback = sendVideoCallback;
cursor = 0;
clearInterval(sendVideoTimer);
sendVideo();
sendVideoTimer = setInterval(sendVideo, VIDEO_INTERVAL);
}
exports.start = start;
exports.currentVid = function() {
return currentVid;
};
exports.numVideos = function() {
return videos.length;
};
exports.videos = function() {
return videos;
}