summary refs log tree commit diff
path: root/vendor/maunium.net/go/mautrix/event/voip.go
blob: 28f56c9533d1797420410a91623e04f9f85f17c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2021 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package event

import (
	"encoding/json"
	"fmt"
	"strconv"
)

type CallHangupReason string

const (
	CallHangupICEFailed       CallHangupReason = "ice_failed"
	CallHangupInviteTimeout   CallHangupReason = "invite_timeout"
	CallHangupUserHangup      CallHangupReason = "user_hangup"
	CallHangupUserMediaFailed CallHangupReason = "user_media_failed"
	CallHangupUnknownError    CallHangupReason = "unknown_error"
)

type CallDataType string

const (
	CallDataTypeOffer  CallDataType = "offer"
	CallDataTypeAnswer CallDataType = "answer"
)

type CallData struct {
	SDP  string       `json:"sdp"`
	Type CallDataType `json:"type"`
}

type CallCandidate struct {
	Candidate     string `json:"candidate"`
	SDPMLineIndex int    `json:"sdpMLineIndex"`
	SDPMID        string `json:"sdpMid"`
}

type CallVersion string

func (cv *CallVersion) UnmarshalJSON(raw []byte) error {
	var numberVersion int
	err := json.Unmarshal(raw, &numberVersion)
	if err != nil {
		var stringVersion string
		err = json.Unmarshal(raw, &stringVersion)
		if err != nil {
			return fmt.Errorf("failed to parse CallVersion: %w", err)
		}
		*cv = CallVersion(stringVersion)
	} else {
		*cv = CallVersion(strconv.Itoa(numberVersion))
	}
	return nil
}

func (cv *CallVersion) MarshalJSON() ([]byte, error) {
	for _, char := range *cv {
		if char < '0' || char > '9' {
			// The version contains weird characters, return as string.
			return json.Marshal(string(*cv))
		}
	}
	// The version consists of only ASCII digits, return as an integer.
	return []byte(*cv), nil
}

func (cv *CallVersion) Int() (int, error) {
	return strconv.Atoi(string(*cv))
}

type BaseCallEventContent struct {
	CallID  string      `json:"call_id"`
	PartyID string      `json:"party_id"`
	Version CallVersion `json:"version"`
}

type CallInviteEventContent struct {
	BaseCallEventContent
	Lifetime int      `json:"lifetime"`
	Offer    CallData `json:"offer"`
}

type CallCandidatesEventContent struct {
	BaseCallEventContent
	Candidates []CallCandidate `json:"candidates"`
}

type CallRejectEventContent struct {
	BaseCallEventContent
}

type CallAnswerEventContent struct {
	BaseCallEventContent
	Answer CallData `json:"answer"`
}

type CallSelectAnswerEventContent struct {
	BaseCallEventContent
	SelectedPartyID string `json:"selected_party_id"`
}

type CallNegotiateEventContent struct {
	BaseCallEventContent
	Lifetime    int      `json:"lifetime"`
	Description CallData `json:"description"`
}

type CallHangupEventContent struct {
	BaseCallEventContent
	Reason CallHangupReason `json:"reason"`
}