| 1 |
using AVFoundation; |
| 2 |
using CoreGraphics; |
| 3 |
using pjsua2xamarin; |
| 4 |
using pjsua2xamarin.pjsua2; |
| 5 |
using pjsua2xamarin.iOS; |
| 6 |
using Foundation; |
| 7 |
using System; |
| 8 |
using UIKit; |
| 9 |
using Xamarin.Forms; |
| 10 |
using Xamarin.Forms.Platform.iOS; |
| 11 |
|
| 12 |
[assembly: ExportRenderer(typeof(CallPage), typeof(CallPageRenderer))] |
| 13 |
namespace pjsua2xamarin.iOS |
| 14 |
{ |
| 15 |
public class CallPageRenderer : PageRenderer |
| 16 |
{ |
| 17 |
UIView incomingVideoView; |
| 18 |
UIButton acceptCallButton; |
| 19 |
UIButton hangupCallButton; |
| 20 |
UILabel peerLabel; |
| 21 |
UILabel callStatusLabel; |
| 22 |
private static CallInfo lastCallInfo; |
| 23 |
private CallPage callPage; |
| 24 |
|
| 25 |
public CallPageRenderer() |
| 26 |
{ |
| 27 |
MessagingCenter.Subscribe<BuddyPage, CallInfo> |
| 28 |
(this, "UpdateCallState", (obj, info) => { |
| 29 |
lastCallInfo = info as CallInfo; |
| 30 |
Device.BeginInvokeOnMainThread(() => { |
| 31 |
updateCallState(lastCallInfo); |
| 32 |
}); |
| 33 |
|
| 34 |
if (lastCallInfo.state == |
| 35 |
pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) |
| 36 |
{ |
| 37 |
Device.BeginInvokeOnMainThread(() => { |
| 38 |
callPage.Navigation.PopAsync(); |
| 39 |
}); |
| 40 |
} |
| 41 |
}); |
| 42 |
MessagingCenter.Subscribe<BuddyPage, CallInfo> |
| 43 |
(this, "UpdateMediaCallState", (obj, info) => { |
| 44 |
lastCallInfo = info as CallInfo; |
| 45 |
|
| 46 |
if (MyApp.currentCall.vidWin != null) { |
| 47 |
incomingVideoView.Hidden = false; |
| 48 |
} |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
protected override void OnElementChanged(VisualElementChangedEventArgs e) |
| 53 |
{ |
| 54 |
base.OnElementChanged(e); |
| 55 |
|
| 56 |
if (e.OldElement != null || Element == null) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
callPage = (CallPage)Element; |
| 61 |
try { |
| 62 |
SetupUserInterface(); |
| 63 |
SetupEventHandlers(); |
| 64 |
|
| 65 |
if (MyApp.currentCall != null) { |
| 66 |
try { |
| 67 |
lastCallInfo = MyApp.currentCall.getInfo(); |
| 68 |
} catch (Exception ex) { |
| 69 |
System.Diagnostics.Debug.WriteLine(@"ERROR: ", |
| 70 |
ex.Message); |
| 71 |
} |
| 72 |
Device.BeginInvokeOnMainThread(() => { |
| 73 |
updateCallState(lastCallInfo); |
| 74 |
}); |
| 75 |
} else { |
| 76 |
incomingVideoView.Hidden = true; |
| 77 |
} |
| 78 |
} catch (Exception ex) { |
| 79 |
System.Diagnostics.Debug.WriteLine($"ERROR: {ex.Message}"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
protected override void Dispose(bool disposing) |
| 84 |
{ |
| 85 |
updateVideoWindow(false); |
| 86 |
base.Dispose(disposing); |
| 87 |
} |
| 88 |
|
| 89 |
void SetupUserInterface() |
| 90 |
{ |
| 91 |
var controlWidth = 150; |
| 92 |
var controlHeight = 50; |
| 93 |
var centerButtonX = View.Bounds.GetMidX() - 35f; |
| 94 |
var topLeftX = View.Bounds.X + 25; |
| 95 |
var topRightX = View.Bounds.Right - controlWidth - 25; |
| 96 |
var bottomButtonY = View.Bounds.Bottom - 150; |
| 97 |
var bottomLabelY = View.Bounds.Top + 15; |
| 98 |
|
| 99 |
incomingVideoView = new UIView() { |
| 100 |
Frame = new CGRect(0f, 0f, View.Bounds.Width, |
| 101 |
View.Bounds.Height) |
| 102 |
}; |
| 103 |
|
| 104 |
acceptCallButton = new UIButton() { |
| 105 |
Frame = new CGRect(topLeftX, bottomButtonY, controlWidth, |
| 106 |
controlHeight) |
| 107 |
}; |
| 108 |
acceptCallButton.SetTitle("Accept", UIControlState.Normal); |
| 109 |
acceptCallButton.SetTitleColor(color:UIColor.Black, |
| 110 |
UIControlState.Normal); |
| 111 |
acceptCallButton.BackgroundColor = UIColor.White; |
| 112 |
|
| 113 |
hangupCallButton = new UIButton() { |
| 114 |
Frame = new CGRect(topRightX, bottomButtonY, controlWidth, |
| 115 |
controlHeight) |
| 116 |
}; |
| 117 |
hangupCallButton.SetTitle("Hangup", UIControlState.Normal); |
| 118 |
hangupCallButton.SetTitleColor(color: UIColor.Black, |
| 119 |
UIControlState.Normal); |
| 120 |
hangupCallButton.BackgroundColor = UIColor.White; |
| 121 |
|
| 122 |
peerLabel = new UILabel { |
| 123 |
TextAlignment = UITextAlignment.Center, |
| 124 |
Frame = new CGRect(View.Bounds.X, |
| 125 |
bottomLabelY, |
| 126 |
View.Bounds.Right, |
| 127 |
controlHeight) |
| 128 |
}; |
| 129 |
callStatusLabel = new UILabel { |
| 130 |
TextAlignment = UITextAlignment.Center, |
| 131 |
Frame = new CGRect(View.Bounds.X, |
| 132 |
bottomLabelY + controlHeight, |
| 133 |
View.Bounds.Right, |
| 134 |
controlHeight) |
| 135 |
}; |
| 136 |
|
| 137 |
View.Add(incomingVideoView); |
| 138 |
View.Add(acceptCallButton); |
| 139 |
View.Add(hangupCallButton); |
| 140 |
View.Add(peerLabel); |
| 141 |
View.Add(callStatusLabel); |
| 142 |
} |
| 143 |
|
| 144 |
void SetupEventHandlers() |
| 145 |
{ |
| 146 |
acceptCallButton.TouchUpInside += (object sender, EventArgs e) => { |
| 147 |
AcceptCall(); |
| 148 |
}; |
| 149 |
|
| 150 |
hangupCallButton.TouchUpInside += (object sender, EventArgs e) => { |
| 151 |
HangupCall(); |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
void AcceptCall() |
| 156 |
{ |
| 157 |
CallOpParam prm = new CallOpParam(); |
| 158 |
prm.statusCode = pjsip_status_code.PJSIP_SC_OK; |
| 159 |
try { |
| 160 |
MyApp.currentCall.answer(prm); |
| 161 |
} catch (Exception ex) { |
| 162 |
System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message); |
| 163 |
} |
| 164 |
|
| 165 |
acceptCallButton.Hidden = true; |
| 166 |
} |
| 167 |
|
| 168 |
void HangupCall() |
| 169 |
{ |
| 170 |
if (MyApp.currentCall != null) { |
| 171 |
CallOpParam prm = new CallOpParam(); |
| 172 |
prm.statusCode = pjsip_status_code.PJSIP_SC_DECLINE; |
| 173 |
try { |
| 174 |
MyApp.currentCall.hangup(prm); |
| 175 |
} catch (Exception ex) { |
| 176 |
System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
private void updateCallState(CallInfo ci) |
| 182 |
{ |
| 183 |
String call_state = ""; |
| 184 |
|
| 185 |
if (ci == null) { |
| 186 |
acceptCallButton.Hidden = true; |
| 187 |
hangupCallButton.SetTitle("OK", UIControlState.Normal); |
| 188 |
callStatusLabel.Text = "Call disconnected"; |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if (ci.role == pjsip_role_e.PJSIP_ROLE_UAC) { |
| 193 |
acceptCallButton.Hidden = true; |
| 194 |
} |
| 195 |
|
| 196 |
if (ci.state < |
| 197 |
pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) { |
| 198 |
if (ci.role == pjsip_role_e.PJSIP_ROLE_UAS) { |
| 199 |
call_state = "Incoming call.."; |
| 200 |
|
| 201 |
} else { |
| 202 |
hangupCallButton.SetTitle("Cancel", UIControlState.Normal); |
| 203 |
call_state = ci.stateText; |
| 204 |
} |
| 205 |
} else if (ci.state >= |
| 206 |
pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) |
| 207 |
{ |
| 208 |
acceptCallButton.Hidden = true; |
| 209 |
call_state = ci.stateText; |
| 210 |
if (ci.state == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) { |
| 211 |
hangupCallButton.SetTitle("Hangup", UIControlState.Normal); |
| 212 |
} else if (ci.state == |
| 213 |
pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) |
| 214 |
{ |
| 215 |
hangupCallButton.SetTitle("OK", UIControlState.Normal); |
| 216 |
call_state = "Call disconnected: " + ci.lastReason; |
| 217 |
} |
| 218 |
if (ci.state == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) { |
| 219 |
updateVideoWindow(true); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
peerLabel.Text = ci.remoteUri; |
| 224 |
callStatusLabel.Text = call_state; |
| 225 |
} |
| 226 |
|
| 227 |
private void updateVideoWindow(bool show) |
| 228 |
{ |
| 229 |
if (MyApp.currentCall != null && |
| 230 |
MyApp.currentCall.vidWin != null && |
| 231 |
MyApp.currentCall.vidPrev != null) |
| 232 |
{ |
| 233 |
if (show) { |
| 234 |
VideoWindowInfo winInfo = |
| 235 |
MyApp.currentCall.vidWin.getInfo(); |
| 236 |
IntPtr winPtr = winInfo.winHandle.handle.window; |
| 237 |
UIView inView = |
| 238 |
(UIView)ObjCRuntime.Runtime.GetNSObject(winPtr); |
| 239 |
try { |
| 240 |
Device.BeginInvokeOnMainThread(() => { |
| 241 |
incomingVideoView.AddSubview(inView); |
| 242 |
inView.ContentMode = UIViewContentMode.ScaleAspectFit; |
| 243 |
inView.Center = incomingVideoView.Center; |
| 244 |
inView.Frame = incomingVideoView.Bounds; |
| 245 |
}); |
| 246 |
} catch (Exception ex) { |
| 247 |
System.Diagnostics.Debug.WriteLine(@"ERROR: ", |
| 248 |
ex.Message); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|