Skip to content

Commit

Permalink
MM-56540 - Live Captions: add client.Send; recState -> jobState (#130)
Browse files Browse the repository at this point in the history
* add client.Send to send a ws event to the server

* blank commit

* client.Send -> client.WsSend

* hah, prefix != suffix

* "recState" -> "jobState"

* call_job_state wsEvent

* add type to CallJobState

* wsSend -> SendWS; emit WSCallJobState

* clean ups
  • Loading branch information
cpoile authored Mar 22, 2024
1 parent 7a6415d commit e9e2e1f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
6 changes: 3 additions & 3 deletions client/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *Client) joinCall() error {
if err := c.wsSend(wsEventJoin, CallJoinMessage{
if err := c.SendWS(wsEventJoin, CallJoinMessage{
ChannelID: c.cfg.ChannelID,
JobID: c.cfg.JobID,
}, false); err != nil {
Expand All @@ -19,15 +19,15 @@ func (c *Client) joinCall() error {
}

func (c *Client) leaveCall() error {
if err := c.wsSend(wsEventLeave, nil, false); err != nil {
if err := c.SendWS(wsEventLeave, nil, false); err != nil {
return fmt.Errorf("failed to send ws msg: %w", err)
}

return nil
}

func (c *Client) reconnectCall() error {
if err := c.wsSend(wsEventReconnect, CallReconnectMessage{
if err := c.SendWS(wsEventReconnect, CallReconnectMessage{
ChannelID: c.cfg.ChannelID,
OriginalConnID: c.originalConnID,
PrevConnID: c.currentConnID,
Expand Down
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
WSDisconnectEvent = "WSDisconnect"
WSCallJoinEvent = "WSCallJoin"
WSCallRecordingState = "WSCallRecordingState"
WSCallJobState = "WSCallJobState"
WSJobStopEvent = "WSStopJobEvent"
RTCConnectEvent = "RTCConnect"
RTCDisconnectEvent = "RTCDisconnect"
Expand Down
6 changes: 3 additions & 3 deletions client/rtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *Client) handleWSEventSignal(evData map[string]any) error {
return fmt.Errorf("failed to encode answer: %w", err)
}
w.Close()
return c.wsSend(wsEventSDP, map[string]any{
return c.SendWS(wsEventSDP, map[string]any{
"data": sdpData.Bytes(),
}, true)
case signalMsgAnswer:
Expand Down Expand Up @@ -158,7 +158,7 @@ func (c *Client) initRTCSession() error {
return
}

if err := c.wsSend(wsEventICE, map[string]any{
if err := c.SendWS(wsEventICE, map[string]any{
"data": string(data),
}, true); err != nil {
log.Printf(err.Error())
Expand Down Expand Up @@ -258,7 +258,7 @@ func (c *Client) initRTCSession() error {
return
}
w.Close()
err = c.wsSend(wsEventSDP, map[string]any{
err = c.SendWS(wsEventSDP, map[string]any{
"data": sdpData.Bytes(),
}, true)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ type CallReconnectMessage struct {
}

type CallJobState struct {
Type string `json:"type"`
InitAt int64 `json:"init_at"`
StartAt int64 `json:"start_at"`
EndAt int64 `json:"end_at"`
Err string `json:"err,omitempty"`
}

func (cjs *CallJobState) FromMap(m map[string]any) {
jobType, _ := m["type"].(string)
initAt, _ := m["init_at"].(float64)
startAt, _ := m["start_at"].(float64)
endAt, _ := m["end_at"].(float64)
err, _ := m["err"].(string)

cjs.Type = jobType
cjs.InitAt = int64(initAt)
cjs.StartAt = int64(startAt)
cjs.EndAt = int64(endAt)
Expand Down
40 changes: 23 additions & 17 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ const (
)

const (
wsEventJoin = wsEvPrefix + "join"
wsEventLeave = wsEvPrefix + "leave"
wsEventReconnect = wsEvPrefix + "reconnect"
wsEventSignal = wsEvPrefix + "signal"
wsEventICE = wsEvPrefix + "ice"
wsEventSDP = wsEvPrefix + "sdp"
wsEventError = wsEvPrefix + "error"
wsEventUserLeft = wsEvPrefix + "user_left"
wsEventCallEnd = wsEvPrefix + "call_end"
wsEventCallRecordingState = wsEvPrefix + "call_recording_state"
wsEventJobStop = wsEvPrefix + "job_stop"
wsEventJoin = wsEvPrefix + "join"
wsEventLeave = wsEvPrefix + "leave"
wsEventReconnect = wsEvPrefix + "reconnect"
wsEventSignal = wsEvPrefix + "signal"
wsEventICE = wsEvPrefix + "ice"
wsEventSDP = wsEvPrefix + "sdp"
wsEventError = wsEvPrefix + "error"
wsEventUserLeft = wsEvPrefix + "user_left"
wsEventCallEnd = wsEvPrefix + "call_end"
wsEventCallJobState = wsEvPrefix + "call_job_state"
wsEventJobStop = wsEvPrefix + "job_stop"
)

var (
wsReconnectionTimeout = 30 * time.Second
errCallEnded = errors.New("call ended")
)

func (c *Client) wsSend(ev string, msg any, binary bool) error {
func (c *Client) SendWS(ev string, msg any, binary bool) error {
c.mut.Lock()
defer c.mut.Unlock()

Expand Down Expand Up @@ -183,14 +183,20 @@ func (c *Client) handleWSMsg(msg ws.Message) error {
log.Printf("received call end event, closing client")
return errCallEnded
}
case wsEventCallRecordingState:
data, ok := ev.GetData()["recState"].(map[string]any)
case wsEventCallJobState:
data, ok := ev.GetData()["jobState"].(map[string]any)
if !ok {
return fmt.Errorf("invalid recording state")
}
var recState CallJobState
recState.FromMap(data)
c.emit(WSCallRecordingState, recState)
var jobState CallJobState
jobState.FromMap(data)
c.emit(WSCallJobState, jobState)

// Below is deprecated as of v0.14.0, kept for compatibility with earlier versions
// of transcriber
if jobState.Type == "recording" {
c.emit(WSCallRecordingState, jobState)
}
case wsEventJobStop:
jobID, _ := ev.GetData()["job_id"].(string)
c.emit(WSJobStopEvent, jobID)
Expand Down

0 comments on commit e9e2e1f

Please sign in to comment.