api.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #pragma once
  2. #include <cstdint>
  3. #include <string>
  4. //#ifdef WIN32
  5. //#include "comm.h"
  6. //#define MRS_CALL
  7. // #if defined(WEBRTC_WIN)
  8. // #define __declspec(dllexport)
  9. // #define MRS_CALL __stdcall
  10. // #else
  11. // #define __declspec(dllimport)
  12. // #define MRS_CALL __stdcall
  13. // #endif
  14. // #else
  15. // #endif
  16. /*
  17. enum class VideoDesc :std::int32_t {
  18. OK,
  19. Busy,
  20. Reject,
  21. NoFound,
  22. IsVideoing,
  23. };
  24. enum class CaptureType
  25. {
  26. USB,
  27. RealSense,
  28. Zed2,
  29. RTSP,
  30. };
  31. */
  32. using PeerConnectionHandle = void*;
  33. //class INativeNotify;
  34. extern "C" {
  35. //
  36. // Errors
  37. //
  38. using mrsResult = std::uint32_t;
  39. constexpr const mrsResult MRS_SUCCESS{ 0 };
  40. // Unknown generic error
  41. constexpr const mrsResult MRS_E_UNKNOWN{ 0x80000000 };
  42. // Peer conection (0x0xx)
  43. constexpr const mrsResult MRS_E_INVALID_PEER_HANDLE{ 0x80000001 };
  44. constexpr const mrsResult MRS_E_PEER_NOT_INITIALIZED{ 0x80000002 };
  45. // Data (0x3xx)
  46. constexpr const mrsResult MRS_E_SCTP_NOT_NEGOTIATED{ 0x80000301 };
  47. constexpr const mrsResult MRS_E_INVALID_DATA_CHANNEL_ID{ 0x80000302 };
  48. constexpr const mrsResult MRS_E_INVALID_PARAMETER{ 0x8000303 };
  49. //
  50. // Generic utilities
  51. //
  52. /// Opaque enumerator type.
  53. struct mrsEnumerator;
  54. /// Handle to an enumerator.
  55. /// This must be freed after use with |mrsCloseEnum|.
  56. using mrsEnumHandle = mrsEnumerator*;
  57. /// Close an enumerator previously obtained from one of the EnumXxx() calls.
  58. void mrsCloseEnum(mrsEnumHandle* handleRef) ;
  59. //
  60. // Video capture enumeration
  61. //
  62. /// Callback invoked for each enumerated video capture device.
  63. using mrsVideoCaptureDeviceEnumCallback = void(*)(const char* id,
  64. const char* name,
  65. void* user_data);
  66. /// Callback invoked on video capture device enumeration completed.
  67. using mrsVideoCaptureDeviceEnumCompletedCallback =
  68. void(*)(void* user_data);
  69. /// Enumerate the video capture devices asynchronously.
  70. /// For each device found, invoke the mandatory |callback|.
  71. /// At the end of the enumeration, invoke the optional |completedCallback| if it
  72. /// was provided (non-null).
  73. void mrsEnumVideoCaptureDevicesAsync(
  74. mrsVideoCaptureDeviceEnumCallback callback,
  75. void* callbackUserData,
  76. mrsVideoCaptureDeviceEnumCompletedCallback completedCallback,
  77. void* completedCallbackUserData) ;
  78. enum class mrsVideoProfileKind : int32_t {
  79. kUnspecified,
  80. kVideoRecording,
  81. kHighQualityPhoto,
  82. kBalancedVideoAndPhoto,
  83. kVideoConferencing,
  84. kPhotoSequence,
  85. kHighFrameRate,
  86. kVariablePhotoSequence,
  87. kHdrWithWcgVideo,
  88. kHdrWithWcgPhoto,
  89. kVideoHdr8,
  90. };
  91. /// Video profile info.
  92. struct mrsVideoProfileInfo {
  93. // Unique identifier of the video profile.
  94. const char* id;
  95. };
  96. using mrsVideoProfileEnumCallback =
  97. void(*)(void* user_data, const mrsVideoProfileInfo* profile_info);
  98. using mrsVideoProfileEnumCompletedCallback = void(*)(void* user_data);
  99. void mrsEnumVideoProfilesAsync(
  100. const char* device_id,
  101. mrsVideoProfileKind profile_kind,
  102. mrsVideoProfileEnumCallback enumCallback,
  103. void* enumCallbackUserData,
  104. mrsVideoProfileEnumCompletedCallback completedCallback,
  105. void* completedCallbackUserData) ;
  106. /// Video capture format info.
  107. struct mrsVideoCaptureFormatInfo {
  108. // Capture width, in pixels.
  109. uint32_t width;
  110. // Capture height, in pixels.
  111. uint32_t height;
  112. // Maximum capture framerate, in frames per seconds. Video capture devices
  113. // commonly have adaptive framerate based on luminosity, and this value
  114. // reports the maximum framerate the device supports.
  115. float framerate;
  116. // Capture format as a FOURCC code.
  117. // https://docs.microsoft.com/en-us/windows/win32/medfound/video-fourccs
  118. uint32_t fourcc;
  119. };
  120. /// Callback invoked for each enumerated video capture format.
  121. using mrsVideoCaptureFormatEnumCallback = void(
  122. *)(void* user_data, const mrsVideoCaptureFormatInfo* format_info);
  123. /// Callback invoked on video capture format enumeration completed. If the
  124. /// result is not |mrsResult::kSuccess| then some or all of the device formats
  125. /// might not have been enumerated.
  126. using mrsVideoCaptureFormatEnumCompletedCallback =
  127. void(*)(void* user_data, mrsResult result);
  128. void mrsEnumVideoCaptureFormatsAsync(
  129. const char* device_id,
  130. const char* profile_id,
  131. mrsVideoProfileKind profile_kind,
  132. mrsVideoCaptureFormatEnumCallback enumCallback,
  133. void* enumCallbackUserData,
  134. mrsVideoCaptureFormatEnumCompletedCallback completedCallback,
  135. void* completedCallbackUserData) ;
  136. //
  137. // Peer connection
  138. //
  139. /// Opaque handle to a PeerConnection object.
  140. /// Callback fired when the peer connection is connected, that is it finished
  141. /// the JSEP offer/answer exchange successfully.
  142. using PeerConnectionConnectedCallback = void(*)(void* user_data);
  143. /// Callback fired when a local SDP message has been prepared and is ready to be
  144. /// sent by the user via the signaling service.
  145. using PeerConnectionLocalSdpReadytoSendCallback =
  146. void(*)(void* user_data, int32_t peer,int32_t index, const char* type, const char* sdp_data);
  147. /// Callback fired when an ICE candidate has been prepared and is ready to be
  148. /// sent by the user via the signaling service.
  149. using PeerConnectionIceCandidateReadytoSendCallback =
  150. void(*)(void* user_data,
  151. int32_t pid,
  152. int32_t index,
  153. const char* candidate,
  154. int sdpMlineindex,
  155. const char* sdpMid);
  156. /// Callback fired when a renegotiation of the current session needs to occur to
  157. /// account for new parameters (e.g. added or removed tracks).
  158. using PeerConnectionRenegotiationNeededCallback =
  159. void(*)(void* user_data);
  160. /// Callback fired when a remote track is added to a connection.
  161. using PeerConnectionTrackAddedCallback = void(*)(void* user_data);
  162. /// Callback fired when a remote track is removed from a connection.
  163. using PeerConnectionTrackRemovedCallback = void(*)(void* user_data);
  164. /// Callback fired when a local or remote (depending on use) video frame is
  165. /// available to be consumed by the caller, usually for display.
  166. using PeerConnectionI420VideoFrameCallback =
  167. void(*)(void* user_data,
  168. const void* yptr,
  169. const void* uptr,
  170. const void* vptr,
  171. const void* aptr,
  172. const int ystride,
  173. const int ustride,
  174. const int vstride,
  175. const int astride,
  176. const int frame_width, //< TODO : uint?
  177. const int frame_height);
  178. using PeerConnectionARGBVideoFrameCallback =
  179. void(*)(void* user_data,
  180. const uint8_t* yptr, int strideY,
  181. const uint8_t* uptr, int strideU,
  182. const uint8_t* vptr, int strideV,
  183. const int stride, const int frame_width,
  184. const int frame_height);
  185. using PeerConnectionDataChannelMessageCallback =
  186. void(*)(void* user_data, const void* data, const int32_t size);
  187. using PeerConnectionDataChannelBufferingCallback =
  188. void(*)(void* user_data,
  189. const uint64_t previous,
  190. const uint64_t current,
  191. const uint64_t limit);
  192. using PeerConnectionDataChannelStateCallback = void(*)(void* user_data,
  193. int state,
  194. int id);
  195. struct IceServer {
  196. const char* url = nullptr;
  197. const char* username = nullptr;
  198. const char* password = nullptr;
  199. };
  200. bool mrsWebrtcCreateFactory(bool NullCodec);
  201. /// Create a peer connection and return a handle to it.
  202. /// On UWP this must be invoked from another thread than the main UI thread.
  203. PeerConnectionHandle
  204. mrsPeerConnectionCreate() ;
  205. /// Register a callback fired once connected to a remote peer.
  206. /// To unregister, simply pass nullptr as the callback pointer.
  207. void mrsPeerConnectionRegisterConnectedCallback(
  208. PeerConnectionHandle peerHandle,
  209. PeerConnectionConnectedCallback callback,
  210. void* user_data) ;
  211. /// Register a callback fired when a local message is ready to be sent via the
  212. /// signaling service to a remote peer.
  213. void mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(
  214. PeerConnectionHandle peerHandle,
  215. int32_t peer,
  216. int32_t index,
  217. PeerConnectionLocalSdpReadytoSendCallback callback,
  218. void* user_data) ;
  219. /// Register a callback fired when an ICE candidate message is ready to be sent
  220. /// via the signaling service to a remote peer.
  221. void mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(
  222. PeerConnectionHandle peerHandle,
  223. int32_t peer,
  224. int32_t index,
  225. PeerConnectionIceCandidateReadytoSendCallback callback,
  226. void* user_data) ;
  227. /// Register a callback fired when a renegotiation of the current session needs
  228. /// to occur to account for new parameters (e.g. added or removed tracks).
  229. void mrsPeerConnectionRegisterRenegotiationNeededCallback(
  230. PeerConnectionHandle peerHandle,
  231. PeerConnectionRenegotiationNeededCallback callback,
  232. void* user_data) ;
  233. /// Register a callback fired when a remote track is added to the current peer
  234. /// connection.
  235. void mrsPeerConnectionRegisterTrackAddedCallback(
  236. PeerConnectionHandle peerHandle,
  237. PeerConnectionTrackAddedCallback callback,
  238. void* user_data) ;
  239. /// Register a callback fired when a remote track is removed from the current
  240. /// peer connection.
  241. void mrsPeerConnectionRegisterTrackRemovedCallback(
  242. PeerConnectionHandle peerHandle,
  243. PeerConnectionTrackRemovedCallback callback,
  244. void* user_data) ;
  245. /// Register a callback fired when a video frame is available from a local video
  246. /// track, usually from a local video capture device (local webcam).
  247. void mrsPeerConnectionRegisterI420LocalVideoFrameCallback(
  248. PeerConnectionHandle peerHandle,
  249. PeerConnectionI420VideoFrameCallback callback,
  250. void* user_data) ;
  251. /// Register a callback fired when a video frame is available from a local video
  252. /// track, usually from a local video capture device (local webcam).
  253. void mrsPeerConnectionRegisterARGBLocalVideoFrameCallback(
  254. PeerConnectionHandle peerHandle,
  255. PeerConnectionARGBVideoFrameCallback callback,
  256. void* user_data) ;
  257. /// Register a callback fired when a video frame from a video track was received
  258. /// from the remote peer.
  259. void mrsPeerConnectionRegisterI420RemoteVideoFrameCallback(
  260. PeerConnectionHandle peerHandle,
  261. PeerConnectionI420VideoFrameCallback callback,
  262. void* user_data) ;
  263. /// Register a callback fired when a video frame from a video track was received
  264. /// from the remote peer.
  265. void mrsPeerConnectionRegisterARGBRemoteVideoFrameCallback(
  266. PeerConnectionHandle peerHandle,
  267. PeerConnectionARGBVideoFrameCallback callback,
  268. void* user_data) ;
  269. /// Add a local video track from a local video capture device (webcam) to
  270. /// the collection of tracks to send to the remote peer.
  271. /// |video_device_id| specifies the unique identifier of a video capture
  272. /// device to open, as obtained by enumerating devices with
  273. /// mrsEnumVideoCaptureDevicesAsync(), or null for any device.
  274. /// |enable_mrc| allows enabling Mixed Reality Capture on HoloLens devices, and
  275. /// is otherwise ignored for other video capture devices. On UWP this must be
  276. /// invoked from another thread than the main UI thread.
  277. /* bool mrsPeerConnectionAddLocalVideoTrack(
  278. PeerConnectionHandle peerHandle,
  279. CaptureType type,
  280. DisplayResolution res,
  281. int32_t fps) ; */
  282. #ifdef WEBRTC_LINUX
  283. // bool mrsPeerConnectionAddLocalRtspTrack(PeerConnectionHandle peerHandle,const std::string& label,const std::string& videourl,
  284. // int32_t index, int32_t width, int32_t height);
  285. bool mrsPeerConnectionAddLocalVideoTrack(
  286. PeerConnectionHandle peerHandle,
  287. RenderPosition type,
  288. int32_t index);
  289. bool mrsPeerConnectionSwitchCapture(
  290. PeerConnectionHandle peerHandle,
  291. bool front
  292. );
  293. bool mrsPeerConnectionSetCtx(PeerConnectionHandle peerHandle,void * data);
  294. void * mrsPeerConnectionCurrentCtx(PeerConnectionHandle peerHandle);
  295. #endif
  296. /// Add a local audio track from a local audio capture device (microphone) to
  297. /// the collection of tracks to send to the remote peer.
  298. bool mrsPeerConnectionAddLocalAudioTrack(PeerConnectionHandle peerHandle) ;
  299. void mrsPeerConnectionRegisterChannelCallback(
  300. PeerConnectionHandle peerHandle,
  301. PeerConnectionDataChannelMessageCallback message_callback,
  302. void* message_user_data,
  303. PeerConnectionDataChannelBufferingCallback buffering_callback,
  304. void* buffering_user_data,
  305. PeerConnectionDataChannelStateCallback state_callback,
  306. void* state_user_data) ;
  307. /// Add a new data channel.
  308. /// This function has two distinct uses:
  309. /// - If id < 0, then it adds a new in-band data channel with an ID that will be
  310. /// selected by the WebRTC implementation itself, and will be available later.
  311. /// In that case the channel is announced to the remote peer for it to create a
  312. /// channel with the same ID.
  313. /// - If id >= 0, then it adds a new out-of-band negotiated channel with the
  314. /// given ID, and it is the responsibility of the app to create a channel with
  315. /// the same ID on the remote peer to be able to use the channel.
  316. mrsResult mrsPeerConnectionAddDataChannel(
  317. PeerConnectionHandle peerHandle,
  318. // int id, // -1 for auto, >=0 for negotiated
  319. bool ordered,
  320. bool reliable) ;
  321. void mrsPeerConnectionRemoveLocalVideoTrack(
  322. PeerConnectionHandle peerHandle) ;
  323. void mrsPeerConnectionRemoveLocalAudioTrack(
  324. PeerConnectionHandle peerHandle) ;
  325. bool
  326. mrsPeerConnectionRemoveDataChannel(PeerConnectionHandle peerHandle) ;
  327. bool
  328. mrsPeerConnectionSendDataChannelMessage(PeerConnectionHandle peerHandle,
  329. const void* data,
  330. uint64_t size) ;
  331. /// Add a new ICE candidate received from a signaling service.
  332. bool
  333. mrsPeerConnectionAddIceCandidate(PeerConnectionHandle peerHandle,
  334. const char* sdp,
  335. const int sdp_mline_index,
  336. const char* sdp_mid) ;
  337. /// Create a new JSEP offer to try to establish a connection with a remote peer.
  338. /// This will generate a local offer message, then fire the
  339. /// "LocalSdpReadytoSendCallback" callback, which should send this message via
  340. /// the signaling service to a remote peer.
  341. bool
  342. mrsPeerConnectionCreateOffer(PeerConnectionHandle peerHandle) ;
  343. /// Create a new JSEP answer to a received offer to try to establish a
  344. /// connection with a remote peer. This will generate a local answer message,
  345. /// then fire the "LocalSdpReadytoSendCallback" callback, which should send this
  346. /// message via the signaling service to a remote peer.
  347. bool
  348. mrsPeerConnectionCreateAnswer(PeerConnectionHandle peerHandle) ;
  349. /// Set a remote description received from a remote peer via the signaling
  350. /// service.
  351. bool
  352. mrsPeerConnectionSetRemoteDescription(PeerConnectionHandle peerHandle,
  353. const char* type,
  354. const char* sdp) ;
  355. /// Close a peer connection and free all resources associated with it.
  356. void
  357. mrsPeerConnectionClose(PeerConnectionHandle* peerHandle) ;
  358. //
  359. // SDP utilities
  360. //
  361. /// Force audio and video codecs when advertizing capabilities in an SDP offer.#
  362. ///
  363. /// This is a workaround for the lack of access to codec selection. Instead of
  364. /// selecting codecs in code, this can be used to intercept a generated SDP
  365. /// offer before it is sent to the remote peer, and modify it by removing the
  366. /// codecs the user does not want.
  367. ///
  368. /// Codec names are compared to the list of supported codecs in the input
  369. /// message string, and if found then other codecs are pruned out. If the codec
  370. /// name is not found, the codec is assumed to be unsupported, so codecs for
  371. /// that type are not modified.
  372. ///
  373. /// On return the SDP offer message string to be sent via the signaler is stored
  374. /// into the output buffer pointed to by |buffer|.
  375. ///
  376. /// Note that because this function always return a message shorter or equal to
  377. /// the input message, one way to ensure this function doesn't fail is to pass
  378. /// an output buffer as large as the input message.
  379. ///
  380. /// |message| SDP message string to deserialize.
  381. /// |audio_codec_name| Optional SDP name of the audio codec to
  382. /// force if supported, or nullptr or empty string to leave unmodified.
  383. /// |video_codec_name| Optional SDP name of the video codec to force if
  384. /// supported, or nullptr or empty string to leave unmodified.
  385. /// |buffer| Output buffer of capacity *|buffer_size|.
  386. /// |buffer_size| Pointer to the buffer capacity on input, modified on output
  387. /// with the actual size of the null-terminated string, including the null
  388. /// terminator, so the size of the used part of the buffer, in bytes.
  389. /// Returns true on success or false if the buffer is not large enough to
  390. /// contain the new SDP message.
  391. bool mrsSdpForceCodecs(const char* message,
  392. const char* audio_codec_name,
  393. const char* video_codec_name,
  394. char* buffer,
  395. int* buffer_size);
  396. //
  397. // Generic utilities
  398. //
  399. /// Optimized helper to copy a contiguous block of memory.
  400. /// This is equivalent to the standard malloc() function.
  401. void mrsMemCpy(void* dst, const void* src, int size);
  402. /// Optimized helper to copy a block of memory with source and destination
  403. /// stride.
  404. void mrsMemCpyStride(void* dst,
  405. int dst_stride,
  406. const void* src,
  407. int src_stride,
  408. int elem_size,
  409. int elem_count);
  410. // using mrsWebsocketCallback = mrsResult(*)(void* user_data,
  411. // const char* desc);
  412. //
  413. // mrsResult mrsWebsocketCreate(INativeNotify* data,
  414. // mrsWebSocketHandle* handle);
  415. //
  416. // void mrsWebsocketConnect(mrsWebSocketHandle handle,
  417. // const char* name,
  418. // const char* password);
  419. //
  420. // void mrsWebsocketClose(mrsWebSocketHandle handle);
  421. //
  422. // void mrsWebsocketVideoReq(mrsWebSocketHandle handle, int32_t peer, int32_t width, int32_t height, int32_t fps);
  423. // void mrsWebsocketVideoRep(mrsWebSocketHandle handle,
  424. // int32_t peer,
  425. // VideoDesc desc,
  426. // int32_t width,
  427. // int32_t height,
  428. // int32_t fps);
  429. /* void mrsWebsocketCreateOffer(mrsWebSocketHandle handle, int32_t
  430. * peer);*/
  431. // void mrsWebSocketLocalSdpReadytoSend(mrsWebSocketHandle handle,
  432. // const char* type,
  433. // const char* sdp);
  434. // void mrsWebSocketIceCandidateReadytoSend(mrsWebSocketHandle handle,
  435. // const char* sdp,
  436. // const int32_t sdp_mline_index,
  437. // const char* sdp_mid);
  438. // void mrsWebSocketHangup(mrsWebSocketHandle handle, int32_t peer);
  439. // /*class RemoteVideoTrackWrapper;*/
  440. // void mrsWebSocketCancelReq(mrsWebSocketHandle handle, int32_t peer);
  441. }