Menu
andipanda.dev

CaRadio v2: investigating the causes of video stream delays

In the previous article, I described the transition from C++ and SDL to Kotlin and the direct rendering of decoded video to an Android Surface.

These changes produced a positive result. The image became noticeably smoother, and using CarPlay on the head unit became quite comfortable.

However, intermittent video stream delays still occurred, causing some discomfort. Initially, I assumed that the cause was still related to the software implementation, so I continued looking for possible bugs.

As it turned out, further code optimization was not necessary to resolve this problem.

Video stream transmission diagnostics

The first step was to add additional diagnostic mechanisms to CaRaBack and CaRadio_Android.

On the CaRaBack side, I monitored:

  • the number of received and transmitted video frames;
  • the transmission queue size;
  • the time required to send data through the socket;
  • TCP parameters available through TCP_INFO.

On the Android application side, I measured packet reception and processing times, queue states, and intervals between incoming data.

Whenever delays occurred, the following behavior was observed:

  • TCP transmission times increased to 350–500 ms;
  • the video data queue on the CaRaBack side grew to several megabytes;
  • RTT increased;
  • the TCP congestion window decreased;
  • the number of TCP retransmissions increased.

Meanwhile, processing received data on the Android side generally took less than 20 ms.

These results allowed me to shift my attention from application and video decoder performance to the network connection.

Testing Wi-Fi with iperf3

To rule out the influence of the software, I installed iperf3, compiled for the armeabi-v7a architecture, on the head unit.

The server was started on the head unit:

/data/local/tmp/iperf3 -s

The tests were run on a computer through WSL.

TCP test without a bandwidth limit:

iperf3 -c 192.168.1.116 -t 300 -i 1

TCP test with a bandwidth limit of 5 Mbps:

iperf3 -c 192.168.1.116 -b 5M -t 120 -i 1

Test in the reverse direction:

iperf3 -c 192.168.1.116 -b 5M -t 120 -i 1 -R

The main objective was not to achieve the highest possible data transfer rate, but to determine whether the connection could consistently transmit data at a relatively low rate.

For video streaming, connection stability is much more important than high average throughput.

As a result, I was able to reproduce the problem without involving CaRaBack, CaRadio_Android, CarPlay, or the video decoder.

Checking Wi-Fi power saving

One possible source of the problem was the Wi-Fi power-saving mode.

Its status could be checked using the following command:

iw dev wlan0 get power_save

To disable power saving, I used:

iw dev wlan0 set power_save off

I then repeated the TCP test with a bandwidth limit of 5 Mbps and a duration of 120 seconds.

Parameter Power Save ON Power Save OFF
Sender throughput, Mbps 3.90 3.66
Receiver throughput, Mbps 3.86 3.63
TCP retransmissions 340 497

Disabling power saving did not resolve the problem. Moreover, the results of this particular test were worse.

I was unable to establish a clear relationship between power-saving mode and connection stability, so I decided to leave Power Save enabled.

An unexpected discovery about the Wi-Fi antenna

After several tests, my attention was drawn to the antenna that came with the head unit. It was connected to the Wi-Fi connector, while the head unit also had two identical connectors intended for 4G and GPS antennas.

As a reminder, the head unit was purchased second-hand, so the original purpose of this antenna remained unknown.

The antenna was quite short, which made me question whether it was suitable for the Wi-Fi operating frequency range.

Among my spare components, I found a Wi-Fi antenna from a Mini PC and installed it as a replacement.

The results of subsequent testing were noticeably better.

Parameter Supplied antenna Mini PC antenna
Average throughput, Mbps 3.90 5.01
TCP retransmissions 340 147

With the same bandwidth limit of 5 Mbps:

  • average throughput increased by 28.5%;
  • the number of TCP retransmissions decreased by 56.8%.

Later, out of curiosity, I disassembled the supplied antenna and measured the length of its radiating element. Based on the measurement, I concluded that the antenna supplied with the head unit was most likely intended for 4G rather than Wi-Fi.

After replacing the antenna, I went for a test drive.

The difference was noticeable not only in the iperf3 results but also when using CarPlay. The video stream became more stable, although the intermittent delays did not disappear completely.

That concluded the first evening of investigation.

Periodic drops in throughput

The following evening, I continued testing the network connection.

During TCP tests without a bandwidth limit, average throughput ranged from 16 to 25 Mbps.

This was sufficient for transmitting the CarPlay video stream. However, analyzing the results of individual one-second intervals revealed an interesting pattern.

Test Duration, s Throughput, Mbps TCP retransmissions One-second intervals without data transmission
1 215 18.0 447 19
2 88 19.4 160 1
3 300 20.5 533 12
4 224 16.2 629 21
5 300 24.8 204 0

Despite the relatively high average throughput, the connection periodically almost stopped transmitting data.

These drops lasted approximately 18–20 seconds, and in some tests, they occurred roughly every 100 seconds.

These drops were the main problem affecting video stream transmission.

Is switching to UDP worthwhile?

During the investigation, I also considered changing the IPC architecture and switching from TCP to UDP for video data transmission.

Unlike TCP, UDP does not require retransmission of lost packets at the transport layer. This can potentially prevent delays from accumulating over an unstable connection.

To investigate this possibility, I performed a UDP test at 10 Mbps:

iperf3 -c 192.168.1.116 -u -b 10M -l 1400 -t 300 -i 1

However, the results showed that the problem also occurred when using UDP.

During one of the characteristic throughput drops, the following values were recorded:

Parameter Value
Target bitrate 10 Mbps
Duration of the drop 19 s
Maximum packet loss per second approximately 68%
Minimum receive throughput 3.66 Mbps

The transmission rate remained stable on the sender side, while significant packet loss was observed on the head unit side.

Switching to UDP could therefore have changed how the problem manifested itself, but it would not have eliminated the underlying cause of the connection instability.

I therefore decided not to change the IPC architecture and to continue using TCP.

Unexpected Bluetooth activity

Alongside the network tests, I started recording Android system logs.

Before starting another test, the log was cleared and recording was started:

logcat -c
logcat -v threadtime > /data/local/tmp/wifi-test.log &
LOGCAT_PID=$!

After the test was completed, recording was stopped:

kill $LOGCAT_PID

The resulting log was copied to the computer for further analysis:

adb pull /data/local/tmp/wifi-test.log .

During the analysis, I discovered periodic activity from the blink processes associated with the head unit's built-in Bluetooth implementation.

Although I had previously disabled the built-in Android Bluetooth applications, the native part of the software continued running.

Moreover, it periodically attempted to establish an HFP connection with a previously paired device.

What made this particularly interesting was that I had never connected my phone to this head unit via Bluetooth.

The connection attempts failed after approximately 18 seconds and were repeated roughly every 99–100 seconds.

These values almost exactly matched the duration and recurrence interval of the Wi-Fi throughput drops.

Wi-Fi and Bluetooth in this head unit are implemented using a single combination chip and share the same frequency band when operating at 2.4 GHz.

This suggested that unwanted Bluetooth activity could be affecting the stability of data transmission over Wi-Fi.

Disabling the built-in Bluetooth implementation

Since CaRaBack independently handles communication with the iPhone, I did not need the head unit's built-in Bluetooth implementation for CarPlay to function.

I therefore decided to disable it at two levels: the Android applications and the head unit manufacturer's native service.

All changes were made through ADB with root privileges.

Disabling Android applications

The following commands were used to disable the built-in Bluetooth applications:

pm disable-user --user 0 android.car.service.bt
pm disable-user --user 0 android.car.app.bt
pm disable-user --user 0 com.android.bluetooth

These changes persist after rebooting the head unit.

During diagnostics, I also used commands to temporarily stop the services:

svc bluetooth disable
stop bt_sdk
stop bluetooth-1-0

However, the native services could start again after a reboot.

Disabling automatic startup of bt_sdk

The bt_sdk service, which launches the blink processes, was defined in the following file:

/system/etc/init/ziqi.rc

To make changes, the filesystem was remounted in read-write mode:

mount -o remount,rw /

Before editing, I created a backup of the file:

cp -p /system/etc/init/ziqi.rc /data/local/tmp/ziqi.rc.bak

Two changes had to be made to the configuration.

The first was to comment out the explicit service startup command:

# start bt_sdk

The second was to add the disabled option to the service definition itself:

service bt_sdk /system/bin/bt_sdk
    disabled
    class car
    user root
    group root
    seclabel u:object_r:su_exec:s0

The following commands were used to make these changes:

sed -i 's/^    start bt_sdk$/    # start bt_sdk/' /system/etc/init/ziqi.rc

sed -i '/^service bt_sdk \/system\/bin\/bt_sdk$/a\    disabled' /system/etc/init/ziqi.rc

After verifying the changes, the head unit was rebooted.

To check the service status and the presence of blink processes, I used:

getprop init.svc.bt_sdk
ps -A | grep blink

Both commands returned empty results. The blink processes were no longer being started.

I did not permanently disable the standard Bluetooth HAL service at the init level. The kernel drivers also remained unchanged.

These commands apply to the specific firmware of my Vordon VW-910S head unit and are not universal instructions for all Android head units.

Further testing

During experiments with disabling Bluetooth services, I obtained the following results from a five-minute TCP test.

Parameter Before improvement After improvement
Duration, s 300 300
Average throughput, Mbps 20.5 24.8
TCP retransmissions 533 204
One-second intervals without data transmission 12 0

After permanently disabling the automatic startup of bt_sdk, additional TCP and UDP tests were also performed.

UDP test results:

Parameter Before disabling blink After disabling blink
Target bitrate, Mbps 10 10
Test duration, s 173 40
Maximum packet loss per second approximately 68% approximately 3.5%
Prolonged throughput drops 19 s Not detected

The results of further testing showed a noticeable improvement in network connection stability. However, my main criterion remained CarPlay performance under real-world operating conditions.

Final configuration

After completing the experiments, the head unit configuration was as follows:

Parameter Status
Wi-Fi antenna Replaced
Wi-Fi frequency band 2.4 GHz
Channel frequency during measurements 2462 MHz
Link speed 65 Mbps
RSSI during measurements approximately −49 dBm
Wi-Fi Power Save Enabled
Background Wi-Fi scanning Disabled
Wi-Fi Wakeup Disabled
Built-in Bluetooth applications Disabled
Automatic startup of bt_sdk Disabled
blink processes Not running
Bluetooth HAL Unchanged
IPC protocol TCP

Road testing

After making the changes, I went for another test drive.

This time, the result fully met my expectations.

Even when using the satellite map view with a large amount of detail, the image remained smooth, and the interface responded well to user input.

I no longer observed any visually noticeable video stream delays during the drive.

Over the course of two evenings, I managed to resolve a problem that I had initially attempted to eliminate through further software optimization.

The first step was replacing the antenna, which noticeably improved connection quality. The second was disabling unwanted activity from the built-in Bluetooth implementation.

No changes to the IPC architecture or the video stream transmission implementation were required.

Conclusion

The transition to Kotlin significantly reduced the load on the head unit's hardware platform. Further investigation showed that the remaining video stream delays were no longer related to application performance, but rather to network connection instability.

Replacing the antenna and disabling the built-in Bluetooth implementation produced the desired practical result, although the exact mechanism behind the network throughput drops at the combination chip and driver level remained outside the scope of this investigation.

At this point, I decided to conclude the optimization stage.

CaRadio_Android had reached the state I originally wanted to achieve: the ability to comfortably use CarPlay on a relatively inexpensive Android head unit in combination with my own CaRaBack backend.

The first working version of the system can now be considered complete and ready for everyday use.