How to Monitor HLS Video Streams in Connected Cars Using NGINX and Lua
This article explains a lightweight, loosely‑coupled solution for real‑time monitoring of HLS video playback in vehicle IoT, detailing how to embed device and session identifiers in HLS requests, modify M3U8 files, and collect statistics with NGINX Lua scripts.
1. Vehicle‑IoT Video Monitoring Basic Framework
2. HLS Protocol Overview
HLS (HTTP Live Streaming) is an Apple‑proposed HTTP‑based streaming protocol that splits video into small TS segments and provides an M3U8 index file. The client repeatedly downloads the M3U8 file and then fetches the referenced TS files for playback.
3. Link‑Management Goals and Challenges
Link Keep‑Alive : Detect whether a 4G‑connected device is actively streaming; stop the stream when no user is watching to save bandwidth.
Online User Count : Real‑time tally of clients playing via HLS for dynamic scaling.
Traffic Statistics : Measure per‑user or per‑organization data consumption.
Stream Speed Monitoring : Show current media‑transfer speed on the client UI.
Challenges include the stateless nature of HTTP (making it hard to associate requests with devices) and the inability of Safari‑based players to expose download metrics via APIs.
4. Overall Approach
Embed a unique deviceID in the HLS resource path and a unique sessionID as a query parameter. Modify the returned M3U8 file so that TS segment URLs also contain sessionID. This enables the server to distinguish devices and users without changing the player or the HLS packaging logic.
Standard HLS request:
Modified HLS request (deviceID and sessionID added):
5. Detailed Solution
Return an HLS URL that includes deviceID and sessionID (e.g., https://ip:port/appName/deviceID.m3u8?sessionID=XXX).
Ensure TS segment requests also carry sessionID by rewriting the M3U8 content before it is sent to the client.
Use an NGINX Lua script to intercept each TS download request, extract deviceID, sessionID, timestamp, and Content‑Length, then publish the data to Redis via a publish/subscribe channel.
Because the Content‑Length header is only available in the header_filter_by_lua phase, schedule a zero‑delay ngx.timer task to hand the data to a worker thread that can safely use the Cosocket API to communicate with Redis.
The statistics service consumes the Redis stream, aggregates online user counts, traffic usage, and stream speed, and provides query APIs for clients to retrieve per‑device or per‑session metrics.
6. Summary
By adding deviceID and sessionID to HLS request URLs and rewriting M3U8 files on the fly, the platform can perform link keep‑alive, online‑user counting, traffic accounting, and speed monitoring without modifying the player or the underlying HLS packaging process, achieving a decoupled and scalable link‑management solution.
G7 EasyFlow Tech Circle
Official G7 EasyFlow tech channel! All the hardcore tech, cutting‑edge innovations, and practical sharing you want are right here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
