File size: 1,790 Bytes
02eac4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Factory functions for creating LeRobot Arena video clients
 */

import { VideoProducer } from './producer.js';
import { VideoConsumer } from './consumer.js';
import type { ParticipantRole, ClientOptions } from './types.js';

/**
 * Factory function to create the appropriate client based on role
 */
export function createClient(
  role: ParticipantRole,
  baseUrl = 'http://localhost:8000',
  options: ClientOptions = {}
): VideoProducer | VideoConsumer {
  if (role === 'producer') {
    return new VideoProducer(baseUrl, options);
  }
  if (role === 'consumer') {
    return new VideoConsumer(baseUrl, options);
  }
  throw new Error(`Invalid role: ${role}. Must be 'producer' or 'consumer'`);
}

/**
 * Create and connect a producer client
 */
export async function createProducerClient(
  baseUrl = 'http://localhost:8000',
  workspaceId?: string,
  roomId?: string,
  participantId?: string,
  options: ClientOptions = {}
): Promise<VideoProducer> {
  const producer = new VideoProducer(baseUrl, options);

  const roomData = await producer.createRoom(workspaceId, roomId);
  const connected = await producer.connect(roomData.workspaceId, roomData.roomId, participantId);

  if (!connected) {
    throw new Error('Failed to connect as video producer');
  }

  return producer;
}

/**
 * Create and connect a consumer client
 */
export async function createConsumerClient(
  workspaceId: string,
  roomId: string,
  baseUrl = 'http://localhost:8000',
  participantId?: string,
  options: ClientOptions = {}
): Promise<VideoConsumer> {
  const consumer = new VideoConsumer(baseUrl, options);
  const connected = await consumer.connect(workspaceId, roomId, participantId);

  if (!connected) {
    throw new Error('Failed to connect as video consumer');
  }

  return consumer;
}