libkurento.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (C) 2020 Michael Van Canneyt
  4. Kurento import classes for pas2js
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit libkurento;
  12. {$mode objfpc}
  13. {$modeswitch externalclass}
  14. interface
  15. uses
  16. Types, JS, Web, node.events;
  17. Type
  18. { ---------------------------------------------------------------------
  19. EventEmitter
  20. ---------------------------------------------------------------------}
  21. TKJSEventEmitterHandler = TNJSEventEmitterHandler;
  22. TKJSEventEmitter = TNJSEventEmitter;
  23. { ---------------------------------------------------------------------
  24. Kurento-Client
  25. ---------------------------------------------------------------------}
  26. Type
  27. TKurentoMediaObject = Class;
  28. TKurentoMediaElement = Class;
  29. TKurentoMediaPipeline = Class;
  30. TKurentoClient = Class;
  31. TElementConnectionData = Class;
  32. TKurentoClientOptions = class external name 'Object' (TJSObject)
  33. failAfter : Integer;
  34. enableTransactions : Boolean;
  35. strict_ : boolean; external name 'strict';
  36. access_token : string;
  37. max_retries : integer;
  38. request_timeout : integer;
  39. response_timeout : integer;
  40. duplicates_timeoout : Integer;
  41. socket : TJSObject;
  42. end;
  43. TErrorCallBack = reference to Procedure(aError : TJSError);
  44. TMediaObjectCallBack = reference to procedure(aError : TJSError; aResult : TKurentoMediaObject);
  45. TIntegerCallBack = reference to procedure (aError : TJSError; aResult : NativeInt);
  46. TBooleanCallBack = reference to procedure (aError : TJSError; aResult : Boolean);
  47. TObjectCallBack = reference to procedure (aError : TJSError; aResult : TJSObject);
  48. TStringCallBack = reference to procedure (aError : TJSError; aResult : String);
  49. TFloatCallBack = reference to procedure (aError : TJSError; aResult : Double);
  50. TArrayCallBack = reference to procedure (aError : TJSError; aResult : TJSArray);
  51. TMediaPipeLineCallBack = reference to procedure(aError : TJSError; aResult : TKurentoMediaPipeline);
  52. TElementConnectionDataCallBack = reference to procedure(aError : TJSError; aResult : TElementConnectionData);
  53. TKurentoMediaObject = class external name 'Object' (TKJSEventEmitter)
  54. id : string;
  55. name : string;
  56. tags : TJSObject;
  57. Function addTag(aKey,aValue : String; aCallBack : TErrorCallBack) : TJSPromise;
  58. Function getTag(aKey: String; aCallBack : TStringCallBack) : TJSPromise;
  59. Function getTags(aKey: String; aCallBack : TArrayCallBack) : TJSPromise;
  60. Function getChildren(aCallback : TMediaObjectCallBack) : TJSPromise;
  61. Function getCreationTime(aCallBack : TIntegerCallBack) : TJSPromise;
  62. Function getMediaPipeLine(aCallBack: TMediaPipeLineCallBack) : TJSPromise;
  63. Function getName(aCallBack : TStringCallBack) : TJSPromise;
  64. Function getParent(aCallBack : TMediaObjectCallBack) : TJSPromise;
  65. Function getSendTagsInEvents(aCallback : TBooleanCallBack) : TJSPromise;
  66. Function removeTag(aKey: String; aCallBack : TStringCallBack) : TJSPromise;
  67. Function setName(aCallBack : TErrorCallBack) : TJSPromise;
  68. Function setSendTagsInEvents(aValue : Boolean;aCallBack : TErrorCallBack) : TJSPromise;
  69. procedure release; // Not documented but present...
  70. end;
  71. TKurentoMediaObjectDynArray = array of TKurentoMediaObject;
  72. TMediaType = Class external name 'Object' (TJSObject);
  73. TElementConnectionData = Class external name 'Object' (TJSObject)
  74. source : TKurentoMediaElement;
  75. sink : TKurentoMediaElement;
  76. type_ : TMediaType; external name 'type';
  77. sourceDescription : String;
  78. sinkDescription : String;
  79. end;
  80. TSinkCallBack = TElementConnectionDataCallBack;
  81. TKurentoMediaElement = class external name 'Object' (TKurentoMediaObject)
  82. function connect(sink : TKurentoMediaElement; CallBack : TErrorCallBack) : TJSPromise; overload;
  83. function connect(sink : TKurentoMediaElement; MediaType : TMediaType; CallBack : TErrorCallBack) : TJSPromise; overload;
  84. function connect(sink : TKurentoMediaElement; MediaType : TMediaType; SourceDescription : String; CallBack : TErrorCallBack) : TJSPromise; overload;
  85. function connect(sink : TKurentoMediaElement; MediaType : TMediaType; SourceDescription,SinkDescription : String; CallBack : TErrorCallBack) : TJSPromise; overload;
  86. function disconnect(sink : TKurentoMediaElement; CallBack : TErrorCallBack) : TJSPromise; overload;
  87. function disconnect(sink : TKurentoMediaElement; MediaType : TMediaType; CallBack : TErrorCallBack) : TJSPromise;overload;
  88. function disconnect(sink : TKurentoMediaElement; MediaType : TMediaType; SourceDescription : String; CallBack : TErrorCallBack) : TJSPromise;overload;
  89. function disconnect(sink : TKurentoMediaElement; MediaType : TMediaType; SourceDescription,SinkDescription : String; CallBack : TErrorCallBack) : TJSPromise;overload;
  90. function getSinkConnections(callBack : TSinkCallBack) : TJSPromise;overload;
  91. function getSinkConnections(mediaType : TMediaType; callBack : TSinkCallBack) : TJSPromise;overload;
  92. function getSinkConnections(mediaType : TMediaType; aDescription : string; callBack : TSinkCallBack) : TJSPromise;overload;
  93. function getSourceConnections(callBack : TSinkCallBack) : TJSPromise;overload;
  94. function getSourceConnections(mediaType : TMediaType; callBack : TSinkCallBack) : TJSPromise;overload;
  95. function getSourceConnections(mediaType : TMediaType; aDescription : string; callBack : TSinkCallBack) : TJSPromise;overload;
  96. function getStats(callBack : TSinkCallBack) : TJSPromise;overload;
  97. function getStats(mediaType : TMediaType; callBack : TObjectCallBack) : TJSPromise;overload;
  98. function isMediaFlowingIn(callBack : TBooleanCallBack) : TJSPromise;overload;
  99. function isMediaFlowingIn(mediaType : TMediaType; callBack : TBooleanCallBack) : TJSPromise;overload;
  100. function isMediaFlowingIn(mediaType : TMediaType; Description : String; callBack : TBooleanCallBack) : TJSPromise;overload;
  101. function isMediaFlowingOut(callBack : TBooleanCallBack) : TJSPromise;overload;
  102. function isMediaFlowingOut(mediaType : TMediaType; callBack : TBooleanCallBack) : TJSPromise;overload;
  103. function isMediaFlowingOut(mediaType : TMediaType; Description : String; callBack : TBooleanCallBack) : TJSPromise;overload;
  104. function isMediaTranscoding(callBack : TBooleanCallBack) : TJSPromise;overload;
  105. function isMediaTranscoding(mediaType : TMediaType; callBack : TBooleanCallBack) : TJSPromise;overload;
  106. function isMediaTranscoding(mediaType : TMediaType; Description : String; callBack : TBooleanCallBack) : TJSPromise;overload;
  107. function setAudioFormat(caps : TJSObject; Callback : TErrorCallBack) : TJSPromise;
  108. function setOutputBitrate(aRate : NativeInt; Callback : TErrorCallBack) : TJSPromise;
  109. function setVideoFormat(caps : TJSObject; Callback : TErrorCallBack) : TJSPromise;
  110. function getMaxOutputBitrate(Callback : TIntegerCallBack) : TJSPromise;
  111. function getMinOutputBitrate(Callback : TIntegerCallBack) : TJSPromise;
  112. function setMaxOutputBitrate(aValue : NativeInt;Callback : TErrorCallBack) : TJSPromise;
  113. function setMinOutputBitrate(aValue : NativeInt;Callback : TErrorCallBack) : TJSPromise;
  114. end;
  115. TKurentoMediaElementDynArray = array of TKurentoMediaElement;
  116. TKurentoServerManager = Class external name 'Object' (TKurentoMediaObject)
  117. function getCpuCount(aCallBack : TIntegerCallBack) : TJSPromise;
  118. function getKmd(aCallBack : TStringCallBack) : TJSPromise;
  119. function getUsedCpu(aCallBack : TFloatCallBack) : TJSPromise;
  120. function getUsedMemory(aCallBack : TIntegerCallBack) : TJSPromise;
  121. function getInfo(aCallBack : TObjectCallBack) : TJSPromise;
  122. function getMetadata(aCallBack : TStringCallBack) : TJSPromise;
  123. function getPipeLines(aCallBack : TMediaPipeLineCallBack) : TJSPromise;
  124. function getSessions(aCallBack : TStringCallBack) : TJSPromise;
  125. end;
  126. TKurentoCreateCallBack = reference to Procedure(aError : TJSError; aResult : TKurentoMediaObject);
  127. TKurentoCreateArrayCallBack = reference to Procedure(aError : TJSError; aResult : TKurentoMediaElementDynArray);
  128. TKurentoMediaPipeline = class external name 'Object' (TKurentoMediaObject)
  129. // Missing from documentation :-(
  130. // single
  131. function create(aType : string; params : TStringDynArray; aCallback: TKurentoCreateCallBack) : TKurentoMediaObject;
  132. function create(aType : string; aCallback: TKurentoCreateCallBack) : TKurentoMediaObject;
  133. // Multi
  134. function create(aType : string; aCallback: TKurentoCreateArrayCallBack) : TKurentoMediaObjectDynArray;
  135. function create(aType : string; params : TStringDynArray; aCallback: TKurentoCreateArrayCallBack) : TKurentoMediaObjectDynArray;
  136. end;
  137. TKurentoEndPoint = class external name 'Object' (TKurentoMediaElement);
  138. TKurentoSessionEndPoint = class external name 'Object' (TKurentoEndPoint);
  139. TKurentoSdpEndPoint = class external name 'Object' (TKurentoSessionEndPoint)
  140. Function generateOffer(aCallBack : TStringCallBack) : TJSPromise;
  141. Function getLocalSessionDescriptor(aCallBack : TStringCallBack) : TJSPromise;
  142. Function getRemoteSessionDescriptor(aCallBack : TStringCallBack) : TJSPromise;
  143. function processAnswer(answer : String;aCallBack : TStringCallBack) : TJSPromise;
  144. function processOffer(offer : String;aCallBack : TStringCallBack) : TJSPromise;
  145. function getMaxAudioRecvBandwidth(aCallBack : TIntegerCallBack): TJSPromise;
  146. function getMaxVideoRecvBandwidth(aCallBack : TIntegerCallBack): TJSPromise;
  147. function setMaxAudioRecvBandwidth(aValue : NativeInt; aCallBack : TErrorCallBack): TJSPromise;
  148. function setMaxVideoRecvBandwidth(aValue : NativeInt; aCallBack : TErrorCallBack): TJSPromise;
  149. end;
  150. TKurentoBaseRtpEndPoint = class external name 'Object' (TKurentoSdpEndPoint)
  151. Function getConnectionState(callback : TObjectCallBack) : TJSPromise;
  152. // audio Recv/send
  153. function getMinAudioRecvBandwidth(aCallBack : TIntegerCallBack): TJSPromise;
  154. function getMinAudioSendBandwidth(aCallBack : TIntegerCallBack): TJSPromise;
  155. function setMinAudioRecvBandwidth(aValue : NativeInt; aCallBack : TErrorCallBack): TJSPromise;
  156. function setMinAudioSendBandwidth(aValue : NativeInt; aCallBack : TErrorCallBack): TJSPromise;
  157. // video Recv/send
  158. function getMinVideoRecvBandwidth(aCallBack : TIntegerCallBack): TJSPromise;
  159. function getMinVideoSendBandwidth(aCallBack : TIntegerCallBack): TJSPromise;
  160. function setMinVideoRecvBandwidth(aValue : NativeInt; aCallBack : TErrorCallBack): TJSPromise;
  161. function setMinVideoSendBandwidth(aValue : NativeInt; aCallBack : TErrorCallBack): TJSPromise;
  162. Function getRembParams(callback : TObjectCallBack) : TJSPromise;
  163. Function setRembParams(aValue : string; Callback : TErrorCallBack) : TJSPromise;
  164. Function getMtu(Callback : TIntegerCallBack) : TJSPromise;
  165. Function setMtu(aValue : Integer; Callback : TErrorCallBack) : TJSPromise;
  166. end;
  167. TKurentoIceCandidate = class external name 'Object' (TJSObject)
  168. end;
  169. TKurentoWebRtcEndPoint = class external name 'Object' (TKurentoBaseRtpEndPoint)
  170. Constructor new;
  171. Function addIceCandidate(aCandidate : TKurentoIceCandidate; callback : TErrorCallBack) : TJSPromise;
  172. Function closeDataChannel(channelID : Integer; callback : TErrorCallBack) : TJSPromise;
  173. Function createDataChannel(aLabel: String; Ordered : Boolean; MaxPacketLifeTime : Integer; maxRetransMits : Integer;Protocol : string; Callback : TErrorCallBack) : TJSPromise;
  174. Function createDataChannel(aLabel: String; Ordered : Boolean; MaxPacketLifeTime : Integer; maxRetransMits : Integer; Callback : TErrorCallBack) : TJSPromise;
  175. Function createDataChannel(aLabel: String; Ordered : Boolean; MaxPacketLifeTime : Integer; Callback : TErrorCallBack) : TJSPromise;
  176. Function createDataChannel(aLabel: String; Ordered : Boolean; Callback : TErrorCallBack) : TJSPromise;
  177. Function createDataChannel(aLabel: String; Callback : TErrorCallBack) : TJSPromise;
  178. Function createDataChannel(Callback : TErrorCallBack) : TJSPromise;
  179. Function gatherCandidates(Callback : TErrorCallBack) : TJSPromise;
  180. Function getExternalAddress(Callback : TStringCallBack) : TJSPromise;
  181. Function getICECandidatePairs(Callback : TObjectCallBack) : TJSPromise;
  182. Function getIceConnectionState(Callback : TObjectCallBack) : TJSPromise;
  183. Function getNetworkInterfaces(Callback : TStringCallBack) : TJSPromise;
  184. Function getStunServerAddress(Callback : TStringCallBack) : TJSPromise;
  185. Function getStunServerPort(Callback : TIntegerCallBack) : TJSPromise;
  186. Function getTurnUrl(Callback : TStringCallBack) : TJSPromise;
  187. Function setExternalAddress(aValue : string; Callback : TErrorCallBack) : TJSPromise;
  188. Function setNetworkInterfaces(aValue : string; Callback : TErrorCallBack) : TJSPromise;
  189. Function setStunServerAddress(aValue : string; Callback : TErrorCallBack) : TJSPromise;
  190. Function setStunServerPort(aValue : Integer; Callback : TErrorCallBack) : TJSPromise;
  191. Function setTurnUrl(aValue : string; Callback : TErrorCallBack) : TJSPromise;
  192. end;
  193. TKurentoClientCallBack = reference to Procedure(aError : TJSError; aClient : TKurentoClient);
  194. TKurentoErrorCallBack = TErrorCallBack;
  195. TKurentoServerManagerCallback = reference to Procedure(aError : TJSError; aResult : TKurentoServerManager);
  196. TKurentoClient = class external name 'kurentoClient.KurentoClient' (TNJSEventEmitter)
  197. Constructor new(aURL : String; aOptions : TKurentoClientOptions; aCallBack : TKurentoClientCallBack);
  198. Constructor new(aURL : String; aCallBack : TKurentoClientCallBack);
  199. procedure beginTransaction;
  200. procedure endTransaction;
  201. procedure close;
  202. function catch(onRejected : TJSPromiseResolver) : TJSPromise;
  203. function connect(aMedia : TKurentoMediaObject; aCallBack : TKurentoErrorCallBack) : TJSPromise;
  204. // single
  205. function create(aType : string; params : TStringDynArray; aCallback: TKurentoCreateCallBack) : TKurentoMediaObject;
  206. function create(aType : string; aCallback: TKurentoCreateCallBack) : TKurentoMediaObject;
  207. // Multi
  208. function create(aType : string; aCallback: TKurentoCreateArrayCallBack) : TKurentoMediaObjectDynArray;
  209. function create(aType : string; params : TStringDynArray; aCallback: TKurentoCreateArrayCallBack) : TKurentoMediaObjectDynArray;
  210. function getComplexType(aType : string) : JSValue;
  211. function getMediaObjectById(aID : String; aCallBack : TKurentoCreateCallBack) : TJSPromise;
  212. function getServerManager(aCallBack : TKurentoServerManagerCallBack) : TJSPromise;
  213. function then_(onfulfilled, onRejected : TJSPromiseResolver) : TJSPromise;
  214. end;
  215. TMediaObjectCreator = class external name 'kurentoClient.MediaObjectCreator' (TJSObject)
  216. constructor new(aHost : String; encodeCreate,encodeRPC,encodeTransaction,describe : JSValue);
  217. // single
  218. function create(aType : string; params : TStringDynArray; aCallback: TKurentoCreateCallBack) : TKurentoMediaObject;
  219. function create(aType : string; aCallback: TKurentoCreateCallBack) : TKurentoMediaObject;
  220. // Multi
  221. function create(aType : string; aCallback: TKurentoCreateArrayCallBack) : TKurentoMediaObjectDynArray;
  222. function create(aType : string; params : TStringDynArray; aCallback: TKurentoCreateArrayCallBack) : TKurentoMediaObjectDynArray;
  223. end;
  224. TKurentoClientGlobal = Class external name 'kurentoClient' (TJSObject)
  225. MediaObjectCreator : TMediaObjectCreator;
  226. Class function getComplexType(aType : string) : JSValue;
  227. end;
  228. Function KurentoClient (aURL : String; aOptions : TKurentoClientOptions; aCallBack : TKurentoClientCallBack) : TJSPromise; external name 'kurentoClient.KurentoClient';
  229. Function KurentoClient (aURL : String; aCallBack : TKurentoClientCallBack) : TJSPromise; external name 'kurentoClient.KurentoClient';
  230. { ---------------------------------------------------------------------
  231. Kurento-utils
  232. ---------------------------------------------------------------------}
  233. Const
  234. sModeRecv = 'recv';
  235. sModeSend = 'send';
  236. sModeSendRecv = 'sendRecv';
  237. sSourceWebCam = 'webcam';
  238. sSourceScreen = 'screen';
  239. sSourceWindow = 'window';
  240. Type
  241. TWebRtcProc = reference to procedure;
  242. TWebRtcCallBack = reference to procedure(aError : TJSError);
  243. TPeerConnection = Class external name 'Object' (TJSObject);
  244. TDataChannel = Class external name 'Object' (TJSObject);
  245. TKurentoOffer = string;
  246. TKurentoAnswer = string;
  247. TKurentoOfferCallBack = reference to Procedure(aError : TJSError; aOffer : TKurentoOffer);
  248. TKurentoProcessAnswerCallback = reference to Procedure(aError : TJSError; aAnswer : TKurentoAnswer);
  249. TDataChannelConfig = class external name 'Object' (TJSObject)
  250. id : string;
  251. options : TJSObject;
  252. onopen : TWebRtcProc;
  253. onclose : TWebRtcProc;
  254. onmessage : TWebRtcProc;
  255. onbufferedamountlow : TWebRtcProc;
  256. onerror : TWebRTCCallBack;
  257. end;
  258. TICEConfig = class external name 'Object' (TJSObject)
  259. iceServers : TJSObjectDynArray;
  260. end;
  261. TWebRtcPeerOptions = class external name 'Object' (TJSObject)
  262. localVideo : TJSHTMLElement;
  263. remoteVideo : TJSHTMLElement;
  264. videoStream : TJSObject;
  265. audioStream : TJSObject;
  266. mediaConstraints : TJSObject;
  267. peerConnection : TPeerConnection;
  268. sendSource : String;
  269. dataChannels : String;
  270. dataChannelConfig : TDataChannelConfig;
  271. onstreamended : TWebRtcProc;
  272. onicecandidate : TWebRtcProc;
  273. onicecandidategatheringdone : TWebRtcProc;
  274. simulcast : boolean;
  275. configuration : TICEConfig;
  276. end;
  277. TWebRtcPeer = class external name 'kurentoUtils.WebRtcPeer' (TKJSEventEmitter)
  278. Private
  279. FPeerConnection : TPeerConnection; external name 'peerConnection';
  280. FID : String; external name 'id';
  281. FlocalVideo : TJSHTMLElement; external name 'localVideo';
  282. FRemoteVideo : TJSHTMLElement external name 'remoteVideo';
  283. FDataChannel : TDataChannel; external name 'dataChannel';
  284. FCurrentFrame : TJSHTMLCanvasElement ; external name 'currentFrame';
  285. FaudioEnabled : Boolean; External name 'audioEnabled';
  286. FvideoEnabled : Boolean; External name 'videoEnabled';
  287. Public
  288. Constructor new(Mode : String; Options: TWebRtcPeerOptions; CallBack : TWebRtcCallBack);
  289. Constructor new(Mode : String; Options: TJSObject; CallBack : TWebRtcCallBack);
  290. Class function WebRtcPeerRecvOnly(Options: TWebRtcPeerOptions; CallBack : TWebRtcCallBack) : TWebRtcPeer;
  291. Class function WebRtcPeerSendOnly(Options: TWebRtcPeerOptions; CallBack : TWebRtcCallBack) : TWebRtcPeer;
  292. Class function WebRtcPeerSendrecv(Options: TWebRtcPeerOptions; CallBack : TWebRtcCallBack) : TWebRtcPeer;
  293. Class function harkUtils(stream , options : TJSObject) :JSValue;
  294. Class procedure bufferSizeCandidates(pc : TPeerConnection;CallBack : TWebRtcCallBack);
  295. Procedure addIceCandidate(aCandidate : TJSObject; callback : TErrorCallBack) ;
  296. function getPeerConnection : TPeerConnection;
  297. function getLocalSessionDescriptor : JSValue;
  298. function getRemoteSessionDescriptor : JSValue;
  299. procedure setRemoteVideo;
  300. procedure showLocalVideo;
  301. procedure send(Data : TJSObject);
  302. procedure dispose;
  303. procedure generateOffer(aOfferCallBack : TKurentoOfferCallBack);
  304. procedure processOffer(aOffer : JSValue; CallBack : TKurentoOfferCallBack);
  305. procedure processAnswer(aOffer : JSValue; CallBack : TErrorCallBack);
  306. function getLocalStream(aIndex : integer) : TJSMediaStreamTrack;
  307. function getRemoteStream(aIndex : integer) : TJSMediaStreamTrack;
  308. Property PeerConnection : TPeerConnection Read FPeerConnection;
  309. Property ID : String Read FID;
  310. Property LocalVideo : TJSHTMLElement Read FlocalVideo;
  311. Property RemoteVideo : TJSHTMLElement Read FRemoteVideo;
  312. Property DataChannel : TDataChannel Read FDataChannel;
  313. Property CurrentFrame : TJSHTMLCanvasElement Read FCurrentFrame;
  314. Property AudioEnabled : Boolean Read FAudioEnabled;
  315. Property VideoEnabled : Boolean Read FVideoEnabled;
  316. end;
  317. implementation
  318. end.