SpatialAudio.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. namespace Agora.Rtc
  2. {
  3. //public sealed class CloudSpatialAudioEngine : ICloudSpatialAudioEngine
  4. //{
  5. // private IRtcEngine _rtcEngineInstance = null;
  6. // private CloudSpatialAudioEngineImpl _cloudSpatialAudioEngineImpl = null;
  7. // private const string ErrorMsgLog = "[CloudSpatialAudioEngine]:Api not supported!";
  8. // private const int ErrorCode = -7;
  9. // private CloudSpatialAudioEngine(IRtcEngine rtcEngine, CloudSpatialAudioEngineImpl impl)
  10. // {
  11. // _rtcEngineInstance = null;
  12. // _cloudSpatialAudioEngineImpl = impl;
  13. // }
  14. // ~CloudSpatialAudioEngine()
  15. // {
  16. // _rtcEngineInstance = null;
  17. // }
  18. // private static ICloudSpatialAudioEngine instance = null;
  19. // public static ICloudSpatialAudioEngine Instance
  20. // {
  21. // get
  22. // {
  23. // return instance;
  24. // }
  25. // }
  26. // public override CloudSpatialAudioEventHandler GetCloudSpatialAudioEventHandler()
  27. // {
  28. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  29. // {
  30. // AgoraLog.LogError(ErrorMsgLog);
  31. // return null;
  32. // }
  33. // return _cloudSpatialAudioEngineImpl.GetCloudSpatialAudioEventHandler();
  34. // }
  35. // public override void InitEventHandler(ICloudSpatialAudioEventHandler engineEventHandler)
  36. // {
  37. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  38. // {
  39. // AgoraLog.LogError(ErrorMsgLog);
  40. // return;
  41. // }
  42. // _cloudSpatialAudioEngineImpl.InitEventHandler(engineEventHandler);
  43. // }
  44. // public override int Initialize(CloudSpatialAudioConfig config)
  45. // {
  46. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  47. // {
  48. // AgoraLog.LogError(ErrorMsgLog);
  49. // return ErrorCode;
  50. // }
  51. // return _cloudSpatialAudioEngineImpl.Initialize(config);
  52. // }
  53. // public override void Dispose()
  54. // {
  55. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  56. // {
  57. // AgoraLog.LogError(ErrorMsgLog);
  58. // return;
  59. // }
  60. // _cloudSpatialAudioEngineImpl.Dispose();
  61. // }
  62. // public override int SetMaxAudioRecvCount(int maxCount)
  63. // {
  64. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  65. // {
  66. // AgoraLog.LogError(ErrorMsgLog);
  67. // return ErrorCode;
  68. // }
  69. // return _cloudSpatialAudioEngineImpl.SetMaxAudioRecvCount(maxCount);
  70. // }
  71. // public override int SetAudioRecvRange(float range)
  72. // {
  73. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  74. // {
  75. // AgoraLog.LogError(ErrorMsgLog);
  76. // return ErrorCode;
  77. // }
  78. // return _cloudSpatialAudioEngineImpl.SetAudioRecvRange(range);
  79. // }
  80. // public override int SetDistanceUnit(float unit)
  81. // {
  82. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  83. // {
  84. // AgoraLog.LogError(ErrorMsgLog);
  85. // return ErrorCode;
  86. // }
  87. // return _cloudSpatialAudioEngineImpl.SetDistanceUnit(unit);
  88. // }
  89. // public override int UpdateSelfPosition(float[] position, float[] axisForward, float[] axisRight, float[] axisUp)
  90. // {
  91. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  92. // {
  93. // AgoraLog.LogError(ErrorMsgLog);
  94. // return ErrorCode;
  95. // }
  96. // return _cloudSpatialAudioEngineImpl.UpdateSelfPosition(position, axisForward, axisRight, axisUp);
  97. // }
  98. // public override int UpdateSelfPositionEx(float[] position, float[] axisForward, float[] axisRight, float[] axisUp, RtcConnection connection)
  99. // {
  100. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  101. // {
  102. // AgoraLog.LogError(ErrorMsgLog);
  103. // return ErrorCode;
  104. // }
  105. // return _cloudSpatialAudioEngineImpl.UpdateSelfPositionEx(position, axisForward, axisRight, axisUp, connection);
  106. // }
  107. // public override int UpdatePlayerPositionInfo(int playerId, float[] position, float[] forward)
  108. // {
  109. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  110. // {
  111. // AgoraLog.LogError(ErrorMsgLog);
  112. // return ErrorCode;
  113. // }
  114. // return _cloudSpatialAudioEngineImpl.UpdatePlayerPositionInfo(playerId, position, forward);
  115. // }
  116. // public override int SetParameters(string @params)
  117. // {
  118. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  119. // {
  120. // AgoraLog.LogError(ErrorMsgLog);
  121. // return ErrorCode;
  122. // }
  123. // return _cloudSpatialAudioEngineImpl.SetParameters(@params);
  124. // }
  125. // public override int MuteLocalAudioStream(bool mute)
  126. // {
  127. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  128. // {
  129. // AgoraLog.LogError(ErrorMsgLog);
  130. // return ErrorCode;
  131. // }
  132. // return _cloudSpatialAudioEngineImpl.MuteLocalAudioStream(mute);
  133. // }
  134. // public override int MuteAllRemoteAudioStreams(bool mute)
  135. // {
  136. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  137. // {
  138. // AgoraLog.LogError(ErrorMsgLog);
  139. // return ErrorCode;
  140. // }
  141. // return _cloudSpatialAudioEngineImpl.MuteAllRemoteAudioStreams(mute);
  142. // }
  143. // public override int EnableSpatializer(bool enable, bool applyToTeam)
  144. // {
  145. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  146. // {
  147. // AgoraLog.LogError(ErrorMsgLog);
  148. // return ErrorCode;
  149. // }
  150. // return _cloudSpatialAudioEngineImpl.EnableSpatializer(enable, applyToTeam);
  151. // }
  152. // public override int SetTeamId(int teamId)
  153. // {
  154. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  155. // {
  156. // AgoraLog.LogError(ErrorMsgLog);
  157. // return ErrorCode;
  158. // }
  159. // return _cloudSpatialAudioEngineImpl.SetTeamId(teamId);
  160. // }
  161. // public override int SetAudioRangeMode(AUDIO_RANGE_MODE_TYPE rangeMode)
  162. // {
  163. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  164. // {
  165. // AgoraLog.LogError(ErrorMsgLog);
  166. // return ErrorCode;
  167. // }
  168. // return _cloudSpatialAudioEngineImpl.SetAudioRangeMode(rangeMode);
  169. // }
  170. // public override int EnterRoom(string token, string roomName, uint uid)
  171. // {
  172. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  173. // {
  174. // AgoraLog.LogError(ErrorMsgLog);
  175. // return ErrorCode;
  176. // }
  177. // return _cloudSpatialAudioEngineImpl.EnterRoom(token, roomName, uid);
  178. // }
  179. // public override int ExitRoom()
  180. // {
  181. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  182. // {
  183. // AgoraLog.LogError(ErrorMsgLog);
  184. // return ErrorCode;
  185. // }
  186. // return _cloudSpatialAudioEngineImpl.ExitRoom();
  187. // }
  188. // public override int GetTeammates(ref uint[] uids, ref int userCount)
  189. // {
  190. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  191. // {
  192. // AgoraLog.LogError(ErrorMsgLog);
  193. // return ErrorCode;
  194. // }
  195. // return _cloudSpatialAudioEngineImpl.GetTeammates(ref uids, ref userCount);
  196. // }
  197. // public override int RenewToken(string token)
  198. // {
  199. // if (_rtcEngineInstance == null || _cloudSpatialAudioEngineImpl == null)
  200. // {
  201. // AgoraLog.LogError(ErrorMsgLog);
  202. // return ErrorCode;
  203. // }
  204. // return _cloudSpatialAudioEngineImpl.RenewToken(token);
  205. // }
  206. // internal static ICloudSpatialAudioEngine GetInstance(IRtcEngine rtcEngine, CloudSpatialAudioEngineImpl impl)
  207. // {
  208. // return instance ?? (instance = new CloudSpatialAudioEngine(rtcEngine, impl));
  209. // }
  210. // internal static void ReleaseInstance()
  211. // {
  212. // instance = null;
  213. // }
  214. //}
  215. public sealed class LocalSpatialAudioEngine : ILocalSpatialAudioEngine
  216. {
  217. private IRtcEngine _rtcEngineInstance = null;
  218. private LocalSpatialAudioEngineImpl _localSpatialAudioEngineImpl = null;
  219. private const int ErrorCode = -7;
  220. private LocalSpatialAudioEngine(IRtcEngine rtcEngine, LocalSpatialAudioEngineImpl impl)
  221. {
  222. _rtcEngineInstance = rtcEngine;
  223. _localSpatialAudioEngineImpl = impl;
  224. }
  225. ~LocalSpatialAudioEngine()
  226. {
  227. _rtcEngineInstance = null;
  228. }
  229. private static ILocalSpatialAudioEngine instance = null;
  230. public static ILocalSpatialAudioEngine Instance
  231. {
  232. get
  233. {
  234. return instance;
  235. }
  236. }
  237. public override void Dispose()
  238. {
  239. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  240. {
  241. return;
  242. }
  243. _localSpatialAudioEngineImpl.Dispose();
  244. }
  245. public override int Initialize()
  246. {
  247. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  248. {
  249. return ErrorCode;
  250. }
  251. return _localSpatialAudioEngineImpl.Initialize();
  252. }
  253. public override int SetMaxAudioRecvCount(int maxCount)
  254. {
  255. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  256. {
  257. return ErrorCode;
  258. }
  259. return _localSpatialAudioEngineImpl.SetMaxAudioRecvCount(maxCount);
  260. }
  261. public override int SetAudioRecvRange(float range)
  262. {
  263. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  264. {
  265. return ErrorCode;
  266. }
  267. return _localSpatialAudioEngineImpl.SetAudioRecvRange(range);
  268. }
  269. public override int SetDistanceUnit(float unit)
  270. {
  271. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  272. {
  273. return ErrorCode;
  274. }
  275. return _localSpatialAudioEngineImpl.SetDistanceUnit(unit);
  276. }
  277. public override int UpdateSelfPosition(float[] position, float[] axisForward, float[] axisRight, float[] axisUp)
  278. {
  279. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  280. {
  281. return ErrorCode;
  282. }
  283. return _localSpatialAudioEngineImpl.UpdateSelfPosition(position, axisForward, axisRight, axisUp);
  284. }
  285. public override int UpdateSelfPositionEx(float[] position, float[] axisForward, float[] axisRight, float[] axisUp, RtcConnection connection)
  286. {
  287. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  288. {
  289. return ErrorCode;
  290. }
  291. return _localSpatialAudioEngineImpl.UpdateSelfPositionEx(position, axisForward, axisRight, axisUp, connection);
  292. }
  293. public override int UpdatePlayerPositionInfo(int playerId, float[] position, float[] forward)
  294. {
  295. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  296. {
  297. return ErrorCode;
  298. }
  299. return _localSpatialAudioEngineImpl.UpdatePlayerPositionInfo(playerId, position, forward);
  300. }
  301. public override int SetParameters(string @params)
  302. {
  303. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  304. {
  305. return ErrorCode;
  306. }
  307. return _localSpatialAudioEngineImpl.SetParameters(@params);
  308. }
  309. public override int MuteLocalAudioStream(bool mute)
  310. {
  311. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  312. {
  313. return ErrorCode;
  314. }
  315. return _localSpatialAudioEngineImpl.MuteLocalAudioStream(mute);
  316. }
  317. public override int MuteAllRemoteAudioStreams(bool mute)
  318. {
  319. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  320. {
  321. return ErrorCode;
  322. }
  323. return _localSpatialAudioEngineImpl.MuteAllRemoteAudioStreams(mute);
  324. }
  325. public override int UpdateRemotePosition(uint uid, float[] position, float[] forward)
  326. {
  327. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  328. {
  329. return ErrorCode;
  330. }
  331. return _localSpatialAudioEngineImpl.UpdateRemotePosition(uid, position, forward);
  332. }
  333. public override int UpdateRemotePositionEx(uint uid, float[] position, float[] forward, RtcConnection connection)
  334. {
  335. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  336. {
  337. return ErrorCode;
  338. }
  339. return _localSpatialAudioEngineImpl.UpdateRemotePositionEx(uid, position, forward, connection);
  340. }
  341. public override int RemoveRemotePosition(uint uid)
  342. {
  343. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  344. {
  345. return ErrorCode;
  346. }
  347. return _localSpatialAudioEngineImpl.RemoveRemotePosition(uid);
  348. }
  349. public override int RemoveRemotePositionEx(uint uid, RtcConnection connection)
  350. {
  351. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  352. {
  353. return ErrorCode;
  354. }
  355. return _localSpatialAudioEngineImpl.RemoveRemotePositionEx(uid, connection);
  356. }
  357. public override int ClearRemotePositions()
  358. {
  359. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  360. {
  361. return ErrorCode;
  362. }
  363. return _localSpatialAudioEngineImpl.ClearRemotePositions();
  364. }
  365. public override int ClearRemotePositionsEx(RtcConnection connection)
  366. {
  367. if (_rtcEngineInstance == null || _localSpatialAudioEngineImpl == null)
  368. {
  369. return ErrorCode;
  370. }
  371. return _localSpatialAudioEngineImpl.ClearRemotePositionsEx(connection);
  372. }
  373. internal static ILocalSpatialAudioEngine GetInstance(IRtcEngine rtcEngine, LocalSpatialAudioEngineImpl impl)
  374. {
  375. return instance ?? (instance = new LocalSpatialAudioEngine(rtcEngine, impl));
  376. }
  377. internal static void ReleaseInstance()
  378. {
  379. instance = null;
  380. }
  381. }
  382. }