api.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 PeerConnectionVideoFrameCallback =
  167. void(*)(void* user_data,
  168. const webrtc::VideoFrame&);
  169. using PeerConnectionARGBVideoFrameCallback =
  170. void(*)(void* user_data,
  171. const uint8_t* yptr, int strideY,
  172. const uint8_t* uptr, int strideU,
  173. const uint8_t* vptr, int strideV,
  174. const int stride, const int frame_width,
  175. const int frame_height);
  176. using PeerConnectionDataChannelMessageCallback =
  177. void(*)(void* user_data, const void* data, const int32_t size);
  178. using PeerConnectionDataChannelBufferingCallback =
  179. void(*)(void* user_data,
  180. const uint64_t previous,
  181. const uint64_t current,
  182. const uint64_t limit);
  183. using PeerConnectionDataChannelStateCallback = void(*)(void* user_data,
  184. int state,
  185. int id);
  186. struct IceServer {
  187. const char* url = nullptr;
  188. const char* username = nullptr;
  189. const char* password = nullptr;
  190. };
  191. bool mrsWebrtcCreateFactory(bool NullEnCodec,bool NullDecode);
  192. /// Create a peer connection and return a handle to it.
  193. /// On UWP this must be invoked from another thread than the main UI thread.
  194. PeerConnectionHandle
  195. mrsPeerConnectionCreate() ;
  196. /// Register a callback fired once connected to a remote peer.
  197. /// To unregister, simply pass nullptr as the callback pointer.
  198. void mrsPeerConnectionRegisterConnectedCallback(
  199. PeerConnectionHandle peerHandle,
  200. PeerConnectionConnectedCallback callback,
  201. void* user_data) ;
  202. /// Register a callback fired when a local message is ready to be sent via the
  203. /// signaling service to a remote peer.
  204. void mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(
  205. PeerConnectionHandle peerHandle,
  206. int32_t peer,
  207. int32_t index,
  208. PeerConnectionLocalSdpReadytoSendCallback callback,
  209. void* user_data) ;
  210. /// Register a callback fired when an ICE candidate message is ready to be sent
  211. /// via the signaling service to a remote peer.
  212. void mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(
  213. PeerConnectionHandle peerHandle,
  214. int32_t peer,
  215. int32_t index,
  216. PeerConnectionIceCandidateReadytoSendCallback callback,
  217. void* user_data) ;
  218. /// Register a callback fired when a renegotiation of the current session needs
  219. /// to occur to account for new parameters (e.g. added or removed tracks).
  220. void mrsPeerConnectionRegisterRenegotiationNeededCallback(
  221. PeerConnectionHandle peerHandle,
  222. PeerConnectionRenegotiationNeededCallback callback,
  223. void* user_data) ;
  224. /// Register a callback fired when a remote track is added to the current peer
  225. /// connection.
  226. void mrsPeerConnectionRegisterTrackAddedCallback(
  227. PeerConnectionHandle peerHandle,
  228. PeerConnectionTrackAddedCallback callback,
  229. void* user_data) ;
  230. /// Register a callback fired when a remote track is removed from the current
  231. /// peer connection.
  232. void mrsPeerConnectionRegisterTrackRemovedCallback(
  233. PeerConnectionHandle peerHandle,
  234. PeerConnectionTrackRemovedCallback callback,
  235. void* user_data) ;
  236. /// Register a callback fired when a video frame is available from a local video
  237. /// track, usually from a local video capture device (local webcam).
  238. void mrsPeerConnectionRegisterLocalVideoFrameCallback(
  239. PeerConnectionHandle peerHandle,
  240. PeerConnectionVideoFrameCallback callback,
  241. void* user_data) ;
  242. /// Register a callback fired when a video frame is available from a local video
  243. /// track, usually from a local video capture device (local webcam).
  244. // void mrsPeerConnectionRegisterARGBLocalVideoFrameCallback(
  245. // PeerConnectionHandle peerHandle,
  246. // PeerConnectionARGBVideoFrameCallback callback,
  247. // void* user_data) ;
  248. /// Register a callback fired when a video frame from a video track was received
  249. /// from the remote peer.
  250. void mrsPeerConnectionRegisterRemoteVideoFrameCallback(
  251. PeerConnectionHandle peerHandle,
  252. PeerConnectionVideoFrameCallback callback,
  253. void* user_data) ;
  254. /// Register a callback fired when a video frame from a video track was received
  255. /// from the remote peer.
  256. // void mrsPeerConnectionRegisterARGBRemoteVideoFrameCallback(
  257. // PeerConnectionHandle peerHandle,
  258. // PeerConnectionARGBVideoFrameCallback callback,
  259. // void* user_data) ;
  260. /// Add a local video track from a local video capture device (webcam) to
  261. /// the collection of tracks to send to the remote peer.
  262. /// |video_device_id| specifies the unique identifier of a video capture
  263. /// device to open, as obtained by enumerating devices with
  264. /// mrsEnumVideoCaptureDevicesAsync(), or null for any device.
  265. /// |enable_mrc| allows enabling Mixed Reality Capture on HoloLens devices, and
  266. /// is otherwise ignored for other video capture devices. On UWP this must be
  267. /// invoked from another thread than the main UI thread.
  268. /* bool mrsPeerConnectionAddLocalVideoTrack(
  269. PeerConnectionHandle peerHandle,
  270. CaptureType type,
  271. DisplayResolution res,
  272. int32_t fps) ; */
  273. #ifdef WEBRTC_LINUX
  274. // bool mrsPeerConnectionAddLocalRtspTrack(PeerConnectionHandle peerHandle,const std::string& label,const std::string& videourl,
  275. // int32_t index, int32_t width, int32_t height);
  276. bool mrsPeerConnectionAddLocalVideoTrack(
  277. PeerConnectionHandle peerHandle,
  278. RenderPosition type,
  279. int32_t index);
  280. bool mrsPeerConnectionSwitchCapture(
  281. PeerConnectionHandle peerHandle,
  282. bool front
  283. );
  284. bool mrsPeerConnectionSetCtx(PeerConnectionHandle peerHandle,void * data);
  285. void * mrsPeerConnectionCurrentCtx(PeerConnectionHandle peerHandle);
  286. #else
  287. class IOverlayVideo;
  288. IOverlayVideo* mrsPeerConnectionCreateOverlayVideo();
  289. bool mrsPeerConnectionAddLocalVideoTrack(
  290. PeerConnectionHandle peerHandle,
  291. IOverlayVideo* video);
  292. #endif
  293. /// Add a local audio track from a local audio capture device (microphone) to
  294. /// the collection of tracks to send to the remote peer.
  295. bool mrsPeerConnectionAddLocalAudioTrack(PeerConnectionHandle peerHandle) ;
  296. void mrsPeerConnectionRegisterChannelCallback(
  297. PeerConnectionHandle peerHandle,
  298. PeerConnectionDataChannelMessageCallback message_callback,
  299. void* message_user_data,
  300. PeerConnectionDataChannelBufferingCallback buffering_callback,
  301. void* buffering_user_data,
  302. PeerConnectionDataChannelStateCallback state_callback,
  303. void* state_user_data) ;
  304. /// Add a new data channel.
  305. /// This function has two distinct uses:
  306. /// - If id < 0, then it adds a new in-band data channel with an ID that will be
  307. /// selected by the WebRTC implementation itself, and will be available later.
  308. /// In that case the channel is announced to the remote peer for it to create a
  309. /// channel with the same ID.
  310. /// - If id >= 0, then it adds a new out-of-band negotiated channel with the
  311. /// given ID, and it is the responsibility of the app to create a channel with
  312. /// the same ID on the remote peer to be able to use the channel.
  313. mrsResult mrsPeerConnectionAddDataChannel(
  314. PeerConnectionHandle peerHandle,
  315. // int id, // -1 for auto, >=0 for negotiated
  316. bool ordered,
  317. bool reliable) ;
  318. void mrsPeerConnectionRemoveLocalVideoTrack(
  319. PeerConnectionHandle peerHandle) ;
  320. void mrsPeerConnectionRemoveLocalAudioTrack(
  321. PeerConnectionHandle peerHandle) ;
  322. bool
  323. mrsPeerConnectionRemoveDataChannel(PeerConnectionHandle peerHandle) ;
  324. bool
  325. mrsPeerConnectionSendDataChannelMessage(PeerConnectionHandle peerHandle,
  326. const void* data,
  327. uint64_t size) ;
  328. /// Add a new ICE candidate received from a signaling service.
  329. bool
  330. mrsPeerConnectionAddIceCandidate(PeerConnectionHandle peerHandle,
  331. const char* sdp,
  332. const int sdp_mline_index,
  333. const char* sdp_mid) ;
  334. /// Create a new JSEP offer to try to establish a connection with a remote peer.
  335. /// This will generate a local offer message, then fire the
  336. /// "LocalSdpReadytoSendCallback" callback, which should send this message via
  337. /// the signaling service to a remote peer.
  338. bool
  339. mrsPeerConnectionCreateOffer(PeerConnectionHandle peerHandle) ;
  340. /// Create a new JSEP answer to a received offer to try to establish a
  341. /// connection with a remote peer. This will generate a local answer message,
  342. /// then fire the "LocalSdpReadytoSendCallback" callback, which should send this
  343. /// message via the signaling service to a remote peer.
  344. bool
  345. mrsPeerConnectionCreateAnswer(PeerConnectionHandle peerHandle) ;
  346. /// Set a remote description received from a remote peer via the signaling
  347. /// service.
  348. bool
  349. mrsPeerConnectionSetRemoteDescription(PeerConnectionHandle peerHandle,
  350. const char* type,
  351. const char* sdp) ;
  352. /// Close a peer connection and free all resources associated with it.
  353. void
  354. mrsPeerConnectionClose(PeerConnectionHandle* peerHandle) ;
  355. //
  356. // SDP utilities
  357. //
  358. /// Force audio and video codecs when advertizing capabilities in an SDP offer.#
  359. ///
  360. /// This is a workaround for the lack of access to codec selection. Instead of
  361. /// selecting codecs in code, this can be used to intercept a generated SDP
  362. /// offer before it is sent to the remote peer, and modify it by removing the
  363. /// codecs the user does not want.
  364. ///
  365. /// Codec names are compared to the list of supported codecs in the input
  366. /// message string, and if found then other codecs are pruned out. If the codec
  367. /// name is not found, the codec is assumed to be unsupported, so codecs for
  368. /// that type are not modified.
  369. ///
  370. /// On return the SDP offer message string to be sent via the signaler is stored
  371. /// into the output buffer pointed to by |buffer|.
  372. ///
  373. /// Note that because this function always return a message shorter or equal to
  374. /// the input message, one way to ensure this function doesn't fail is to pass
  375. /// an output buffer as large as the input message.
  376. ///
  377. /// |message| SDP message string to deserialize.
  378. /// |audio_codec_name| Optional SDP name of the audio codec to
  379. /// force if supported, or nullptr or empty string to leave unmodified.
  380. /// |video_codec_name| Optional SDP name of the video codec to force if
  381. /// supported, or nullptr or empty string to leave unmodified.
  382. /// |buffer| Output buffer of capacity *|buffer_size|.
  383. /// |buffer_size| Pointer to the buffer capacity on input, modified on output
  384. /// with the actual size of the null-terminated string, including the null
  385. /// terminator, so the size of the used part of the buffer, in bytes.
  386. /// Returns true on success or false if the buffer is not large enough to
  387. /// contain the new SDP message.
  388. bool mrsSdpForceCodecs(const char* message,
  389. const char* audio_codec_name,
  390. const char* video_codec_name,
  391. char* buffer,
  392. int* buffer_size);
  393. //
  394. // Generic utilities
  395. //
  396. /// Optimized helper to copy a contiguous block of memory.
  397. /// This is equivalent to the standard malloc() function.
  398. void mrsMemCpy(void* dst, const void* src, int size);
  399. /// Optimized helper to copy a block of memory with source and destination
  400. /// stride.
  401. void mrsMemCpyStride(void* dst,
  402. int dst_stride,
  403. const void* src,
  404. int src_stride,
  405. int elem_size,
  406. int elem_count);
  407. // using mrsWebsocketCallback = mrsResult(*)(void* user_data,
  408. // const char* desc);
  409. //
  410. // mrsResult mrsWebsocketCreate(INativeNotify* data,
  411. // mrsWebSocketHandle* handle);
  412. //
  413. // void mrsWebsocketConnect(mrsWebSocketHandle handle,
  414. // const char* name,
  415. // const char* password);
  416. //
  417. // void mrsWebsocketClose(mrsWebSocketHandle handle);
  418. //
  419. // void mrsWebsocketVideoReq(mrsWebSocketHandle handle, int32_t peer, int32_t width, int32_t height, int32_t fps);
  420. // void mrsWebsocketVideoRep(mrsWebSocketHandle handle,
  421. // int32_t peer,
  422. // VideoDesc desc,
  423. // int32_t width,
  424. // int32_t height,
  425. // int32_t fps);
  426. /* void mrsWebsocketCreateOffer(mrsWebSocketHandle handle, int32_t
  427. * peer);*/
  428. // void mrsWebSocketLocalSdpReadytoSend(mrsWebSocketHandle handle,
  429. // const char* type,
  430. // const char* sdp);
  431. // void mrsWebSocketIceCandidateReadytoSend(mrsWebSocketHandle handle,
  432. // const char* sdp,
  433. // const int32_t sdp_mline_index,
  434. // const char* sdp_mid);
  435. // void mrsWebSocketHangup(mrsWebSocketHandle handle, int32_t peer);
  436. // /*class RemoteVideoTrackWrapper;*/
  437. // void mrsWebSocketCancelReq(mrsWebSocketHandle handle, int32_t peer);
  438. }