| 1 |
** |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
using System; |
| 12 |
using System.Threading.Tasks; |
| 13 |
using System.Diagnostics; |
| 14 |
using VoipTasks.BackgroundOperations; |
| 15 |
using Windows.ApplicationModel.AppService; |
| 16 |
using Windows.ApplicationModel.Background; |
| 17 |
using Windows.ApplicationModel.Calls; |
| 18 |
using Windows.Foundation.Collections; |
| 19 |
using VoipBackEnd; |
| 20 |
|
| 21 |
namespace VoipTasks.Helpers |
| 22 |
{ |
| 23 |
static class Current |
| 24 |
{ |
| 25 |
public static String RtcCallTaskName |
| 26 |
{ |
| 27 |
get { return _rtcCallTaskName; } |
| 28 |
} |
| 29 |
|
| 30 |
public static AppServiceRequest AppRequest |
| 31 |
{ |
| 32 |
set |
| 33 |
{ |
| 34 |
lock (_lock) |
| 35 |
{ |
| 36 |
_appRequest = value; |
| 37 |
} |
| 38 |
} |
| 39 |
get |
| 40 |
{ |
| 41 |
lock (_lock) |
| 42 |
{ |
| 43 |
return _appRequest; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static AppServiceDeferral AppRequestDeferal |
| 49 |
{ |
| 50 |
set |
| 51 |
{ |
| 52 |
lock (_lock) |
| 53 |
{ |
| 54 |
_appDeferral = value; |
| 55 |
} |
| 56 |
} |
| 57 |
get |
| 58 |
{ |
| 59 |
lock (_lock) |
| 60 |
{ |
| 61 |
return _appDeferral; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public static BackgroundTaskDeferral RTCTaskDeferral |
| 67 |
{ |
| 68 |
set |
| 69 |
{ |
| 70 |
lock (_lock) |
| 71 |
{ |
| 72 |
_rtcTaskDeferral = value; |
| 73 |
} |
| 74 |
} |
| 75 |
get |
| 76 |
{ |
| 77 |
lock (_lock) |
| 78 |
{ |
| 79 |
return _rtcTaskDeferral; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public static VoipPhoneCall VoipCall |
| 85 |
{ |
| 86 |
set |
| 87 |
{ |
| 88 |
lock (_lock) |
| 89 |
{ |
| 90 |
_voipCall = value; |
| 91 |
} |
| 92 |
} |
| 93 |
get |
| 94 |
{ |
| 95 |
lock (_lock) |
| 96 |
{ |
| 97 |
return _voipCall; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public static BackgroundRequest Request |
| 103 |
{ |
| 104 |
set |
| 105 |
{ |
| 106 |
lock (_lock) |
| 107 |
{ |
| 108 |
_request = value; |
| 109 |
} |
| 110 |
} |
| 111 |
get |
| 112 |
{ |
| 113 |
lock (_lock) |
| 114 |
{ |
| 115 |
return _request; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public static AppServiceConnection AppConnection |
| 121 |
{ |
| 122 |
set |
| 123 |
{ |
| 124 |
lock (_lock) |
| 125 |
{ |
| 126 |
_connection = value; |
| 127 |
} |
| 128 |
} |
| 129 |
get |
| 130 |
{ |
| 131 |
lock (_lock) |
| 132 |
{ |
| 133 |
return _connection; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public static MyAppRT MyApp |
| 139 |
{ |
| 140 |
get |
| 141 |
{ |
| 142 |
lock (_lock) |
| 143 |
{ |
| 144 |
return _myApp; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public static async void SendResponse(ValueSet response) |
| 150 |
{ |
| 151 |
AppServiceRequest request = AppRequest; |
| 152 |
AppServiceDeferral deferal = AppRequestDeferal; |
| 153 |
|
| 154 |
try |
| 155 |
{ |
| 156 |
if (request != null) |
| 157 |
{ |
| 158 |
await request.SendResponseAsync(response); |
| 159 |
} |
| 160 |
} |
| 161 |
finally |
| 162 |
{ |
| 163 |
if (deferal != null) |
| 164 |
{ |
| 165 |
deferal.Complete(); |
| 166 |
deferal = null; |
| 167 |
} |
| 168 |
|
| 169 |
AppRequest = null; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public static async Task<ValueSet> SendMessageAsync(ValueSet message) |
| 174 |
{ |
| 175 |
ValueSet returnValue = null; |
| 176 |
|
| 177 |
if (_connection != null) |
| 178 |
{ |
| 179 |
AppServiceResponse response = await _connection.SendMessageAsync(message); |
| 180 |
|
| 181 |
if (response.Status == AppServiceResponseStatus.Success) |
| 182 |
{ |
| 183 |
if (response.Message.Keys.Contains(BackgroundOperation.Result)) |
| 184 |
{ |
| 185 |
returnValue = response.Message; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return returnValue; |
| 191 |
} |
| 192 |
|
| 193 |
public static async void EndCallAsync() |
| 194 |
{ |
| 195 |
AppServiceRequest request = AppRequest; |
| 196 |
AppServiceDeferral deferal = AppRequestDeferal; |
| 197 |
|
| 198 |
try |
| 199 |
{ |
| 200 |
EndCall(); |
| 201 |
|
| 202 |
if (request != null) |
| 203 |
{ |
| 204 |
ValueSet response = new ValueSet(); |
| 205 |
response[BackgroundOperation.Result] = (int)OperationResult.Succeeded; |
| 206 |
await request.SendResponseAsync(response); |
| 207 |
} |
| 208 |
|
| 209 |
if (deferal != null) |
| 210 |
{ |
| 211 |
deferal.Complete(); |
| 212 |
} |
| 213 |
} |
| 214 |
finally |
| 215 |
{ |
| 216 |
AppRequest = null; |
| 217 |
AppRequestDeferal = null; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
public static void EndCall() |
| 223 |
{ |
| 224 |
VoipPhoneCall call = VoipCall; |
| 225 |
BackgroundTaskDeferral deferral = RTCTaskDeferral; |
| 226 |
|
| 227 |
if (_voipCall == null) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
try |
| 232 |
{ |
| 233 |
_myApp.hangupCall(); |
| 234 |
} |
| 235 |
catch (Exception e) |
| 236 |
{ |
| 237 |
_myApp.writeLog(2, e.Message); |
| 238 |
} |
| 239 |
|
| 240 |
try |
| 241 |
{ |
| 242 |
if (call != null) |
| 243 |
{ |
| 244 |
call.NotifyCallEnded(); |
| 245 |
} |
| 246 |
} |
| 247 |
catch |
| 248 |
{ |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
try |
| 253 |
{ |
| 254 |
if (deferral != null) |
| 255 |
{ |
| 256 |
deferral.Complete(); |
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
catch |
| 261 |
{ |
| 262 |
|
| 263 |
} |
| 264 |
finally |
| 265 |
{ |
| 266 |
VoipCall = null; |
| 267 |
RTCTaskDeferral = null; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
public static async Task<VoipPhoneCallResourceReservationStatus> RequestNewCallAsync(string dstURI) |
| 272 |
{ |
| 273 |
VccCallHelper vccCallHelper; |
| 274 |
VoipPhoneCallResourceReservationStatus status = VoipPhoneCallResourceReservationStatus.ResourcesNotAvailable; |
| 275 |
|
| 276 |
lock (_lock) |
| 277 |
{ |
| 278 |
vccCallHelper = _vccCallHelper; |
| 279 |
} |
| 280 |
|
| 281 |
if (vccCallHelper != null) |
| 282 |
{ |
| 283 |
status = await vccCallHelper.RequestNewCallAsync(dstURI); |
| 284 |
} |
| 285 |
|
| 286 |
return status; |
| 287 |
} |
| 288 |
|
| 289 |
public static void RequestNewIncomingCall(string context, string contactName, string serviceName) |
| 290 |
{ |
| 291 |
VccCallHelper vccCallHelper; |
| 292 |
|
| 293 |
|
| 294 |
lock (_lock) |
| 295 |
{ |
| 296 |
vccCallHelper = _vccCallHelper; |
| 297 |
} |
| 298 |
|
| 299 |
if (vccCallHelper != null) |
| 300 |
{ |
| 301 |
vccCallHelper.NewIncomingCall(context, contactName, serviceName); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
public static void StartService() |
| 306 |
{ |
| 307 |
if (_started) |
| 308 |
return; |
| 309 |
|
| 310 |
_myApp.init(_epHelper, _epHelper); |
| 311 |
|
| 312 |
_started = true; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
public static void StopService() |
| 317 |
{ |
| 318 |
if (!_started) |
| 319 |
return; |
| 320 |
|
| 321 |
_myApp.deInit(); |
| 322 |
|
| 323 |
_started = false; |
| 324 |
} |
| 325 |
|
| 326 |
public static void ModifyAccount(String id, String registrar, |
| 327 |
String proxy, String username, |
| 328 |
String password) |
| 329 |
{ |
| 330 |
try |
| 331 |
{ |
| 332 |
MyAppRT.Instance.modifyAccount(id, registrar, proxy, username, password); |
| 333 |
} |
| 334 |
catch (Exception e) |
| 335 |
{ |
| 336 |
MyAppRT.Instance.writeLog(2, e.Message); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
async public static void GetAccountInfo() |
| 341 |
{ |
| 342 |
if (!_started) |
| 343 |
return; |
| 344 |
|
| 345 |
AccountInfo accInfo = MyAppRT.Instance.getAccountInfo(); |
| 346 |
|
| 347 |
ValueSet message = new ValueSet(); |
| 348 |
|
| 349 |
message[UpdateAccountInfoArguments.id.ToString()] = accInfo.id; |
| 350 |
message[UpdateAccountInfoArguments.registrar.ToString()] = accInfo.registrar; |
| 351 |
message[UpdateAccountInfoArguments.proxy.ToString()] = accInfo.proxy; |
| 352 |
message[UpdateAccountInfoArguments.username.ToString()] = accInfo.username; |
| 353 |
message[UpdateAccountInfoArguments.password.ToString()] = accInfo.password; |
| 354 |
|
| 355 |
message[ForegroundOperation.NewForegroundRequest] = (int)ForegroundReguest.UpdateAcccountInfo; |
| 356 |
|
| 357 |
ValueSet response = await Current.SendMessageAsync(message); |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
private const String _rtcCallTaskName = "VoipTasks.CallRtcTask"; |
| 362 |
private static BackgroundRequest _request = BackgroundRequest.InValid; |
| 363 |
private static Object _lock = new Object(); |
| 364 |
private static AppServiceRequest _appRequest = null; |
| 365 |
private static AppServiceDeferral _appDeferral = null; |
| 366 |
private static VoipPhoneCall _voipCall = null; |
| 367 |
private static VccCallHelper _vccCallHelper = new VccCallHelper(); |
| 368 |
private static BackgroundTaskDeferral _rtcTaskDeferral = null; |
| 369 |
|
| 370 |
|
| 371 |
private static MyAppRT _myApp = MyAppRT.Instance; |
| 372 |
private static bool _started = false; |
| 373 |
|
| 374 |
private static EndpointHelper _epHelper = new EndpointHelper(); |
| 375 |
private static AppServiceConnection _connection = null; |
| 376 |
} |
| 377 |
} |