llwebrtc.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @file llwebrtc.h
  3. * @brief WebRTC interface
  4. *
  5. * $LicenseInfo:firstyear=2023&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2023, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free tSoftware
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. /*
  27. * llwebrtc wraps the native webrtc c++ library in a dynamic library with a simlified interface
  28. * so that the viewer can use it. This is done because native webrtc has a different
  29. * overall threading model than the viewer.
  30. * The native webrtc library is also compiled with clang, and has memory management
  31. * functions that conflict namespace-wise with those in the viewer.
  32. *
  33. * Due to these differences, code from the viewer cannot be pulled in to this
  34. * dynamic library, so it remains very simple.
  35. */
  36. #ifndef LLWEBRTC_H
  37. #define LLWEBRTC_H
  38. #include <string>
  39. #include <vector>
  40. #ifdef LL_MAKEDLL
  41. #ifdef WEBRTC_WIN
  42. #define LLSYMEXPORT __declspec(dllexport)
  43. #elif WEBRTC_LINUX
  44. #define LLSYMEXPORT __attribute__((visibility("default")))
  45. #else
  46. #define LLSYMEXPORT /**/
  47. #endif
  48. #else
  49. #define LLSYMEXPORT /**/
  50. #endif // LL_MAKEDLL
  51. namespace llwebrtc
  52. {
  53. // LLWebRTCVoiceDevice is a simple representation of the
  54. // components of a device, used to communicate this
  55. // information to the viewer.
  56. // A note on threading.
  57. // Native WebRTC has it's own threading model. Some discussion
  58. // can be found here (https://webrtc.github.io/webrtc-org/native-code/native-apis/)
  59. //
  60. // Note that all callbacks to observers will occurr on one of the WebRTC native threads
  61. // (signaling, worker, etc.) Care should be taken to assure there are not
  62. // bad interactions with the viewer threads.
  63. class LLWebRTCVoiceDevice
  64. {
  65. public:
  66. std::string mDisplayName; // friendly name for user interface purposes
  67. std::string mID; // internal value for selection
  68. LLWebRTCVoiceDevice(const std::string &display_name, const std::string &id) :
  69. mDisplayName(display_name),
  70. mID(id)
  71. {
  72. if (mID.empty())
  73. {
  74. mID = display_name;
  75. }
  76. };
  77. };
  78. typedef std::vector<LLWebRTCVoiceDevice> LLWebRTCVoiceDeviceList;
  79. // The LLWebRTCDeviceObserver should be implemented by the viewer
  80. // webrtc module, which will receive notifications when devices
  81. // change (are unplugged, etc.)
  82. class LLWebRTCDevicesObserver
  83. {
  84. public:
  85. virtual void OnDevicesChanged(const LLWebRTCVoiceDeviceList &render_devices,
  86. const LLWebRTCVoiceDeviceList &capture_devices) = 0;
  87. };
  88. // The LLWebRTCDeviceInterface provides a way for the viewer
  89. // to enumerate, set, and get notifications of changes
  90. // for both capture (microphone) and render (speaker)
  91. // devices.
  92. class LLWebRTCDeviceInterface
  93. {
  94. public:
  95. struct AudioConfig {
  96. bool mAGC { true };
  97. bool mEchoCancellation { true };
  98. // TODO: The various levels of noise suppression are configured
  99. // on the APM which would require setting config on the APM.
  100. // We should pipe the various values through
  101. // later.
  102. typedef enum {
  103. NOISE_SUPPRESSION_LEVEL_NONE = 0,
  104. NOISE_SUPPRESSION_LEVEL_LOW,
  105. NOISE_SUPPRESSION_LEVEL_MODERATE,
  106. NOISE_SUPPRESSION_LEVEL_HIGH,
  107. NOISE_SUPPRESSION_LEVEL_VERY_HIGH
  108. } ENoiseSuppressionLevel;
  109. ENoiseSuppressionLevel mNoiseSuppressionLevel { NOISE_SUPPRESSION_LEVEL_VERY_HIGH };
  110. };
  111. virtual void setAudioConfig(AudioConfig config) = 0;
  112. // instructs webrtc to refresh the device list.
  113. virtual void refreshDevices() = 0;
  114. // set the capture and render devices using the unique identifier for the device
  115. virtual void setCaptureDevice(const std::string& id) = 0;
  116. virtual void setRenderDevice(const std::string& id) = 0;
  117. // Device observers for device change callbacks.
  118. virtual void setDevicesObserver(LLWebRTCDevicesObserver *observer) = 0;
  119. virtual void unsetDevicesObserver(LLWebRTCDevicesObserver *observer) = 0;
  120. // tuning and audio levels
  121. virtual void setTuningMode(bool enable) = 0;
  122. virtual float getTuningAudioLevel() = 0; // for use during tuning
  123. virtual float getPeerConnectionAudioLevel() = 0; // for use when not tuning
  124. virtual void setPeerConnectionGain(float gain) = 0;
  125. };
  126. // LLWebRTCAudioInterface provides the viewer with a way
  127. // to set audio characteristics (mute, send and receive volume)
  128. class LLWebRTCAudioInterface
  129. {
  130. public:
  131. virtual void setMute(bool mute) = 0;
  132. virtual void setReceiveVolume(float volume) = 0; // volume between 0.0 and 1.0
  133. virtual void setSendVolume(float volume) = 0; // volume between 0.0 and 1.0
  134. };
  135. // LLWebRTCDataObserver allows the viewer voice module to be notified when
  136. // data is received over the data channel.
  137. class LLWebRTCDataObserver
  138. {
  139. public:
  140. virtual void OnDataReceived(const std::string& data, bool binary) = 0;
  141. };
  142. // LLWebRTCDataInterface allows the viewer to send data over the data channel.
  143. class LLWebRTCDataInterface
  144. {
  145. public:
  146. virtual void sendData(const std::string& data, bool binary=false) = 0;
  147. virtual void setDataObserver(LLWebRTCDataObserver *observer) = 0;
  148. virtual void unsetDataObserver(LLWebRTCDataObserver *observer) = 0;
  149. };
  150. // LLWebRTCIceCandidate is a basic structure containing
  151. // information needed for ICE trickling.
  152. struct LLWebRTCIceCandidate
  153. {
  154. std::string mCandidate;
  155. std::string mSdpMid;
  156. int mMLineIndex;
  157. };
  158. // LLWebRTCSignalingObserver provides a way for the native
  159. // webrtc library to notify the viewer voice module of
  160. // various state changes.
  161. class LLWebRTCSignalingObserver
  162. {
  163. public:
  164. typedef enum e_ice_gathering_state {
  165. ICE_GATHERING_NEW,
  166. ICE_GATHERING_GATHERING,
  167. ICE_GATHERING_COMPLETE
  168. } EIceGatheringState;
  169. // Called when ICE gathering states have changed.
  170. // This may be called at any time, as ICE gathering
  171. // can be redone while a connection is up.
  172. virtual void OnIceGatheringState(EIceGatheringState state) = 0;
  173. // Called when a new ice candidate is available.
  174. virtual void OnIceCandidate(const LLWebRTCIceCandidate& candidate) = 0;
  175. // Called when an offer is available after a connection is requested.
  176. virtual void OnOfferAvailable(const std::string& sdp) = 0;
  177. // Called when a connection enters a failure state and renegotiation is needed.
  178. virtual void OnRenegotiationNeeded() = 0;
  179. // Called when a peer connection has shut down
  180. virtual void OnPeerConnectionClosed() = 0;
  181. // Called when the audio channel has been established and audio
  182. // can begin.
  183. virtual void OnAudioEstablished(LLWebRTCAudioInterface *audio_interface) = 0;
  184. // Called when the data channel has been established and data
  185. // transfer can begin.
  186. virtual void OnDataChannelReady(LLWebRTCDataInterface *data_interface) = 0;
  187. };
  188. // LLWebRTCPeerConnectionInterface representsd a connection to a peer,
  189. // in most cases a Secondlife WebRTC server. This interface
  190. // allows for management of this peer connection.
  191. class LLWebRTCPeerConnectionInterface
  192. {
  193. public:
  194. struct InitOptions
  195. {
  196. // equivalent of PeerConnectionInterface::IceServer
  197. struct IceServers {
  198. // Valid formats are described in RFC7064 and RFC7065.
  199. // Urls should containe dns hostnames (not IP addresses)
  200. // as the TLS certificate policy is 'secure.'
  201. // and we do not currentply support TLS extensions.
  202. std::vector<std::string> mUrls;
  203. std::string mUserName;
  204. std::string mPassword;
  205. };
  206. std::vector<IceServers> mServers;
  207. };
  208. virtual bool initializeConnection(const InitOptions& options) = 0;
  209. virtual bool shutdownConnection() = 0;
  210. virtual void setSignalingObserver(LLWebRTCSignalingObserver* observer) = 0;
  211. virtual void unsetSignalingObserver(LLWebRTCSignalingObserver* observer) = 0;
  212. virtual void AnswerAvailable(const std::string &sdp) = 0;
  213. };
  214. // The following define the dynamic linked library
  215. // exports.
  216. // This library must be initialized before use.
  217. LLSYMEXPORT void init();
  218. // And should be terminated as part of shutdown.
  219. LLSYMEXPORT void terminate();
  220. // Return an interface for device management.
  221. LLSYMEXPORT LLWebRTCDeviceInterface* getDeviceInterface();
  222. // Allocate and free peer connections.
  223. LLSYMEXPORT LLWebRTCPeerConnectionInterface* newPeerConnection();
  224. LLSYMEXPORT void freePeerConnection(LLWebRTCPeerConnectionInterface *connection);
  225. }
  226. #endif // LLWEBRTC_H