|
| 1 | +#include "libKmpWebrtc.h" |
| 2 | + |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +#if defined(WEBRTC_WIN) |
| 6 | +#include "kmp_webrtc_api.h" |
| 7 | +#elif defined(WEBRTC_LINUX) |
| 8 | +#include "libkmp_webrtc_api.h" |
| 9 | +#else |
| 10 | +#error "Unknown target" |
| 11 | +#endif |
| 12 | + |
| 13 | +#include "lib_pc_client.h" |
| 14 | + |
| 15 | +#if defined(WEBRTC_WIN) |
| 16 | +#define KT_SYMBOL(sym) sym |
| 17 | +#elif defined(WEBRTC_LINUX) |
| 18 | +#define KT_SYMBOL(sym) lib##sym |
| 19 | +#else |
| 20 | +#error "Unknown target" |
| 21 | +#endif |
| 22 | + |
| 23 | +#define KFunc(NAME) g_lib->kotlin.root.com.piasy.kmp.webrtc.##NAME |
| 24 | +#define KType(NAME) kmp_webrtc_kref_com_piasy_kmp_webrtc_##NAME |
| 25 | + |
| 26 | +static KT_SYMBOL(kmp_webrtc_ExportedSymbols)* g_lib = nullptr; |
| 27 | + |
| 28 | +int InitializeWebRTC(const char* field_trials, int debug_log) { |
| 29 | + if (!g_lib) { |
| 30 | + g_lib = KT_SYMBOL(kmp_webrtc_symbols)(); |
| 31 | + } |
| 32 | + return KFunc(initializeWebRTC)(kmp_webrtc_kref_kotlin_Any(), field_trials, debug_log); |
| 33 | +} |
| 34 | + |
| 35 | +struct PCClientFactoryConfig* DefaultPCClientFactoryConfig() { |
| 36 | + PCClientFactoryConfig* config = new PCClientFactoryConfig(); |
| 37 | + config->video_capture_impl = kKmpWebRTCCaptureSystemCamera; |
| 38 | + config->video_capture_width = 1280; |
| 39 | + config->video_capture_height = 720; |
| 40 | + config->video_capture_fps = 30; |
| 41 | + config->private_config.hwnd = nullptr; |
| 42 | + return config; |
| 43 | +} |
| 44 | + |
| 45 | +void PCClientFactoryConfigDestroy(struct PCClientFactoryConfig** config) { |
| 46 | + delete (*config); |
| 47 | + *config = nullptr; |
| 48 | +} |
| 49 | + |
| 50 | +struct PcClientFactoryHolder { |
| 51 | + KType(PeerConnectionClientFactory) factory; |
| 52 | +}; |
| 53 | + |
| 54 | +void* CreatePCClientFactory(struct PCClientFactoryConfig* config, PCClientFactoryErrorHandler handler, void* opaque) { |
| 55 | + KType(PeerConnectionClientFactory_PrivateConfig) private_config = KFunc(PeerConnectionClientFactory.PrivateConfig.PrivateConfig)(); |
| 56 | + KType(PeerConnectionClientFactory_Config) k_config = KFunc(PeerConnectionClientFactory.Config.Config)( |
| 57 | + (int) config->video_capture_impl, config->video_capture_width, config->video_capture_height, |
| 58 | + config->video_capture_fps, 0, private_config); |
| 59 | + KType(WinPrivateConfig) win_private_config = KFunc(WinPrivateConfig.WinPrivateConfig)(config->private_config.hwnd, config->private_config.disable_encryption); |
| 60 | + KType(PeerConnectionClientFactory_Config) k_config_with_pri = KFunc(utils.createPcClientFactoryConfig)(k_config, win_private_config); |
| 61 | + |
| 62 | + kmp_webrtc_kref_kotlin_Function2 error_handler = KFunc(utils.createErrorHandler)(handler, opaque); |
| 63 | + |
| 64 | + PcClientFactoryHolder* holder = new PcClientFactoryHolder(); |
| 65 | + holder->factory = KFunc(createPeerConnectionClientFactory)(k_config_with_pri, error_handler); |
| 66 | + return holder; |
| 67 | +} |
| 68 | + |
| 69 | +void DestroyPCClientFactory(void** pc_client_factory) { |
| 70 | + PcClientFactoryHolder* holder = reinterpret_cast<PcClientFactoryHolder*>(*pc_client_factory); |
| 71 | + KFunc(PeerConnectionClientFactory.destroyPeerConnectionFactory)(holder->factory); |
| 72 | + delete (*pc_client_factory); |
| 73 | + *pc_client_factory = nullptr; |
| 74 | +} |
| 75 | + |
| 76 | +void CreateLocalTracks(void* pc_client_factory) { |
| 77 | + PcClientFactoryHolder* holder = reinterpret_cast<PcClientFactoryHolder*>(pc_client_factory); |
| 78 | + KFunc(PeerConnectionClientFactory.createLocalTracks)(holder->factory); |
| 79 | +} |
| 80 | + |
| 81 | +void StartVideoCapture(void* pc_client_factory) { |
| 82 | + PcClientFactoryHolder* holder = reinterpret_cast<PcClientFactoryHolder*>(pc_client_factory); |
| 83 | + KFunc(PeerConnectionClientFactory.startVideoCapture)(holder->factory); |
| 84 | +} |
| 85 | + |
| 86 | +void StopVideoCapture(void* pc_client_factory) { |
| 87 | + PcClientFactoryHolder* holder = reinterpret_cast<PcClientFactoryHolder*>(pc_client_factory); |
| 88 | + KFunc(PeerConnectionClientFactory.stopVideoCapture)(holder->factory); |
| 89 | +} |
| 90 | + |
| 91 | +struct PcClientHolder { |
| 92 | + KType(PeerConnectionClient) client; |
| 93 | +}; |
| 94 | + |
| 95 | +void* CreatePeerConnectionClient(void* pc_client_factory, const char* peer_uid, KmpWebRTCDir dir, int has_video, |
| 96 | + int video_max_bitrate_kbps, int video_max_frame_rate, PCClientCallback* callback, void* opaque) { |
| 97 | + PcClientFactoryHolder* factory_holder = reinterpret_cast<PcClientFactoryHolder*>(pc_client_factory); |
| 98 | + KType(PeerConnectionClientCallback) pc_client_callback = KFunc(utils.createPcClientCallback)(callback, opaque); |
| 99 | + |
| 100 | + PcClientHolder* holder = new PcClientHolder(); |
| 101 | + holder->client = KFunc(PeerConnectionClientFactory.createPeerConnectionClient)(factory_holder->factory, peer_uid, (int)dir, |
| 102 | + has_video, video_max_bitrate_kbps, video_max_frame_rate, pc_client_callback); |
| 103 | + return holder; |
| 104 | +} |
| 105 | + |
| 106 | +void ClosePeerConnectionClient(void** pc_client) { |
| 107 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(*pc_client); |
| 108 | + KFunc(PeerConnectionClient.close)(holder->client); |
| 109 | + delete (*pc_client); |
| 110 | + *pc_client = nullptr; |
| 111 | +} |
| 112 | + |
| 113 | +void CreatePeerConnection(void* pc_client) { |
| 114 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(pc_client); |
| 115 | + kmp_webrtc_kref_kotlin_collections_List ice_servers = KFunc(utils.emptyIceServers)(); |
| 116 | + KFunc(PeerConnectionClient.createPeerConnection)(holder->client, ice_servers); |
| 117 | +} |
| 118 | + |
| 119 | +void CreateOffer(void* pc_client) { |
| 120 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(pc_client); |
| 121 | + KFunc(PeerConnectionClient.createOffer)(holder->client); |
| 122 | +} |
| 123 | + |
| 124 | +void SetRemoteDescription(void* pc_client, KmpWebRTCSdpType type, const char* sdp) { |
| 125 | + KType(data_SessionDescription) answer = KFunc(data.SessionDescription.SessionDescription)((int) type, sdp); |
| 126 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(pc_client); |
| 127 | + KFunc(PeerConnectionClient.setRemoteDescription)(holder->client, answer); |
| 128 | +} |
| 129 | + |
| 130 | +void AddIceCandidate(void* pc_client, const char* sdp_mid, int m_line_index, const char* sdp) { |
| 131 | + KType(data_IceCandidate) candidate = KFunc(data.IceCandidate.IceCandidate)(sdp_mid, m_line_index, sdp); |
| 132 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(pc_client); |
| 133 | + KFunc(PeerConnectionClient.addIceCandidate)(holder->client, candidate); |
| 134 | +} |
| 135 | + |
| 136 | +void GetStats(void* pc_client) { |
| 137 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(pc_client); |
| 138 | + KFunc(PeerConnectionClient.getStats)(holder->client); |
| 139 | +} |
| 140 | + |
| 141 | +#if defined(WEBRTC_WIN) |
| 142 | +void AddRemoteTrackRenderer(void* pc_client, void* renderer) { |
| 143 | + PcClientHolder* holder = reinterpret_cast<PcClientHolder*>(pc_client); |
| 144 | + KFunc(utils.addRemoteTrackRenderer)(holder->client, renderer); |
| 145 | +} |
| 146 | +#endif |
| 147 | + |
| 148 | +void LogInfo(const char* log) { |
| 149 | + KFunc(utils.logInfo)(log); |
| 150 | +} |
0 commit comments