| 1 |
/* |
| 2 |
* Copyright (C) 2021-2021 Teluu Inc. (http://www.teluu.com) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
import SwiftUI |
| 20 |
|
| 21 |
struct VidView: UIViewRepresentable { |
| 22 |
@Binding var vid_view: UIView? |
| 23 |
|
| 24 |
func makeUIView(context: Context) -> UIView { |
| 25 |
let view = UIView(); |
| 26 |
view.backgroundColor = UIColor.black; |
| 27 |
return view; |
| 28 |
} |
| 29 |
|
| 30 |
func updateUIView(_ uiView: UIView, context: Context) { |
| 31 |
if let vid_win = vid_view { |
| 32 |
/* Add the video window as subview */ |
| 33 |
if (!vid_win.isDescendant(of: uiView)) { |
| 34 |
uiView.addSubview(vid_win); |
| 35 |
vid_win.center = uiView.center; |
| 36 |
vid_win.frame = uiView.bounds; |
| 37 |
vid_win.contentMode = .scaleAspectFit; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private func call_func(user_data: UnsafeMutableRawPointer?) { |
| 44 |
let pjsip_vars = Unmanaged<PjsipVars>.fromOpaque(user_data!).takeUnretainedValue() |
| 45 |
if (!pjsip_vars.calling) { |
| 46 |
var status: pj_status_t; |
| 47 |
var opt = pjsua_call_setting(); |
| 48 |
|
| 49 |
pjsua_call_setting_default(&opt); |
| 50 |
opt.aud_cnt = 1; |
| 51 |
opt.vid_cnt = 1; |
| 52 |
|
| 53 |
let dest_str = strdup(pjsip_vars.dest); |
| 54 |
var dest:pj_str_t = pj_str(dest_str); |
| 55 |
status = pjsua_call_make_call(0, &dest, &opt, nil, nil, &pjsip_vars.call_id); |
| 56 |
DispatchQueue.main.sync { |
| 57 |
pjsip_vars.calling = (status == PJ_SUCCESS.rawValue); |
| 58 |
} |
| 59 |
free(dest_str); |
| 60 |
} else { |
| 61 |
if (pjsip_vars.call_id != PJSUA_INVALID_ID.rawValue) { |
| 62 |
DispatchQueue.main.sync { |
| 63 |
pjsip_vars.calling = false; |
| 64 |
} |
| 65 |
pjsua_call_hangup(pjsip_vars.call_id, 200, nil, nil); |
| 66 |
pjsip_vars.call_id = PJSUA_INVALID_ID.rawValue; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
struct ContentView: View { |
| 73 |
@EnvironmentObject var pjsip_vars: PjsipVars; |
| 74 |
|
| 75 |
var body: some View { |
| 76 |
VStack(alignment: .center) { |
| 77 |
HStack(alignment: .center) { |
| 78 |
Text(pjsip_vars.calling != true ? "Destination:" : |
| 79 |
pjsip_vars.vid_win != nil ? "Video" : "") |
| 80 |
if (!pjsip_vars.calling) { |
| 81 |
TextField(pjsip_vars.dest, text: $pjsip_vars.dest) |
| 82 |
.frame(minWidth:0, maxWidth:200) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
Button(action: { |
| 87 |
let user_data = |
| 88 |
UnsafeMutableRawPointer(Unmanaged.passUnretained(pjsip_vars).toOpaque()) |
| 89 |
|
| 90 |
/* IMPORTANT: |
| 91 |
* We need to call PJSIP API from a separate thread since |
| 92 |
* PJSIP API can potentially block the main/GUI thread. |
| 93 |
* And make sure we don't use Apple's Dispatch / gcd since |
| 94 |
* it's incompatible with POSIX threads. |
| 95 |
* In this example, we take advantage of PJSUA's timer thread |
| 96 |
* to make and hangup call. For a more complex application, |
| 97 |
* it is recommended to create your own separate thread |
| 98 |
* instead for this purpose. |
| 99 |
*/ |
| 100 |
pjsua_schedule_timer2_dbg(call_func, user_data, 0, "swift", 0); |
| 101 |
}) { |
| 102 |
if (!pjsip_vars.calling) { |
| 103 |
Text("Make call") |
| 104 |
.foregroundColor(Color.black) |
| 105 |
} else { |
| 106 |
Text("Hangup call") |
| 107 |
.foregroundColor(Color.black) |
| 108 |
} |
| 109 |
} |
| 110 |
.padding(.all, 8.0) |
| 111 |
.background(Color.green); |
| 112 |
|
| 113 |
VidView(vid_view: $pjsip_vars.vid_win) |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
struct ContentView_Previews: PreviewProvider { |
| 119 |
static var previews: some View { |
| 120 |
ContentView() |
| 121 |
} |
| 122 |
} |