This repository has been archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
181 lines (149 loc) · 5.27 KB
/
main.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
const {app, BrowserWindow, screen, ipcMain } = require('electron')
const path = require('path')
require('dotenv').config()
//require('electron-reloader')(module)
const express = require("express");
const exp = express();
const server = require("http").createServer(exp);
const io = require("socket.io").listen(server);
const fs = require('fs-extra');
const peerflix = require("./vendor/peerflix");
const subsApi = require('./src/subtitles/subtitles');
const port = process.env.PORT;
let g_activeMedia = null;
let mainWindow = null;
let g_engine = null;
let g_dirName = null;
//electron stuff
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
frame:false,
webPreferences: {
autoplayPolicy: 'no-user-gesture-required',
webSecurity: false,
preload: path.join(__dirname, './electron/preload.js')
},
})
//cant make the player fullscreen without user interaction, so just set the electron window to fullscreen
//and the player width and height to the screen resolution
mainWindow.fullScreen = true
mainWindow.loadFile('./electron/index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
ipcMain.on('RES_PLAYER_STATUS', (event, arg)=>{
io.sockets.emit('SET_PLAYER_PAUSED', {isPaused:arg.isPaused});
})
//
io.on("connection", socket => {
console.log("App connected");
//receives the movie data from the mobile app
socket.on('APP_START_STREAM', (data)=>{
const magnet = data.value.url
g_activeMedia = data;
//console.log(data);
start(data, magnet);
})
//mobile app requesting status when the component is mounted
socket.on('APP_GET_STATUS',()=>{
updateAppStatus();
})
socket.on('APP_PAUSE_PLAYER', ()=>{
mainWindow.webContents.send('PLAYER_PAUSE');
})
socket.on('APP_GET_PAUSED', ()=>{
mainWindow.webContents.send('PLAYER_STATUS');
})
//received when the mobile app press the button to close the player
socket.on('APP_CLOSE_PROCESS', ()=>{
if (g_engine){
mainWindow.webContents.send('PLAYER_CLOSE');
g_engine.destroy(()=>{
g_activeMedia = null;
g_engine = null;
updateAppStatus()
fs.emptyDir(g_dirName);
});
}
})
});
const updateAppStatus = () =>{
if (g_engine != null && g_activeMedia != null){
const media = {
...g_activeMedia.value,
title: g_activeMedia.title,
largeImage: g_activeMedia.largeImage,
}
io.sockets.emit('SET_STATUS_APP', {condition:true, media});
}else{
io.sockets.emit('SET_STATUS_APP',{condition:false});
}
}
//starts the torrent-stream engine and opens the vlc with the engine stream;
async function start(data, uri) {
if (!uri) {
throw new Error("Uri is required");
}
console.log(`Starting ${data.title}`);
g_engine = await startEngine(uri);
await openPlayer(data);
return g_engine
};
//starts the engine with the url provided by the yifi api
function startEngine(uri) {
return new Promise((resolve, reject) => {
const engine = peerflix(uri, {path:process.env.torrentsPath});
engine.server.on('listening', () => {
resolve(engine);
});
//todo error?
});
}
//opens the player on the electron frontend
openPlayer = async(data)=>{
try{
g_dirName = process.env.torrentsPath + '/' + g_engine.torrent.name + '/';
const {status, subs} = await subsApi.getSubtitles(data, g_dirName);
let hasSubs = true;
if(status != 200 || subs < 1){
console.log('Starting without subtitles');
hasSubs = false;
}
//stream url address
let localHref = `http://localhost:${g_engine.server.address().port}/`;
//opening player on electron frontend
let {width, height} = screen.getPrimaryDisplay().size
let downPercent = 0;
let pieces = 0;
//saving the total pieces downloaded to check the download percentage
const changePiece = (i) =>{
pieces = i;
}
g_engine.on('verify', changePiece);
//checks until the torrent has downloaded at least 5%
let interval = setInterval(()=>{
downPercent = Math.round(((pieces + 1) / g_engine.torrent.pieces.length) * 100.0)
if (downPercent > 1){
clearInterval(interval);
//send watching status to mobile app to show the buttons
io.sockets.emit('SET_STATUS_APP', {condition: true, activeMedia: g_activeMedia});
//streaming crashes if i remove the verify listener
//g_engine.removeListener('verify', changePiece)
mainWindow.webContents.send('PLAYER_START', {url: localHref, subDir: g_dirName, size: {width: width, height: height}, hasSubs});
}
},500)
}
catch(e){
console.error(e.message);
}
}
server.listen(port,() => console.log("server running on port:" + port));