api.h 19 KB

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