This commit is contained in:
xyj 2023-12-29 17:59:05 +08:00
parent ed39133704
commit 51a89836a7
4 changed files with 65 additions and 5 deletions

28
ap.py Normal file
View File

@ -0,0 +1,28 @@
from fastapi import FastAPI
import asyncio
app = FastAPI()
user_sched = {}
async def my_async_task():
# 这里是异步任务的代码
# 倒计时3秒
await asyncio.sleep(10)
print("close")
async def task_start(username, device):
task = asyncio.create_task(my_async_task())
user_sched[username] = task
@app.get("/video/{username}/{device}")
async def startup(username, device):
if username not in user_sched:
await task_start(username, device)
else:
t = user_sched['1']
t.cancel()
await task_start()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8004)

View File

@ -1,3 +1,3 @@
#!/bin/bash
device_name=`cat /home/pi/device_name`
/usr/bin/ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.123:554/mpeg4 -c copy -preset ultrafast -r 15 -s 1280x720 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rtsp://47.108.186.87:554/live/$device_name
/usr/bin/ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.123:554/mpeg4 -c copy -preset fast -r 20 -s 1280x720 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rtsp://47.108.186.87:554/live/$device_name

View File

@ -4,4 +4,5 @@ uvicorn~=0.23.2
fastapi~=0.95.1
paho-mqtt~=1.6.1
requests~=2.31.0
pyserial~=3.5
pyserial~=3.5
board~=1.0

37
test.py
View File

@ -1,3 +1,4 @@
import asyncio
import json
import uvicorn
@ -34,7 +35,38 @@ class MQTTClient:
self.client.connect(self.broker, self.port)
@app.post("/push_stream")
user_sched = {}
async def close(username, device):
# 倒计时600秒
await asyncio.sleep(30)
user_sched.pop(username)
print("结束推流")
close_stream(username, device)
async def task_start(username, device):
print("开始推流")
push_stream(username, device)
task = asyncio.create_task(close(username, device))
user_sched[username] = task
@app.get("/video/{username}/{device}")
async def startup(username, device):
try:
if username not in user_sched:
await task_start(username, device)
else:
t = user_sched[username]
# 取消重来
t.cancel()
await task_start()
except:
pass
def push_stream(username, device):
MQTT = MQTTClient(broker, port, device, username, username)
try:
@ -45,7 +77,6 @@ def push_stream(username, device):
pass
@app.post("/close_stream")
def close_stream(username, device):
try:
MQTT = MQTTClient(broker, port, device, username, username)
@ -59,4 +90,4 @@ def close_stream(username, device):
if __name__ == '__main__':
broker = 'mqtt.lihaink.cn'
port = 1883
uvicorn.run(app, host="127.0.0.1", port=8000)
uvicorn.run(app, host="127.0.0.1", port=8001)