Skip to content

Commit bf371ea

Browse files
authored
Merge pull request sipeed#158 from 916BGAI/dev
add webrtc stream support
2 parents 8e8eb54 + ef846d2 commit bf371ea

File tree

10 files changed

+405
-0
lines changed

10 files changed

+405
-0
lines changed

docs/doc/en/sidebar.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ items:
155155
label: RTMP streaming
156156
- file: video/uvc_streaming.md
157157
label: UVC streaming
158+
- file: video/webrtc_streaming.md
159+
label: WebRTC streaming
158160

159161
- label: Network
160162
items:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: MaixCAM MaixPy Video Streaming WebRTC Push Streaming
3+
update:
4+
- date: 2025-12-12
5+
author: 916BGAI
6+
version: 1.0.0
7+
content: Initial documentation
8+
---
9+
10+
<br/>
11+
12+
> WebRTC streaming requires a browser that supports the WebRTC protocol. Please use the latest version of Chrome for testing.
13+
14+
## Introduction
15+
16+
This document explains how to push camera video using WebRTC.
17+
18+
## Usage
19+
20+
```python
21+
from maix import time, webrtc, camera, image
22+
23+
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
24+
server = webrtc.WebRTC()
25+
server.bind_camera(cam)
26+
server.start()
27+
28+
print(server.get_url())
29+
30+
while True:
31+
time.sleep(1)
32+
```
33+
34+
Steps:
35+
36+
1. Import the `time`, `webrtc`, `camera`, and `image` modules
37+
38+
```python
39+
from maix import time, webrtc, camera, image
40+
```
41+
42+
2. Initialize the camera
43+
44+
```python
45+
# Initialize the camera with output resolution 640x480 and NV21 format
46+
47+
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
48+
```
49+
50+
- Note: The WebRTC module currently only supports NV21 format, so the camera must be configured to output NV21.
51+
52+
3. Initialize and start the WebRTC server
53+
54+
```python
55+
server = webrtc.WebRTC()
56+
server.bind_camera(cam)
57+
server.start()
58+
```
59+
60+
- `server = webrtc.WebRTC()` creates a `WebRTC` instance.
61+
- `server.bind_camera(cam)` binds a `Camera` instance to the WebRTC server. After binding, the original `Camera` object should no longer be used directly.
62+
- `server.start()` starts the WebRTC streaming service.
63+
64+
4. Print the WebRTC stream URL
65+
66+
```python
67+
print(server.get_url())
68+
```
69+
70+
- `server.get_url()` returns the playback URL for the WebRTC stream.
71+
72+
5. Done — after running the script above, open the printed URL in your browser to view the camera stream.

docs/doc/zh/sidebar.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ items:
155155
label: RTMP 串流
156156
- file: video/uvc_streaming.md
157157
label: UVC 串流
158+
- file: video/webrtc_streaming.md
159+
label: WebRTC 串流
158160

159161
- label: 网络通信
160162
items:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: MaixCAM MaixPy 视频流 WebRTC 推流
3+
update:
4+
- date: 2025-12-11
5+
author: 916BGAI
6+
version: 1.0.0
7+
content: 初版文档
8+
---
9+
10+
<br/>
11+
12+
> WebRTC 推流需要浏览器支持 WebRTC 协议,请使用最新版本的 Chrome 浏览器进行测试。
13+
14+
## 简介
15+
16+
本文档提供通过 WebRTC 推流摄像头画面的方法
17+
18+
## 使用方法
19+
20+
```python
21+
from maix import time, webrtc, camera, image
22+
23+
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
24+
server = webrtc.WebRTC()
25+
server.bind_camera(cam)
26+
server.start()
27+
28+
print(server.get_url())
29+
30+
while True:
31+
time.sleep(1)
32+
```
33+
34+
步骤:
35+
36+
1. 导入 time, webrtc, camera 和 image 模块
37+
38+
```python
39+
from maix import time, webrtc, camera, image
40+
```
41+
42+
2. 初始化摄像头
43+
44+
```python
45+
# 初始化摄像头,输出分辨率640x480 NV21格式
46+
47+
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
48+
```
49+
50+
- 注意 WebRTC 模块目前只支持 NV21 格式, 因此摄像头需要配置为 NV21 格式输出
51+
52+
3. 初始化并启动 WebRTC 对象
53+
54+
```python
55+
server = webrtc.WebRTC()
56+
server.bind_camera(cam)
57+
server.start()
58+
```
59+
60+
- `server = webrtc.WebRTC()` 用来创建一个 `WebRTC` 对象
61+
- `server.bind_camera(cam)` 用来绑定一个 `Camera` 对象, 绑定后原 `Camera` 对象将不能再使用
62+
- `server.start()` 用来启动 `webrtc` 推流
63+
64+
4. 打印当前WebRTC流的URL
65+
66+
```python
67+
print(server.get_url())
68+
```
69+
70+
- `server.get_url()` 用来获取 `WebRTC``播放地址`
71+
72+
6. 完成,运行上须代码后, 你可以通过浏览器访问打印出的URL地址来查看摄像头画面
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from maix import time, webrtc, camera, image
2+
3+
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
4+
server = webrtc.WebRTC()
5+
server.bind_camera(cam)
6+
server.start()
7+
8+
print(server.get_url())
9+
10+
while True:
11+
time.sleep(1)

projects/webrtc_stream/app.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
id: webrtc_stream
2+
name: WebRTC Stream
3+
name[zh]: WebRTC 推流
4+
version: 1.0.0
5+
author: Sipeed Ltd
6+
icon: app.png
7+
desc: WebRTC Stream
8+
desc[zh]: WebRTC 推流
9+
files:
10+
- assets
11+
- app.yaml
12+
- main.py
6 KB
Loading
785 Bytes
Loading
6.5 KB
Loading

0 commit comments

Comments
 (0)