Utils.cpp 7.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
Utils.cpp
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "Address.h"
#include "AuthInfo.h"
#include "Call.h"
#include "CallLog.h"
#include "CallParams.h"
#include "CallStats.h"
#include "ChatMessage.h"
#include "ChatRoom.h"
25
#include "Core.h"
26 27 28 29 30 31 32 33 34 35 36 37
#include "LpConfig.h"
#include "PayloadType.h"
#include "ProxyConfig.h"
#include "Transports.h"
#include "Utils.h"
#include "VideoPolicy.h"
#include "VideoSize.h"

#include <Stringapiset.h>
#include <time.h>


38
using namespace BelledonneCommunications::Linphone::Native;
39 40 41
using namespace Windows::Storage;


42
std::wstring Utils::UTF8ToUTF16(const char *utf8)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
	if ((utf8 == nullptr) || (*utf8 == '\0'))
		return std::wstring();

	int utf8len = static_cast<int>(strlen(utf8));

	// Get the size to alloc for utf-16 string
	int utf16len = MultiByteToWideChar(CP_UTF8, 0, utf8, utf8len, nullptr, 0);
	if (utf16len == 0) {
		DWORD error = GetLastError();
		ms_error("Invalid UTF-8 character, can't convert to UTF-16: %d", error);
		return std::wstring();
	}

	// Do the conversion
	std::wstring utf16;
	utf16.resize(utf16len);
	if (MultiByteToWideChar(CP_UTF8, 0, utf8, utf8len, &utf16[0], (int)utf16.length()) == 0) {
		DWORD error = GetLastError();
		ms_error("Error during string conversion from UTF-8 to UTF-16: %d", error);
		return std::wstring();
	}
	return utf16;
}

68
std::string Utils::UTF16ToUTF8(const wchar_t *utf16)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
	if ((utf16 == nullptr) || (*utf16 == L'\0'))
		return std::string();

	// Get the size to alloc for utf-8 string
	int utf16len = static_cast<int>(wcslen(utf16));
	int utf8len = WideCharToMultiByte(CP_UTF8, 0, utf16, utf16len, NULL, 0, NULL, NULL);
	if (utf8len == 0) {
		DWORD error = GetLastError();
		ms_error("Invalid UTF-16 character, can't convert to UTF-8: %d", error);
		return std::string();
	}

	// Do the conversion
	std::string utf8;
	utf8.resize(utf8len);
	if (WideCharToMultiByte(CP_UTF8, 0, utf16, utf16len, &utf8[0], (int)utf8.length(), NULL, NULL) == 0) {
		DWORD error = GetLastError();
		ms_error("Error during string conversion from UTF-16 to UTF-8: %d", error);
		return std::string();
	}
	return utf8;
}

93
const char* Utils::pstoccs(Platform::String^ ps)
94 95 96 97
{
	if (ps == nullptr || ps->Length() == 0)
		return NULL;

98
	std::string s = Utils::UTF16ToUTF8(ps->Data());
99 100 101 102 103 104
	char* cc = (char*) malloc(s.length()+1);
	memcpy(cc, s.c_str(), s.length());
	cc[s.length()] = '\0';
	return cc;
}

105
Platform::String^ Utils::cctops(const char* cc)
106 107 108 109
{
	if (cc == NULL)
		return nullptr;

110
	std::wstring wid_str = Utils::UTF8ToUTF16(cc);
111 112 113 114 115 116
	const wchar_t* w_char = wid_str.c_str();
	return ref new Platform::String(w_char);
}



117
Platform::Object^ Utils::CreateAddress(const char *address)
118
{
119
	Address^ addr = ref new Address(address);
120 121 122 123
	if (addr->address == nullptr) return nullptr;
	return addr;
}

124
Platform::Object^ Utils::CreateAddress(void *address)
125
{
126
	return ref new Address((::LinphoneAddress *)address);
127 128
}

129
Platform::Object^ Utils::CreateAuthInfo(void *auth_info)
130
{
131
	return ref new AuthInfo((::LinphoneAuthInfo *)auth_info);
132 133
}

134
Platform::Object^ Utils::CreateCallStats(void *callStats)
135
{
136
	return ref new CallStats((::LinphoneCallStats *)callStats);
137 138
}

139
Platform::Object^ Utils::CreateCallStats(void *call, int mediaType)
140
{
141
	return ref new CallStats((::LinphoneCall *)call, (MediaType)mediaType);
142 143
}

144
Platform::Object^ Utils::CreateLpConfig(void *config)
145
{
146
	return ref new LpConfig((::LpConfig *)config);
147 148
}

149
Platform::Object^ Utils::CreateLpConfig(Platform::String^ configPath, Platform::String^ factoryConfigPath)
150
{
151
	return ref new LpConfig(configPath, factoryConfigPath);
152 153
}

154
Platform::Object^ Utils::CreatePayloadType(void *pt)
155
{
156
	return ref new PayloadType((::PayloadType *)pt);
157 158
}

159
Platform::Object^ Utils::GetCall(void *call)
160
{
161 162 163 164 165
	::LinphoneCall *lCall = (::LinphoneCall *)call;
	if (lCall == nullptr) {
		return nullptr;
	}
	if (linphone_call_get_user_data(lCall) == nullptr) {
166
		return ref new Call(lCall);
167
	}
168
	RefToPtrProxy<Call^> *proxy = reinterpret_cast<RefToPtrProxy<Call^> *>(linphone_call_get_user_data(lCall));
169
	return proxy->Ref();
170 171
}

172
Platform::Object^ Utils::GetCallLog(void *callLog)
173
{
174 175 176 177 178
	::LinphoneCallLog *lCallLog = (::LinphoneCallLog *)callLog;
	if (lCallLog == nullptr) {
		return nullptr;
	}
	if (linphone_call_log_get_user_data(lCallLog) == nullptr) {
179
		return ref new CallLog(lCallLog);
180
	}
181
	RefToPtrProxy<CallLog^> *proxy = reinterpret_cast<RefToPtrProxy<CallLog^> *>(linphone_call_log_get_user_data(lCallLog));
182
	return proxy->Ref();
183 184
}

185
Platform::Object^ Utils::GetCallParams(void *callParams)
186 187 188 189 190 191
{
	::LinphoneCallParams *lCallParams = (::LinphoneCallParams *)callParams;
	if (lCallParams == nullptr) {
		return nullptr;
	}
	if (linphone_call_params_get_user_data(lCallParams) == nullptr) {
192
		return ref new CallParams(lCallParams);
193
	}
194
	RefToPtrProxy<CallParams^> *proxy = reinterpret_cast<RefToPtrProxy<CallParams^> *>(linphone_call_params_get_user_data(lCallParams));
195 196
	return proxy->Ref();
}
197

198
Platform::Object^ Utils::GetChatMessage(void *message)
199
{
200 201 202 203 204
	::LinphoneChatMessage *lChatMessage = (::LinphoneChatMessage *)message;
	if (lChatMessage == nullptr) {
		return nullptr;
	}
	if (linphone_chat_message_get_user_data(lChatMessage) == nullptr) {
205
		return ref new ChatMessage(lChatMessage);
206
	}
207
	RefToPtrProxy<ChatMessage^> *proxy = reinterpret_cast<RefToPtrProxy<ChatMessage^> *>(linphone_chat_message_get_user_data(lChatMessage));
208
	return proxy->Ref();
209 210
}

211
Platform::Object^ Utils::GetChatRoom(void *room)
212
{
213 214 215 216 217
	::LinphoneChatRoom *lChatRoom = (::LinphoneChatRoom *)room;
	if (lChatRoom == nullptr) {
		return nullptr;
	}
	if (linphone_chat_room_get_user_data(lChatRoom) == nullptr) {
218
		return ref new ChatRoom(lChatRoom);
219
	}
220
	RefToPtrProxy<ChatRoom^> *proxy = reinterpret_cast<RefToPtrProxy<ChatRoom^> *>(linphone_chat_room_get_user_data(lChatRoom));
221
	return proxy->Ref();
222 223
}

224
Platform::Object^ Utils::GetCore(void *core)
225
{
226 227 228 229
	::LinphoneCore *lCore = (::LinphoneCore *)core;
	if (linphone_core_get_user_data(lCore) == nullptr) {
		return nullptr;
	}
230
	RefToPtrProxy<Core^> *proxy = reinterpret_cast<RefToPtrProxy<Core^> *>(linphone_core_get_user_data(lCore));
231
	return proxy->Ref();
232 233
}

234
Platform::Object^ Utils::GetProxyConfig(void *proxy_config)
235
{
236 237 238 239 240
	::LinphoneProxyConfig *lProxyConfig = (::LinphoneProxyConfig *)proxy_config;
	if (lProxyConfig == nullptr) {
		return nullptr;
	}
	if (linphone_proxy_config_get_user_data(lProxyConfig) == nullptr) {
241
		return ref new ProxyConfig(lProxyConfig);
242
	}
243
	RefToPtrProxy<ProxyConfig^> *proxy = reinterpret_cast<RefToPtrProxy<ProxyConfig^> *>(linphone_proxy_config_get_user_data(lProxyConfig));
244
	return proxy->Ref();
245 246
}

247
void Utils::SetLogLevel(int loglevel)
248
{
249 250
	API_LOCK;
	linphone_core_set_log_level(static_cast<OrtpLogLevel>(loglevel));
251 252
}

253
void Utils::EchoCalibrationCallback(void *lc, int status, int delay_ms, void *data)
254 255 256 257 258 259
{
	API_LOCK;
	EchoCalibrationData *ecData = static_cast<EchoCalibrationData *>(data);
	if (ecData != nullptr) {
		delete ecData;
	}
260 261
	RefToPtrProxy<Core^> *proxy = reinterpret_cast< RefToPtrProxy<Core^> *>(linphone_core_get_user_data(static_cast<::LinphoneCore *>(lc)));
	Core^ lCore = (proxy) ? proxy->Ref() : nullptr;
262
	EcCalibratorStatus ecStatus = (EcCalibratorStatus) status;
263 264 265
	if (lCore) {
		lCore->listener->EcCalibrationStatus(ecStatus, delay_ms);
	}
266
}