diff --git a/ap.py b/ap.py new file mode 100644 index 0000000..9c1b940 --- /dev/null +++ b/ap.py @@ -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) diff --git a/bash/stream.sh b/bash/stream.sh index d21404c..b24d370 100644 --- a/bash/stream.sh +++ b/bash/stream.sh @@ -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 \ No newline at end of file +/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 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index acda6c7..3c8abca 100755 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ uvicorn~=0.23.2 fastapi~=0.95.1 paho-mqtt~=1.6.1 requests~=2.31.0 -pyserial~=3.5 \ No newline at end of file +pyserial~=3.5 +board~=1.0 \ No newline at end of file diff --git a/test.py b/test.py index f87f559..166e704 100644 --- a/test.py +++ b/test.py @@ -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)