-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
271 lines (229 loc) · 8.15 KB
/
main.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from flask import Flask, Response, request, jsonify, render_template_string
import subprocess
import sqlite3
import os
from typing import Any, List, Optional, Tuple
from contextlib import contextmanager
app = Flask(__name__)
DATABASE = 'data/streams.db'
@contextmanager
def get_db_connection():
conn = sqlite3.connect(DATABASE)
try:
yield conn
finally:
conn.close()
def init_db() -> None:
"""
Initialize the database to store stream configurations.
This function creates a SQLite database file if it doesn't exist and
creates a table named 'streams' to store stream configuration.
Args:
None
Returns:
None
"""
os.makedirs(os.path.dirname(DATABASE), exist_ok=True)
with get_db_connection() as conn:
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS streams
(id INTEGER PRIMARY KEY, url TEXT, active INTEGER, quality TEXT, resolution TEXT, fps INTEGER)
''')
conn.commit()
# Call init_db to ensure the database is initialized on startup
init_db()
def query_db(query: str, args: Tuple[Any] = (), one: bool = False) -> Optional[List[Tuple[Any]]]:
"""
Query the database and return a list of results or a single result.
Args:
query (str): The SQL query to execute.
args (Tuple[Any], optional): The arguments to pass to the query. Defaults to ().
one (bool, optional): Whether to return a single result or a list of results. Defaults to False.
Returns:
Optional[List[Tuple[Any]]]: A list of results or a single result, depending on the value of `one`.
"""
with get_db_connection() as conn:
cur = conn.cursor()
cur.execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
def add_stream(url: str, quality: str, resolution: str, fps: int) -> None:
"""
Add a new stream to the database.
Args:
url (str): The URL of the stream.
quality (str): The quality of the stream.
resolution (str): The resolution of the stream.
fps (int): The frames per second of the stream.
Returns:
None
"""
with get_db_connection() as conn:
c = conn.cursor()
c.execute('INSERT INTO streams (url, active, quality, resolution, fps) VALUES (?, 1, ?, ?, ?)',
(url, quality, resolution, fps))
conn.commit()
conn.close()
def remove_stream(stream_id: int) -> None:
"""
Remove a stream from the database.
Args:
stream_id (int): The ID of the stream to be removed.
Returns:
None
"""
with get_db_connection() as conn:
c = conn.cursor()
c.execute('DELETE FROM streams WHERE id = ?', (stream_id,))
conn.commit()
conn.close()
def generate_frames(stream_id: int) -> bytes:
"""
Generate frames from an RTSP stream.
Args:
stream_id (int): The ID of the stream.
Yields:
bytes: The frames in JPEG format.
Raises:
None
Returns:
None
"""
stream = query_db('SELECT url, quality, resolution, fps FROM streams WHERE id = ?', [
stream_id], one=True)
if not stream:
return
stream_url, quality, resolution, fps = stream
command = [
'ffmpeg',
'-rtsp_transport', 'tcp',
'-i', stream_url,
'-r', str(fps), # Frame rate
'-c:v', 'mjpeg',
'-vf', f'scale={resolution}',
'-q:v', quality,
'-f', 'image2pipe',
'-'
]
with subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=-1) as p:
data = b""
try:
while True:
chunk = p.stdout.read(4096)
if not chunk:
break
data += chunk
while True:
start = data.find(b'\xff\xd8')
end = data.find(b'\xff\xd9', start + 2)
if start != -1 and end != -1:
frame = data[start:end + 2]
data = data[end + 2:]
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
else:
break
finally:
p.kill()
@app.route('/video_feed/<int:stream_id>')
def video_feed(stream_id: str) -> Response:
"""
Video feed route to serve the MJPEG stream.
Args:
stream_id (str): The ID of the stream.
Returns:
Response: The response object containing the MJPEG stream.
"""
return Response(generate_frames(stream_id),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/add_stream', methods=['POST'])
def add_stream_endpoint() -> Response:
"""
API endpoint to add a new stream.
Args:
None
Returns:
Response: The response object indicating the success of the operation.
"""
data: dict[str, Any] = request.json
add_stream(data['url'], data['quality'], data['resolution'], data['fps'])
return jsonify(success=True)
@app.route('/remove_stream/<int:stream_id>', methods=['DELETE'])
def remove_stream_endpoint(stream_id: int) -> Response:
"""
API endpoint to remove a stream.
Args:
stream_id (int): The ID of the stream to be removed.
Returns:
Response: The response object indicating the success of the operation.
"""
remove_stream(stream_id)
return jsonify(success=True)
@app.route('/')
def index() -> str:
"""
Main page to display available streams and management options.
Returns:
str: The HTML content of the main page.
"""
streams = query_db('SELECT * FROM streams')
return render_template_string('''
<h1>Stream Manager</h1>
<div>
<form action="/add_stream" method="post" id="addForm">
<input type="text" name="url" placeholder="RTSP URL" required />
<input type="text" name="quality" placeholder="Quality (1-31)" required title="Enter a value from 1 to 31, where 1 is the highest quality and 31 is the lowest." />
<input type="text" name="resolution" placeholder="Resolution (e.g., 640x480)" required />
<input type="number" name="fps" placeholder="FPS (e.g., 15)" required />
<button type="submit">Add Stream</button>
</form>
</div>
<ul>
{% for stream in streams %}
<li>
<div>
<strong>Stream {{ stream[0] }}:</strong> {{ stream[1] }}
<a href="{{ url_for('video_feed', stream_id=stream[0]) }}">Watch Stream</a>
<button onclick="removeStream({{ stream[0] }})">Remove</button>
</div>
</li>
{% endfor %}
</ul>
<script>
document.getElementById('addForm').onsubmit = function(e) {
e.preventDefault();
const formData = new FormData(this);
fetch('/add_stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: formData.get('url'),
quality: formData.get('quality'),
resolution: formData.get('resolution'),
fps: formData.get('fps')
})
}).then(response => response.json())
.then(data => {
if (data.success) {
location.reload();
}
});
};
function removeStream(id) {
fetch('/remove_stream/' + id, { method: 'DELETE' })
.then(response => response.json())
.then(data => {
if (data.success) {
location.reload();
}
});
}
</script>
''', streams=streams)
if __name__ == '__main__':
init_db()
app.run(host='0.0.0.0', port=5000, debug=True)