|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "unicode/utf8" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + errInvalidChar = errors.New("message contains invalid chars") |
| 13 | + errInvalidOp = errors.New("invalid start operation") |
| 14 | +) |
| 15 | + |
| 16 | +// WebRTCSignalMessage represents a signalling message between peers and the singalling server |
| 17 | +type WebRTCSignalMessage struct { |
| 18 | + Type string `json:"type,omitempty"` |
| 19 | + Src string `json:"src,omitempty"` |
| 20 | + Dst string `json:"dst,omitempty"` |
| 21 | + Payload json.RawMessage `json:"payload,omitempty"` |
| 22 | + |
| 23 | + parsedPayload *Payload |
| 24 | + isParsed bool |
| 25 | + mu sync.Mutex |
| 26 | +} |
| 27 | + |
| 28 | +// Payload is the content of `payload` in the json |
| 29 | +type Payload struct { |
| 30 | + Msg *string `json:"msg,omitempty"` |
| 31 | + Sdp *struct { |
| 32 | + Type *string `json:"type,omitempty"` |
| 33 | + Sdp *string `json:"sdp,omitempty"` |
| 34 | + } `json:"sdp,omitempty"` |
| 35 | + Type *string `json:"type,omitempty"` |
| 36 | + Label *string `json:"label,omitempty"` |
| 37 | + ConnectionID *string `json:"connectionId,omitempty"` |
| 38 | + Reliable *bool `json:"reliable,omitempty"` |
| 39 | + Serialization *string `json:"serialization,omitempty"` |
| 40 | + Browser *string `json:"browser,omitempty"` |
| 41 | + Candidate *struct { |
| 42 | + Candidate *string `json:"candidate,omitempty"` |
| 43 | + SdpMid *string `json:"sdpMid,omitempty"` |
| 44 | + SdpMLineIndex *int `json:"sdpMLineIndex,omitempty"` |
| 45 | + } `json:"candidate,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +// ParsePayload parses the payload if it is not parsed previously. This method |
| 49 | +// can be called concurrently. |
| 50 | +func (w *WebRTCSignalMessage) ParsePayload() (*Payload, error) { |
| 51 | + w.mu.Lock() |
| 52 | + defer w.mu.Unlock() |
| 53 | + |
| 54 | + if w.isParsed { |
| 55 | + return w.parsedPayload, nil |
| 56 | + } |
| 57 | + |
| 58 | + payload := &Payload{} |
| 59 | + if err := json.Unmarshal(w.Payload, payload); err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + w.parsedPayload = payload |
| 64 | + return payload, nil |
| 65 | +} |
| 66 | + |
| 67 | +// ParseWebRTCSignalMessage parses the web rtc command/message |
| 68 | +func ParseWebRTCSignalMessage(msg string) (*WebRTCSignalMessage, error) { |
| 69 | + // All messages are text (utf-8 encoded at present) |
| 70 | + if !utf8.Valid([]byte(msg)) { |
| 71 | + return nil, errInvalidChar |
| 72 | + } |
| 73 | + |
| 74 | + w := &WebRTCSignalMessage{} |
| 75 | + if err := json.Unmarshal([]byte(msg), w); err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + if err := validateOperation(w.Type); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + return w, nil |
| 84 | +} |
| 85 | + |
| 86 | +func validateOperation(op string) error { |
| 87 | + switch strings.ToUpper(op) { |
| 88 | + case "ANSWER", "OFFER", "CANDIDATE", "LEAVE": |
| 89 | + return nil |
| 90 | + default: |
| 91 | + return errInvalidOp |
| 92 | + } |
| 93 | +} |
0 commit comments