@apirtc/react-lib 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 ApiRTC
3
+ Copyright (c) 2023 ApiRTC
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -14,62 +14,76 @@ requires to have @apirtc/apirtc peer dependency installed too:
14
14
 
15
15
  ### useSession
16
16
 
17
- ```
17
+ Get a stateful session:
18
+
19
+ ```ts
18
20
  import { useSession } from '@apirtc/react-lib'
19
21
  const { session } = useSession({ apiKey: 'your_api_key' });
20
22
  ```
21
23
  ### useUserMediaDevices
22
24
 
23
- ```
25
+ Get a stateful list of available media devices:
26
+
27
+ ```ts
24
28
  import { useUserMediaDevices } from '@apirtc/react-lib'
25
29
  const { userMediaDevices } = useUserMediaDevices(session);
26
30
  ```
27
31
 
28
32
  ### useCameraStream
29
33
 
30
- ```
34
+ Get a stateful value for the camera stream:
35
+
36
+ ```ts
31
37
  import { useCameraStream } from '@apirtc/react-lib'
32
38
  const { stream } = useCameraStream(session);
33
39
  ```
34
40
 
35
41
  ### useStreamApplyVideoProcessor
36
42
 
37
- ```
43
+ ```ts
38
44
  import { useStreamApplyVideoProcessor } from '@apirtc/react-lib'
39
45
  const { stream: blurredStream } = useStreamApplyVideoProcessor(stream, 'blur');
40
46
  ```
41
47
 
42
48
  ### usePresence
43
49
 
44
- ```
50
+ Get a stateful map of contacts by group:
51
+
52
+ ```ts
45
53
  import { usePresence } from '@apirtc/react-lib'
46
54
  const { contactsByGroup } = usePresence(session, ['groupName1', 'groupName2']);
47
55
  ```
48
56
 
49
57
  ### useConversation
50
58
 
51
- ```
59
+ Get a stateful **Conversation**:
60
+
61
+ ```ts
52
62
  import { useConversation } from '@apirtc/react-lib'
53
63
  const { conversation } = useConversation(session, 'conversationName', undefined, true);
54
64
  ```
55
65
 
56
66
  ### useConversationModeration
57
67
 
58
- ```
68
+ Get a set of candidates **Contacts**, and get notified of ejection:
69
+
70
+ ```ts
59
71
  import { useConversationModeration } from '@apirtc/react-lib'
60
- const { candidates } = useConversationModeration(conversation);
72
+ const { candidates, onEjected, onEjectedSelf } = useConversationModeration(conversation);
61
73
  ```
62
74
 
63
75
  ### useConversationMessages
64
76
 
65
- ```
77
+ ```ts
66
78
  import { useConversationMessages } from '@apirtc/react-lib'
67
79
  const { messages, sendMessage } = useConversationMessages(conversation);
68
80
  ```
69
81
 
70
82
  ### useConversationStreams
71
83
 
72
- ```
84
+ Control **Stream**s to publish, and get stateful arrays of published and subscribed **Stream**s:
85
+
86
+ ```ts
73
87
  import { useConversationStreams } from '@apirtc/react-lib'
74
88
  const { publishedStreams, subscribedStreams } = useConversationStreams(
75
89
  conversation, stream ? [{ stream: stream }] : []);
@@ -81,7 +95,7 @@ const { publishedStreams, subscribedStreams } = useConversationStreams(
81
95
 
82
96
  Use it to display any **ApiRTC** **Stream**.
83
97
 
84
- ```
98
+ ```tsx
85
99
  import { VideoStream } from '@apirtc/react-lib'
86
100
 
87
101
  <VideoStream stream={stream} muted={false}></VideoStream>
@@ -98,7 +112,7 @@ Available log levels:
98
112
 
99
113
  from web app code:
100
114
 
101
- ```
115
+ ```ts
102
116
  import { setLogLevel } from '@apirtc/react-lib'
103
117
 
104
118
  setLogLevel('warn')
@@ -106,6 +120,6 @@ setLogLevel('warn')
106
120
 
107
121
  from console:
108
122
 
109
- ```
123
+ ```js
110
124
  setApirtcReactLibLogLevel('debug')
111
125
  ```
@@ -1,8 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { Stream } from '@apirtc/apirtc';
3
- export interface VideoStreamProps {
3
+ export type VideoStreamProps = {
4
4
  stream: Stream;
5
5
  autoPlay?: boolean;
6
6
  muted?: boolean;
7
- }
7
+ };
8
8
  export default function VideoStream(props: VideoStreamProps): JSX.Element;
@@ -1 +1 @@
1
- export { default } from "./VideoStream";
1
+ export { default, VideoStreamProps } from "./VideoStream";
@@ -1 +1 @@
1
- export { default as VideoStream } from "./VideoStream";
1
+ export { default as VideoStream, VideoStreamProps } from "./VideoStream";
@@ -1,8 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { Stream } from '@apirtc/apirtc';
3
- export interface VideoStreamProps {
3
+ export type VideoStreamProps = {
4
4
  stream: Stream;
5
5
  autoPlay?: boolean;
6
6
  muted?: boolean;
7
- }
7
+ };
8
8
  export default function VideoStream(props: VideoStreamProps): JSX.Element;
@@ -1 +1 @@
1
- export { default } from "./VideoStream";
1
+ export { default, VideoStreamProps } from "./VideoStream";
@@ -1 +1 @@
1
- export { default as VideoStream } from "./VideoStream";
1
+ export { default as VideoStream, VideoStreamProps } from "./VideoStream";
@@ -4,6 +4,6 @@ export { default as useConversationMessages } from "./useConversationMessages";
4
4
  export { default as useConversationModeration } from "./useConversationModeration";
5
5
  export { default as useConversationStreams } from "./useConversationStreams";
6
6
  export { default as usePresence } from "./usePresence";
7
- export { default as useSession, Credentials } from "./useSession";
7
+ export { ApiKey, Credentials, default as useSession, LoginPassword, Token } from "./useSession";
8
8
  export { default as useStreamApplyVideoProcessor } from './useStreamApplyVideoProcessor';
9
9
  export { default as useUserMediaDevices } from "./useUserMediaDevices";
@@ -1,6 +1,10 @@
1
1
  import { Conversation, GetOrCreateConversationOptions, Session } from '@apirtc/apirtc';
2
2
  /**
3
- * Please note that the hook won't react on autoJoin change
3
+ * A hook to getOrCreate a named conversation and manage join/leave
4
+ * @param session an ApiRTC Session
5
+ * @param name the conversation name
6
+ * @param options getOrCreateConversation options
7
+ * @param join true by default.
4
8
  */
5
9
  export default function useConversation(session: Session | undefined, name: string | undefined, options?: GetOrCreateConversationOptions, join?: boolean): {
6
10
  conversation: Conversation | undefined;
@@ -1,4 +1,4 @@
1
- import { Session, Contact } from '@apirtc/apirtc';
1
+ import { Contact, Session } from '@apirtc/apirtc';
2
2
  export default function usePresence(session: Session | undefined, groups: Array<string>): {
3
3
  contactsByGroup: Map<string, Set<Contact>>;
4
4
  };
@@ -1,12 +1,12 @@
1
- import { Session, RegisterInformation } from '@apirtc/apirtc';
2
- type LoginPassword = {
1
+ import { RegisterInformation, Session } from '@apirtc/apirtc';
2
+ export type LoginPassword = {
3
3
  username: string;
4
4
  password: string;
5
5
  };
6
- type ApiKey = {
6
+ export type ApiKey = {
7
7
  apiKey: string;
8
8
  };
9
- type Token = {
9
+ export type Token = {
10
10
  token: string;
11
11
  };
12
12
  export type Credentials = LoginPassword | ApiKey | Token;
@@ -17,4 +17,3 @@ export default function useSession(credentials?: Credentials, options?: Register
17
17
  disconnect: () => void;
18
18
  error: any;
19
19
  };
20
- export {};
@@ -8,7 +8,9 @@ import { Stream, VideoProcessorOptions } from '@apirtc/apirtc';
8
8
  * The hook never releases the input stream.
9
9
  *
10
10
  * @param stream
11
- * @returns stream blurred or not, toggle method, boolean blurred state.
11
+ * @param videoProcessorType
12
+ * @param {VideoProcessorOptions} options
13
+ * @returns new stream with video processor applied (or original stream if no processor applied)
12
14
  */
13
15
  export default function useStreamApplyVideoProcessor(stream: Stream | undefined, videoProcessorType: 'none' | 'blur' | 'backgroundImage', options?: VideoProcessorOptions, errorCallback?: (error: any) => void): {
14
16
  stream: Stream | undefined;
@@ -1,6 +1,6 @@
1
1
  export * from './components';
2
2
  export * from './hooks';
3
- type LogLevel = {
3
+ export type LogLevel = {
4
4
  level: 'debug' | 'info' | 'warn' | 'error';
5
5
  isDebugEnabled: boolean;
6
6
  isInfoEnabled: boolean;
@@ -8,6 +8,6 @@ type LogLevel = {
8
8
  };
9
9
  declare global {
10
10
  var apirtcReactLibLogLevel: LogLevel;
11
- var setApirtcReactLibLogLevel: Function;
11
+ var setApirtcReactLibLogLevel: (logLevelText: 'debug' | 'info' | 'warn' | 'error') => void;
12
12
  }
13
- export declare function setLogLevel(logLevelText: 'debug' | 'info' | 'warn' | 'error' | string): LogLevel;
13
+ export declare function setLogLevel(logLevelText: 'debug' | 'info' | 'warn' | 'error'): LogLevel;
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import e,{useRef as o,useEffect as n,useState as t,useCallback as i}from"react";import{UserAgent as a}from"@apirtc/apirtc";function s(t){const{autoPlay:i=!0}=t,a=o(null);return n((()=>{const e=a.current;if(e&&t.stream)return t.stream.attachToElement(e),()=>{e.src=""}}),[t.stream]),e.createElement("video",{id:t.stream.getId(),style:{maxWidth:"100%"},ref:a,autoPlay:i,muted:t.muted})}const r="useCameraStream";function l(e,o={}){const[i,a]=t();return n((()=>{if(e){e.getUserAgent().createStream(o).then((e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(r+"|createStream",o,e),a(e)})).catch((e=>{console.error(r+"|createStream",o,e),a(void 0)}))}else a(void 0)}),[e,JSON.stringify(o)]),n((()=>()=>{i&&(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(r+"|release stream",i),i.release())}),[i]),{stream:i}}const c="useConversation";function b(e,o,a,s=!1){const[r,l]=t(),[b,g]=t(!1),[d,u]=t(!1),L=i((()=>(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|join",r),new Promise(((e,o)=>{r?r.isJoined()?o(c+"|join|conversation already joined"):(u(!0),r.join().then((()=>{g(!0),e()})).catch((e=>{o(e)})).finally((()=>{u(!1)}))):o(c+"|join|conversation not defined")})))),[r]),f=i((()=>(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|leave",r),new Promise(((e,o)=>{r?r.isJoined()?r.leave().then((()=>{g(!1),e()})).catch((e=>{o(e)})):o(c+"|leave|conversation is not joined"):o(c+"|leave|conversation not defined")})))),[r]);return n((()=>{if(e&&o){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|getOrCreateConversation",o,a,s);const n=e.getOrCreateConversation(o,a);return l(n),()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|useEffect cleanup",o,a,s),n.isJoined()?n.leave().then((()=>{})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(c+"|useEffect conversation.leave()",e)})).finally((()=>{n.destroy(),l(void 0),g(!1)})):(n.destroy(),l(void 0))}}}),[e,o,JSON.stringify(a)]),n((()=>{if(r&&s){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|useEffect",r,s);const e=r,o=s;return o&&(u(!0),e.join().then((()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(c+"|joined",e),g(!0)})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(c+"|useEffect conversation.join()",e)})).finally((()=>{u(!1)}))),()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|useEffect cleanup",e,o),e.isJoined()&&e.leave().then((()=>{g(!1)})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(c+"|useEffect conversation.leave()",e)}))}}}),[r,s]),{conversation:r,joining:d,joined:b,join:L,leave:f}}const g="useConversationMessages";function d(e){const[o]=t(new Array),[a,s]=t(new Array);n((()=>{if(e){const n=n=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(g+"|on:message:",e.getName(),n),o.push(n),s(Array.from(o))};return e.on("message",n),()=>{e.removeListener("message",n),o.length=0,s(new Array)}}}),[e]);return{messages:a,sendMessage:i(((n,t)=>new Promise(((i,a)=>{null==e||e.sendMessage(n).then((a=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(g+"|sentMessage",e.getName(),a,n),o.push({content:n,sender:t,time:new Date}),s(Array.from(o)),i()})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(g+"|sendMessage error",e),a(e)}))}))),[e])}}const u="useConversationModeration";function L(e,o,i){const[a,s]=t(new Set);return n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(u+"|useEffect conversation",e),e){const n=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|on:contactJoinedWaitingRoom",e),a.add(e),s(new Set(a))},t=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|on:contactLeftWaitingRoom",e),a.delete(e),s(new Set(a))},r=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|on:participantEjected",e),!0===e.self?(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|Self participant was ejected"),i&&i()):o&&o(e.contact)};return e.on("contactJoinedWaitingRoom",n).on("contactLeftWaitingRoom",t).on("participantEjected",r),()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(u+"|conversation clear",e),e.removeListener("contactJoinedWaitingRoom",n).removeListener("contactLeftWaitingRoom",t).removeListener("participantEjected",r),s(new Set)}}}),[e]),{candidates:a}}function f(e){return null!=e}const p="useConversationStreams";function h(e,o=[],a){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|hook`);const[s,r]=t([]),[l]=t(new Array),[c,b]=t(new Array),[g]=t(new Array),[d,u]=t(new Array),L=i(((o,n)=>new Promise(((t,i)=>{e&&(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|publish|${e.getName()}`,o,n),e.publish(o,n).then((o=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(`${p}|published|${e.getName()}`,o),l.push(o),b(Array.from(l)),t(o)})).catch((e=>{i(e)})))}))),[e]),h=i(((o,n,t)=>new Promise(((i,a)=>{if(e){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|replacePublishedStream|${e.getName()}|${o.getId()} -> ${n.getId()}(${JSON.stringify(t)})`);const s=e.getConversationCall(o);s&&s.replacePublishedStream(n,void 0,t).then((n=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(`${p}|stream replaced|${e.getName()}`,o,n,t);const a=l.indexOf(o);a>=0&&(l.splice(a,1,n),b(Array.from(l))),i(n)})).catch((e=>{a(e)}))}}))),[e]),v=i((o=>{if(e){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|unpublish|${e.getName()}`,o),e.unpublish(o);const n=l.indexOf(o);n>=0&&(l.splice(n,1),b(Array.from(l)))}}),[e]),m=i((()=>{const n=Math.max(s.length,o.length);globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|doHandlePublication",o,JSON.stringify(s.map((e=>null==e?void 0:e.stream.getId()))),n);const t=[...o];r(t);const i=new Set(o.filter(f).map((e=>e.stream)));for(let r=0;r<n;r++){const n=s[r],l=o[r];if(n&&l){const o=()=>{h(n.stream,l.stream,l.options).catch((e=>{t.splice(r,1,null),a?a(e):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(`${p}|replacePublishedStream|error`,e)}))};n.stream===l.stream?JSON.stringify(n.options)!==JSON.stringify(l.options)&&o():i.has(n.stream)?e&&!e.isPublishedStream(l.stream)&&L(l.stream,l.options).catch((e=>{t.splice(r,1,null),a?a(e):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(`${p}|publish|error`,e)})):e&&!e.isPublishedStream(l.stream)?o():v(n.stream)}else n&&!l?i.has(n.stream)||v(n.stream):!n&&l&&e&&!e.isPublishedStream(l.stream)&&L(l.stream,l.options).catch((e=>{t.splice(r,1,null),a?a(e):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(`${p}|publish|error`,e)}))}}),[e,o,s,L,v,h]);n((()=>{if(e){const o=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_streamAdded",e),g.push(e),u(Array.from(g))},n=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_streamRemoved",e);const o=g.indexOf(e);o>=0&&(g.splice(o,1),u(Array.from(g)))},t=o=>{const n=String(o.streamId);!0===o.isRemote&&("added"===o.listEventType?e.subscribeToStream(n):"removed"===o.listEventType&&e.unsubscribeToStream(n))};return e.on("streamAdded",o),e.on("streamRemoved",n),e.on("streamListChanged",t),()=>{e.removeListener("streamListChanged",t),e.removeListener("streamRemoved",n),e.removeListener("streamAdded",o)}}}),[e]);const E=e=>{l.forEach((o=>{e.unpublish(o)})),l.length=0,r([]),g.forEach((o=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|unsubscribeToStream stream",e,o),e.unsubscribeToStream(o.getId())})),g.length=0,b(new Array),u(new Array)};return n((()=>{if(e){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|useEffect doHandlePublication",e.getName());const n=()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_joined",e.getName(),o),m()},t=()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_left",e.getName()),E(e)};return e.on("joined",n),e.on("left",t),()=>{e.removeListener("joined",n),e.removeListener("left",t)}}}),[m]),n((()=>{if(e)return globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|conversation|${e.getName()}`,e),e.getAvailableStreamList().forEach((o=>{const n=String(o.streamId);!0===o.isRemote&&e.subscribeToStream(n)})),()=>{E(e)}}),[e]),n((()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|streamsToPublish`,JSON.stringify(o.map((e=>(null==e?void 0:e.stream.getId())+"-"+JSON.stringify(null==e?void 0:e.options))))),e&&e.isJoined()&&m()}),[JSON.stringify(o.map((e=>(null==e?void 0:e.stream.getId())+"-"+JSON.stringify(null==e?void 0:e.options))))]),{publishedStreams:c,subscribedStreams:d,publish:L,unpublish:v,replacePublishedStream:h}}const v="usePresence";function m(e,o){const[i]=t(new Set),[a]=t(new Map),[s,r]=t(new Map);n((()=>{if(e)return()=>{a.clear(),r(new Map(a)),i.clear()}}),[e]);const l=e=>{var o;const n=null!==(o=a.get(e))&&void 0!==o?o:new Set;return a.has(e)||a.set(e,n),n};return n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(v+"|useEffect session, groups",o),e){const n=e,t=new Set(o);t.forEach((e=>{i.has(e)||(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(v+"|subscribeToGroup",e),i.add(e),n.subscribeToGroup(e))}));let s=!1;if(i.forEach((e=>{t.has(e)||(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(v+"|unsubscribeToGroup",e),n.unsubscribeToGroup(e),i.delete(e),a.delete(e),s=!0)})),s&&r(new Map(a)),o.length>0){const e=e=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(v+"|contactListUpdate",e);let o=!1;for(const n of Object.keys(e.joinedGroup))if(t.has(n)){const t=l(n);for(const i of e.joinedGroup[n])t.add(i),o=!0}for(const n of Object.keys(e.leftGroup))if(t.has(n)){const t=l(n);for(const i of e.leftGroup[n])t.delete(i),o=!0,0===t.size&&a.delete(n)}for(const n of e.userDataChanged)for(const e of a.values())if(e.has(n)){o=!0;break}o&&r(new Map(a))};return n.on("contactListUpdate",e),()=>{n.removeListener("contactListUpdate",e)}}}}),[e,JSON.stringify(o)]),{contactsByGroup:s}}const E="useSession";function T(e,o){const[i,s]=t(),[r,l]=t(!1),[c,b]=t();n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(E+"|useEffect credentials, options",e,o),e){let n=!0;return g(e,o).catch((e=>{console.error(E+"|connection failed",e,n),s(void 0),n&&b(e)})),()=>{n=!1,s(void 0),l(!1),b(void 0)}}}),[JSON.stringify(e),JSON.stringify(o)]),n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(E+"|useEffect session",i),i){const e=i;return()=>{e.disconnect().then((()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(E+"|disconnected",e)})).catch((e=>{console.error(E+"|disconnect",e)}))}}}),[i]);const g=(e,o)=>new Promise(((n,t)=>{const i=o||{cloudUrl:"https://cloud.apirtc.com"};let r;if("object"==typeof(c=e)&&"username"in c)i.password=e.password,r=new a({uri:"apirtc:"+e.username});else if(function(e){return"object"==typeof e&&"apiKey"in e}(e))r=new a({uri:`apiKey:${e.apiKey}`});else{if(!function(e){return"object"==typeof e&&"token"in e}(e))return void t("credentials not recognized");r=new a({uri:`token:${e.token}`})}var c;l(!0),r.register(i).then((e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(E+"|connected",e),s(e),n()})).catch((e=>{t(e)})).finally((()=>{l(!1)}))}));return{session:i,connecting:r,connect:g,disconnect:()=>{s(void 0)},error:c}}const R="useStreamApplyVideoProcessor";function y(e,o,a,s){const[r,l]=t(e),[c,b]=t("none");n((()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(R+"|useEffect",e,o,a),e&&"none"!==o?e.applyVideoProcessor(o,a).then((e=>{l(e),b(o)})).catch((n=>{l(e),s?s(n):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(R+"|useEffect",e,o,a,n),b((e=>e))})):(l(e),b("none"))}),[e,o,a]);const g=i((()=>{r&&r!==e&&(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(R+"|releasing outStream",r),r.release())}),[e,r]);return n((()=>()=>{g()}),[r]),{stream:r,applied:c}}const S={audioinput:{},audiooutput:{},videoinput:{}};function w(e){const[o,i]=t(S);return n((()=>{if(e){const o=e.getUserAgent(),n=()=>{const e=o.getUserMediaDevices();globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info("useUserMediaDevices|mediaDeviceChanged",e),i(e)};return o.on("mediaDeviceChanged",n),()=>{o.removeListener("mediaDeviceChanged",n),i(S)}}}),[e]),{userMediaDevices:o}}const D={level:"info",isDebugEnabled:!1,isInfoEnabled:!0,isWarnEnabled:!0};function I(e){switch(e){case"debug":globalThis.apirtcReactLibLogLevel={level:"debug",isDebugEnabled:!0,isInfoEnabled:!0,isWarnEnabled:!0};break;case"info":default:globalThis.apirtcReactLibLogLevel=D;break;case"warn":globalThis.apirtcReactLibLogLevel={level:"warn",isDebugEnabled:!1,isInfoEnabled:!1,isWarnEnabled:!0};break;case"error":globalThis.apirtcReactLibLogLevel={level:"error",isDebugEnabled:!1,isInfoEnabled:!1,isWarnEnabled:!1}}return globalThis.apirtcReactLibLogLevel}globalThis.apirtcReactLibLogLevel=D,globalThis.setApirtcReactLibLogLevel=I;export{s as VideoStream,I as setLogLevel,l as useCameraStream,b as useConversation,d as useConversationMessages,L as useConversationModeration,h as useConversationStreams,m as usePresence,T as useSession,y as useStreamApplyVideoProcessor,w as useUserMediaDevices};
1
+ import e,{useRef as o,useEffect as n,useState as i,useCallback as t}from"react";import{UserAgent as a}from"@apirtc/apirtc";function s(i){const{autoPlay:t=!0}=i,a=o(null);return n((()=>{const e=a.current;if(e&&i.stream)return i.stream.attachToElement(e),()=>{e.src=""}}),[i.stream]),e.createElement("video",{id:i.stream.getId(),style:{maxWidth:"100%"},ref:a,autoPlay:t,muted:i.muted})}const l="useCameraStream";function r(e,o={}){const[t,a]=i();return n((()=>{if(e){e.getUserAgent().createStream(o).then((e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(l+"|createStream",o,e),a(e)})).catch((e=>{console.error(l+"|createStream",o,e),a(void 0)}))}else a(void 0)}),[e,JSON.stringify(o)]),n((()=>()=>{t&&(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(l+"|release stream",t),t.release())}),[t]),{stream:t}}const c="useConversation";function b(e,o,a,s=!0){const[l,r]=i(),[b,g]=i(!1),[d,u]=i(!1),L=t((()=>(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|join",l),new Promise(((e,o)=>{l?l.isJoined()?o(c+"|join|conversation already joined"):(u(!0),l.join().then((()=>{g(!0),e()})).catch((e=>{o(e)})).finally((()=>{u(!1)}))):o(c+"|join|conversation not defined")})))),[l]),f=t((()=>(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|leave",l),new Promise(((e,o)=>{l?l.isJoined()?l.leave().then((()=>{g(!1),e()})).catch((e=>{o(e)})):o(c+"|leave|conversation is not joined"):o(c+"|leave|conversation not defined")})))),[l]);return n((()=>{if(e&&o){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|getOrCreateConversation",o,a,s);const n=e.getOrCreateConversation(o,a);return r(n),()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|useEffect cleanup",o,a,s),n.isJoined()?n.leave().then((()=>{})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(c+"|useEffect conversation.leave()",e)})).finally((()=>{n.destroy(),r(void 0),g(!1)})):(n.destroy(),r(void 0))}}}),[e,o,JSON.stringify(a)]),n((()=>{if(l&&s){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|useEffect",l,s);const e=l,o=s;return o&&(u(!0),e.join().then((()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(c+"|joined",e),g(!0)})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(c+"|useEffect conversation.join()",e)})).finally((()=>{u(!1)}))),()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(c+"|useEffect cleanup",e,o),e.isJoined()&&e.leave().then((()=>{g(!1)})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(c+"|useEffect conversation.leave()",e)}))}}}),[l,s]),{conversation:l,joining:d,joined:b,join:L,leave:f}}const g="useConversationMessages";function d(e){const[o]=i(new Array),[a,s]=i(new Array);n((()=>{if(e){const n=n=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(g+"|on:message:",e.getName(),n),o.push(n),s(Array.from(o))};return e.on("message",n),()=>{e.removeListener("message",n),o.length=0,s(new Array)}}}),[e]);return{messages:a,sendMessage:t(((n,i)=>new Promise(((t,a)=>{null==e||e.sendMessage(n).then((a=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(g+"|sentMessage",e.getName(),a,n),o.push({content:n,sender:i,time:new Date}),s(Array.from(o)),t()})).catch((e=>{globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(g+"|sendMessage error",e),a(e)}))}))),[e])}}const u="useConversationModeration";function L(e,o,t){const[a,s]=i(new Set);return n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(u+"|useEffect conversation",e),e){const n=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|on:contactJoinedWaitingRoom",e),a.add(e),s(new Set(a))},i=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|on:contactLeftWaitingRoom",e),a.delete(e),s(new Set(a))},l=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|on:participantEjected",e),!0===e.self?(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(u+"|Self participant was ejected"),t&&t()):o&&o(e.contact)};return e.on("contactJoinedWaitingRoom",n).on("contactLeftWaitingRoom",i).on("participantEjected",l),()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(u+"|conversation clear",e),e.removeListener("contactJoinedWaitingRoom",n).removeListener("contactLeftWaitingRoom",i).removeListener("participantEjected",l),s(new Set)}}}),[e]),{candidates:a}}function f(e){return null!=e}const p="useConversationStreams";function h(e,o=[],a){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|hook`);const[s,l]=i([]),[r]=i(new Array),[c,b]=i(new Array),[g]=i(new Array),[d,u]=i(new Array),L=t(((o,n)=>new Promise(((i,t)=>{e&&(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|publish|${e.getName()}`,o,n),e.publish(o,n).then((o=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(`${p}|published|${e.getName()}`,o),r.push(o),b(Array.from(r)),i(o)})).catch((e=>{t(e)})))}))),[e]),h=t(((o,n,i)=>new Promise(((t,a)=>{if(e){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|replacePublishedStream|${e.getName()}|${o.getId()} -> ${n.getId()}(${JSON.stringify(i)})`);const s=e.getConversationCall(o);s&&s.replacePublishedStream(n,void 0,i).then((n=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(`${p}|stream replaced|${e.getName()}`,o,n,i);const a=r.indexOf(o);a>=0&&(r.splice(a,1,n),b(Array.from(r))),t(n)})).catch((e=>{a(e)}))}}))),[e]),v=t((o=>{if(e){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|unpublish|${e.getName()}`,o),e.unpublish(o);const n=r.indexOf(o);n>=0&&(r.splice(n,1),b(Array.from(r)))}}),[e]),m=t((()=>{const n=Math.max(s.length,o.length);globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|doHandlePublication",o,JSON.stringify(s.map((e=>null==e?void 0:e.stream.getId()))),n);const i=[...o];l(i);const t=new Set(o.filter(f).map((e=>e.stream)));for(let l=0;l<n;l++){const n=s[l],r=o[l];if(n&&r){const o=()=>{h(n.stream,r.stream,r.options).catch((e=>{i.splice(l,1,null),a?a(e):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(`${p}|replacePublishedStream|error`,e)}))};n.stream===r.stream?JSON.stringify(n.options)!==JSON.stringify(r.options)&&o():t.has(n.stream)?e&&!e.isPublishedStream(r.stream)&&L(r.stream,r.options).catch((e=>{i.splice(l,1,null),a?a(e):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(`${p}|publish|error`,e)})):e&&!e.isPublishedStream(r.stream)?o():v(n.stream)}else n&&!r?t.has(n.stream)||v(n.stream):!n&&r&&e&&!e.isPublishedStream(r.stream)&&L(r.stream,r.options).catch((e=>{i.splice(l,1,null),a?a(e):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(`${p}|publish|error`,e)}))}}),[e,o,s,L,v,h]);n((()=>{if(e){const o=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_streamAdded",e),g.push(e),u(Array.from(g))},n=e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_streamRemoved",e);const o=g.indexOf(e);o>=0&&(g.splice(o,1),u(Array.from(g)))},i=o=>{const n=String(o.streamId);!0===o.isRemote&&("added"===o.listEventType?e.subscribeToStream(n):"removed"===o.listEventType&&e.unsubscribeToStream(n))};return e.on("streamAdded",o),e.on("streamRemoved",n),e.on("streamListChanged",i),()=>{e.removeListener("streamListChanged",i),e.removeListener("streamRemoved",n),e.removeListener("streamAdded",o)}}}),[e]);const E=e=>{r.forEach((o=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|unpublish stream",e,o),e.unpublish(o)})),r.length=0,l([]),g.forEach((o=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|unsubscribeToStream stream",e,o),e.unsubscribeToStream(o.getId())})),g.length=0,b(new Array),u(new Array)};return n((()=>{if(e){globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(p+"|useEffect doHandlePublication",e.getName());const n=()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_joined",e.getName(),o),m()},i=()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(p+"|on_left",e.getName()),E(e)};return e.on("joined",n),e.on("left",i),()=>{e.removeListener("joined",n),e.removeListener("left",i)}}}),[m]),n((()=>{if(e)return globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|conversation|${e.getName()}`,e),e.getAvailableStreamList().forEach((o=>{const n=String(o.streamId);!0===o.isRemote&&e.subscribeToStream(n)})),()=>{E(e)}}),[e]),n((()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(`${p}|streamsToPublish`,JSON.stringify(o.map((e=>(null==e?void 0:e.stream.getId())+"-"+JSON.stringify(null==e?void 0:e.options))))),e&&e.isJoined()&&m()}),[JSON.stringify(o.map((e=>(null==e?void 0:e.stream.getId())+"-"+JSON.stringify(null==e?void 0:e.options))))]),{publishedStreams:c,subscribedStreams:d,publish:L,unpublish:v,replacePublishedStream:h}}const v="usePresence";function m(e,o){const[t]=i(new Set),[a]=i(new Map),[s,l]=i(new Map);n((()=>{if(e)return()=>{a.clear(),l(new Map(a)),t.clear()}}),[e]);const r=e=>{var o;const n=null!==(o=a.get(e))&&void 0!==o?o:new Set;return a.has(e)||a.set(e,n),n};return n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(v+"|useEffect session, groups",o),e){const n=e,i=new Set(o);i.forEach((e=>{t.has(e)||(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(v+"|subscribeToGroup",e),t.add(e),n.subscribeToGroup(e))}));let s=!1;if(t.forEach((e=>{i.has(e)||(globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(v+"|unsubscribeToGroup",e),n.unsubscribeToGroup(e),t.delete(e),a.delete(e),s=!0)})),s&&l(new Map(a)),o.length>0){const e=e=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(v+"|contactListUpdate",e);let o=!1;for(const n of Object.keys(e.joinedGroup))if(i.has(n)){const i=r(n);for(const t of e.joinedGroup[n])i.add(t),o=!0}for(const n of Object.keys(e.leftGroup))if(i.has(n)){const i=r(n);for(const t of e.leftGroup[n])i.delete(t),o=!0,0===i.size&&a.delete(n)}for(const n of e.userDataChanged)for(const e of a.values())if(e.has(n)){o=!0;break}o&&l(new Map(a))};return n.on("contactListUpdate",e),()=>{n.removeListener("contactListUpdate",e)}}}}),[e,JSON.stringify(o)]),{contactsByGroup:s}}const E="useSession";function T(e,o){const[t,s]=i(),[l,r]=i(!1),[c,b]=i();n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(E+"|useEffect credentials, options",e,o),e){let n=!0;return g(e,o).catch((e=>{console.error(E+"|connection failed",e,n),s(void 0),n&&b(e)})),()=>{n=!1,s(void 0),r(!1),b(void 0)}}}),[JSON.stringify(e),JSON.stringify(o)]),n((()=>{if(globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(E+"|useEffect session",t),t){const e=t;return()=>{e.disconnect().then((()=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(E+"|disconnected",e)})).catch((e=>{console.error(E+"|disconnect",e)}))}}}),[t]);const g=(e,o)=>new Promise(((n,i)=>{const t=o||{cloudUrl:"https://cloud.apirtc.com"};let l;if("object"==typeof(c=e)&&"username"in c)t.password=e.password,l=new a({uri:"apirtc:"+e.username});else if(function(e){return"object"==typeof e&&"apiKey"in e}(e))l=new a({uri:`apiKey:${e.apiKey}`});else{if(!function(e){return"object"==typeof e&&"token"in e}(e))return void i("credentials not recognized");l=new a({uri:`token:${e.token}`})}var c;r(!0),l.register(t).then((e=>{globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info(E+"|connected",e),s(e),n()})).catch((e=>{i(e)})).finally((()=>{r(!1)}))}));return{session:t,connecting:l,connect:g,disconnect:()=>{s(void 0)},error:c}}const R="useStreamApplyVideoProcessor";function y(e,o,t,a){const[s,l]=i(e),[r,c]=i(e?e.videoAppliedFilter:"none");return n((()=>{globalThis.apirtcReactLibLogLevel.isDebugEnabled&&console.debug(R+"|useEffect",e,o,t,e?JSON.stringify(e.children):"empty"),e?e.applyVideoProcessor(o,t).then((e=>{l(e),c(o)})).catch((n=>{l(e),c(e.videoAppliedFilter),a?a(n):globalThis.apirtcReactLibLogLevel.isWarnEnabled&&console.warn(R+"|useEffect",e,o,t,n)})):(l(e),c(e?e.videoAppliedFilter:"none"))}),[e,o,JSON.stringify(t)]),{stream:s,applied:r}}const S={audioinput:{},audiooutput:{},videoinput:{}};function w(e){const[o,t]=i(S);return n((()=>{if(e){const o=e.getUserAgent(),n=()=>{const e=o.getUserMediaDevices();globalThis.apirtcReactLibLogLevel.isInfoEnabled&&console.info("useUserMediaDevices|mediaDeviceChanged",e),t(e)};return o.on("mediaDeviceChanged",n),()=>{o.removeListener("mediaDeviceChanged",n),t(S)}}}),[e]),{userMediaDevices:o}}const D={level:"info",isDebugEnabled:!1,isInfoEnabled:!0,isWarnEnabled:!0};function I(e){switch(e){case"debug":globalThis.apirtcReactLibLogLevel={level:"debug",isDebugEnabled:!0,isInfoEnabled:!0,isWarnEnabled:!0};break;case"info":default:globalThis.apirtcReactLibLogLevel=D;break;case"warn":globalThis.apirtcReactLibLogLevel={level:"warn",isDebugEnabled:!1,isInfoEnabled:!1,isWarnEnabled:!0};break;case"error":globalThis.apirtcReactLibLogLevel={level:"error",isDebugEnabled:!1,isInfoEnabled:!1,isWarnEnabled:!1}}return globalThis.apirtcReactLibLogLevel}globalThis.apirtcReactLibLogLevel=D,globalThis.setApirtcReactLibLogLevel=I;export{s as VideoStream,I as setLogLevel,r as useCameraStream,b as useConversation,d as useConversationMessages,L as useConversationModeration,h as useConversationStreams,m as usePresence,T as useSession,y as useStreamApplyVideoProcessor,w as useUserMediaDevices};
2
2
  //# sourceMappingURL=index.js.map