Skip to content

Commit 31dc105

Browse files
authored
Add python interface to bandwidth estimator (#67)
* Add cmdinfer Signed-off-by: Ze Gan <[email protected]> * Refactor directory structure Signed-off-by: Ze Gan <[email protected]> * Fix azurepipeline Signed-off-by: Ze Gan <[email protected]> * Insert python exception traceback to logfile Signed-off-by: Ze Gan <[email protected]> * Update Readme Signed-off-by: Ze Gan <[email protected]>
1 parent f6d3b91 commit 31dc105

File tree

15 files changed

+388
-22
lines changed

15 files changed

+388
-22
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ output_dir := out/Default
55
target_dir := target
66
target_lib_dir := $(target_dir)/lib
77
target_bin_dir := $(target_dir)/bin
8+
target_pylib_dir := $(target_dir)/pylib
89

910
compile_docker := alphartc-compile
1011
release_docker := alphartc
@@ -37,7 +38,8 @@ peerconnection_serverless:
3738
make docker-$@ \
3839
output_dir=$(output_dir) \
3940
target_lib_dir=$(target_lib_dir) \
40-
target_bin_dir=$(target_bin_dir)
41+
target_bin_dir=$(target_bin_dir) \
42+
target_pylib_dir=$(target_pylib_dir)
4143

4244
# Docker internal command
4345

@@ -51,8 +53,14 @@ docker-app: docker-peerconnection_serverless
5153

5254
docker-peerconnection_serverless:
5355
ninja -C $(output_dir) peerconnection_serverless
56+
5457
mkdir -p $(target_lib_dir)
5558
cp modules/third_party/onnxinfer/lib/*.so $(target_lib_dir)
5659
cp modules/third_party/onnxinfer/lib/*.so.* $(target_lib_dir)
60+
5761
mkdir -p $(target_bin_dir)
5862
cp $(output_dir)/peerconnection_serverless $(target_bin_dir)
63+
cp examples/peerconnection/serverless/peerconnection_serverless_pyinfer $(target_bin_dir)
64+
65+
mkdir -p $(target_pylib_dir)
66+
cp modules/third_party/cmdinfer/*.py $(target_pylib_dir)/

README.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
AlphaRTC is a fork of Google's WebRTC project using ML-based bandwidth estimation, delivered by the OpenNetLab team. By equipping WebRTC with a more accurate bandwidth estimator, our mission is to eventually increase the quality of transmission.
4141

42-
AlphaRTC replaces Google Congestion Control (GCC) with ONNXInfer, an ML-powered bandwidth estimator, which takes in an ONNX model to make bandwidth estimation more accurate. ONNXInfer is proudly powered by Microsoft's [ONNXRuntime](https://github.com/microsoft/onnxruntime).
42+
AlphaRTC replaces Google Congestion Control (GCC) with two customized congestion control interfaces, PyInfer and ONNXInfer. The PyInfer provides an opportunity to load external bandwidth estimator written by Python. The external bandwidth estimator could be based on ML framework, like PyTorch or TensorFlow, or a pure Python algorithm without any dependencies. And the ONNXInfer is an ML-powered bandwidth estimator, which takes in an ONNX model to make bandwidth estimation more accurate. ONNXInfer is proudly powered by Microsoft's [ONNXRuntime](https://github.com/microsoft/onnxruntime).
4343

4444
## Environment
4545

@@ -107,7 +107,7 @@ Note: all commands below work for both Linux (sh) and Windows (pwsh), unless oth
107107
gn gen out/Default
108108
```
109109

110-
5. Comile
110+
5. Compile
111111
```shell
112112
ninja -C out/Default peerconnection_serverless
113113
```
@@ -151,9 +151,6 @@ This section describes required fields for the json configuration file.
151151

152152
- **bwe_feedback_duration**: The duration the receiver sends its estimated target rate every time(*in millisecond*)
153153

154-
- **onnx**
155-
- **onnx_model_path**: The path of the [onnx](https://www.onnxruntime.ai/) model
156-
157154
- **video_source**
158155
- **video_disabled**:
159156
- **enabled**: If set to `true`, the client will not take any video source as input
@@ -188,11 +185,57 @@ This section describes required fields for the json configuration file.
188185
- **fps**: Frames per second of the output video file
189186
- **file_path**: The file path of the output video file in YUV format
190187

188+
#### Use PyInfer or ONNXInfer
189+
190+
##### PyInfer
191+
192+
The default bandwidth estimator is PyInfer, You should implement your Python class named `Estimator` with required methods `report_states` and `get_estimated_bandwidth` in Python file `BandwidthEstimator.py ` and put this file in your workspace. And you should use the `peerconnection_serverless_pyinfer` as the start program.
193+
There is an example of Estimator with fixed estimated bandwidth 1Mbps. Here is an example [BandwidthEstimator.py](examples/peerconnection/serverless/corpus/BandwidthEstimator.py).
194+
195+
```python
196+
class Estimator(object):
197+
def report_states(self, stats: dict):
198+
'''
199+
stats is a dict with the following items
200+
{
201+
"send_time_ms": uint,
202+
"arrival_time_ms": uint,
203+
"payload_type": int,
204+
"sequence_number": uint,
205+
"ssrc": int,
206+
"padding_length": uint,
207+
"header_length": uint,
208+
"payload_size": uint
209+
}
210+
'''
211+
pass
212+
213+
def get_estimated_bandwidth(self)->int:
214+
return int(1e6) # 1Mbps
215+
216+
```
217+
218+
##### ONNXInfer
219+
220+
If you want to use the ONNXInfer as the bandwidth estimator, you should specify the path of onnx model in the config file. Here is an example configuration [receiver.json](examples/peerconnection/serverless/corpus/receiver.json)
221+
222+
- **onnx**
223+
- **onnx_model_path**: The path of the [onnx](https://www.onnxruntime.ai/) model
224+
225+
191226
#### Run peerconnection_serverless
192227
- Dockerized environment
193228

194229
To better demonstrate the usage of peerconnection_serverless, we provide an all-inclusive corpus in `examples/peerconnection/serverless/corpus`. You can use the following commands to execute a tiny example. After these commands terminates, you will get `outvideo.yuv` and `outaudio.wav`.
195-
230+
231+
232+
PyInfer:
233+
```shell
234+
sudo docker run -d --rm -v `pwd`/examples/peerconnection/serverless/corpus:/app -w /app --name alphartc alphartc peerconnection_serverless_pyinfer receiver_pyinfer.json
235+
sudo docker exec alphartc sender_pyinfer sender.json
236+
```
237+
238+
ONNXInfer:
196239
``` shell
197240
sudo docker run -d --rm -v `pwd`/examples/peerconnection/serverless/corpus:/app -w /app --name alphartc alphartc peerconnection_serverless receiver.json
198241
sudo docker exec alphartc peerconnection_serverless sender.json

api/alphacc_config.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ bool ParseAlphaCCConfig(const std::string& file_path) {
6868
RETURN_ON_FAIL(
6969
GetInt(top, "bwe_feedback_duration", &config->bwe_feedback_duration_ms));
7070

71-
RETURN_ON_FAIL(GetValue(top, "onnx", &second));
72-
RETURN_ON_FAIL(
73-
GetString(second, "onnx_model_path", &config->onnx_model_path));
74-
second.clear();
71+
if (GetValue(top, "onnx", &second)) {
72+
GetString(second, "onnx_model_path", &config->onnx_model_path);
73+
second.clear();
74+
}
7575

7676
bool enabled = false;
7777
RETURN_ON_FAIL(GetValue(top, "video_source", &second));

azure-pipelines.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ steps:
3333
- script: docker run -d --rm -v `pwd`/examples/peerconnection/serverless/corpus:/app -w /app --name alphartc alphartc peerconnection_serverless receiver.json
3434
&& docker exec alphartc peerconnection_serverless sender.json
3535
displayName: 'run example'
36+
37+
- script: docker run -d --rm -v `pwd`/examples/peerconnection/serverless/corpus:/app -w /app --name alphartc_pyinfer alphartc peerconnection_serverless_pyinfer receiver_pyinfer.json
38+
&& docker exec alphartc_pyinfer peerconnection_serverless_pyinfer sender_pyinfer.json
39+
displayName: 'run cmdinfer example'

dockers/Dockerfile.release

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
FROM ubuntu:18.04
22

33
RUN apt-get update && apt-get install -y \
4-
libx11-6 libgomp1
4+
libx11-6 libgomp1 python3
55

66
COPY lib /usr/lib/
77

88
COPY bin /usr/bin/
99

10+
COPY pylib /usr/lib/python3/dist-packages/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Estimator(object):
3+
def report_states(self, stats: dict):
4+
'''
5+
stats is a dict with the following items
6+
{
7+
"send_time_ms": uint,
8+
"arrival_time_ms": uint,
9+
"payload_type": int,
10+
"sequence_number": uint,
11+
"ssrc": int,
12+
"padding_length": uint,
13+
"header_length": uint,
14+
"payload_size": uint
15+
}
16+
'''
17+
pass
18+
19+
def get_estimated_bandwidth(self)->int:
20+
return int(1e6) # 1Mbps
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"serverless_connection": {
3+
"autoclose": 20,
4+
"sender": {
5+
"enabled": false
6+
},
7+
"receiver": {
8+
"enabled": true,
9+
"listening_ip": "0.0.0.0",
10+
"listening_port": 8000
11+
}
12+
},
13+
"bwe_feedback_duration": 200,
14+
"video_source": {
15+
"video_disabled": {
16+
"enabled": true
17+
},
18+
"webcam": {
19+
"enabled": false
20+
},
21+
"video_file": {
22+
"enabled": false,
23+
"height": 240,
24+
"width": 320,
25+
"fps": 10,
26+
"file_path": "testmedia/test.yuv"
27+
}
28+
},
29+
"audio_source": {
30+
"microphone": {
31+
"enabled": false
32+
},
33+
"audio_file": {
34+
"enabled": true,
35+
"file_path": "testmedia/test.wav"
36+
}
37+
},
38+
"save_to_file": {
39+
"enabled": true,
40+
"audio": {
41+
"file_path": "outaudio.wav"
42+
},
43+
"video": {
44+
"width": 320,
45+
"height": 240,
46+
"fps": 10,
47+
"file_path": "outvideo.yuv"
48+
}
49+
},
50+
"logging": {
51+
"enabled": true,
52+
"log_output_path": "webrtc.log"
53+
}
54+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"serverless_connection": {
3+
"autoclose": 20,
4+
"sender": {
5+
"enabled": true,
6+
"dest_ip": "0.0.0.0",
7+
"dest_port": 8000
8+
},
9+
"receiver": {
10+
"enabled": false
11+
}
12+
},
13+
"bwe_feedback_duration": 200,
14+
"video_source": {
15+
"video_disabled": {
16+
"enabled": false
17+
},
18+
"webcam": {
19+
"enabled": false
20+
},
21+
"video_file": {
22+
"enabled": true,
23+
"height": 240,
24+
"width": 320,
25+
"fps": 10,
26+
"file_path": "testmedia/test.yuv"
27+
}
28+
},
29+
"audio_source": {
30+
"microphone": {
31+
"enabled": false
32+
},
33+
"audio_file": {
34+
"enabled": true,
35+
"file_path": "testmedia/test.wav"
36+
}
37+
},
38+
"save_to_file": {
39+
"enabled": false
40+
},
41+
"logging": {
42+
"enabled": false
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import os
6+
import subprocess
7+
import traceback
8+
import json
9+
10+
sys.path.append(os.getcwd())
11+
12+
import cmdinfer
13+
14+
15+
def main():
16+
app = subprocess.Popen(
17+
["peerconnection_serverless"] + sys.argv[1:],
18+
bufsize=1,
19+
stdin=subprocess.PIPE,
20+
stdout=subprocess.PIPE,
21+
stderr=subprocess.STDOUT)
22+
try:
23+
cmdinfer.main(app.stdout, app.stdin)
24+
app.wait()
25+
except:
26+
if len(sys.argv[1:]) == 0:
27+
return
28+
config_file = sys.argv[1]
29+
config_file = json.load(open(config_file, "r"))
30+
if "logging" not in config_file:
31+
return
32+
if "enabled" not in config_file["logging"] or not config_file["logging"]["enabled"]:
33+
return
34+
app.terminate()
35+
app.wait()
36+
with open(config_file["logging"]["log_output_path"], "a") as log_file:
37+
error_message = traceback.format_exc()
38+
error_message = "\n{}".format(error_message)
39+
log_file.write(error_message)
40+
sys.stderr.write(error_message)
41+
42+
43+
if __name__ == "__main__":
44+
main()

modules/remote_bitrate_estimator/BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ rtc_library("remote_bitrate_estimator") {
6262
"../../system_wrappers:metrics",
6363
"//third_party/abseil-cpp/absl/strings",
6464
"//third_party/abseil-cpp/absl/types:optional",
65-
"//modules/third_party/statcollect:stat_collect"
65+
"//modules/third_party/statcollect:stat_collect",
66+
"//modules/third_party/cmdinfer:cmdinfer"
6667
]
6768

6869
if (is_linux) {

0 commit comments

Comments
 (0)