Debian Codegraph @

Xpjproject/pjsip-apps/src/swig/csharp/pjsua2xamarin/pjsua2xamarin.Android/CallPageRenderer.cs

raw symbols SCIP
1 using System;
2 using System.IO;
3 using Xamarin.Forms;
4 using Xamarin.Forms.Platform.Android;
5 using pjsua2xamarin;
6 using pjsua2xamarin.Droid;
7 using Android.App;
8 using Android.Content;
9 using Android.Hardware;
10 using Android.Views;
11 using Android.Graphics;
12 using Android.Widget;
13 using Android.Renderscripts;
14 using static Xamarin.Essentials.Permissions;
15 using pjsua2xamarin.pjsua2;
16 using AndroidX.Lifecycle;
17 using System.Runtime.InteropServices;
18 using Android.Runtime;
19
20 [assembly: ExportRenderer(typeof(CallPage), typeof(CallPageRenderer))]
21 namespace pjsua2xamarin.Droid
22 {
23 public class CallPageRenderer : PageRenderer, ISurfaceHolderCallback
24 {
25 global::Android.Widget.Button acceptCallButton;
26 global::Android.Widget.Button hangupCallButton;
27 global::Android.Widget.TextView peerTxt;
28 global::Android.Widget.TextView statusTxt;
29 global::Android.Views.View view;
30 private static CallInfo lastCallInfo;
31 private CallPage callPage;
32 SurfaceView incomingView;
33
34 [DllImport("android")]
35 private static extern IntPtr ANativeWindow_fromSurface(IntPtr jni, IntPtr surface);
36
37 public CallPageRenderer(Context context) : base(context)
38 {
39 MessagingCenter.Subscribe<BuddyPage, CallInfo>
40 (this, "UpdateCallState", (obj, info) => {
41 if (callPage == null)
42 return;
43
44 lastCallInfo = info as CallInfo;
45 if (lastCallInfo.state == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) {
46 Device.BeginInvokeOnMainThread(() => {
47 callPage.Navigation.PopAsync();
48 });
49 } else {
50 Device.BeginInvokeOnMainThread(() => {
51 updateCallState(lastCallInfo);
52 });
53 }
54 });
55 MessagingCenter.Subscribe<BuddyPage, CallInfo>
56 (this, "UpdateMediaCallState", (obj, info) => {
57 lastCallInfo = info as CallInfo;
58
59 if (MyApp.currentCall.vidWin != null) {
60 incomingView.Visibility = ViewStates.Visible;
61 }
62 });
63 }
64
65 ~CallPageRenderer() {
66 MessagingCenter.Unsubscribe<BuddyPage, CallInfo>(this, "UpdateCallState");
67 MessagingCenter.Unsubscribe<BuddyPage, CallInfo>(this, "UpdateMediaCallState");
68 }
69
70 protected override void OnLayout(bool changed, int l, int t, int r, int b)
71 {
72 base.OnLayout(changed, l, t, r, b);
73
74 var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
75 var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
76
77 view.Measure(msw, msh);
78 view.Layout(0, 0, r - l, b - t);
79 }
80
81 protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
82 {
83 base.OnElementChanged(e);
84
85 if (e.OldElement != null || Element == null) {
86 return;
87 }
88
89 try {
90 SetupUserInterface();
91 SetupEventHandlers();
92 AddView(view);
93 callPage = (CallPage)Element;
94 System.Diagnostics.Debug.WriteLine(@"Call page done initialize");
95 if (MyApp.currentCall != null) {
96 try {
97 lastCallInfo = MyApp.currentCall.getInfo();
98 } catch (Exception ex) {
99 System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message);
100 }
101 Device.BeginInvokeOnMainThread(() => {
102 updateCallState(lastCallInfo);
103 });
104 }
105 } catch (Exception ex) {
106 System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message);
107 }
108 }
109
110 void SetupUserInterface()
111 {
112 var activity = this.Context as Activity;
113 view = activity.LayoutInflater.Inflate(Resource.Layout.activity_call, this, false);
114
115 incomingView = view.FindViewById<SurfaceView>(Resource.Id.incomingVideoView);
116 incomingView.Holder.AddCallback(this);
117
118 peerTxt = view.FindViewById<TextView>(Resource.Id.peerTxt);
119 statusTxt = view.FindViewById<TextView>(Resource.Id.statusTxt);
120
121 if (MyApp.currentCall == null || MyApp.currentCall.vidWin == null) {
122 incomingView.Visibility = ViewStates.Gone;
123 }
124 }
125
126 void SetupEventHandlers()
127 {
128 acceptCallButton = view.FindViewById<global::Android.Widget.Button>(Resource.Id.acceptCallButton);
129 acceptCallButton.Click += AcceptCallButtonTapped;
130
131 hangupCallButton = view.FindViewById<global::Android.Widget.Button>(Resource.Id.hangupCallButton);
132 hangupCallButton.Click += HangupCallButtonTapped;
133 }
134
135 void AcceptCallButtonTapped(object sender, EventArgs e)
136 {
137 CallOpParam prm = new CallOpParam();
138 prm.statusCode = pjsip_status_code.PJSIP_SC_OK;
139 try {
140 MyApp.currentCall.answer(prm);
141 } catch (Exception ex) {
142 System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message);
143 }
144
145 acceptCallButton.Visibility = ViewStates.Gone;
146 }
147
148 void HangupCallButtonTapped(object sender, EventArgs e)
149 {
150 if (MyApp.currentCall != null) {
151 CallOpParam prm = new CallOpParam();
152 prm.statusCode = pjsip_status_code.PJSIP_SC_DECLINE;
153 try {
154 MyApp.currentCall.hangup(prm);
155 } catch (Exception ex) {
156 System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message);
157 }
158 }
159 }
160
161 private void updateVideoWindow(bool show)
162 {
163 if (MyApp.currentCall != null &&
164 MyApp.currentCall.vidWin != null &&
165 MyApp.currentCall.vidPrev != null)
166 {
167 long windHandle = 0;
168 VideoWindowHandle vidWH = new VideoWindowHandle();
169 if (show) {
170 IntPtr winPtr = ANativeWindow_fromSurface(JNIEnv.Handle,
171 incomingView.Holder.Surface.Handle);
172 windHandle = winPtr.ToInt64();
173 }
174 vidWH.handle.setWindow(windHandle);
175 try {
176 MyApp.currentCall.vidWin.setWindow(vidWH);
177 } catch (Exception ex) {
178 System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message);
179 }
180 }
181 }
182
183 private void updateCallState(CallInfo ci)
184 {
185 String call_state = "";
186
187 if (ci == null) {
188 acceptCallButton.Visibility = ViewStates.Gone;
189 hangupCallButton.Text = "OK";
190 statusTxt.Text = "Call disconnected";
191 return;
192 }
193
194 if (ci.role == pjsip_role_e.PJSIP_ROLE_UAC) {
195 acceptCallButton.Visibility = ViewStates.Gone;
196 }
197
198 if (ci.state <
199 pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED)
200 {
201 if (ci.role == pjsip_role_e.PJSIP_ROLE_UAS) {
202 call_state = "Incoming call..";
203 /* Default button texts are already 'Accept' & 'Reject' */
204 } else {
205 hangupCallButton.Text = "Cancel";
206 call_state = ci.stateText;
207 }
208 } else if (ci.state >=
209 pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED)
210 {
211 acceptCallButton.Visibility = ViewStates.Gone;
212 call_state = ci.stateText;
213 if (ci.state == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) {
214 hangupCallButton.Text = "Hangup";
215 } else if (ci.state ==
216 pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED)
217 {
218 hangupCallButton.Text = "OK";
219 call_state = "Call disconnected: " + ci.lastReason;
220 }
221 if (ci.state == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) {
222 updateVideoWindow(true);
223 }
224 }
225
226 peerTxt.Text = ci.remoteUri;
227 statusTxt.Text = call_state;
228 }
229
230 void ISurfaceHolderCallback.SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
231 {
232 updateVideoWindow(true);
233 }
234
235 void ISurfaceHolderCallback.SurfaceCreated(ISurfaceHolder holder)
236 {
237 }
238
239 void ISurfaceHolderCallback.SurfaceDestroyed(ISurfaceHolder holder)
240 {
241 updateVideoWindow(false);
242 }
243 }
244 }
245