Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
irvingoujAtDevolution committed Jan 10, 2025
1 parent e4f8ee3 commit 33dc82c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
8 changes: 7 additions & 1 deletion crates/video-streamer/src/streamer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn webm_stream(
let (chunk_writer, chunk_receiver) = ChannelWriter::new();
let (error_sender, error_receiver) = mpsc::channel(1);
let stop_notifier = Arc::new(Notify::new());

spawn_sending_task(
ws_frame,
chunk_receiver,
Expand Down Expand Up @@ -113,7 +114,7 @@ pub fn webm_stream(
}
Err(e) => {
error_sender.blocking_send(UserFriendlyError::UnexpectedEOF)?;
anyhow::bail!(e);
break Err(e.into());
}
}
}
Expand All @@ -126,17 +127,22 @@ pub fn webm_stream(
Ok(WriterResult::Continue) => continue,
Err(e) => {
let Some(TagWriterError::WriteError { source }) = e.downcast_ref::<TagWriterError>() else {
error_sender.blocking_send(UserFriendlyError::InternalServerError)?;
break Err(e);
};

if source.kind() != std::io::ErrorKind::Other {
error_sender.blocking_send(UserFriendlyError::UnexpectedError)?;
break Err(e);
}

let Some(ChannelWriterError::ChannelClosed) =
source.get_ref().and_then(|e| e.downcast_ref::<ChannelWriterError>())
else {
error_sender.blocking_send(UserFriendlyError::UnexpectedError)?;
break Err(e);
};

// Channel is closed, we can break
break Ok(());
}
Expand Down
2 changes: 2 additions & 0 deletions crates/video-streamer/src/streamer/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ impl codec::Decoder for ProtocolCodeC {
pub(crate) enum UserFriendlyError {
UnexpectedError,
UnexpectedEOF,
InternalServerError,
}

impl UserFriendlyError {
pub(crate) fn as_str(&self) -> &'static str {
match self {
UserFriendlyError::UnexpectedError => "UnexpectedError",
UserFriendlyError::UnexpectedEOF => "UnexpectedEOF",
UserFriendlyError::InternalServerError => "InternalServerError",
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions webapp/player-project/src/streamers/webm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import '../../../shadow-player/src/streamer';
import { ShadowPlayer } from '../../../shadow-player/src/streamer';
import {
ShadowPlayer,
ShadowPlayerError,
} from '../../../shadow-player/src/streamer';
import { GatewayAccessApi } from '../gateway';
import { showNotification } from '../notification';

Expand All @@ -24,8 +27,12 @@ export async function handleWebm(gatewayAccessApi: GatewayAccessApi) {
showNotification('Playback has ended', 'success');
});

shadowPlayer.onError((error) => {
showNotification(`An error occurred: ${error}`, 'error');
shadowPlayer.onError((error: ShadowPlayerError) => {
if (error.type === 'serverSentError') {
showNotification(`An error occurred: ${error.error}`, 'error');
} else {
showNotification(`An error occurred: ${error.error.message}`, 'error');
}
});

return shadowPlayer;
Expand Down
2 changes: 1 addition & 1 deletion webapp/shadow-player/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ChunkMessage {

export interface ErrorMessage {
type: 'error';
error: 'UnexpectedError' | 'UnexpectedEOF';
error: 'UnexpectedError' | 'UnexpectedEOF' | 'InternalServerError';
}

export interface MetaDataMessage {
Expand Down
19 changes: 16 additions & 3 deletions webapp/shadow-player/src/streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ import { ErrorMessage } from './protocol';
import { ReactiveSourceBuffer } from './sourceBuffer';
import { ServerWebSocket } from './websocket';

export type ShadowPlayerError =
| {
type: 'webSocketError';
error: ErrorEvent;
}
| {
type: 'serverSentError';
error: ErrorMessage;
};

export class ShadowPlayer extends HTMLElement {
shadowRoot: ShadowRoot | null = null;
_videoElement: HTMLVideoElement | null = null;
_src: string | null = null;
_buffer: ReactiveSourceBuffer | null = null;
onErrorCallback: ((ev: ErrorMessage) => void) | null = null;
onErrorCallback: ((ev: ShadowPlayerError) => void) | null = null;
onEndCallback: (() => void) | null = null;
debug = false;
static get observedAttributes() {
Expand All @@ -31,7 +41,7 @@ export class ShadowPlayer extends HTMLElement {
}
}

onError(callback: (ev: ErrorMessage) => void) {
onError(callback: (ev: ShadowPlayerError) => void) {
this.onErrorCallback = callback;
}

Expand Down Expand Up @@ -146,7 +156,10 @@ export class ShadowPlayer extends HTMLElement {
}

if (ev.type === 'error') {
this.onErrorCallback?.(ev);
this.onErrorCallback?.({
type: 'serverSentError',
error: ev,
});
}

if (ev.type === 'end') {
Expand Down

0 comments on commit 33dc82c

Please sign in to comment.