FFFF Add inline WebView media playback by bu5hm4nn · Pull Request #5 · bu5hm4nn/gossip · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,037 changes: 825 additions & 212 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[workspace]
members = [
"egui-webview",
"egui-webview/examples/feed",
"gossip-lib",
"gossip-bin",
"gossip-cache",
]
default-members = ["gossip-lib", "gossip-bin", "gossip-cache"]
resolver = "2"

[workspace.dependencies]
Expand Down Expand Up @@ -70,7 +73,3 @@ egui-winit = { git = "https://github.com/mikedilger/egui", rev = "20d9aaf91f3f79
egui_extras = { git = "https://github.com/mikedilger/egui", rev = "20d9aaf91f3f792c53ce252fd8bc8cc4bdc51f78" }
ecolor = { git = "https://github.com/mikedilger/egui", rev = "20d9aaf91f3f792c53ce252fd8bc8cc4bdc51f78" }
epaint = { git = "https://github.com/mikedilger/egui", rev = "20d9aaf91f3f792c53ce252fd8bc8cc4bdc51f78" }

[patch."https://github.com/mikedilger/egui-video"]
egui = { git = "https://github.com/mikedilger/egui", rev = "20d9aaf91f3f792c53ce252fd8bc8cc4bdc51f78" }
eframe = { git = "https://github.com/mikedilger/egui", rev = "20d9aaf91f3f792c53ce252fd8bc8cc4bdc51f78" }
23 changes: 14 additions & 9 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ if [ $? -ne 0 ] ; then
exit 1
fi

FFMPEG=$(ffmpeg --help >/dev/null 2>&1)
if [ $? -eq 0 ] ; then
FEATURE_FFMPEG=",video-ffmpeg"
else
FEATURE_FFMPEG=""
fi
# Apple Silicon has native video; other supported hosts need FFmpeg.
case "$(rustc -vV)" in
*"host: aarch64-apple-darwin"*) FEATURE_VIDEO=",egui-video" ;;
*)
if ffmpeg --help >/dev/null 2>&1; then
FEATURE_VIDEO=",egui-video"
else
FEATURE_VIDEO=""
fi
;;
esac

while true; do
read -p "Do you need to use locally installed certificates (usually no)? (y/n) " yn
Expand Down Expand Up @@ -43,13 +48,13 @@ while true; do
esac
done

FEATURES=$FEATURE_TLS$FEATURE_CJK$FEATURE_FFMPEG
FEATURES=$FEATURE_TLS$FEATURE_CJK$FEATURE_VIDEO

echo "Building with FEATURES=$FEATURES"

export RUSTFLAGS="-C target-cpu=native --cfg tokio_unstable"
cargo build --release --features=$FEATURES
strip ./target/release/gossip
cargo build --release --features="$FEATURES" || exit $?
strip ./target/release/gossip || exit $?

echo
echo "The gossip binary is at ./target/release/gossip and can be moved anywhere you want."
28 changes: 23 additions & 5 deletions docs/INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ With [scoop](https://scoop.sh/) on **Microsoft Windows**: `scoop install extras/

## Building from Source

On Apple Silicon macOS, direct video and YouTube share one inline WebKit view.
Direct video requests start only when you press Play on a visible video; media
privacy and offline settings still apply. With **Prefer Streaming** enabled,
Gossip uses a fresh cached copy of eligible non-volatile videos if available.
Otherwise it resolves the HTTPS media URL with a range request before WebKit
can stream from that origin. If the probe or playback startup fails, Gossip
tries a download and plays a private local copy. Videos declaring a SHA-256
checksum are never probed or streamed: Gossip verifies the bytes before playback.

To compare the previous direct video renderer, build with `egui-video` and set
`GOSSIP_LEGACY_DIRECT_VIDEO=1`. It uses the same acquisition policy, streaming
progressive HTTP(S) only when supported by the renderer. Other platforms keep their
existing renderer. Downloading a video does not add codec support.

### Step 0 - Possible Reset of Master Branch

If when you pull gossip it doesn't pull cleanly, I may have done a rare force-push. Run these commands to reset your master branch:
Expand All @@ -44,7 +58,7 @@ Most dependencies are probably already installed in your base operating system.
- pkg-config (debian: "pkg-config")
- openssl (debian: "libssl-dev") (this is only needed if not compiling with feature "rustls-tls")
- fontconfig (debian: "libfontconfig1-dev")
- ffmpeg support (debian: libavutil-dev libavformat-dev libavfilter-dev libavdevice-dev libxext-dev libclang-dev) (this is only needed if compiling with feature "video-ffmpeg")
- ffmpeg support (debian: libavutil-dev libavformat-dev libavfilter-dev libavdevice-dev libxext-dev libclang-dev) (this is only needed if compiling with feature "egui-video")

#### macOS

Expand All @@ -53,7 +67,7 @@ b. Install homebrew if you don't have it yet <https://brew.sh/>
c. Install these dependencies:

```bash
brew install cmake sdl2 pkg-config ffmpeg
brew install cmake pkg-config
```

### Step 3 - Clone this Repository
Expand All @@ -76,7 +90,7 @@ This binary should be portable to similar systems with similar hardware and oper
If you want a binary optimized for your exact processor with the newest CPU features enabled, and all gossip features enabled, do something more like this (for exact features to use, see the next section):

````bash
RUSTFLAGS="-C target-cpu=native --cfg tokio_unstable" cargo build --features=lang-cjk,video-ffmpeg --release
RUSTFLAGS="-C target-cpu=native --cfg tokio_unstable" cargo build --features=lang-cjk,egui-video --release
````

Everything gossip needs (fonts, icons) is baked into this executable. It doesn't need to find assets. So you can move the "gossip" binary and run it from anywhere.
Expand Down Expand Up @@ -130,12 +144,16 @@ There are so many of these (172) that it becomes a real pain to add them all. Bu

### Video Playback

You will need to install sdl2 (follow the instructions in the [readme](https://github.com/Rust-SDL2/rust-sdl2/)) and ffmpeg on your system.
Apple Silicon macOS uses WebKit for direct video by default. The optional
`egui-video` renderer uses AVFoundation there and does not require SDL2 or
FFmpeg. With a compatible wgpu/Metal renderer it can import Metal textures
directly. On Linux, Windows, and Intel macOS, `egui-video` uses bundled SDL2
and requires FFmpeg 9 development libraries.

Compile with

````
--features=video-ffmpeg
--features=egui-video
````

## Configuration
Expand Down
70 changes: 70 additions & 0 deletions docs/YOUTUBE_EMBEDS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Inline YouTube playback in Gossip

## Prior art (2026-11-28)

- [Ferrite](https://github.com/OlaProeis/Ferrite/blob/master/docs/technical/markdown/video-embeds.md) already embeds YouTube in a scrolling egui Markdown preview. Its [renderer](https://github.com/OlaProeis/Ferrite/blob/master/src/markdown/video_render.rs) places a Wry child view over an egui slot. It hides the player when partly clipped or covered by egui UI, rather than resizing the iframe. A thumbnail remains underneath. Ferrite documents a relay page for YouTube Error 153; we did not reproduce this on macOS.
- [egui `EmbeddedView`](https://github.com/emilk/egui/pull/8854) reports the full and visible rectangles to the host, and [a follow-up](https://github.com/emilk/egui/pull/8945) synchronizes egui's Metal frames with native view movement. These are not in Gossip's pinned eframe 0.31.1. The host still owns native clipping.
- [Rerun's `egui_webview`](https://github.com/rerun-io/egui_crates/tree/main/crates/egui_webview) renders offscreen CEF into egui textures. It gets egui clipping and layering but adds a Chromium runtime. [ytmini](https://github.com/dennissoftman/ytmini) renders with libmpv, but resolves YouTube streams rather than using the official player. Neither is the starting point for Gossip.

## Shared player on Apple Silicon macOS

`egui-webview/` owns the inline widget and native lifecycle. Gossip owns media
policy, visibility, and Play intent. One `WKWebView` plays both YouTube embeds
and direct videos; see [Installation](INSTALLATION.md#video-playback) for
other platforms and the optional legacy direct-video backend.

YouTube thumbnails load as eligible cards enter view when Gossip is online and
Fetch Media is enabled. Direct messages keep ordinary YouTube links. Playback
starts only after clicking a fully visible card. When a player scrolls out of
view, the webview is parked off-screen, paused, and captured before being hidden
or reused. The card remembers its position and shows a snapshot of the native
controls. Click Resume to play again. YouTube may resume near a keyframe because
its [`start` parameter](https://developers.google.com/youtube/player_parameters#start)
uses whole seconds. Leaving the feed unloads playback.

Run `cargo test -p egui-webview` for offline tests or `cargo run -p egui-webview-demo`
for a feed containing an MP4 and a YouTube embed. Gossip routes wheel and trackpad
events over the player to the feed; the demo exposes this as **Scroll feed over video**.

Unlike Ferrite's relay, start with the direct embed request and the `Referer` header based on Gossip's macOS bundle identifier. This follows [YouTube's documented macOS approach](https://developers.google.com/youtube/terms/required-minimum-functionality). A native webview is not an egui texture. If fully visible only is too limiting, investigate a clipping AppKit container before upgrading eframe or adding CEF.

### Automated native lifecycle test

On an unlocked Apple Silicon desktop running macOS 12 or newer:

```sh
cargo run --locked -p egui-webview --features native-e2e --bin egui-webview-e2e
```

This test exercises the standalone `YouTubePlayer`, not Gossip's shared `FeedPlayer`. It opens one temporary window and closes it with a nonzero exit on failure. It uses a local, muted HTML media fixture in real `WKWebView` child views. It checks duplicate slots, scroll clipping and independent visibility, restore at the same position without resuming playback, native resize, hidden-view eviction and paused recreation at a saved nonzero position, and closing one slot without closing the other. No network, screen recording, or Accessibility permission is needed. This checks native view and media state, not screen pixels or live YouTube. Use the demo for the live provider and physical wheel/trackpad smoke tests below.

### Frozen controls pixel test

On an unlocked macOS desktop with Screen Recording permission, run the shared
player against live media. The test captures only its own window and saves the
paused live player and frozen card as PNGs for comparison. It checks the video
area and bottom controls separately; the centered Resume label is excluded.

```sh
captures=$(mktemp -d)
cargo build --locked -p egui-webview --features native-e2e,shared-player --bin egui-webview-e2e
EGUI_CAPTURE_OUTPUT="$captures/youtube" ./target/debug/egui-webview-e2e --capture-controls
EGUI_CAPTURE_OUTPUT="$captures/mp4" EGUI_LOCAL_MP4=/path/to/fixture.mp4 \
./target/debug/egui-webview-e2e --capture-controls
```

Add `--scroll-out` after `--capture-controls` (with a fresh output directory) to
scroll the original post out of view before switching and then scroll back to
inspect its frozen card. For MP4, use `--scroll-while-playing` instead. This
moves the real pointer out of the player, posts a mouse-moved event (which may
require Accessibility permission), and verifies with window captures that the
controls auto-hid during playback before scrolling. Use a
static-image clip at least 12 seconds long so the pixel comparison measures the
controls rather than changes in the video. Set `EGUI_YOUTUBE_URL` to test a
different embed. The test requires network access for the second post.

### Native smoke test (manual)

1. Start the demo and confirm the thumbnails load but no player loads automatically. With the slot fully on screen, click the thumbnail once and confirm the video starts without pressing Play again. Repeat in the packaged Gossip app to verify the `Referer` works without Error 153. Check the player's **Watch on YouTube** action and the thumbnail context menu fallback for a video that blocks embeds.
2. Let the MP4 controls hide, then scroll the player out of view. Audio should stop. Scroll back: the frozen card should show native controls, and clicking Resume should continue near the previous position. Try wheel and trackpad input with **Scroll feed over video** off and on.
3. Switch between the MP4 and YouTube cards and check that the demo's native-view count stays at one. Open **Test menu**, toggle **Show feed**, and resize or zoom the window. Verify the native view never covers egui controls or gets out of sync with its slot.
39 changes: 39 additions & 0 deletions egui-webview/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "egui-webview"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false

[features]
native-e2e = ["dep:eframe", "dep:objc2-core-graphics", "webview-snapshot"]
shared-player = ["webview-snapshot", "dep:base64", "dep:tempfile", "objc2-foundation/NSURL"]
webview-snapshot = ["dep:objc2-foundation", "objc2-app-kit/NSBitmapImageRep", "objc2-app-kit/NSImageRep", "objc2-web-kit/WKSnapshotConfiguration"]

[[bin]]
name = "egui-webview-e2e"
path = "tests/native/runner.rs"
required-features = ["native-e2e"]

[dependencies]
url = { workspace = true }

[target.'cfg(all(target_os = "macos", target_arch = "aarch64"))'.dependencies]
block2 = "0.6"
base64 = { version = "0.22", optional = true }
eframe = { version = "0.31.1", default-features = false, features = ["default_fonts", "wgpu"], optional = true }
egui = "0.31.1"
image = { version = "0.25", default-features = false, features = ["jpeg", "png", "webp"] }
objc2 = "0.6"
objc2-app-kit = { version = "0.3.2", default-features = false, features = ["std", "block2", "NSEvent", "NSResponder", "NSView", "NSWindow", "objc2-core-foundation"] }
objc2-core-graphics = { version = "0.3.2", default-features = false, features = ["CGEvent", "CGEventSource", "CGEventTypes"], optional = true }
objc2-foundation = { version = "0.3.2", default-features = false, features = ["NSDictionary"], optional = true }
objc2-web-kit = { version = "0.3.2", default-features = false, features = ["std", "WKPreferences", "WKWebViewConfiguration", "WKWebsiteDataStore"] }
raw-window-handle = "0.6"
# Share Gossip's existing reqwest version instead of adding a second HTTP stack.
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls-native-roots"] }
serde = { workspace = true }
serde_json = { workspace = true }
tempfile = { version = "3", optional = true }
tracing = { workspace = true }
wry = { version = "0.58.2", default-features = false }
10 changes: 10 additions & 0 deletions egui-webview/examples/feed/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "egui-webview-demo"
version = "0.1.0"
edition = "2021"
publish = false

[target.'cfg(all(target_os = "macos", target_arch = "aarch64"))'.dependencies]
eframe = { version = "0.31.1", default-features = false, features = ["default_fonts", "wgpu"] }
egui-webview = { path = "../..", features = ["shared-player"] }
url = { workspace = true }
Loading
0