Skip to content

Commit 26e1e1d

Browse files
authored
Add video streaming mode to TinyPilot logs (#1666)
Resolves #1490 Video demo: https://github.com/tiny-pilot/tinypilot/assets/6730025/0165c85d-f9fa-4389-b98d-44de90843f5a ### Notes 1. When there are active WebRTC consumers, I see this in the Janus logs: ``` Oct 30 15:27:57 tinypilot janus[1790]: Creating new session: 3545622190134115; 0xed914e28 Oct 30 15:27:57 tinypilot janus[1790]: Creating new handle in session 3545622190134115: 8464226001965214; 0xed914e28 0xefb019c8 Oct 30 15:27:57 tinypilot janus[1790]: == ustreamer/main -- Creating session 0xefb01650 ... Oct 30 15:27:57 tinypilot janus[1790]: == ustreamer/video -- Memsink opened; reading frames ... Oct 30 15:27:57 tinypilot janus[1790]: [8464226001965214] Creating ICE agent (ICE Full mode, controlling) Oct 30 15:27:57 tinypilot janus[1790]: == ustreamer/audio -- Detected host audio Oct 30 15:27:57 tinypilot janus[1790]: == ustreamer/audio -- Pipeline configured on 48000Hz; capturing ... Oct 30 15:27:58 tinypilot janus[1790]: [8464226001965214] The DTLS handshake has been completed ``` When there are no active WebRTC consumers, I see this in the Janus logs: ``` Oct 30 15:30:15 tinypilot janus[1790]: [WSS-0xed9005f8] Destroying WebSocket client Oct 30 15:30:15 tinypilot janus[1790]: Destroying session 3545622190134115; 0xed914e28 Oct 30 15:30:15 tinypilot janus[1790]: Detaching handle from ustreamer; 0xefb019c8 0xefb01650 0xefb019c8 (nil) Oct 30 15:30:15 tinypilot janus[1790]: [8464226001965214] WebRTC resources freed; 0xefb019c8 0xed914e28 Oct 30 15:30:15 tinypilot janus[1790]: == ustreamer/main -- Removing session 0xefb01650 ... Oct 30 15:30:15 tinypilot janus[1790]: == ustreamer/audio -- Pipeline closed Oct 30 15:30:15 tinypilot janus[1790]: == ustreamer/video -- Memsink closed Oct 30 15:30:16 tinypilot janus[1790]: == ustreamer/video -- No active watchers, memsink disconnected ``` So based on the above log examples, [H264 mode is active](https://github.com/tiny-pilot/tinypilot/blob/dda4d8b161eaac91db7f05a4e6bc14510b99f415/debian-pkg/opt/tinypilot-privileged/scripts/collect-debug-logs#L171-L173) when the last Janus log that contains `ustreamer/video`, also contains `Memsink opened; reading frames`. For example: ``` Oct 30 15:27:57 tinypilot janus[1790]: == ustreamer/video -- Memsink opened; reading frames ... ``` Otherwise, it is assumed that MJPEG is active. <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1666"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a>
1 parent 959a5a8 commit 26e1e1d

4 files changed

Lines changed: 63 additions & 0 deletions

File tree

app/cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from flask import Blueprint
2+
3+
import db.settings
4+
5+
# For invoking the commands on the CLI you need to specify the FLASK_APP env
6+
# variable containing the path to the main file. E.g.: FLASK_APP=app/main.py
7+
cli_blueprint = Blueprint('cli', __name__)
8+
9+
10+
@cli_blueprint.cli.command('streaming-mode')
11+
def streaming_mode():
12+
"""Prints TinyPilot's preferred video streaming mode, either H264 or MJPEG.
13+
14+
Note: this doesn't represent the currently active video streaming mode
15+
because H264 can fail and fall back to MJPEG.
16+
17+
Example of invocation:
18+
flask cli streaming-mode
19+
"""
20+
print(db.settings.Settings().get_streaming_mode().value)

app/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# app-wide logger class before any other module loads it.
1212
import log
1313
import api
14+
import cli
1415
import json_response
1516
import license_notice
1617
import secret_key
@@ -49,6 +50,7 @@
4950
app.register_blueprint(api.api_blueprint)
5051
app.register_blueprint(license_notice.blueprint)
5152
app.register_blueprint(views.views_blueprint)
53+
app.register_blueprint(cli.cli_blueprint)
5254

5355

5456
@app.errorhandler(flask_wtf.csrf.CSRFError)

debian-pkg/opt/tinypilot-privileged/scripts/collect-debug-logs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ print_info "Checking for voltage issues..."
163163
printf "\n\n"
164164
} >> "${LOG_FILE}"
165165

166+
print_info "Checking TinyPilot streaming mode..."
167+
{
168+
echo "Streaming mode"
169+
echo "Selected mode: $(/opt/tinypilot/scripts/streaming-mode)"
170+
printf "Current mode: "
171+
# H264 mode is considered active when the last Janus video log line contains
172+
# "Memsink opened; reading frames".
173+
# https://github.com/tiny-pilot/ustreamer/blob/89fd83bfc187ed592e9582be077fa1a005098532/janus/src/plugin.c#L148
174+
if LAST_JANUS_VIDEO_LINE="$(journalctl --unit janus.service --quiet --reverse --lines 1 --grep 'ustreamer/video')" &&
175+
[[ "${LAST_JANUS_VIDEO_LINE}" =~ 'Memsink opened; reading frames' ]]; then
176+
printf "H264"
177+
else
178+
printf "MJPEG"
179+
fi
180+
printf "\n\n"
181+
} >> "${LOG_FILE}"
182+
166183
print_info "Checking TinyPilot settings..."
167184
{
168185
printf "TinyPilot settings.yml\n"

scripts/streaming-mode

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# Prints TinyPilot's preferred video streaming mode, either H264 or MJPEG.
4+
5+
# Exit on first failure.
6+
set -e
7+
8+
# Exit on unset variable.
9+
set -u
10+
11+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
12+
readonly SCRIPT_DIR
13+
cd "${SCRIPT_DIR}/.."
14+
. ./venv/bin/activate
15+
16+
# Create a dummy app settings file. The flask app needs a settings file to
17+
# start, but it's not necessary for this simple script, so we use a blank,
18+
# dummy version.
19+
APP_SETTINGS_FILE="$(mktemp)"
20+
readonly APP_SETTINGS_FILE
21+
export APP_SETTINGS_FILE
22+
23+
export FLASK_APP='app/main.py'
24+
flask cli streaming-mode

0 commit comments

Comments
 (0)