@4players/odin-nodejs 0.7.0

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.
Files changed (99) hide show
  1. package/.idea/.name +1 -0
  2. package/.idea/jsLibraryMappings.xml +6 -0
  3. package/.idea/misc.xml +4 -0
  4. package/.idea/modules.xml +9 -0
  5. package/.idea/odin-nodejs.iml +8 -0
  6. package/.idea/odin_nodejs.iml +2 -0
  7. package/.idea/php.xml +19 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/CMakeLists.txt +25 -0
  10. package/README.md +53 -0
  11. package/binding.gyp +61 -0
  12. package/build/Makefile +352 -0
  13. package/build/Release/.deps/Release/nothing.a.d +1 -0
  14. package/build/Release/.deps/Release/obj.target/nothing/node_modules/node-addon-api/nothing.o.d +4 -0
  15. package/build/Release/.deps/Release/obj.target/odin/cppsrc/binding.o.d +27 -0
  16. package/build/Release/.deps/Release/obj.target/odin/cppsrc/odinbindings.o.d +24 -0
  17. package/build/Release/.deps/Release/obj.target/odin/cppsrc/odinclient.o.d +24 -0
  18. package/build/Release/.deps/Release/obj.target/odin/cppsrc/odinmedia.o.d +24 -0
  19. package/build/Release/.deps/Release/obj.target/odin/cppsrc/odinroom.o.d +24 -0
  20. package/build/Release/.deps/Release/obj.target/odin/cppsrc/utilities.o.d +21 -0
  21. package/build/Release/.deps/Release/odin.node.d +1 -0
  22. package/build/Release/nothing.a +0 -0
  23. package/build/Release/obj.target/nothing/node_modules/node-addon-api/nothing.o +0 -0
  24. package/build/Release/obj.target/odin/cppsrc/binding.o +0 -0
  25. package/build/Release/obj.target/odin/cppsrc/odinbindings.o +0 -0
  26. package/build/Release/obj.target/odin/cppsrc/odinclient.o +0 -0
  27. package/build/Release/obj.target/odin/cppsrc/odinmedia.o +0 -0
  28. package/build/Release/obj.target/odin/cppsrc/odinroom.o +0 -0
  29. package/build/Release/obj.target/odin/cppsrc/utilities.o +0 -0
  30. package/build/binding.Makefile +6 -0
  31. package/build/gyp-mac-tool +772 -0
  32. package/build/node_modules/node-addon-api/node_api.Makefile +6 -0
  33. package/build/node_modules/node-addon-api/nothing.target.mk +186 -0
  34. package/build/odin.target.mk +201 -0
  35. package/cmake-build-debug/.cmake/api/v1/query/cache-v2 +0 -0
  36. package/cmake-build-debug/.cmake/api/v1/query/cmakeFiles-v1 +0 -0
  37. package/cmake-build-debug/.cmake/api/v1/query/codemodel-v2 +0 -0
  38. package/cmake-build-debug/.cmake/api/v1/query/toolchains-v1 +0 -0
  39. package/cmake-build-debug/.cmake/api/v1/reply/cache-v2-912a6d0c3c2c7ebbadf8.json +1183 -0
  40. package/cmake-build-debug/.cmake/api/v1/reply/cmakeFiles-v1-0825651e80f1fb47890c.json +161 -0
  41. package/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-bc24edc239de864eb886.json +60 -0
  42. package/cmake-build-debug/.cmake/api/v1/reply/directory-.-Debug-f5ebdc15457944623624.json +14 -0
  43. package/cmake-build-debug/.cmake/api/v1/reply/index-2023-03-18T11-25-22-0179.json +108 -0
  44. package/cmake-build-debug/.cmake/api/v1/reply/target-odin_nodejs-Debug-1ac16802f252a85492c7.json +322 -0
  45. package/cmake-build-debug/.cmake/api/v1/reply/toolchains-v1-b57293a485ebf36c82cc.json +88 -0
  46. package/cmake-build-debug/.ninja_log +1 -0
  47. package/cmake-build-debug/CMakeCache.txt +367 -0
  48. package/cmake-build-debug/CMakeFiles/3.24.2/CMakeCCompiler.cmake +72 -0
  49. package/cmake-build-debug/CMakeFiles/3.24.2/CMakeCXXCompiler.cmake +83 -0
  50. package/cmake-build-debug/CMakeFiles/3.24.2/CMakeDetermineCompilerABI_C.bin +0 -0
  51. package/cmake-build-debug/CMakeFiles/3.24.2/CMakeDetermineCompilerABI_CXX.bin +0 -0
  52. package/cmake-build-debug/CMakeFiles/3.24.2/CMakeSystem.cmake +15 -0
  53. package/cmake-build-debug/CMakeFiles/3.24.2/CompilerIdC/CMakeCCompilerId.c +838 -0
  54. package/cmake-build-debug/CMakeFiles/3.24.2/CompilerIdC/CMakeCCompilerId.o +0 -0
  55. package/cmake-build-debug/CMakeFiles/3.24.2/CompilerIdCXX/CMakeCXXCompilerId.cpp +826 -0
  56. package/cmake-build-debug/CMakeFiles/3.24.2/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
  57. package/cmake-build-debug/CMakeFiles/CMakeError.log +22 -0
  58. package/cmake-build-debug/CMakeFiles/CMakeOutput.log +250 -0
  59. package/cmake-build-debug/CMakeFiles/TargetDirectories.txt +3 -0
  60. package/cmake-build-debug/CMakeFiles/clion-Debug-log.txt +4 -0
  61. package/cmake-build-debug/CMakeFiles/clion-environment.txt +3 -0
  62. package/cmake-build-debug/CMakeFiles/cmake.check_cache +1 -0
  63. package/cmake-build-debug/CMakeFiles/rules.ninja +74 -0
  64. package/cmake-build-debug/Testing/Temporary/LastTest.log +3 -0
  65. package/cmake-build-debug/build.ninja +205 -0
  66. package/cmake-build-debug/cmake_install.cmake +49 -0
  67. package/cppsrc/binding.cpp +15 -0
  68. package/cppsrc/odinbindings.cpp +58 -0
  69. package/cppsrc/odinbindings.h +9 -0
  70. package/cppsrc/odinclient.cpp +162 -0
  71. package/cppsrc/odinclient.h +17 -0
  72. package/cppsrc/odinmedia.cpp +242 -0
  73. package/cppsrc/odinmedia.h +35 -0
  74. package/cppsrc/odinroom.cpp +738 -0
  75. package/cppsrc/odinroom.h +65 -0
  76. package/cppsrc/utilities.cpp +102 -0
  77. package/cppsrc/utilities.h +175 -0
  78. package/index.cjs +2 -0
  79. package/index.d.ts +5 -0
  80. package/libs/bin/linux/arm64/libodin_static.a +0 -0
  81. package/libs/bin/linux/ia32/libodin_static.a +0 -0
  82. package/libs/bin/linux/x64/libodin_static.a +0 -0
  83. package/libs/bin/macos/arm64/libodin_static.a +0 -0
  84. package/libs/bin/macos/x64/libodin_static.a +0 -0
  85. package/libs/bin/windows/arm64/odin_static.lib +0 -0
  86. package/libs/bin/windows/ia32/odin_static.lib +0 -0
  87. package/libs/bin/windows/x64/odin_static.lib +0 -0
  88. package/libs/include/odin.h +896 -0
  89. package/odin.client.d.ts +22 -0
  90. package/odin.media.d.ts +33 -0
  91. package/odin.room.d.ts +473 -0
  92. package/package.json +50 -0
  93. package/prebuilds/darwin-arm64/node.napi.node +0 -0
  94. package/test.js +211 -0
  95. package/tests/audio-recording/README.md +24 -0
  96. package/tests/audio-recording/index.js +148 -0
  97. package/tests/sending-audio/README.md +20 -0
  98. package/tests/sending-audio/index.js +81 -0
  99. package/tests/sending-audio/santa.mp3 +0 -0
@@ -0,0 +1,65 @@
1
+ //
2
+ // Created by Phillip Schuster on 11.02.23.
3
+ //
4
+
5
+ #ifndef ODIN_NODEJS_ODINROOM_H
6
+ #define ODIN_NODEJS_ODINROOM_H
7
+
8
+ #include <napi.h>
9
+ #include <odin.h>
10
+ #include "utilities.h"
11
+ #include <map>
12
+ #include <thread>
13
+
14
+ class OdinRoom : public Napi::ObjectWrap<OdinRoom> {
15
+ struct Media {
16
+ OdinMediaStreamHandle Handle;
17
+ uint16_t Id;
18
+ uint64_t PeerId;
19
+ };
20
+
21
+ public:
22
+ OdinRoom(const Napi::CallbackInfo& info); //Constructor to initialise
23
+ static Napi::FunctionReference *New;
24
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
25
+ static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
26
+ void Finalize(Napi::Env env) override;
27
+ static void HandleOdinEvent(OdinRoomHandle room, const OdinEvent *event, void *data);
28
+ static Napi::Object PrepareEventObject(Napi::Env env, const EventData *event);
29
+ static Napi::Object PrepareEventObjectTest(Napi::Env env, const OdinEvent *event);
30
+ OdinRoomHandle GetRoomHandle() const;
31
+ static Napi::FunctionReference *constructor;
32
+ static Napi::Object NewInstance(Napi::Value arg);
33
+ private:
34
+ OdinRoomHandle _roomHandle;
35
+ std::string _token;
36
+ Napi::ThreadSafeFunction _eventListener;
37
+ std::map<uint16_t, Media> _mediaStreams;
38
+
39
+ std::thread _nativeThread;
40
+ bool _started;
41
+ bool _joined;
42
+ float _audioSamplesBuffer[960];
43
+
44
+ std::map<std::string, Napi::ThreadSafeFunction> _eventListeners;
45
+ Napi::ThreadSafeFunction _audioDataReceivedEventListener;
46
+
47
+ void Close(const Napi::CallbackInfo& info);
48
+ void Join(const Napi::CallbackInfo& info);
49
+ void UpdatePeerUserData(const Napi::CallbackInfo &info);
50
+ void UpdateRoomUserData(const Napi::CallbackInfo &info);
51
+ void SendMessage(const Napi::CallbackInfo &info);
52
+ void SetEventListener(const Napi::CallbackInfo &info);
53
+ void AddEventListener(const Napi::CallbackInfo &info);
54
+ void RemoveEventListener(const Napi::CallbackInfo &info);
55
+ Napi::Value GetOwnPeerId(const Napi::CallbackInfo &info);
56
+ Napi::Value RoomId(const Napi::CallbackInfo &info);
57
+ EventData* PrepareEventData(OdinEvent* event);
58
+ static uint16_t GetMediaIdFromHandle(OdinMediaStreamHandle handle);
59
+ void HandleAudioData();
60
+
61
+ Napi::Value CreateAudioStream(const Napi::CallbackInfo &info);
62
+ };
63
+
64
+
65
+ #endif //ODIN_NODEJS_ODINROOM_H
@@ -0,0 +1,102 @@
1
+ #include "utilities.h"
2
+
3
+ std::string string_format(const std::string fmt, ...) {
4
+ int size = ((int)fmt.size()) * 2 + 50; // Use a rubric appropriate for your code
5
+ std::string str;
6
+ va_list ap;
7
+ while (1) { // Maximum two passes on a POSIX system...
8
+ str.resize(size);
9
+ va_start(ap, fmt);
10
+ int n = vsnprintf((char *)str.data(), size, fmt.c_str(), ap);
11
+ va_end(ap);
12
+ if (n > -1 && n < size) { // Everything worked
13
+ str.resize(n);
14
+ return str;
15
+ }
16
+ if (n > -1) // Needed size returned
17
+ size = n + 1; // For null char
18
+ else
19
+ size *= 2; // Guess at a larger size (OS specific)
20
+ }
21
+ return str;
22
+ }
23
+
24
+ void OdinUtilities::ThrowNapiException(Napi::Env env, OdinReturnCode error, const char* message)
25
+ {
26
+ char buffer[512];
27
+ odin_error_format(error, buffer, sizeof(buffer));
28
+
29
+ std::string text = string_format("%s: %s", message, buffer);
30
+
31
+ Napi::TypeError::New(env, text.c_str()).ThrowAsJavaScriptException();
32
+ }
33
+
34
+ /**
35
+ * @brief Returns a human-readable string representation for a specified connection state
36
+ *
37
+ * @param state The room connection state to translate
38
+ */
39
+ const char* OdinUtilities::GetNameFromConnectionState(OdinRoomConnectionState state)
40
+ {
41
+ switch (state)
42
+ {
43
+ case OdinRoomConnectionState_Connecting:
44
+ return "connecting";
45
+ case OdinRoomConnectionState_Connected:
46
+ return "connected";
47
+ case OdinRoomConnectionState_Disconnecting:
48
+ return "disconnecting";
49
+ case OdinRoomConnectionState_Disconnected:
50
+ return "disconnected";
51
+ default:
52
+ return "unknown";
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @brief Returns a human-readable string representation for a specified connection state change reason
58
+ *
59
+ * @param state The room connection state change reason to translate
60
+ */
61
+ const char* OdinUtilities::GetNameFromConnectionStateChangeReason(OdinRoomConnectionStateChangeReason reason)
62
+ {
63
+ switch (reason)
64
+ {
65
+ case OdinRoomConnectionStateChangeReason_ClientRequested:
66
+ return "client_request";
67
+ case OdinRoomConnectionStateChangeReason_ServerRequested:
68
+ return "server_request";
69
+ case OdinRoomConnectionStateChangeReason_ConnectionLost:
70
+ return "timeout";
71
+ default:
72
+ return "unknown";
73
+ }
74
+ }
75
+
76
+ /**
77
+ * @brief Basic helper function to get the internal media ID from a specified handle
78
+ *
79
+ * @param handle The media stream handle to get the ID from
80
+ */
81
+ uint16_t OdinUtilities::GetMediaIdFromHandle(OdinMediaStreamHandle handle)
82
+ {
83
+ uint16_t media_id;
84
+ int error = odin_media_stream_media_id(handle, &media_id);
85
+ return odin_is_error(error) ? 0 : media_id;
86
+ }
87
+
88
+
89
+ void convert_float32_to_float16(const float *in_buffer, size_t in_buffer_len, short *out_buffer) {
90
+ for (size_t i = 0; i < in_buffer_len; i++) {
91
+ float f = in_buffer[i];
92
+ // Clamp to [-1, 1] range
93
+ if (f > 1.0f) {
94
+ f = 1.0f;
95
+ } else if (f < -1.0f) {
96
+ f = -1.0f;
97
+ }
98
+ // Convert to 16-bit float
99
+ int16_t f16 = (int16_t) (f * 32767.0f);
100
+ out_buffer[i] = f16;
101
+ }
102
+ }
@@ -0,0 +1,175 @@
1
+ #include <odin.h>
2
+ #include <napi.h>
3
+
4
+ #ifndef ODIN_NODEJS_UTILITIES_H
5
+ #define ODIN_NODEJS_UTILITIES_H
6
+
7
+ namespace OdinUtilities
8
+ {
9
+ void ThrowNapiException(Napi::Env env, OdinReturnCode error, const char* message);
10
+ const char* GetNameFromConnectionState(OdinRoomConnectionState state);
11
+ const char* GetNameFromConnectionStateChangeReason(OdinRoomConnectionStateChangeReason reason);
12
+ uint16_t GetMediaIdFromHandle(OdinMediaStreamHandle handle);
13
+ }
14
+
15
+ /**
16
+ * Event data base structure used to copy OdinEvent data as a base for sending events to JS side
17
+ */
18
+ struct EventData {
19
+ OdinEventTag Tag;
20
+ std::string Event;
21
+ };
22
+
23
+ /**
24
+ * Event data structure used to copy OdinEvent data as a base for sending events to JS side including data
25
+ */
26
+ struct EventDataWithData: EventData
27
+ {
28
+ const uint8_t *Data;
29
+ size_t DataLen;
30
+
31
+ void SetData(const uint8_t *data, size_t len)
32
+ {
33
+ Data = new uint8_t[len];
34
+ memcpy((void*)Data, data, len);
35
+ DataLen = len;
36
+ }
37
+
38
+ ~EventDataWithData()
39
+ {
40
+ delete[] Data;
41
+ }
42
+ };
43
+
44
+ /**
45
+ * Event data structure used to copy The Joined event data
46
+ */
47
+ struct JoinedEventData: EventDataWithData
48
+ {
49
+ std::string RoomId;
50
+ std::string UserId;
51
+ uint64_t PeerId;
52
+ };
53
+
54
+ /**
55
+ * Event data structure used to copy The Left event data
56
+ */
57
+ struct PeerJoinedEventData: EventDataWithData
58
+ {
59
+ uint64_t PeerId;
60
+ std::string UserId;
61
+ };
62
+
63
+ /**
64
+ * Event data structure used to copy The PeerLeft event data
65
+ */
66
+ struct PeerLeftEventData: EventData
67
+ {
68
+ uint64_t PeerId;
69
+ };
70
+
71
+ /**
72
+ * Event data structure used to copy The PeerUserDataChanged event data
73
+ */
74
+ struct PeerUserDataChangedEventData: EventDataWithData
75
+ {
76
+ uint64_t PeerId;
77
+ };
78
+
79
+ /**
80
+ * Event data structure used to copy The RoomUserDataChanged event data
81
+ */
82
+ struct RoomUserDataChangedEventData: EventDataWithData
83
+ {
84
+
85
+ };
86
+
87
+ /**
88
+ * Event data structure used to copy The MessageReceived event data
89
+ */
90
+ struct MessageReceivedEventData: EventDataWithData
91
+ {
92
+ uint64_t PeerId;
93
+ };
94
+
95
+ /**
96
+ * Event data structure used to copy The RoomConnectionStateChanged event data
97
+ */
98
+ struct RoomConnectionStateChangedEventData: EventData
99
+ {
100
+ int State;
101
+ int Reason;
102
+ std::string StateName;
103
+ std::string ReasonName;
104
+ };
105
+
106
+ /**
107
+ * Event data structure used to copy The MediaAdded event data
108
+ */
109
+ struct MediaAddedEventData: EventData
110
+ {
111
+ uint64_t PeerId;
112
+ uint16_t MediaId;
113
+ OdinMediaStreamHandle MediaStreamHandle;
114
+ };
115
+
116
+ /**
117
+ * Event data structure used to copy The MediaRemoved event data
118
+ */
119
+ struct MediaRemovedEventData: EventData
120
+ {
121
+ uint64_t PeerId;
122
+ uint16_t MediaId;
123
+ };
124
+
125
+ /**
126
+ * Event data structure used to copy The MediaActiveStateChanged event data
127
+ */
128
+ struct MediaActiveStateChangedEventData: EventData
129
+ {
130
+ uint64_t PeerId;
131
+ uint16_t MediaId;
132
+ bool Active;
133
+ };
134
+
135
+ /**
136
+ * Data structure to store audio samples used to copy The AudioDataReceived event data
137
+ */
138
+ struct AudioSamples {
139
+ short Data[960];
140
+ float OriginalData[960];
141
+ size_t Len;
142
+ uint64_t PeerId;
143
+ uint16_t MediaId;
144
+
145
+ void ConvertFloat32ToFloat16(const float *in_buffer, size_t in_buffer_len, short *out_buffer) {
146
+ for (size_t i = 0; i < in_buffer_len; i++) {
147
+ float f = in_buffer[i];
148
+ // Clamp to [-1, 1] range
149
+ if (f > 1.0f) {
150
+ f = 1.0f;
151
+ } else if (f < -1.0f) {
152
+ f = -1.0f;
153
+ }
154
+ // Convert to 16-bit float
155
+ int16_t f16 = (int16_t) (f * 32767.0f);
156
+ out_buffer[i] = f16;
157
+ }
158
+ }
159
+
160
+ void SetSamples(const float *samples, size_t len)
161
+ {
162
+ ConvertFloat32ToFloat16(samples, len, Data);
163
+ memcpy(OriginalData, samples, len * sizeof(float));
164
+ Len = len;
165
+ }
166
+
167
+ ~AudioSamples()
168
+ {
169
+ }
170
+ };
171
+
172
+
173
+ void convert_float32_to_float16(const float *in_buffer, size_t in_buffer_len, short *out_buffer);
174
+
175
+ #endif //ODIN_NODEJS_UTILITIES_H
package/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ var binding = require('node-gyp-build')(__dirname);
2
+ module.exports = binding;
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./odin.client";
2
+ export * from "./odin.room";
3
+ export * from "./odin.media";
4
+
5
+ export declare function generateAccessToken(accessKey: string, roomId: string, userId: string): string;