forked from amilajack/popcorn-time-desktop
/
ChromeCastProvider.js
209 lines (161 loc) 路 5.19 KB
/
ChromeCastProvider.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
204
205
206
207
208
209
// @flow
import ReduxClazz from 'redux-clazz'
import chromecastAPI from 'chromecasts'
import debug from 'debug'
import network from 'network-address'
import Power from 'api/Power'
import * as PlayerConstants from 'api/Player/PlayerConstants'
import type { ContentType } from 'api/Metadata/MetadataTypes'
import type { DeviceType } from '../StreamingTypes'
import { StreamingInterface } from '../StreamingInterface'
import type { ChromeCastApiType, ChromeCastType } from './ChromeCastTypes'
const log = debug('api:players:chromecast')
export default class extends ReduxClazz implements StreamingInterface {
provider: string = 'Chromecast'
supportsSubtitles: boolean = true
selectedDevice: ChromeCastType
devices: Array<ChromeCastType> = []
chromecasts: ChromeCastApiType
status: string = PlayerConstants.STATUS_NONE
checkProgressInterval: number
loadedItem: ContentType
states = {
PLAYING : PlayerConstants.STATUS_PLAYING,
BUFFERING: PlayerConstants.STATUS_BUFFERING,
PAUSED : PlayerConstants.STATUS_PAUSED,
IDLE : PlayerConstants.STATUS_NONE,
FINISHED : PlayerConstants.STATUS_ENDED,
}
constructor(...props) {
super(...props)
this.chromecasts = chromecastAPI()
this.chromecasts.on('update', this.onChromecastsUpdate)
}
onChromecastsUpdate = player => this.devices.push(player)
getDevices = (timeout: number = 1000) => new Promise((resolve) => {
this.chromecasts.update()
setTimeout(() => {
resolve(this.devices.map(device => ({
name : device.name,
host : device.host,
provider: this.provider,
})))
}, timeout)
})
selectDevice = (device: DeviceType) => {
log(`Selecting device ${device.name}`)
this.selectedDevice = this.devices.find(chromecast => chromecast.host === device.host)
}
play = (uri: string, item: ContentType) => {
if (this.status !== PlayerConstants.STATUS_NONE) {
return
}
log(`Play ${item.title}`)
let streamingUrl = uri
if (uri.indexOf('localhost')) {
streamingUrl = uri.replace('localhost', network())
}
if (!this.selectedDevice) {
throw new Error('No device selected')
}
Power.enableSaveMode()
log(`Connecting to: ${this.selectedDevice.name} (${this.selectedDevice.host})`)
this.updateStatus(PlayerConstants.STATUS_CONNECTING)
this.loadedItem = item
const media = {
type: 'video/mp4',
// tracks: <-- For subtitles
autoSubtitles: false,
metadata : {
type : 0,
metadataType: 0,
title : item.title,
images : [{
url: item.images.poster.medium,
}],
},
}
this.selectedDevice.play(streamingUrl, media, () => {
this.selectedDevice.on('status', (status) => {
if (status && status.playerState === 'IDLE') {
this.updateStatus(this.states[status.idleReason])
} else {
this.updateStatus(this.states[status.playerState])
}
})
})
}
togglePlay = () => {
log('Toggle Play...')
if (this.selectedDevice && this.status !== PlayerConstants.STATUS_NONE) {
if (this.status === PlayerConstants.STATUS_PAUSED) {
this.selectedDevice.play()
} else {
this.selectedDevice.pause()
}
}
}
stop = () => {
log('Stop...')
if (this.selectedDevice && this.status !== PlayerConstants.STATUS_NONE) {
this.selectedDevice.stop()
}
}
isPlaying = () => this.status === PlayerConstants.STATUS_PLAYING
updateStatus = (nStatus) => {
log('updateStatus', nStatus)
const newStatus = nStatus && nStatus !== 'undefined' ? nStatus : PlayerConstants.STATUS_NONE
if (newStatus !== this.status) {
log(`Update status to ${newStatus}`)
const { updateStatus } = this.props
this.status = newStatus
updateStatus(newStatus)
this.clearIntervals()
if (newStatus === PlayerConstants.STATUS_PLAYING) {
this.checkProgressInterval = this.progressInterval()
} else if (newStatus === PlayerConstants.STATUS_NONE) {
this.destroyPlayer()
}
}
}
progressInterval = () => setInterval(() => {
if (this.selectedDevice) {
this.selectedDevice.status((err, data) => {
if (typeof data !== 'undefined') {
const { updatePercentage } = this.props
const percentage = (data.currentTime / data.media.duration) * 100
if (percentage > 95) {
this.clearIntervals()
updatePercentage(this.loadedItem, 100)
} else {
updatePercentage(this.loadedItem, percentage)
}
} else {
this.clearIntervals()
this.destroy()
}
})
}
}, 10000)
clearIntervals = () => {
if (this.checkProgressInterval) {
clearInterval(this.checkProgressInterval)
}
}
destroy = () => {
if (this.status !== PlayerConstants.STATUS_NONE) {
log('Destroy...')
this.destroyPlayer()
this.updateStatus(PlayerConstants.STATUS_NONE)
}
}
destroyPlayer = () => {
Power.disableSaveMode()
if (this.selectedDevice) {
this.selectedDevice.stop()
}
if (this.chromecasts) {
this.chromecasts.destroy()
}
}
}