#!/bin/sh # Host test of apps/chapters.c (the M4B chapter reader) against audiobooks # made with ffmpeg: Nero 'chpl' chapters, QuickTime chapter tracks only, # the index (moov) at the end or the start, UTF-8 / long / empty titles, # and more chapters than the 255 kept. Built with AddressSanitizer and # UBSan; the shim/ headers stand in for the Rockbox ones. # # tests/chapters/run.sh [ROCKBOX_TREE] default: build/rockbox (scripts/build.sh) # # Needs: gcc, ffmpeg, python3. set -eu HERE=$(cd "$(dirname "$0")" && pwd) REPO=$(cd "$HERE/../.." && pwd) TREE=${1:-$REPO/build/rockbox} [ -f "$TREE/apps/chapters.c" ] || { echo "no $TREE/apps/chapters.c: run scripts/build.sh first, or name a patched tree" >&2 exit 1 } T=$(mktemp -d) trap 'rm -rf "$T"' EXIT # chapters.c includes by quotes: compile copies next to the shims mkdir -p "$T/src" "$T/media" cp "$HERE"/shim/*.h "$TREE/apps/chapters.c" "$TREE/apps/chapters.h" "$T/src/" gcc -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \ -Wall -Wextra -Wno-unused-parameter -I"$T/src" \ -o "$T/chtest" "$HERE/test.c" "$T/src/chapters.c" cd "$T/media" python3 -I - <<'EOF' def meta(name, chapters, total_ms): with open(name, 'w') as f: f.write(";FFMETADATA1\ntitle=Test Book\nartist=Some Author\n") for i, (start, title) in enumerate(chapters): end = chapters[i + 1][0] if i + 1 < len(chapters) else total_ms f.write(f"[CHAPTER]\nTIMEBASE=1/1000\nSTART={start}\nEND={end}\ntitle={title}\n") meta('basic.txt', [(0, 'Opening Credits'), (65000, 'Chapter One: The Beginning'), (190500, 'Chäpter Twö – ünïcode ✓ 日本語'), (400000, 'X' * 200), (500000, '')], 600000) meta('many.txt', [(i * 2000, f'Part {i + 1}') for i in range(300)], 600000) EOF enc() { # metadata output [movflags] ffmpeg -nostdin -loglevel error -f lavfi -i "sine=frequency=440:duration=600" \ -i "$1" -map_metadata 1 -map_chapters 1 -c:a aac -b:a 32k -ac 1 \ ${3:+-movflags "$3"} -y "$2" } for f in basic many; do enc $f.txt $f.m4b # chpl + chapter track enc $f.txt $f-qt.m4b +disable_chpl # chapter track only enc $f.txt $f-qt-fast.m4b +faststart+disable_chpl # ... index first done ffmpeg -nostdin -loglevel error -f lavfi -i "sine=frequency=440:duration=60" \ -c:a aac -b:a 32k -y nochap.m4b "$T/chtest" basic.m4b basic-qt.m4b basic-qt-fast.m4b many.m4b many-qt.m4b \ many-qt-fast.m4b nochap.m4b /nonexistent.m4b > "$T/out.txt" cat "$T/out.txt" # expected results fail=0 expect() { # file-section pattern if ! awk -v f="== $1:" -v p="$2" ' index($0, "== ") == 1 { on = (index($0, f) == 1) } on && index($0, p) { found = 1 } END { exit !found }' "$T/out.txt"; then echo "FAIL: $1: expected \"$2\""; fail=1 fi } for f in basic.m4b basic-qt.m4b basic-qt-fast.m4b; do expect $f "5 chapters" expect $f "65000 ms [Chapter One: The Beginning]" expect $f "190500 ms [Chäpter Twö – ünïcode ✓ 日本語]" expect $f "500000 ms [Chapter 5]" expect $f "prev 1s into ch2 -> 0" expect $f "prev 10s into ch2 -> 65000" expect $f "next from ch2 -> 190500" expect $f "next from last -> nomore=1" expect $f "next title [XXXX" done for f in many.m4b many-qt.m4b many-qt-fast.m4b; do expect $f "255 chapters" expect $f "508000 ms [Part 255]" done expect nochap.m4b "0 chapters" expect nochap.m4b "prevs=1 next->nexts=1 prev@10s->seek 0" expect /nonexistent.m4b "0 chapters" [ $fail = 0 ] && echo "PASS" || { echo "FAILED"; exit 1; }