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

add VoIP fields for MSC3401 #77

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions event/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ var TypeMap = map[Type]reflect.Type{
CallSelectAnswer: reflect.TypeOf(CallSelectAnswerEventContent{}),
CallNegotiate: reflect.TypeOf(CallNegotiateEventContent{}),
CallHangup: reflect.TypeOf(CallHangupEventContent{}),

ToDeviceCallInvite: reflect.TypeOf(CallInviteEventContent{}),
ToDeviceCallCandidates: reflect.TypeOf(CallCandidatesEventContent{}),
ToDeviceCallAnswer: reflect.TypeOf(CallAnswerEventContent{}),
ToDeviceCallReject: reflect.TypeOf(CallRejectEventContent{}),
ToDeviceCallSelectAnswer: reflect.TypeOf(CallSelectAnswerEventContent{}),
ToDeviceCallNegotiate: reflect.TypeOf(CallNegotiateEventContent{}),
ToDeviceCallHangup: reflect.TypeOf(CallHangupEventContent{}),

FocusCallTrackSubscription: reflect.TypeOf(FocusCallTrackSubscriptionEventContent{}),
FocusCallNegotiate: reflect.TypeOf(FocusCallNegotiateEventContent{}),
FocusCallSDPStreamMetadataChanged: reflect.TypeOf(FocusCallSDPStreamMetadataChangedEventContent{}),
FocusCallPing: reflect.TypeOf(FocusCallPingEventContent{}),
FocusCallPong: reflect.TypeOf(FocusCallPongEventContent{}),
}

// Content stores the content of a Matrix event.
Expand Down Expand Up @@ -496,6 +510,41 @@ func (content *Content) AsCallHangup() *CallHangupEventContent {
}
return casted
}
func (content *Content) AsFocusCallTrackSubscription() *FocusCallTrackSubscriptionEventContent {
casted, ok := content.Parsed.(*FocusCallTrackSubscriptionEventContent)
if !ok {
return &FocusCallTrackSubscriptionEventContent{}
}
return casted
}
func (content *Content) AsFocusCallNegotiate() *FocusCallNegotiateEventContent {
casted, ok := content.Parsed.(*FocusCallNegotiateEventContent)
if !ok {
return &FocusCallNegotiateEventContent{}
}
return casted
}
func (content *Content) AsFocusCallSDPStreamMetadataChanged() *FocusCallSDPStreamMetadataChangedEventContent {
casted, ok := content.Parsed.(*FocusCallSDPStreamMetadataChangedEventContent)
if !ok {
return &FocusCallSDPStreamMetadataChangedEventContent{}
}
return casted
}
func (content *Content) AsFocusCallPing() *FocusCallPingEventContent {
casted, ok := content.Parsed.(*FocusCallPingEventContent)
if !ok {
return &FocusCallPingEventContent{}
}
return casted
}
func (content *Content) AsFocusCallPong() *FocusCallPongEventContent {
casted, ok := content.Parsed.(*FocusCallPongEventContent)
if !ok {
return &FocusCallPongEventContent{}
}
return casted
}
func (content *Content) AsModPolicy() *ModPolicyContent {
casted, ok := content.Parsed.(*ModPolicyContent)
if !ok {
Expand Down
28 changes: 28 additions & 0 deletions event/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (tc TypeClass) Name() string {
return "account data"
case ToDeviceEventType:
return "to-device"
case FocusEventType:
return "focus"
default:
return "unknown"
}
Expand All @@ -51,6 +53,8 @@ const (
AccountDataEventType
// Device-to-device events
ToDeviceEventType
// Focus events
FocusEventType
)

type Type struct {
Expand Down Expand Up @@ -80,6 +84,10 @@ func (et *Type) IsToDevice() bool {
return et.Class == ToDeviceEventType
}

func (et *Type) IsFocus() bool {
return et.Class == FocusEventType
}

func (et *Type) IsInRoomVerification() bool {
switch et.Type {
case InRoomVerificationStart.Type, InRoomVerificationReady.Type, InRoomVerificationAccept.Type,
Expand Down Expand Up @@ -124,6 +132,9 @@ func (et *Type) GuessClass() TypeClass {
CallInvite.Type, CallCandidates.Type, CallAnswer.Type, CallReject.Type, CallSelectAnswer.Type,
CallNegotiate.Type, CallHangup.Type, BeeperMessageStatus.Type:
return MessageEventType
case FocusCallTrackSubscription.Type, FocusCallNegotiate.Type, FocusCallSDPStreamMetadataChanged.Type, FocusCallPing.Type,
FocusCallPong.Type:
return FocusEventType
case ToDeviceRoomKey.Type, ToDeviceRoomKeyRequest.Type, ToDeviceForwardedRoomKey.Type, ToDeviceRoomKeyWithheld.Type:
return ToDeviceEventType
default:
Expand Down Expand Up @@ -253,4 +264,21 @@ var (
ToDeviceVerificationCancel = Type{"m.key.verification.cancel", ToDeviceEventType}

ToDeviceOrgMatrixRoomKeyWithheld = Type{"org.matrix.room_key.withheld", ToDeviceEventType}

ToDeviceCallInvite = Type{"m.call.invite", ToDeviceEventType}
ToDeviceCallCandidates = Type{"m.call.candidates", ToDeviceEventType}
ToDeviceCallAnswer = Type{"m.call.answer", ToDeviceEventType}
ToDeviceCallReject = Type{"m.call.reject", ToDeviceEventType}
ToDeviceCallSelectAnswer = Type{"m.call.select_answer", ToDeviceEventType}
ToDeviceCallNegotiate = Type{"m.call.negotiate", ToDeviceEventType}
ToDeviceCallHangup = Type{"m.call.hangup", ToDeviceEventType}
)

// Focus events
var (
FocusCallTrackSubscription = Type{"m.call.track_subscription", FocusEventType}
FocusCallNegotiate = Type{"m.call.negotiate", FocusEventType}
FocusCallSDPStreamMetadataChanged = Type{"m.call.sdp_stream_metadata_changed", FocusEventType}
FocusCallPing = Type{"m.call.ping", FocusEventType}
FocusCallPong = Type{"m.call.pong", FocusEventType}
)
87 changes: 74 additions & 13 deletions event/voip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import (
"encoding/json"
"fmt"
"strconv"

"maunium.net/go/mautrix/id"
)

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"
CallHangupICEFailed CallHangupReason = "ice_failed"
CallHangupInviteTimeout CallHangupReason = "invite_timeout"
CallHangupUserHangup CallHangupReason = "user_hangup"
CallHangupUserMediaFailed CallHangupReason = "user_media_failed"
CallHangupKeepAliveTimeout CallHangupReason = "keep_alive_timeout"
CallHangupUnknownError CallHangupReason = "unknown_error"
)

type CallDataType string
Expand Down Expand Up @@ -74,15 +77,46 @@ func (cv *CallVersion) Int() (int, error) {
}

type BaseCallEventContent struct {
CallID string `json:"call_id"`
PartyID string `json:"party_id"`
Version CallVersion `json:"version"`
CallID string `json:"call_id"`
ConfID string `json:"conf_id"`
PartyID string `json:"party_id"`
Version CallVersion `json:"version"`
DeviceID id.DeviceID `json:"device_id"`
DestSessionID id.SessionID `json:"dest_session_id"`
SenderSessionID id.SessionID `json:"sender_session_id"`
}

type CallSDPStreamMetadataPurpose string

const (
Usermedia CallSDPStreamMetadataPurpose = "m.usermedia"
Screenshare CallSDPStreamMetadataPurpose = "m.screenshare"
)

type CallSDPStreamMetadataTrack struct {
Kind string `json:"kind,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}

type CallSDPStreamMetadataTracks map[string]CallSDPStreamMetadataTrack

type CallSDPStreamMetadataObject struct {
UserID id.UserID `json:"user_id"`
DeviceID id.DeviceID `json:"device_id"`
Purpose CallSDPStreamMetadataPurpose `json:"purpose"`
AudioMuted bool `json:"audio_muted"`
VideoMuted bool `json:"video_muted"`
Tracks CallSDPStreamMetadataTracks `json:"tracks"`
}

type CallSDPStreamMetadata map[string]CallSDPStreamMetadataObject

type CallInviteEventContent struct {
BaseCallEventContent
Lifetime int `json:"lifetime"`
Offer CallData `json:"offer"`
Lifetime int `json:"lifetime"`
Offer CallData `json:"offer"`
SDPStreamMetadata CallSDPStreamMetadata `json:"org.matrix.msc3077.sdp_stream_metadata"`
}

type CallCandidatesEventContent struct {
Expand All @@ -96,7 +130,8 @@ type CallRejectEventContent struct {

type CallAnswerEventContent struct {
BaseCallEventContent
Answer CallData `json:"answer"`
Answer CallData `json:"answer"`
SDPStreamMetadata CallSDPStreamMetadata `json:"org.matrix.msc3077.sdp_stream_metadata"`
}

type CallSelectAnswerEventContent struct {
Expand All @@ -106,11 +141,37 @@ type CallSelectAnswerEventContent struct {

type CallNegotiateEventContent struct {
BaseCallEventContent
Lifetime int `json:"lifetime"`
Description CallData `json:"description"`
Lifetime int `json:"lifetime"`
Description CallData `json:"description"`
SDPStreamMetadata CallSDPStreamMetadata `json:"org.matrix.msc3077.sdp_stream_metadata"`
}

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

type FocusTrackDescription struct {
StreamID string `json:"stream_id"`
TrackID string `json:"track_id"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}

type FocusCallTrackSubscriptionEventContent struct {
Subscribe []FocusTrackDescription `json:"subscribe"`
Unsubscribe []FocusTrackDescription `json:"unsubscribe"`
}

type FocusCallNegotiateEventContent struct {
Description CallData `json:"description"`
SDPStreamMetadata CallSDPStreamMetadata `json:"sdp_stream_metadata"`
}

type FocusCallSDPStreamMetadataChangedEventContent struct {
SDPStreamMetadata CallSDPStreamMetadata `json:"sdp_stream_metadata"`
}

type FocusCallPingEventContent struct{}

type FocusCallPongEventContent struct{}