zoom_video_sdk.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import messages from './electron_zoomvideosdk_pb.js'
  2. import ZoomVideoSDKSessionInfo from './zoom_video_sdk_session_info.js'
  3. import ZoomVideoSDKAudio from './zoom_video_sdk_audio.js'
  4. import ZoomVideoSDKAudioSetting from './zoom_video_sdk_audio_setting.js'
  5. import ZoomVideoSDKTestAudioDevice from './zoom_video_sdk_test_audio_device.js'
  6. import ZoomVideoSDKShare from './zoom_video_sdk_share.js'
  7. import ZoomVideoSDKVideo from './zoom_video_sdk_video.js'
  8. import ZoomVideoSDKUser from './zoom_video_sdk_user.js'
  9. import ZoomVideoSDKChat from './zoom_video_sdk_chat.js'
  10. import ZoomVideoSDKLiveStream from './zoom_video_sdk_live_stream.js'
  11. import ZoomVideoSDKCmd from './zoom_video_sdk_cmd.js'
  12. import ZoomVideoSDKRecording from './zoom_video_sdk_recording.js'
  13. import ZoomVideoSDKRemoteCameraControl from './zoom_video_sdk_remote_camera_control.js'
  14. import ZoomVideoSDKCameraControlRequestHandler from './zoom_video_sdk_camera_control_request_handler.js'
  15. import ZoomVideoSDKPhone from './zoom_video_sdk_phone.js'
  16. import ZoomVideoSDKLiveTranscription from './zoom_video_sdk_live_transcription.js'
  17. import { ZoomVideoSDKErrors } from './zoom_video_sdk_defines.js'
  18. const { platform, arch } = process
  19. export default (() => {
  20. let instance
  21. /**
  22. * Zoom Video SDK API manager. Main singleton object that controls the video session creation, event callbacks and other main features of video SDK.
  23. * @module zoom_video_sdk
  24. * @param {Function} onNodeAddonCallbacks callback receives the session callback event.
  25. * @return {ZoomVideoSDK}
  26. */
  27. function init (opts) {
  28. // Private methods and variables
  29. const clientOpts = opts || {}
  30. let nativeSdkPath, addon
  31. try {
  32. switch (`${platform}:${arch}`) {
  33. case 'darwin:x64':
  34. case 'darwin:arm64':
  35. nativeSdkPath = 'sdk/mac'
  36. break;
  37. case 'win32:ia32':
  38. nativeSdkPath = 'sdk/win32'
  39. break;
  40. case 'win32:x64':
  41. nativeSdkPath = 'sdk/win64'
  42. break;
  43. case 'linux:x64':
  44. nativeSdkPath = 'sdk/linux'
  45. break;
  46. }
  47. require(`../${nativeSdkPath}/zoomvideosdk_napi_util.node`)
  48. addon = require(`../${nativeSdkPath}/zoomvideosdk.node`).exports()
  49. } catch (error) {
  50. console.log(error)
  51. }
  52. let _isSDKInitialized = false
  53. let _onNodeAddonCallbacks = clientOpts.onNodeAddonCallbacks || null
  54. /**
  55. callback receives the session callback event.
  56. @event onNodeAddonCallbacks
  57. */
  58. function onNodeAddonCallbacks (data) {
  59. if (_onNodeAddonCallbacks) {
  60. _onNodeAddonCallbacks(data)
  61. }
  62. }
  63. if (addon) {
  64. addon.SetNodeAddonCallbacks(onNodeAddonCallbacks)
  65. }
  66. return {
  67. // Public methods and variables
  68. /**
  69. * set session callback event.
  70. * @method setNodeAddonCallbacks
  71. * @param {Function} onNodeAddonCallbacks callback receives all the callbacks related to a session.
  72. * @return {Boolean}
  73. */
  74. setNodeAddonCallbacks: function (onNodeAddonCallbacks) {
  75. if (onNodeAddonCallbacks && onNodeAddonCallbacks instanceof Function) {
  76. _onNodeAddonCallbacks = onNodeAddonCallbacks
  77. return true
  78. }
  79. return false
  80. },
  81. /**
  82. * Get Zoom video sdk object.
  83. * @method createZoomVideoSDKObj
  84. * @param {String} path
  85. * @return {Number} If the function succeed, the return value is Zoom video sdk object. Otherwise NULL.
  86. */
  87. createZoomVideoSDKObj: function (opts) {
  88. if (addon) {
  89. const clientOpts = opts || {}
  90. const path = clientOpts.path || ''
  91. try {
  92. const CreateVideoSDKObjParam = new messages.CreateVideoSDKObjParam()
  93. CreateVideoSDKObjParam.setPath(path)
  94. const bytes = CreateVideoSDKObjParam.serializeBinary()
  95. return addon.CreateZoomVideoSDKObj(bytes)
  96. } catch (error) {
  97. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Invalid_Parameter;
  98. }
  99. }
  100. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  101. },
  102. /**
  103. * Destroy Zoom video sdk object.
  104. * @method destroyZoomVideoSDKObj
  105. * @return {Number}
  106. */
  107. destroyZoomVideoSDKObj: function () {
  108. if (addon) {
  109. return addon.DestroyZoomVideoSDKObj()
  110. }
  111. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  112. },
  113. /**
  114. * Initialize the Zoom SDK with the appropriate parameters.
  115. * @method Initialize
  116. * @param {String} domain
  117. * @param {String} logFilePrefix
  118. * @param {Boolean} enableLog
  119. * @param {Number} audioRawDataMemoryMode Defined in: <a href="../files/lib_settings.js.html#l42">ZoomVideoSDKRawDataMemoryMode</a>
  120. * @param {Number} videoRawDataMemoryMode
  121. * @param {Number} shareRawDataMemoryMode
  122. * @param {String} speakerTestFilePath only support mp3 format,The size cannot exceed 1M
  123. * @return {Number} If the function succeed, the return value is ZoomVideoSDKErrors_Success.
  124. Otherwise failed. To get extended error information, {@link ZoomVideoSDKErrors} enum.
  125. */
  126. initialize: function (opts) {
  127. if (addon) {
  128. const clientOpts = opts || {}
  129. const domain = clientOpts.domain || 'https://www.zoom.us'
  130. const logFilePrefix = clientOpts.logFilePrefix
  131. const enableLog = clientOpts.enableLog
  132. const audioRawDataMemoryMode = Number(clientOpts.audioRawDataMemoryMode)
  133. const videoRawDataMemoryMode = Number(clientOpts.videoRawDataMemoryMode)
  134. const shareRawDataMemoryMode = Number(clientOpts.shareRawDataMemoryMode)
  135. const speakerTestFilePath = clientOpts.speakerTestFilePath
  136. try {
  137. const VideoSDKInitParams = new messages.VideoSDKInitParams()
  138. VideoSDKInitParams.setDomain(domain)
  139. VideoSDKInitParams.setLogfileprefix(logFilePrefix)
  140. VideoSDKInitParams.setEnablelog(enableLog)
  141. VideoSDKInitParams.setAudiorawdatamemorymode(audioRawDataMemoryMode)
  142. VideoSDKInitParams.setZnVideorawdatamemorymode(videoRawDataMemoryMode)
  143. VideoSDKInitParams.setZnSharerawdatamemorymode(shareRawDataMemoryMode)
  144. VideoSDKInitParams.setSpeakertestfilepath(speakerTestFilePath)
  145. const bytes = VideoSDKInitParams.serializeBinary()
  146. const ret = addon.Initialize(bytes)
  147. if (ZoomVideoSDKErrors.ZoomVideoSDKErrors_Success === ret) {
  148. _isSDKInitialized = true
  149. } else {
  150. _isSDKInitialized = false
  151. }
  152. return ret
  153. } catch (error) {
  154. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Invalid_Parameter;
  155. }
  156. }
  157. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  158. },
  159. /**
  160. * Clean up Zoom Video SDK.
  161. * @method cleanup
  162. * @return {Number} If the function succeed, the return value is ZoomVideoSDKErrors_Success.
  163. Otherwise failed. To get extended error information, {@link ZoomVideoSDKErrors} enum.
  164. */
  165. cleanup: function () {
  166. if (addon) {
  167. return addon.CleanUp()
  168. }
  169. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  170. },
  171. /**
  172. * Call this method to join a session with the appropriate parameters. When successful, the SDK will attempt to join a session. Use the callbacks to confirm whether the SDK actually joined.
  173. * @method Join Session
  174. * @param {String} sessionName
  175. * @param {String} sessionPassword
  176. * @param {String} token
  177. * @param {String} userName
  178. * @param {Boolean} localVideoOn
  179. * @param {Boolean} connect
  180. * @param {Boolean} mute
  181. * @param {Boolean} preProcessor
  182. * @param {Boolean} externalVideoSource
  183. * @param {Boolean} sessionIdleTimeoutMins, [Optional] The amount of time in minutes after which an idle session will end. If the value is 0, the session will stay alive indefinitely.
  184. * @return {Number} If the function succeed, the return value is the pointer to IZoomVideoSDKSession object.
  185. Otherwise NULL. To get extended error information, {@link IZoomVideoSDKSession} enum.
  186. */
  187. joinSession: function (opts) {
  188. if (addon) {
  189. const clientOpts = {...opts}
  190. const sessionName = clientOpts.sessionName
  191. const sessionPassword = clientOpts.sessionPassword
  192. const token = clientOpts.token
  193. const username = clientOpts.username
  194. const localVideoOn = clientOpts.localVideoOn
  195. const connect = clientOpts.connect
  196. const mute = clientOpts.mute
  197. const preProcessor = clientOpts.preProcessor
  198. const externalVideoSource = clientOpts.externalVideoSource
  199. const sessionIdleTimeoutMins = clientOpts.sessionIdleTimeoutMins
  200. try {
  201. const VideoSDKSessionContext = new messages.VideoSDKSessionContext()
  202. VideoSDKSessionContext.setSessionname(sessionName)
  203. VideoSDKSessionContext.setSessionpassword(sessionPassword)
  204. VideoSDKSessionContext.setToken(token)
  205. VideoSDKSessionContext.setUsername(username)
  206. VideoSDKSessionContext.setLocalvideoon(localVideoOn)
  207. VideoSDKSessionContext.setConnect(connect)
  208. VideoSDKSessionContext.setMute(mute)
  209. VideoSDKSessionContext.setPreprocessor(preProcessor)
  210. VideoSDKSessionContext.setExternalvideosource(externalVideoSource)
  211. VideoSDKSessionContext.setSessionidletimeoutmins(sessionIdleTimeoutMins)
  212. const bytes = VideoSDKSessionContext.serializeBinary()
  213. return addon.JoinSession(bytes)
  214. } catch (error) {
  215. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Invalid_Parameter;
  216. }
  217. }
  218. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  219. },
  220. /**
  221. * Call this method to leave a session previously joined through joinSession method call. When successful, the SDK will attempt to leave a session. Use the callbacks to confirm whether the SDK actually left.
  222. * @method leaveSession
  223. * @param {Boolean} bEnd true if the host should end the entire session, or false if the host should just leave the session.
  224. * @return {Number} If the function succeed, the return value is ZoomVideoSDKErrors_Success.
  225. Otherwise failed. To get extended error information, {@link ZoomVideoSDKErrors} enum.
  226. */
  227. leaveSession: function (opts) {
  228. if (addon) {
  229. const clientOpts = {...opts}
  230. const bEnd = clientOpts.bEnd
  231. try {
  232. const LeaveSessionParams = new messages.LeaveSessionParams()
  233. LeaveSessionParams.setZnBend(bEnd)
  234. const bytes = LeaveSessionParams.serializeBinary()
  235. return addon.LeaveSession(bytes)
  236. } catch (error) {
  237. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Invalid_Parameter;
  238. }
  239. }
  240. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  241. },
  242. /**
  243. * Check if there is an active session between participants.
  244. * @method isInSession
  245. * @return {Boolean} true if there is; false if not
  246. */
  247. isInSession: function () {
  248. if (addon) {
  249. return addon.IsInSession()
  250. }
  251. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  252. },
  253. /**
  254. * Return Zoom SDK internal version.
  255. * @method getSDKVersion
  256. * @return {String} SDK version.
  257. */
  258. getSDKVersion: function () {
  259. if (addon) {
  260. return addon.GetSDKVersion()
  261. }
  262. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  263. },
  264. /**
  265. * Input session password
  266. * @method inputSessionPassword
  267. * @param {String} password is the session password
  268. * @return {Number} If the function succeed, the return value is ZoomVideoSDKErrors_Success.
  269. Otherwise failed. To get extended error information, {@link ZoomVideoSDKErrors} enum.
  270. */
  271. inputSessionPassword: function (opts) {
  272. if (addon) {
  273. const clientOpts = {...opts}
  274. try {
  275. const password = clientOpts.password
  276. const SessionPasswordParams = new messages.SessionPasswordParams()
  277. SessionPasswordParams.setZnPassword(password)
  278. const bytes = SessionPasswordParams.serializeBinary()
  279. return addon.InputSessionPassword(bytes)
  280. } catch (error) {
  281. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Invalid_Parameter;
  282. }
  283. }
  284. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  285. },
  286. /**
  287. * Cancel input password.
  288. * @method leaveSessionIgnorePassword
  289. * @return {Number} If the function succeed, the return value is ZoomVideoSDKErrors_Success.
  290. Otherwise failed. To get extended error information, {@link ZoomVideoSDKErrors} enum.
  291. */
  292. leaveSessionIgnorePassword: function () {
  293. if (addon) {
  294. return addon.LeaveSessionIgnorePassword()
  295. }
  296. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  297. },
  298. /**
  299. * send one frame data
  300. * @method sendVideoFrame
  301. * @param {String} frameBuffer rameBuffer YUVI420 buffer.
  302. * @param {Number} width frame width.
  303. * @param {Number} height frame height.
  304. * @param {Number} frameLength buffer length.
  305. * @param {Number} rotaion buffer rotation.
  306. * @return {Number}
  307. */
  308. sendVideoFrame: function (opts) {
  309. if (addon) {
  310. const clientOpts = {...opts}
  311. try {
  312. const frameBuffer = clientOpts.frameBuffer
  313. const width = clientOpts.width
  314. const height = clientOpts.height
  315. const frameLength = clientOpts.frameLength
  316. const rotaion = clientOpts.rotaion
  317. const sendVideoFrameParams = new messages.sendVideoFrameParams()
  318. sendVideoFrameParams.setFramebuffer(frameBuffer)
  319. sendVideoFrameParams.setWidth(width)
  320. sendVideoFrameParams.setHeight(height)
  321. sendVideoFrameParams.setFramelength(frameLength)
  322. sendVideoFrameParams.setRotaion(rotaion)
  323. const bytes = sendVideoFrameParams.serializeBinary()
  324. return addon.SendVideoFrame(bytes)
  325. } catch (error) {
  326. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Invalid_Parameter;
  327. }
  328. }
  329. return ZoomVideoSDKErrors.ZoomVideoSDKErrors_Internal_Error
  330. },
  331. getSessionInfo: function (opts) {
  332. if (_isSDKInitialized) {
  333. const clientOpts = opts || {}
  334. clientOpts.addon = addon
  335. return ZoomVideoSDKSessionInfo.getInstance(clientOpts)
  336. }
  337. return null
  338. },
  339. getAudioHelper: function (opts) {
  340. if (_isSDKInitialized) {
  341. const clientOpts = opts || {}
  342. clientOpts.addon = addon
  343. return ZoomVideoSDKAudio.getInstance(clientOpts)
  344. }
  345. return null
  346. },
  347. getAudioSettingHelper: function (opts) {
  348. if (_isSDKInitialized) {
  349. const clientOpts = opts || {}
  350. clientOpts.addon = addon
  351. return ZoomVideoSDKAudioSetting.getInstance(clientOpts)
  352. }
  353. return null
  354. },
  355. getTestAudioDeviceHelper: function (opts) {
  356. if (_isSDKInitialized) {
  357. const clientOpts = opts || {}
  358. clientOpts.addon = addon
  359. return ZoomVideoSDKTestAudioDevice.getInstance(clientOpts)
  360. }
  361. return null
  362. },
  363. getVideoHelper: function (opts) {
  364. if (_isSDKInitialized) {
  365. const clientOpts = opts || {}
  366. clientOpts.addon = addon
  367. return ZoomVideoSDKVideo.getInstance(clientOpts)
  368. }
  369. return null
  370. },
  371. getShareHelper: function (opts) {
  372. if (_isSDKInitialized) {
  373. const clientOpts = opts || {}
  374. clientOpts.addon = addon
  375. return ZoomVideoSDKShare.getInstance(clientOpts)
  376. }
  377. return null
  378. },
  379. getUserHelper: function (opts) {
  380. if (_isSDKInitialized) {
  381. const clientOpts = opts || {}
  382. clientOpts.addon = addon
  383. return ZoomVideoSDKUser.getInstance(clientOpts)
  384. }
  385. return null
  386. },
  387. getChatHelper: function (opts) {
  388. if (_isSDKInitialized) {
  389. const clientOpts = opts || {}
  390. clientOpts.addon = addon
  391. return ZoomVideoSDKChat.getInstance(clientOpts)
  392. }
  393. return null
  394. },
  395. getLiveStreamHelper: function (opts) {
  396. if (_isSDKInitialized) {
  397. const clientOpts = opts || {}
  398. clientOpts.addon = addon
  399. return ZoomVideoSDKLiveStream.getInstance(clientOpts)
  400. }
  401. return null
  402. },
  403. getCmdHelper: function (opts) {
  404. if (_isSDKInitialized) {
  405. const clientOpts = opts || {}
  406. clientOpts.addon = addon
  407. return ZoomVideoSDKCmd.getInstance(clientOpts)
  408. }
  409. return null
  410. },
  411. getRecordingHelper: function (opts) {
  412. if (_isSDKInitialized) {
  413. const clientOpts = opts || {}
  414. clientOpts.addon = addon
  415. return ZoomVideoSDKRecording.getInstance(clientOpts)
  416. }
  417. return null
  418. },
  419. getRemoteCameraControlHelper: function (opts) {
  420. if (_isSDKInitialized) {
  421. const clientOpts = opts || {}
  422. clientOpts.addon = addon
  423. return ZoomVideoSDKRemoteCameraControl.getInstance(clientOpts)
  424. }
  425. return null
  426. },
  427. getCameraControlRequestHelper: function (opts) {
  428. if (_isSDKInitialized) {
  429. const clientOpts = opts || {}
  430. clientOpts.addon = addon
  431. return ZoomVideoSDKCameraControlRequestHandler.getInstance(clientOpts)
  432. }
  433. return null
  434. },
  435. getPhoneHelper: function (opts) {
  436. if (_isSDKInitialized) {
  437. const clientOpts = opts || {}
  438. clientOpts.addon = addon
  439. return ZoomVideoSDKPhone.getInstance(clientOpts)
  440. }
  441. return null
  442. },
  443. getLiveTranscriptionHelper: function (opts) {
  444. if (_isSDKInitialized) {
  445. const clientOpts = opts || {}
  446. clientOpts.addon = addon
  447. return ZoomVideoSDKLiveTranscription.getInstance(clientOpts)
  448. }
  449. return null
  450. }
  451. }
  452. }
  453. return {
  454. getInstance: (opts) => {
  455. if (!instance) {
  456. instance = init(opts)
  457. }
  458. return instance
  459. }
  460. }
  461. })()