Debian Codegraph @

event/voip.go

raw
1 // Copyright (c) 2021 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package event
8
9 import (
10 "encoding/json"
11 "fmt"
12 "strconv"
13 )
14
15 type CallHangupReason string
16
17 const (
18 CallHangupICEFailed CallHangupReason = "ice_failed"
19 CallHangupInviteTimeout CallHangupReason = "invite_timeout"
20 CallHangupUserHangup CallHangupReason = "user_hangup"
21 CallHangupUserMediaFailed CallHangupReason = "user_media_failed"
22 CallHangupUnknownError CallHangupReason = "unknown_error"
23 )
24
25 type CallDataType string
26
27 const (
28 CallDataTypeOffer CallDataType = "offer"
29 CallDataTypeAnswer CallDataType = "answer"
30 )
31
32 type CallData struct {
33 SDP string `json:"sdp"`
34 Type CallDataType `json:"type"`
35 }
36
37 type CallCandidate struct {
38 Candidate string `json:"candidate"`
39 SDPMLineIndex int `json:"sdpMLineIndex"`
40 SDPMID string `json:"sdpMid"`
41 }
42
43 type CallVersion string
44
45 func (cv *CallVersion) UnmarshalJSON(raw []byte) error {
46 var numberVersion int
47 err := json.Unmarshal(raw, &numberVersion)
48 if err != nil {
49 var stringVersion string
50 err = json.Unmarshal(raw, &stringVersion)
51 if err != nil {
52 return fmt.Errorf("failed to parse CallVersion: %w", err)
53 }
54 *cv = CallVersion(stringVersion)
55 } else {
56 *cv = CallVersion(strconv.Itoa(numberVersion))
57 }
58 return nil
59 }
60
61 func (cv *CallVersion) MarshalJSON() ([]byte, error) {
62 for _, char := range *cv {
63 if char < '0' || char > '9' {
64 // The version contains weird characters, return as string.
65 return json.Marshal(string(*cv))
66 }
67 }
68 // The version consists of only ASCII digits, return as an integer.
69 return []byte(*cv), nil
70 }
71
72 func (cv *CallVersion) Int() (int, error) {
73 return strconv.Atoi(string(*cv))
74 }
75
76 type BaseCallEventContent struct {
77 CallID string `json:"call_id"`
78 PartyID string `json:"party_id"`
79 Version CallVersion `json:"version"`
80 }
81
82 type CallInviteEventContent struct {
83 BaseCallEventContent
84 Lifetime int `json:"lifetime"`
85 Offer CallData `json:"offer"`
86 }
87
88 type CallCandidatesEventContent struct {
89 BaseCallEventContent
90 Candidates []CallCandidate `json:"candidates"`
91 }
92
93 type CallRejectEventContent struct {
94 BaseCallEventContent
95 }
96
97 type CallAnswerEventContent struct {
98 BaseCallEventContent
99 Answer CallData `json:"answer"`
100 }
101
102 type CallSelectAnswerEventContent struct {
103 BaseCallEventContent
104 SelectedPartyID string `json:"selected_party_id"`
105 }
106
107 type CallNegotiateEventContent struct {
108 BaseCallEventContent
109 Lifetime int `json:"lifetime"`
110 Description CallData `json:"description"`
111 }
112
113 type CallHangupEventContent struct {
114 BaseCallEventContent
115 Reason CallHangupReason `json:"reason"`
116 }