|
| 1 | +package cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/livekit/protocol/livekit" |
| 8 | + "github.com/livekit/protocol/logger" |
| 9 | + "github.com/livekit/protocol/rpc" |
| 10 | + "github.com/livekit/protocol/sip" |
| 11 | + "github.com/livekit/psrpc" |
| 12 | +) |
| 13 | + |
| 14 | +type CloudTestService struct { |
| 15 | + conf *IntegrationConfig |
| 16 | + |
| 17 | + psrpcClient rpc.SIPInternalClient |
| 18 | +} |
| 19 | + |
| 20 | +func NewCloudTestService(conf *IntegrationConfig, bus psrpc.MessageBus) (*CloudTestService, error) { |
| 21 | + c, err := rpc.NewSIPInternalClient(bus) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + |
| 26 | + return &CloudTestService{ |
| 27 | + conf: conf, |
| 28 | + psrpcClient: c, |
| 29 | + }, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (s *CloudTestService) CreateSIPParticipant(ctx context.Context, req *livekit.CreateSIPParticipantRequest) (*livekit.SIPParticipantInfo, error) { |
| 33 | + token, err := sip.BuildSIPToken(sip.SIPTokenParams{ |
| 34 | + APIKey: s.conf.ApiKey, |
| 35 | + APISecret: s.conf.ApiSecret, |
| 36 | + RoomName: req.RoomName, |
| 37 | + ParticipantIdentity: req.ParticipantIdentity, |
| 38 | + ParticipantName: req.ParticipantName, |
| 39 | + ParticipantMetadata: req.ParticipantMetadata, |
| 40 | + ParticipantAttributes: req.ParticipantAttributes, |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + logger.Errorw("failed to create SIP token", err) |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + callID := sip.NewCallID() |
| 48 | + trunk := &livekit.SIPOutboundTrunkInfo{} |
| 49 | + r, err := rpc.NewCreateSIPParticipantRequest(ProjectID, callID, Host, s.conf.WsUrl, token, req, trunk) |
| 50 | + if err != nil { |
| 51 | + logger.Errorw("failed to build CreateSIPParticipantRequest", err) |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + resp, err := s.psrpcClient.CreateSIPParticipant(ctx, s.conf.ClusterID, r, psrpc.WithRequestTimeout(time.Second*30)) |
| 56 | + if err != nil { |
| 57 | + logger.Errorw("failed to create SIP participant", err) |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + return &livekit.SIPParticipantInfo{ |
| 62 | + ParticipantId: resp.ParticipantId, |
| 63 | + ParticipantIdentity: resp.ParticipantIdentity, |
| 64 | + RoomName: req.RoomName, |
| 65 | + SipCallId: r.SipCallId, |
| 66 | + }, nil |
| 67 | +} |
0 commit comments