mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-05-29 16:39:45 +00:00
feat(jazz): add room joining capability and support existing rooms
This commit is contained in:
@@ -195,6 +195,9 @@ func buildRoomURL(providerName, roomID string) string {
|
||||
case "telemost":
|
||||
return "https://telemost.yandex.ru/j/" + roomID
|
||||
case "jazz":
|
||||
if roomID == "" {
|
||||
return "any"
|
||||
}
|
||||
return roomID
|
||||
default:
|
||||
return roomID
|
||||
|
||||
@@ -130,3 +130,70 @@ func createRoom(ctx context.Context) (*RoomInfo, error) {
|
||||
ConnectorURL: preconnectResp.ConnectorURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func joinRoom(ctx context.Context, roomID string) (*RoomInfo, error) {
|
||||
clientID := uuid.New().String()
|
||||
headers := map[string]string{
|
||||
"X-Jazz-ClientId": clientID,
|
||||
"X-Jazz-AuthType": "ANONYMOUS",
|
||||
"X-Client-AuthType": "ANONYMOUS",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
preconnectPayload := map[string]any{
|
||||
"password": "",
|
||||
"jazzNextMigration": map[string]any{
|
||||
"b2bBaseRoomSupport": true,
|
||||
"demoRoomBaseSupport": true,
|
||||
"demoRoomVersionSupport": 2,
|
||||
"mediaWithoutAutoSubscribeSupport": true,
|
||||
"webinarSpeakerSupport": true,
|
||||
"webinarViewerSupport": true,
|
||||
"sdkRoomSupport": true,
|
||||
"sberclassRoomSupport": true,
|
||||
},
|
||||
}
|
||||
|
||||
preBody, err := json.Marshal(preconnectPayload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal preconnect payload: %w", err)
|
||||
}
|
||||
|
||||
preReq, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf("%s/room/%s/preconnect", apiBase, roomID),
|
||||
bytes.NewReader(preBody),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create preconnect request: %w", err)
|
||||
}
|
||||
|
||||
for k, v := range headers {
|
||||
preReq.Header.Set(k, v)
|
||||
}
|
||||
|
||||
client := protect.NewHTTPClient()
|
||||
preResp, err := client.Do(preReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("do preconnect request: %w", err)
|
||||
}
|
||||
defer preResp.Body.Close()
|
||||
|
||||
if preResp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("preconnect failed: status %d", preResp.StatusCode)
|
||||
}
|
||||
|
||||
var preconnectResp struct {
|
||||
ConnectorURL string `json:"connectorUrl"`
|
||||
}
|
||||
if err := json.NewDecoder(preResp.Body).Decode(&preconnectResp); err != nil {
|
||||
return nil, fmt.Errorf("decode preconnect response: %w", err)
|
||||
}
|
||||
|
||||
return &RoomInfo{
|
||||
RoomID: roomID,
|
||||
Password: "",
|
||||
ConnectorURL: preconnectResp.ConnectorURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -40,13 +40,23 @@ type Peer struct {
|
||||
groupID string
|
||||
}
|
||||
|
||||
func NewPeer(ctx context.Context, name string, onData func([]byte)) (*Peer, error) {
|
||||
roomInfo, err := createRoom(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create room: %w", err)
|
||||
}
|
||||
func NewPeer(ctx context.Context, roomID, name string, onData func([]byte)) (*Peer, error) {
|
||||
var roomInfo *RoomInfo
|
||||
var err error
|
||||
|
||||
log.Printf("Jazz room created: %s (password: %s)", roomInfo.RoomID, roomInfo.Password)
|
||||
if roomID == "" || roomID == "any" || roomID == "dummy" {
|
||||
roomInfo, err = createRoom(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create room: %w", err)
|
||||
}
|
||||
log.Printf("Jazz room created: %s (password: %s)", roomInfo.RoomID, roomInfo.Password)
|
||||
} else {
|
||||
roomInfo, err = joinRoom(ctx, roomID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("join room: %w", err)
|
||||
}
|
||||
log.Printf("Jazz joining room: %s", roomInfo.RoomID)
|
||||
}
|
||||
|
||||
return &Peer{
|
||||
name: name,
|
||||
|
||||
@@ -13,7 +13,7 @@ type jazzProvider struct {
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg provider.Config) (provider.Provider, error) {
|
||||
peer, err := NewPeer(ctx, cfg.Name, cfg.OnData)
|
||||
peer, err := NewPeer(ctx, cfg.RoomURL, cfg.Name, cfg.OnData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create jazz peer: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user