RTMP Streaming
The ASG Client supports live video streaming via RTMP (Real-Time Messaging Protocol), allowing real-time video transmission from the glasses to remote servers.
Overview
RTMP streaming enables:
- Live video broadcasting from glasses
- Real-time video analysis by apps
- Remote assistance scenarios
- Video recording to cloud servers
RTMP Commands
The system uses four main commands for RTMP control:
1. start_rtmp_stream
Initiates an RTMP stream to a specified URL.
Command Structure:
{
"type": "start_rtmp_stream",
"rtmpUrl": "rtmp://server.com/live/stream-key",
"streamId": "unique-stream-id",
"video": {
"width": 1280,
"height": 720,
"bitrate": 2000000
}
}
Requirements:
- Active WiFi connection
- Valid RTMP URL
- Sufficient bandwidth
Response:
{
"type": "rtmp_stream_status",
"status": "initializing",
"streamId": "unique-stream-id"
}
2. stop_rtmp_stream
Terminates the active RTMP stream.
Command Structure:
{
"type": "stop_rtmp_stream",
"streamId": "unique-stream-id"
}
Response:
{
"type": "rtmp_stream_status",
"status": "stopped",
"streamId": "unique-stream-id"
}
3. keep_rtmp_stream_alive
Keep-alive mechanism to prevent stream timeout. Must be sent at least every 60 seconds.
Command Structure:
{
"type": "keep_rtmp_stream_alive",
"streamId": "unique-stream-id",
"ackId": "unique-ack-id",
"timestamp": "2024-01-01T12:00:00Z"
}
ACK Response (from glasses):
{
"type": "keep_alive_ack",
"streamId": "unique-stream-id",
"ackId": "unique-ack-id",
"timestamp": 1234567890
}
4. get_rtmp_status
Queries the current streaming status.
Command Structure:
{
"type": "get_rtmp_status"
}
Response:
{
"type": "rtmp_stream_status",
"status": "active", // or "stopped", "error", etc.
"streamId": "unique-stream-id",
"stats": {
"bitrate": 1950000,
"fps": 30,
"droppedFrames": 5
}
}
Stream Lifecycle
Starting a Stream
- Request received: Phone sends
start_rtmp_stream
via BLE - WiFi check: Verify WiFi connection is active
- Stream init: Initialize RTMP encoder and connection
- Start streaming: Begin sending video data
- Status updates: Send status back to phone/cloud
Keep-Alive Mechanism
The stream has a 60-second timeout that requires periodic keep-alive messages:
- Cloud sends keep-alive every 15 seconds with unique
ackId
- Glasses reset timeout and respond with ACK containing same
ackId
- If no keep-alive for 60 seconds, stream automatically stops
- If 3 ACKs are missed, cloud marks connection as degraded
Cloud → Glasses: keep_rtmp_stream_alive (every 15s)
Glasses → Cloud: keep_alive_ack (immediate response)
Stopping a Stream
Streams can stop in three ways:
- Explicit stop: Via
stop_rtmp_stream
command - Timeout: No keep-alive received for 60 seconds
- Error: Network failure, encoder error, etc.
Implementation Details
RtmpStreamingService
The main service handling RTMP streaming:
// Start streaming
RtmpStreamingService.startStreaming(context, rtmpUrl);
// Stop streaming
RtmpStreamingService.stopStreaming(context);
// Check status
boolean isStreaming = RtmpStreamingService.isStreaming();
Stream Timeout Handling
// In AsgClientService
case "keep_rtmp_stream_alive":
String streamId = dataToProcess.optString("streamId", "");
String ackId = dataToProcess.optString("ackId", "");
// Reset the 60-second timeout
RtmpStreamingService.resetStreamTimeout(streamId);
// Send ACK back
sendKeepAliveAck(streamId, ackId);
break;
Status Messages
The glasses send various status updates during streaming:
initializing
- Stream setup in progressactive
- Streaming successfullyreconnecting
- Attempting to reconnect after failureerror
- Stream failed with error detailsstopped
- Stream terminatedtimeout
- Stream stopped due to keep-alive timeout
Network Requirements
Bandwidth
- Minimum: 1 Mbps upload
- Recommended: 2-3 Mbps upload
- Adapts bitrate based on connection
WiFi Stability
- Requires stable WiFi connection
- Automatic reconnection on brief disconnects
- Stops on extended network loss
Error Handling
Common Errors
-
No WiFi Connection
{
"status": "error",
"error": "no_wifi_connection"
} -
Invalid RTMP URL
{
"status": "error",
"error": "invalid_rtmp_url"
} -
Stream Timeout
{
"status": "error",
"errorDetails": "Stream timed out - no keep-alive from cloud"
}
Recovery Mechanisms
- Auto-reconnect: Attempts reconnection on temporary failures
- Backoff strategy: Increasing delays between reconnection attempts
- Clean shutdown: Proper resource cleanup on errors
Best Practices
- Always send keep-alives at 15-second intervals
- Monitor ACK responses to detect connection issues
- Handle status updates to show stream state in UI
- Implement proper cleanup when app disconnects
- Check WiFi before starting streams
- Use unique streamIds for tracking
Debugging
Log Filters
# All RTMP logs
adb logcat | grep -E "RtmpStreaming|RTMP"
# Keep-alive activity
adb logcat | grep "keep_rtmp_stream_alive\|keep_alive_ack"
# Stream status
adb logcat | grep "rtmp_stream_status"
Common Issues
- Stream stops after ~5 minutes: Check keep-alive implementation
- ACKs not received: Verify BLE communication both ways
- Poor quality: Check WiFi signal strength and bandwidth
- Can't start stream: Ensure only one stream active at a time