Skip to content

Commit 063e257

Browse files
ko80tszumski
authored andcommitted
Improve latency solution report and rework documentation
* Make the font size a bit smaller. * Avoid taking zero delta values. * Filter out anomaly peaks exceeding the average value by 25% from the calculation of the average latency. * Add file info to the diagram. * Print results in console. * Rework the latency measurement solution documentation. Signed-off-by: Konstantin Ilichev <konstantin.ilichev@intel.com>
1 parent e45defa commit 063e257

File tree

4 files changed

+215
-75
lines changed

4 files changed

+215
-75
lines changed

docs/FFmpegPlugin.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ replace `7.0` with `6.1` in the following script.
2525
```bash
2626
./configure-ffmpeg.sh
2727
```
28-
To be able to measure end-to-end latency please use following configuration parameters
29-
```bash
30-
./configure-ffmpeg.sh ${FFMPEG_VER} --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig
31-
```
3228

3329
1. Build and install FFmpeg with the Media Communications Mesh FFmpeg plugin
3430

@@ -279,11 +275,6 @@ ffmpeg version n6.1.1-152-ge821e6c21d Copyright (c) 2000-2023 the FFmpeg develop
279275
built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)
280276
```
281277

282-
While measuring latency, it is necessary to configure FFmpeg with additional parameters. If the error `No such filter: 'drawtext'` occurs, please reconfigure and rebuild FFmpeg:
283-
```bash
284-
./configure-ffmpeg.sh ${FFMPEG_VER} --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig
285-
```
286-
287278
<!-- References -->
288279
[license-img]: https://img.shields.io/badge/License-BSD_3--Clause-blue.svg
289280
[license]: https://opensource.org/license/bsd-3-clause

docs/LatencyMeasurement.md

Lines changed: 157 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,116 @@
1-
# End To End Latency Measurement — Media Communications Mesh
1+
# End-to-End Latency Measurement — Media Communications Mesh
2+
3+
This document describes a simple solution for measuring end-to-end latency in Media Communications Mesh.
4+
5+
## Overview
6+
7+
The solution is based on the FFmpeg ability to print current timestamps on the sender side (Tx) and the receiver side (Rx), and the use of Optical Character Recognition (OCR) to read the timestamps out of each received video frame and calculate the delta. The choice of OCR is determined by the fact that the text can be effectively recognized even if the picture is affected by any sort of a lossy video compression algorithm somewhere in the transmission path in the Mesh. To achieve proper accuracy of the measurement, both Tx and Rx host machines should be synchronized using Precision Time Protocol (PTP).
8+
9+
> Only video payload is supported.
10+
11+
```mermaid
12+
flowchart LR
13+
tx-file((Input
14+
video file))
15+
tx-ffmpeg(Tx
16+
FFmpeg)
17+
subgraph mesh [Media Communications Mesh]
18+
direction LR
19+
proxy1(Proxy1)
20+
proxy2a(. . .)
21+
proxy2b(. . .)
22+
proxy2c(. . .)
23+
proxy3(ProxyN)
24+
proxy1 --> proxy2a --> proxy3
25+
proxy1 --> proxy2b --> proxy3
26+
proxy1 --> proxy2c --> proxy3
27+
end
28+
rx-ffmpeg(Rx
29+
FFmpeg)
30+
rx-file((Output
31+
video file))
32+
33+
tx-file --> tx-ffmpeg --> mesh --> rx-ffmpeg --> rx-file
34+
```
35+
36+
## How it works
37+
38+
1. Tx side – The user starts FFmpeg with special configuration to stream video via the Mesh.
39+
1. Rx side – The user starts FFmpeg with special configuration to receive the video stream from the Mesh.
40+
1. Tx side – FFmpeg prints the current timestamp as a huge text at the top of each video frame and transmits it via the Mesh.
41+
1. Rx side – FFmpeg prints the current timestamp as a huge text at the bottom of each video frame received from the Mesh and saves it on the disk.
42+
1. After transmission is done, there is a resulting MPEG video file on the disk on the Rx side.
43+
1. The user runs the solution script against the MPEG file that recognizes the Tx and Rx timestamps in each frame, and calculates the average latency based on the difference between the timestamps. Additionally, the script generates a latency diagram and stores it in JPEG format on the disk.
44+
45+
## Sample latency diagram
46+
47+
<img src="_static/ffmpeg-based-latency-solution-diagram.jpg" width="520">
48+
49+
## Important notice on latency measurement results
250

3-
Before reading this document, please read [FFmpeg Plugin](FFmpegPlugin.md) to familiarize yourself with FFmpeg usage, as this document provides an extension.
51+
> Please note the calculated average latency is highly dependent on the hardware configuration and CPU background load, and cannot be treated as an absolute value. The provided solution can only be used for comparing the latency in different Mesh configurations and video streaming parameters, as well as latency stability checks.
452
5-
**Note:** Currently, latency measurement is available only for video transport.
653

754
## Build and install steps
855

9-
1. Please follow build and install steps from [FFmpeg Plugin](FFmpegPlugin.md). with one exception, FFmpeg configuration tool requires additional parameters.
56+
> It is assumed that Media Communications Mesh is installed on the Tx and Rx host machines according to [Setup Guide](SetupGuid.md).
57+
58+
If [FFmpeg Plugin](FFmpegPlugin.md) was installed earlier, remove its directory before proceeding with the following.
59+
60+
1. Clone the FFmpeg 7.0 repository and apply patches.
61+
1062
```bash
11-
./configure-ffmpeg.sh ${FFMPEG_VER} --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig
63+
./clone-and-patch-ffmpeg.sh
1264
```
13-
## Time synchronization between hosts
1465

15-
__host-1 Controller clock__
16-
```bash
17-
sudo ptp4l -i <network_interface_1> -m 2
18-
sudo phc2sys -a -r -r -m
19-
```
20-
__host-2 Worker clock__
21-
```bash
22-
sudo ptp4l -i <network_interface_2> -m 2 -s
23-
sudo phc2sys -a -r
24-
```
25-
*Please note that `network_interface_1` and `network_interface_2` have to be physically connected to the same network
66+
1. Run the FFmpeg configuration tool with special features enabled
67+
68+
```bash
69+
./configure-ffmpeg.sh 7.0 --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig
70+
```
71+
72+
1. Build and install FFmpeg with the Media Communications Mesh FFmpeg plugin
73+
74+
```bash
75+
./build-ffmpeg.sh
76+
```
77+
78+
1. Install Tesseract OCR
79+
```bash
80+
apt install tesseract-ocr
81+
```
82+
1. Install Python packages
83+
```bash
84+
pip install opencv-python~=4.11.0 pytesseract~=0.3.13 matplotlib~=3.10.3
85+
```
86+
87+
1. Setup time synchronization on host machines
88+
89+
> Make sure `network_interface_1` and `network_interface_2` are connected to the same network.
2690
91+
* __host-1 Controller clock__
92+
```bash
93+
sudo ptp4l -i <network_interface_1> -m 2
94+
sudo phc2sys -a -r -r -m
95+
```
2796

28-
## Example – Run video transmission between 2 FFmpeg instances on the same host
97+
* __host-2 Worker clock__
98+
```bash
99+
sudo ptp4l -i <network_interface_2> -m 2 -s
100+
sudo phc2sys -a -r
101+
```
29102

30-
This example demonstrates sending a video file from the 1st FFmpeg instance to the 2nd FFmpeg instance via Media Communications Mesh on the same host, with timestamps to measure end-to-end latency using the `drawtext` feature in FFmpeg. The `drawtext` filter allows you to overlay timestamps directly onto the video stream, providing a visual representation of latency as the video is processed and transmitted between instances/hosts.
103+
## Example – Measuring transmission latency between two FFmpeg instances on the same host
31104

32-
### Run Mesh Agent
105+
This example demonstrates sending a video file from the 1st FFmpeg instance to the 2nd FFmpeg instance via Media Communications Mesh on the same host, and then calculation of transmission latency from the recorded video.
106+
107+
108+
1. Run Mesh Agent
33109
```bash
34110
mesh-agent
35111
```
36112

37-
### Run Media Proxy
113+
1. Run Media Proxy
38114

39115
```bash
40116
sudo media_proxy \
@@ -45,7 +121,7 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
45121
-t 8002
46122
```
47123

48-
### Receiver side setup
124+
1. Start the Receiver side FFmpeg instance
49125

50126
```bash
51127
sudo MCM_MEDIA_PROXY_PORT=8002 ffmpeg \
@@ -56,17 +132,17 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
56132
-pixel_format yuv422p10le \
57133
-i - \
58134
-vf \
59-
"drawtext=fontsize=50: \
135+
"drawtext=fontsize=40: \
60136
text='Rx timestamp %{localtime\\:%H\\\\\:%M\\\\\:%S\\\\\:%3N}': \
61137
x=10: y=70: fontcolor=white: box=1: boxcolor=black: boxborderw=10" \
62138
-vcodec mpeg4 -qscale:v 3 recv.mp4
63139
```
64-
### Sender side setup
140+
1. Start the Sender side FFmpeg instance
65141

66142
```bash
67143
sudo MCM_MEDIA_PROXY_PORT=8002 ffmpeg -i <video-file-path> \
68144
-vf \
69-
"drawtext=fontsize=50: \
145+
"drawtext=fontsize=40: \
70146
text='Tx timestamp %{localtime\\:%H\\\\\:%M\\\\\:%S\\\\\:%3N}': \
71147
x=10: y=10: fontcolor=white: box=1: boxcolor=black: boxborderw=10" \
72148
-f mcm \
@@ -76,38 +152,60 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
76152
-pixel_format yuv422p10le -
77153
```
78154

79-
When sending a raw video file that lack metadata, you must explicitly provide FFmpeg
80-
with the necessary video frame details. This includes specifying the format
81-
`-f rawvideo`, pixel format `-pix_fmt`, and resolution `-s WxH`. Example:
155+
When sending a raw video file, e.g. of the YUV format, you have to explicitly specify the file format `-f rawvideo`, the pixel format `-pix_fmt`, and the video resolution `-s WxH`:
82156

83157
```bash
84158
ffmpeg -f rawvideo -pix_fmt yuv422p10le -s 1920x1080 -i <video-file-path> ...
85159
```
86-
To get meaningful latency measurement it is also recommended to provide rate `-readrate` at which FFmpeg will read frames from file. Example:
160+
161+
It is also recommended to provide the read rate `-readrate` at which FFmpeg will read frames from the file:
162+
87163
```bash
88164
ffmpeg -f rawvideo -readrate 2.4 -pix_fmt yuv422p10le -s 1920x1080 -i <video-file-path> ...
89165
```
90-
Please note that `-readrate` value have to be calculated based on `-frame_rate` parameter, simple equation is as follow:
91-
`readrate = frame_rate / 25`, for example:
166+
167+
The `-readrate` value is calculated from the `-frame_rate` parameter value using the following equation: $readrate=framerate\div25$. Use the pre-calculated values from the table below.
168+
92169
| frame_rate | readrate |
93170
|------------|-------------------|
94171
| 25 | 25 / 25 = 1 |
95172
| 50 | 50 / 25 = 2 |
96173
| 60 | 60 / 25 = 2.4 |
97174

175+
1. Run the script against the recorded MPEG file. The first argument is the input video file path. The second argument is the optional latency diagram JPEG file path to be generated.
176+
177+
```bash
178+
python text-detection.py recv.mp4 recv-latency.jpg
179+
```
180+
181+
Console output
182+
```bash
183+
...
184+
Processing Frame: 235
185+
Processing Frame: 236
186+
Processing Frame: 237
187+
Processing Frame: 238
188+
Processing Frame: 239
189+
Processing Frame: 240
190+
Saving the latency chart to: recv-latency.jpg
191+
File: recv.mp4 | Last modified: 2025-06-02 13:49:54 UTC
192+
Resolution: 640x360 | FPS: 25.00
193+
Average End-to-End Latency: 564.61 ms
194+
```
195+
196+
See the [Sample latency diagram](#sample-latency-diagram).
197+
98198

99-
## Example – Run video transmission between 2 FFmpeg instances on different hosts
199+
## Example – Measuring transmission latency between two FFmpeg instances on different hosts
100200

101-
This example demonstrates sending a video file from the 1st FFmpeg instance to the 2nd FFmpeg instance via Media Communications Mesh, with timestamps to measure end-to-end latency using the `drawtext` feature in FFmpeg. The `drawtext` filter allows you to overlay timestamps directly onto the video stream, providing a visual representation of latency as the video is processed and transmitted between instances/hosts.
201+
This example demonstrates sending a video file from the 1st FFmpeg instance to the 2nd FFmpeg instance via Media Communications Mesh on the same host, and then calculation of transmission latency from the recorded video.
102202

103-
### Run Mesh Agent
203+
1. Run Mesh Agent
104204
```bash
105205
mesh-agent
106206
```
107207

108-
### Receiver side setup
109-
110-
1. Start Media Proxy
208+
1. Start Media Proxy on the Receiver host machine
111209

112210
```bash
113211
sudo media_proxy \
@@ -118,7 +216,7 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
118216
-t 8002
119217
```
120218

121-
1. Start FFmpeg to receive frames from Media Communications Mesh and save received data on hard drive
219+
1. Start the Receiver side FFmpeg instance
122220

123221
```bash
124222
sudo MCM_MEDIA_PROXY_PORT=8002 ffmpeg \
@@ -132,15 +230,13 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
132230
-pixel_format yuv422p10le \
133231
-i - \
134232
-vf \
135-
"drawtext=fontsize=50: \
233+
"drawtext=fontsize=40: \
136234
text='Rx timestamp %{localtime\\:%H\\\\\:%M\\\\\:%S\\\\\:%3N}': \
137235
x=10: y=70: fontcolor=white: box=1: boxcolor=black: boxborderw=10" \
138236
-vcodec mpeg4 -qscale:v 3 recv.mp4
139237
```
140238

141-
### Sender side setup
142-
143-
1. Start Media Proxy
239+
1. Start Media Proxy on the Sender host machine
144240

145241
```bash
146242
sudo media_proxy \
@@ -151,12 +247,12 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
151247
-t 8001
152248
```
153249

154-
2. Start FFmpeg to stream a video file to the receiver via Media Communications Mesh
250+
1. Start the Sender side FFmpeg instance
155251

156252
```bash
157253
sudo MCM_MEDIA_PROXY_PORT=8001 ffmpeg -i <video-file-path> \
158254
-vf \
159-
"drawtext=fontsize=50: \
255+
"drawtext=fontsize=40: \
160256
text='Tx timestamp %{localtime\\:%H\\\\\:%M\\\\\:%S\\\\\:%3N}': \
161257
x=10: y=10: fontcolor=white: box=1: boxcolor=black: boxborderw=10" \
162258
-f mcm \
@@ -169,25 +265,28 @@ This example demonstrates sending a video file from the 1st FFmpeg instance to t
169265
-pixel_format yuv422p10le -
170266
```
171267

172-
## Stream postprocessing
173-
The generated stream can be analyzed manually, but it is a long process. To accelerate it, there is a small sript written in Python that automatically extracts and plots latency.
268+
1. Run the script against the recorded MPEG file. The first argument is the input video file path. The second argument is the optional latency diagram JPEG file path to be generated.
174269

175-
1. install Install Tesseract OCR
176270
```bash
177-
apt install tesseract-ocr
178-
```
179-
2. Install Python packages
180-
```bash
181-
pip install opencv-python~=4.11.0 pytesseract~=0.3.13 matplotlib~=3.10.3
182-
```
183-
2. Postprocess stream with command
184-
```bash
185-
python text-detection.py <input_video_file> <optional_output_image_name>
271+
python text-detection.py recv.mp4 recv-latency.jpg
186272
```
273+
274+
Console output
187275
```bash
188-
python text-detection.py recv.mp4 latency.jpg
276+
...
277+
Processing Frame: 235
278+
Processing Frame: 236
279+
Processing Frame: 237
280+
Processing Frame: 238
281+
Processing Frame: 239
282+
Processing Frame: 240
283+
Saving the latency chart to: recv-latency.jpg
284+
File: recv.mp4 | Last modified: 2025-06-02 13:49:54 UTC
285+
Resolution: 640x360 | FPS: 25.00
286+
Average End-to-End Latency: 564.61 ms
189287
```
190-
When preparing FFmpeg command if you change parameters of `drawtext` filter, especialy `fontsize`, `x`, `y` or `text`, you have to adjust script __text-detection.py__ too, please refer to function `extract_text_from_region(image, x, y, font_size, length)`
288+
289+
See the [Sample latency diagram](#sample-latency-diagram).
191290

192291

193292
<!-- References -->
140 KB
Loading

0 commit comments

Comments
 (0)