-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
203 lines (172 loc) · 5.41 KB
/
app.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
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
/**
* iPlay
* Copyright 2013 zfkun.com All rights reserved.
*
* @file main
* @author zfkun([email protected])
*/
var airplay = require( 'airplay2' );
var app = {
// browser: null,
// hls: null,
// device: {}
};
app.log = function () {
console.log.apply( console, arguments );
var el = document.getElementById('log');
el.innerHTML += '<p>' + [].slice.call(arguments).join(' ') + '</p>';
};
app.init = function () {
// browser
app.initBrowser();
// droper
app.initDroper();
// controller
app.initController();
};
app.initHLS = function () {
app.hls = airplay.createHLS();
app.hls.on( 'start', function () {
app.log( 'HLS start, ', this.baseURI );
});
app.hls.on( 'stop', function () {
app.log( 'HLS stop' );
});
app.hls.on( 'request', function ( req ) {
app.log( 'HLS request, ', req );
});
app.hls.on( 'stream', function ( index, size ) {
app.videoSegmentIndex = index;
app.videoSegmentSize = size;
});
app.hls.start( 7001 );
};
app.initBrowser = function () {
app.browser = airplay.createBrowser();
app.browser.on( 'deviceOn', function( device ) {
app.log( 'device online:', device.getInfo() );
app.device = device;
app.device.on( 'ping', function () { app.log( 'device pingback ~' ); });
app.log( 'AppleTV Connected !!' );
app.updateList( device.getInfo() );
app.initHLS();
});
app.browser.on( 'start', function () {
app.log( 'Browser start' );
});
app.browser.on( 'stop', function () {
app.log( 'Browser stop' );
});
app.browser.start();
};
app.initDroper = function () {
var droper = document.getElementById( 'drop' );
droper.addEventListener( 'dragover', onFileDrag, false );
droper.addEventListener( 'dragleave', onFileDrag, false );
droper.addEventListener( 'drop', onFileDrop, false );
function onFileDrag( e ) {
e.preventDefault();
e.target.className = (e.type === "dragover" ? "hover" : "");
}
function onFileDrop( e ) {
e.preventDefault();
var file = (e.target.files || e.dataTransfer.files)[0];
app.log( 'drop video:', file.path );
app.hls.open( file.path, function ( info ) {
app.log( 'HLS opened:', info );
});
}
};
app.initController = function () {
var isPlaying = 0;
document.getElementById('tvPlay').onclick = function () {
if ( !app.device ) return;
// console.info( app, app.hls, app.browser, app.airplay );
if ( isPlaying === 0) {
this.textContent = '暂停';
app.device.play( app.hls.getURI(), 0, function () {
console.info( '开始播放啦~~' );
app.updateStatus();
isPlaying = 1;
});
}
// NOTE: only 0 and 1 seem to be supported for most media types
// rate: 0 = pause, 1 = resume
else if ( isPlaying === 1 ) {
this.textContent = '播放';
app.device.rate( 0, function () {
console.info( '暂停播放啦~~' );
isPlaying = 2;
});
}
else if ( isPlaying === 2 ) {
this.textContent = '暂停';
app.device.rate( 1, function ( res ) {
console.info( '恢复播放啦~~' );
isPlaying = 1;
});
}
};
document.getElementById('tvStop').onclick = function () {
if ( !app.device ) return;
document.getElementById('tvPlay').textContent = '播放';
app.device.stop( function () {
console.info( '终止播放啦~~' );
isPlaying = 0;
clearTimeout( app.statusTimer );
});
};
};
app.dispose = function () {
app.browser.stop();
app.hls.stop();
document.getElementById( 'playVideo' ).onclick = null;
app.browser = null;
app.hls = null;
app.device = null;
};
app.updateList = function ( deviceInfo ) {
var select = document.getElementById( 'devices' );
select.add( new Option( deviceInfo.name, '' ) );
};
app.updateStatus = function () {
app.device.status( function ( info ) {
// console.log( 'playstatus:', info );
// 正在播放时才更新
if ( info && info.readyToPlay ) {
var out = '';
out +=
'<p>' +
'播放进度: ' +
Math.ceil( info.position ) +
' / ' +
info.duration +
'</p>';
if ( info.loadedTimeRanges ) {
out +=
'<p>' +
'加载进度: ' +
Math.ceil(
info.loadedTimeRanges[0].start +
info.loadedTimeRanges[0].duration
) +
' / ' +
info.duration +
'</p>';
}
out +=
'<p>' +
'分段进度:' +
( app.videoSegmentIndex || 0 ) +
' / ' +
( app.videoSegmentSize || 1 ) +
'</p>';
var node = document.querySelector('.control .status');
node.innerHTML = out;
}
// next
app.statusTimer = setTimeout( app.updateStatus.bind( app ), 1000 );
});
};
window.onload = app.init;
window.beforeunload = app.dispose;