Skip to content

Commit

Permalink
working on audience interaction devices
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Jan 3, 2025
1 parent 9419636 commit 71db02b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
20 changes: 20 additions & 0 deletions zss/device/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ export function api_error(
return tape_error(sender, message, player)
}

export function broadcast_createsession(sender: string, player: string) {
hub.emit('broadcast:createsession', sender, undefined, player)
}

export function broadcast_closesession(sender: string, player: string) {
hub.emit('broadcast:closesession', sender, undefined, player)
}

export function broadcast_startstream(
sender: string,
streamkey: string,
player: string,
) {
hub.emit('broadcast:startstream', sender, streamkey, player)
}

export function broadcast_stopstream(sender: string, player: string) {
hub.emit('broadcast:stopstream', sender, undefined, player)
}

export function gadgetclient_reset(
sender: string,
gadgetstate: GADGET_STATE,
Expand Down
59 changes: 54 additions & 5 deletions zss/device/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import IVSBroadcastClient from 'amazon-ivs-web-broadcast'
import IVSBroadcastClient, { Callback } from 'amazon-ivs-web-broadcast'
import { createdevice } from 'zss/device'
import { doasync } from 'zss/mapping/func'
import { ispresent, MAYBE } from 'zss/mapping/types'
import { ispresent, isstring, MAYBE } from 'zss/mapping/types'
import { write } from 'zss/words/writeui'

import { api_error } from './api'
import { synthbroadcastdestination } from './synth'
Expand All @@ -10,20 +11,43 @@ let broadcastclient: MAYBE<IVSBroadcastClient.AmazonIVSBroadcastClient>

const broadcast = createdevice('broadcast', ['second'], (message) => {
switch (message.target) {
case 'opensession':
doasync('broadcast:opensession', async () => {
case 'createsession':
doasync('broadcast:createsession', async () => {
if (ispresent(broadcastclient)) {
api_error(broadcast.name(), 'session', 'session is already open')
} else {
const isportrait = window.innerHeight > window.innerWidth
broadcastclient = IVSBroadcastClient.create({
ingestEndpoint: 'https://g.webrtc.live-video.net:4443',
streamConfig: isportrait
? IVSBroadcastClient.BASIC_PORTRAIT
: IVSBroadcastClient.BASIC_LANDSCAPE,
logLevel: IVSBroadcastClient.LOG_LEVEL.DEBUG,
})

// event handlers
broadcastclient.on(
IVSBroadcastClient.BroadcastClientEvents.ACTIVE_STATE_CHANGE,
// @ts-expect-error wow?
function (activestate: boolean) {
console.info({ activestate })
},
)

broadcastclient.on(
IVSBroadcastClient.BroadcastClientEvents.CONNECTION_STATE_CHANGE,
function (state: string) {
write(broadcast.name(), state)
} as Callback,
)

broadcastclient.on(
IVSBroadcastClient.BroadcastClientEvents.ERROR,
function (error: string) {
api_error(broadcast.name(), 'connection', error)
broadcastclient = undefined
} as Callback,
)

// pull visual content from canvas
const video = document.querySelector('canvas')
if (ispresent(video)) {
Expand All @@ -34,6 +58,8 @@ const broadcast = createdevice('broadcast', ['second'], (message) => {
'video',
'unabled to find canvas element',
)
broadcastclient = undefined
return
}

// pull audio content from tonejs
Expand All @@ -46,21 +72,44 @@ const broadcast = createdevice('broadcast', ['second'], (message) => {
'video',
'unable create media audio node destination',
)
broadcastclient = undefined
return
}

// signal success
write(broadcast.name(), `created client`)
}
})
break
case 'closesession':
if (ispresent(broadcastclient)) {
broadcastclient.delete()
broadcastclient = undefined
write(broadcast.name(), `closed client`)
} else {
api_error(broadcast.name(), 'session', 'session is already closed')
}
break
case 'startstream':
doasync('broadcast:startstream', async () => {
if (isstring(message.data) && ispresent(broadcastclient)) {
await broadcastclient.startBroadcast(
message.data,
'https://g.webrtc.live-video.net:4443',
)
}
})
break
case 'stopstream':
if (ispresent(broadcastclient)) {
broadcastclient.stopBroadcast()
} else {
api_error(
broadcast.name(),
'session',
'need an active session to stop stream',
)
}
break
}
})
10 changes: 10 additions & 0 deletions zss/device/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createdevice } from 'zss/device'

const chat = createdevice('chat', [], (message) => {
switch (message.target) {
default:
break
}
})

//
2 changes: 2 additions & 0 deletions zss/userspace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// these are all front-end devices
import './device/broadcast'
import './device/chat'
import './device/peer'
import './device/gadgetclient'
import './device/modem'
Expand Down

0 comments on commit 71db02b

Please sign in to comment.