Files
openchamber-android-native/third_party/mygo/plugins/videoplayer/videoplayer.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

148 lines
4.1 KiB
Go

// Package videoplayer plays videos and their sound in a window's native UI, on
// phones and tablets, with the system's decoders: AVPlayer on iOS, MediaPlayer
// on Android, from a file or a URL they can read, HLS among them.
//
// p, err := videoplayer.New(win, "https://example.com/film.m3u8", videoplayer.Options{AutoPlay: true})
// ...
// ui.WebView(c, p).Fill() // in the window's view; what it draws after shows over the video
//
// The view shows under the content, as a web view does. On desktops and in
// browsers New reports errors.ErrUnsupported.
package videoplayer
import (
"context"
"encoding/json"
"sync"
"time"
"github.com/egoist/mygo"
"github.com/egoist/mygo/internal/platform"
"github.com/egoist/mygo/internal/surface"
)
const pluginID = "videoplayer"
// Options are how a Player starts.
type Options struct {
// AutoPlay plays it as soon as it can.
AutoPlay bool
// Loop plays it again from the start as it ends.
Loop bool
// Muted plays it without sound.
Muted bool
}
// State is what a Player tells of its video.
type State struct {
// Ready says it can play: Duration and the size are known.
Ready bool
// Playing says it plays now, and Ended that it got to the end.
Playing, Ended bool
// Position is where it is, and Duration how long the video is.
Position, Duration time.Duration
// Width and Height are the video's size in pixels.
Width, Height int
// Error says why it cannot play, if so.
Error string
}
type wireState struct {
ID int `json:"id"`
Ready bool `json:"ready"`
Playing bool `json:"playing"`
Ended bool `json:"ended"`
PositionMS float64 `json:"positionMs"`
DurationMS float64 `json:"durationMs"`
Width int `json:"width"`
Height int `json:"height"`
Error string `json:"error"`
}
// Player plays a video. Show it with ui.WebView.
type Player struct {
view *mygo.PluginView
mu sync.Mutex
state State
onState []func(State)
off func()
}
// New makes a player of a file's path or a URL, in a window showing native UI.
func New(w *mygo.Window, source string, o Options) (*Player, error) {
v, err := mygo.NewPluginView(w, "videoplayer", map[string]any{
"source": source, "autoPlay": o.AutoPlay, "loop": o.Loop, "muted": o.Muted,
})
if err != nil {
return nil, err
}
p := &Player{view: v}
p.off = mygo.Native(pluginID).On("state", func(data []byte) {
var s wireState
if json.Unmarshal(data, &s) != nil || s.ID != v.ID {
return
}
st := State{s.Ready, s.Playing, s.Ended, time.Duration(s.PositionMS) * time.Millisecond,
time.Duration(s.DurationMS) * time.Millisecond, s.Width, s.Height, s.Error}
p.mu.Lock()
p.state = st
list := append([]func(State){}, p.onState...)
p.mu.Unlock()
for _, fn := range list {
fn(st)
}
})
return p, nil
}
// SurfaceWebView gives package ui the view to show.
func (p *Player) SurfaceWebView(conn *surface.Conn) platform.WebView {
return p.view.SurfaceWebView(conn)
}
// State tells what the player last told of its video.
func (p *Player) State() State {
p.mu.Lock()
defer p.mu.Unlock()
return p.state
}
// OnState calls fn on the main thread as the state changes, and a few times a
// second as it plays.
func (p *Player) OnState(fn func(State)) {
p.mu.Lock()
p.onState = append(p.onState, fn)
p.mu.Unlock()
}
func (p *Player) call(method string, args map[string]any) {
if args == nil {
args = map[string]any{}
}
args["id"] = p.view.ID
go mygo.Native(pluginID).Call(context.Background(), method, args, nil)
}
// Play plays it from where it is.
func (p *Player) Play() { p.call("play", nil) }
// Pause stops it where it is.
func (p *Player) Pause() { p.call("pause", nil) }
// Seek goes to a position.
func (p *Player) Seek(to time.Duration) { p.call("seek", map[string]any{"ms": to.Milliseconds()}) }
// SetVolume sets how loud it plays, from 0 to 1.
func (p *Player) SetVolume(v float64) { p.call("volume", map[string]any{"volume": v}) }
// SetRate sets how fast it plays: 1 is normal speed.
func (p *Player) SetRate(r float64) { p.call("rate", map[string]any{"rate": r}) }
// Destroy stops it and takes its view away.
func (p *Player) Destroy() {
p.off()
p.call("dispose", nil)
p.view.Destroy()
}