-
Notifications
You must be signed in to change notification settings - Fork 1
/
live_lobby_service.go
125 lines (110 loc) · 4.95 KB
/
live_lobby_service.go
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
117
118
119
120
121
122
123
124
125
package stream
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/pion/webrtc/v3"
)
type LiveLobbyService struct {
lobbyManager liveLobbyManager
store storage
}
func NewLiveLobbyService(store storage, lobbyManager liveLobbyManager) *LiveLobbyService {
return &LiveLobbyService{
store: store,
lobbyManager: lobbyManager,
}
}
func (s *LiveLobbyService) CreateLobbyIngressEndpoint(ctx context.Context, sdp *webrtc.SessionDescription, stream *LiveStream, userId uuid.UUID) (*webrtc.SessionDescription, string, error) {
resource, err := s.lobbyManager.NewIngressResource(ctx, stream.Lobby.UUID, userId, sdp)
if err != nil {
return nil, "---", fmt.Errorf("accessing lobby: %w", err)
}
return resource.SDP, resource.Id, nil
}
func (s *LiveLobbyService) CreateLobbyEgressEndpoint(ctx context.Context, sdp *webrtc.SessionDescription, stream *LiveStream, userId uuid.UUID) (*webrtc.SessionDescription, string, error) {
resource, err := s.lobbyManager.NewEgressResource(ctx, stream.Lobby.UUID, userId, sdp)
if err != nil {
return nil, "---", fmt.Errorf("accessing lobby: %w", err)
}
return resource.SDP, resource.Id, nil
}
func (s *LiveLobbyService) LeaveLobby(ctx context.Context, stream *LiveStream, userId uuid.UUID) (bool, error) {
left, err := s.lobbyManager.LeaveLobby(ctx, stream.Lobby.UUID, userId)
if err != nil {
return false, fmt.Errorf("leave lobby: %w", err)
}
return left, nil
}
func (s *LiveLobbyService) StartLiveStream(ctx context.Context, stream *LiveStream, streamInfo *LiveStreamInfo, userId uuid.UUID) error {
if err := s.lobbyManager.StartLiveStream(ctx, stream.Lobby.UUID, streamInfo.StreamKey, streamInfo.RtmpUrl, userId); err != nil {
return fmt.Errorf("start live stream: %w", err)
}
return nil
}
func (s *LiveLobbyService) StopLiveStream(ctx context.Context, stream *LiveStream, userId uuid.UUID) error {
if err := s.lobbyManager.StopLiveStream(ctx, stream.Lobby.UUID, userId); err != nil {
return fmt.Errorf("stop live stream: %w", err)
}
return nil
}
// InitLobbyEgressEndpoint
// Deprecated: Because the Endpoint API is getting simpler
func (s *LiveLobbyService) InitLobbyEgressEndpoint(ctx context.Context, stream *LiveStream, userId uuid.UUID) (*webrtc.SessionDescription, error) {
resourceData, err := s.lobbyManager.InitLobbyEgressEndpoint(ctx, stream.Lobby.UUID, userId)
if err != nil {
return nil, fmt.Errorf("start listening lobby: %w", err)
}
if !resourceData.Active {
return nil, ErrLobbyNotActive
}
return resourceData.Offer, nil
}
// FinalCreateLobbyEgressEndpoint
// Deprecated: Because the Endpoint API is getting simpler
func (s *LiveLobbyService) FinalCreateLobbyEgressEndpoint(ctx context.Context, offer *webrtc.SessionDescription, stream *LiveStream, userId uuid.UUID) (bool, error) {
resourceData, err := s.lobbyManager.FinalCreateLobbyEgressEndpoint(ctx, stream.Lobby.UUID, userId, offer)
if err != nil {
return false, fmt.Errorf("listening lobby: %w", err)
}
if !resourceData.Active {
return false, ErrLobbyNotActive
}
return resourceData.Active, nil
}
// CreateMainStreamLobbyEgressEndpoint
// Deprecated: Because the Endpoint API is getting simpler
func (s *LiveLobbyService) CreateMainStreamLobbyEgressEndpoint(ctx context.Context, offer *webrtc.SessionDescription, stream *LiveStream, userId uuid.UUID) (*webrtc.SessionDescription, error) {
resourceData, err := s.lobbyManager.CreateMainStreamLobbyEgressEndpoint(ctx, stream.Lobby.UUID, userId, offer)
if err != nil {
return nil, fmt.Errorf("accessing lobby: %w", err)
}
return resourceData.Answer, nil
}
// CreateLobbyHostPipeConnection
// Deprecated: Because the Endpoint API is getting simpler
func (s *LiveLobbyService) CreateLobbyHostPipeConnection(ctx context.Context, offer *webrtc.SessionDescription, stream *LiveStream, instanceId uuid.UUID) (*webrtc.SessionDescription, string, error) {
resourceData, err := s.lobbyManager.CreateLobbyHostPipe(ctx, stream.Lobby.UUID, offer, instanceId)
if err != nil {
return nil, "", fmt.Errorf("creating lobby host pipe connection: %w", err)
}
return resourceData.Answer, "", nil
}
// CreateLobbyHostIngressConnection
// Deprecated: Because the Endpoint API is getting simpler
func (s *LiveLobbyService) CreateLobbyHostIngressConnection(ctx context.Context, offer *webrtc.SessionDescription, stream *LiveStream, instanceId uuid.UUID) (*webrtc.SessionDescription, string, error) {
resourceData, err := s.lobbyManager.CreateLobbyHostIngress(ctx, stream.Lobby.UUID, offer, instanceId)
if err != nil {
return nil, "", fmt.Errorf("creating lobby host ingress connection: %w", err)
}
return resourceData.Answer, "", nil
}
// CloseLobbyHostConnection
// Deprecated: Because the Endpoint API is getting simpler
func (s *LiveLobbyService) CloseLobbyHostConnection(ctx context.Context, stream *LiveStream, instanceId uuid.UUID) (bool, error) {
left, err := s.lobbyManager.CloseLobbyHostPipe(ctx, stream.Lobby.UUID, instanceId)
if err != nil {
return false, fmt.Errorf("closing host pipe: %w", err)
}
return left, nil
}