Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-56540 - Live Captions: add client.Send #130

Merged
merged 10 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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: 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
35 changes: 21 additions & 14 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,13 +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(WSCallJobState, recState)

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