livestreams.py 2.39 KB
Newer Older
1
2
3
4
from server import *

@sched_func(30)
def livestream_thumbnail():
Andreas Valder's avatar
Andreas Valder committed
5
	livestreams = query('SELECT streams.lecture_id, streams.handle AS livehandle FROM streams WHERE streams.active')
6
	for v in genlive(livestreams):
Julian Rother's avatar
Julian Rother committed
7
		schedule_job('thumbnail', {'lectureid': str(v['lecture_id']), 'path': v['path']})
8

9
@app.route('/internal/streaming/legacy_auth', methods=['GET', 'POST'])
10
11
@app.route('/internal/streaming/legacy_auth/<server>', methods=['GET', 'POST'])
def streamauth(server=None):
12
13
14
15
16
17
18
19
20
	internal = False
	for net in config.get('FSMPI_IP_RANGES', []):
		if ip_address(request.headers['X-Real-IP']) in ip_network(net):
			internal = True
	if request.values['app'] != 'live':
		return 'Bad request', 400
	if not internal:
		return 'Forbidden', 403
	if request.values['call'] == 'publish':
21
22
		if request.values['pass'] != 'caisoh8aht0wuSu':
			return 'Forbidden', 403
23
24
25
26
27
28
29
30
31
32
33
34
35
36
		matches = query("SELECT lectures.* FROM lectures JOIN courses ON lectures.course_id = courses.id WHERE courses.handle = ? ORDER BY lectures.time DESC", request.values['name'])
		now = datetime.now()
		match = {'id': -1}
		for lecture in matches:
			if lecture['time']-timedelta(minutes=30) <= now and \
				now <= lecture['time']+timedelta(minutes=lecture['duration']):
				match = lecture
				break
		if 'lecture' in request.values:
			match = {'id': request.values['lecture']}
		try:
			modify("INSERT INTO streams (handle, active, visible, lecture_id, description, poster) VALUES (?, 0, 1, -1, "", "")", request.values['name'])
		except:
			pass
37
38
39
40
41
42
43
44
45
		if server:
			data = {'src': 'rtmp://%s/live/%s'%(server, request.values['name']),
				'destbase': 'rtmp://%s/hls/%s'%(server, request.values['name'])}
			job_id = schedule_job('simple_live_transcode', data, priority=10)
			modify("UPDATE streams SET active = 1, lecture_id = ?, job_id = ? WHERE handle = ?",
					match['id'], job_id, request.values['name'])
		else:
			modify("UPDATE streams SET active = 1, lecture_id = ? WHERE handle = ?",
					match['id'], request.values['name'])
46
	elif request.values['call'] == 'publish_done':
47
		job_id = query('SELECT job_id FROM streams WHERE handle = ?', request.values['name'])[0]['job_id']
48
		modify("UPDATE streams SET active = 0 WHERE handle = ?", request.values['name'])
49
50
		if job_id:
			cancel_job(job_id)
51
52
53
54
	else:
		return 'Bad request', 400
	return 'OK', 200

55
56
57
58
@job_handler('simple_live_transcode', state='failed')
def restart_failed_live_transcode(id, type, data, state, status):
	restart_job(id)