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
6 changes: 5 additions & 1 deletion images/chromium-headful/client/src/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</main>
<neko-side v-if="!videoOnly && side" />
<neko-connect v-if="!connected && !wasConnected" />
<neko-disconnected v-if="!connected && wasConnected" />
<neko-disconnected v-if="!connected && wasConnected" :reconnecting="reconnecting" />
<neko-about v-if="about" />
<notifications
v-if="!videoOnly"
Expand Down Expand Up @@ -379,6 +379,10 @@
return this.$accessor.connected
}

get reconnecting() {
return this.$accessor.reconnecting
}

get playing() {
return this.$accessor.video.playing
}
Expand Down
23 changes: 19 additions & 4 deletions images/chromium-headful/client/src/components/disconnected.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="disconnected-overlay">
<div :class="['disconnected-overlay', { reconnecting }]" role="status">
<div class="message-container">
<span class="message">{{ $t('disconnected.message') }}</span>
<span class="message">{{ reconnecting ? $t('connection.reconnecting') : $t('disconnected.message') }}</span>
</div>
</div>
</template>
Expand All @@ -15,6 +15,19 @@
justify-content: center;
align-items: center;

// the last frame stays visible underneath while the session is restored
&.reconnecting {
position: fixed;
top: 0;
left: 0;
z-index: 10;
background: rgba($color: #000, $alpha: 0.6);

.message {
color: #fff;
}
}

.message-container {
display: flex;
justify-content: center;
Expand All @@ -31,8 +44,10 @@
</style>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { Component, Prop, Vue } from 'vue-property-decorator'

@Component({ name: 'neko-disconnected' })
export default class extends Vue {}
export default class extends Vue {
@Prop({ type: Boolean, default: false }) readonly reconnecting!: boolean
}
</script>
23 changes: 21 additions & 2 deletions images/chromium-headful/client/src/components/video.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@
</button>
<div v-if="touchNotice && !hideControls" class="touch-button touch-notice" role="status">{{ touchNotice }}</div>
<button
v-if="videoBlocked"
v-if="videoBlocked && connected"
class="touch-button video-resume"
@touchend.stop.prevent="resumeVideo"
@click.stop.prevent="resumeVideo"
:aria-label="startPrompt"
>
Tap to resume video
{{ startPrompt }}
</button>
<neko-resolution ref="resolution" v-if="admin" />
<neko-clipboard
Expand Down Expand Up @@ -470,6 +471,13 @@
return this.$accessor.connected
}

get startPrompt() {
const coarse = window.matchMedia
? window.matchMedia('(pointer: coarse)').matches
: 'ontouchstart' in window || navigator.maxTouchPoints > 0
return coarse ? 'Tap to start live view' : 'Click to start live view'
}

get connecting() {
return this.$accessor.connecting
}
Expand Down Expand Up @@ -728,9 +736,11 @@
} catch (err: any) {
// if it still fails, we're not playing anything
this.$accessor.video.pause()
this.askForTapIfRefused(err)
}
} else {
this.$accessor.video.pause()
this.askForTapIfRefused(err)
}
}
}
Expand Down Expand Up @@ -1204,6 +1214,13 @@
if (!document.hidden) this.resumeVideo()
}

// iOS Safari in Low Power Mode can refuse even a muted play() until a tap,
// so without a prompt the stream stays a black box. Other rejections, such as
// an AbortError from a new stream replacing a pending play(), retry on their own.
askForTapIfRefused(err: any) {
if (this.is_touch_device && err?.name === 'NotAllowedError') this.videoBlocked = true
}

onTouchCancel() {
this.gestures.touchCancel()
}
Expand Down Expand Up @@ -1798,6 +1815,8 @@
onConnectedChanged(connected: boolean) {
if (!connected) {
this.resetKeyboard()
// a tap can't start a stream that is gone; the next one asks again if refused
this.videoBlocked = false
}
}

Expand Down
55 changes: 52 additions & 3 deletions images/chromium-headful/client/src/neko/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export const CONNECT_STAGE_TIMEOUT_MS = resolveConnectStageTimeoutMs(
// prose. The three stages mean a bound expired; the rest never reached one.
export type ConnectFailure = ConnectStage | 'unsupported' | 'peer' | 'server'

// the live view proxy closes a viewer with this once the browser session is gone
export const WS_CLOSE_SESSION_GONE = 4410

export interface BaseEvents {
info: (...message: any[]) => void
warn: (...message: any[]) => void
Expand All @@ -100,6 +103,10 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
protected _timeout?: number
protected _stage?: ConnectStage
protected _everConnected = false
// Some connect in this page succeeded, so a later drop is a dropped session
// to restore, and a reconnect attempt that fails is not a failed first connect.
protected _established = false
protected _sessionGone = false
protected _gaveUp = false
// Tagged where the failure happens, so a pre-connect disconnect is reported
// with a reason an embedder can act on. Unset falls back to 'peer'.
Expand Down Expand Up @@ -129,6 +136,30 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
return this.peerConnected && this.socketOpen
}

// A dropped session can be restored unless the server ended it or the browser is gone.
get recoverable() {
return this._established && this._failure !== 'server' && !this._sessionGone
}

// Whether media still arrives. A page that iOS suspended can come back with
// a socket that still reads as open and a peer the server already destroyed;
// neither fires an event, but no bytes arrive. Audio and video both stream
// continuously, so a short window is enough.
public async streamAlive(windowMs: number): Promise<boolean> {
const peer = this._peer
if (!peer || !this.connected) return false
if (['disconnected', 'failed', 'closed'].includes(this._state)) return false
try {
const before = await receivedBytes(peer)
await new Promise((resolve) => window.setTimeout(resolve, windowMs))
// a drop in the meantime is handled by its own event
if (peer !== this._peer) return true
return (await receivedBytes(peer)) > before
} catch {
return true
}
}

public connect(url: string, password: string, displayname: string) {
if (this._gaveUp) {
this.emit('debug', `not reconnecting, already gave up`)
Expand Down Expand Up @@ -163,6 +194,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this._ws.onclose = (event) => {
this.emit('debug', `websocket closed: code=${event.code}, reason=${event.reason}`)
this._failure = 'transport'
if (event.code === WS_CLOSE_SESSION_GONE) this._sessionGone = true
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
this.onDisconnected(new Error('websocket closed'))
}
this.armStage('transport')
Expand Down Expand Up @@ -226,6 +258,8 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
}

this._state = 'disconnected'
// candidates from a dropped attempt must not reach the next peer
this._candidates = []
this._displayname = undefined
this._id = ''
}
Expand Down Expand Up @@ -481,6 +515,11 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
return
}

if (event === EVENT.SIGNAL.CLOSE) {
this.onDisconnected(new Error('peer closed by server'))
return
}

if (event === EVENT.SIGNAL.CANDIDATE) {
const { data } = payload as SignalCandidatePayload
const candidate: RTCIceCandidate = JSON.parse(data)
Expand Down Expand Up @@ -519,7 +558,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this.emit('error', (event as ErrorEvent).error)
}

private postParentMessage(message: Record<string, unknown>) {
protected postParentMessage(message: Record<string, unknown>) {
if (window.parent === window) {
return
}
Expand Down Expand Up @@ -553,6 +592,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
}

this._everConnected = true
this._established = true

this.emit('debug', `connected`)
this[EVENT.CONNECTED]()
Expand Down Expand Up @@ -591,7 +631,8 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
// The bound expiring is this connect's terminal event, so report it here and
// let onDisconnected take the ordinary disconnect path rather than reporting
// a second event for the same failure.
this.reportFailure('KERNEL_CONNECTION_TIMEOUT', stage)
// a reconnect attempt that times out is retried, not reported as a failed connect
if (!this._established) this.reportFailure('KERNEL_CONNECTION_TIMEOUT', stage)
this.onDisconnected(new Error(`${stage} timeout`))
}

Expand All @@ -616,7 +657,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
// A disconnect before any peer was established is a failed connect, not a
// dropped session. disconnect() clears the bound below, so without this the
// parent frame would hear nothing at all.
if (!this._gaveUp && !this._everConnected) {
if (!this._gaveUp && !this._everConnected && !this._established) {
this.giveUp(this._failure ?? 'peer', reason ?? new Error('connection failed'))
return
}
Expand All @@ -637,3 +678,11 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
protected abstract [EVENT.TRACK](event: RTCTrackEvent): void
protected abstract [EVENT.DATA](data: any): void
}

async function receivedBytes(peer: RTCPeerConnection) {
let bytes = 0
;(await peer.getStats()).forEach((stat) => {
if (stat.type === 'inbound-rtp') bytes += stat.bytesReceived || 0
})
return bytes
}
2 changes: 2 additions & 0 deletions images/chromium-headful/client/src/neko/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const EVENT = {
ANSWER: 'signal/answer',
PROVIDE: 'signal/provide',
CANDIDATE: 'signal/candidate',
CLOSE: 'signal/close',
},
MEMBER: {
LIST: 'member/list',
Expand Down Expand Up @@ -100,6 +101,7 @@ export type SignalEvents =
| typeof EVENT.SIGNAL.ANSWER
| typeof EVENT.SIGNAL.PROVIDE
| typeof EVENT.SIGNAL.CANDIDATE
| typeof EVENT.SIGNAL.CLOSE

export type ChatEvents = typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE

Expand Down
Loading
Loading