forked from zaltar/Blabble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlabbleAPI.cpp
233 lines (203 loc) · 6.71 KB
/
BlabbleAPI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**********************************************************\
Original Author: Andrew Ofisher (zaltar)
License: GNU General Public License, version 3.0
http://www.gnu.org/licenses/gpl-3.0.txt
Copyright 2012 Andrew Ofisher
\**********************************************************/
#include "PjsuaManager.h"
#include "variant_list.h"
#include "DOM/Document.h"
#include "BlabbleAPI.h"
#include "BlabbleAccount.h"
#include "PjsuaManager.h"
#include "BlabbleAudioManager.h"
#include "BlabbleLogging.h"
BlabbleAPIInvalid::BlabbleAPIInvalid(const char* err)
{
error_ = std::string(err);
registerProperty("error", make_property(this, &BlabbleAPIInvalid::error));
}
BlabbleAPI::BlabbleAPI(const FB::BrowserHostPtr& host, const PjsuaManagerPtr& manager) :
browser_host_(host), manager_(manager)
{
registerMethod("createAccount", make_method(this, &BlabbleAPI::CreateAccount));
registerMethod("playWav", make_method(this, &BlabbleAPI::PlayWav));
registerMethod("stopWav", make_method(this, &BlabbleAPI::StopWav));
registerMethod("log", make_method(this, &BlabbleAPI::Log));
registerProperty("tlsEnabled", make_property(this, &BlabbleAPI::has_tls));
registerMethod("getAudioDevices", make_method(this, &BlabbleAPI::GetAudioDevices));
registerMethod("setAudioDevice", make_method(this, &BlabbleAPI::SetAudioDevice));
registerMethod("getCurrentAudioDevice", make_method(this, &BlabbleAPI::GetCurrentAudioDevice));
registerMethod("getVolume", make_method(this, &BlabbleAPI::GetVolume));
registerMethod("setVolume", make_method(this, &BlabbleAPI::SetVolume));
registerMethod("getSignalLevel", make_method(this, &BlabbleAPI::GetSignalLevel));
}
BlabbleAPI::~BlabbleAPI()
{
std::vector<BlabbleAccountWeakPtr>::iterator it;
for (it = accounts_.begin(); it < accounts_.end(); it++) {
BlabbleAccountPtr acct = it->lock();
if (acct) {
acct->Destroy();
}
}
accounts_.clear();
}
void BlabbleAPI::Log(int level, const std::wstring& msg)
{
BLABBLE_JS_LOG(level, msg);
}
BlabbleAccountWeakPtr BlabbleAPI::CreateAccount(const FB::VariantMap ¶ms)
{
BlabbleAccountPtr account = boost::make_shared<BlabbleAccount>(manager_);
try
{
FB::VariantMap::const_iterator iter = params.find("host");
if (iter != params.end())
account->set_server(iter->second.cast<std::string>());
if ((iter = params.find("username")) != params.end())
account->set_username(iter->second.cast<std::string>());
if ((iter = params.find("password")) != params.end())
account->set_password(iter->second.cast<std::string>());
if ((iter = params.find("useTls")) != params.end() &&
iter->second.is_of_type<bool>())
{
account->set_use_tls(iter->second.cast<bool>());
}
if ((iter = params.find("identity")) != params.end() &&
iter->second.is_of_type<std::string>())
{
account->set_default_identity(iter->second.cast<std::string>());
}
if ((iter = params.find("onIncomingCall")) != params.end() &&
iter->second.is_of_type<FB::JSObjectPtr>())
{
account->set_on_incoming_call(iter->second.cast<FB::JSObjectPtr>());
}
if ((iter = params.find("onRegState")) != params.end() &&
iter->second.is_of_type<FB::JSObjectPtr>())
{
account->set_on_reg_state(iter->second.cast<FB::JSObjectPtr>());
}
account->Register();
}
catch (const std::exception &e)
{
throw FB::script_error(std::string("Unable to create account: ") + e.what());
}
accounts_.push_back(account);
return BlabbleAccountWeakPtr(account);
}
BlabbleAccountPtr BlabbleAPI::FindAcc(int acc_id)
{
return manager_->FindAcc(acc_id);
}
bool BlabbleAPI::PlayWav(const std::string& fileName)
{
return manager_->audio_manager()->StartWav(fileName);
}
void BlabbleAPI::StopWav()
{
manager_->audio_manager()->StopWav();
}
FB::VariantList BlabbleAPI::GetAudioDevices()
{
unsigned int count = pjmedia_aud_dev_count();
pjmedia_aud_dev_info* audio_info = new pjmedia_aud_dev_info[count];
pj_status_t status = pjsua_enum_aud_devs(audio_info, &count);
if (status == PJ_SUCCESS) {
FB::VariantList devices;
for (unsigned int i = 0; i < count; i++)
{
FB::VariantMap map;
map["name"] = std::string(audio_info[i].name);
map["driver"] = std::string(audio_info[i].driver);
map["inputs"] = audio_info[i].input_count;
map["outputs"] = audio_info[i].output_count;
map["id"] = i;
devices.push_back(map);
}
delete[] audio_info;
return devices;
}
delete[] audio_info;
return FB::VariantList();
}
FB::VariantMap BlabbleAPI::GetCurrentAudioDevice()
{
int captureId, playbackId;
FB::VariantMap map, capInfo, playInfo;
pjmedia_aud_dev_info audio_info;
pj_status_t status = pjsua_get_snd_dev(&captureId, &playbackId);
if (status == PJ_SUCCESS)
{
status = pjmedia_aud_dev_get_info(captureId, &audio_info);
if (status == PJ_SUCCESS) {
capInfo["name"] = std::string(audio_info.name);
capInfo["driver"] = std::string(audio_info.driver);
capInfo["inputs"] = audio_info.input_count;
capInfo["outputs"] = audio_info.output_count;
capInfo["id"] = captureId;
map["capture"] = capInfo;
}
status = pjmedia_aud_dev_get_info(playbackId, &audio_info);
if (status == PJ_SUCCESS) {
playInfo["name"] = std::string(audio_info.name);
playInfo["driver"] = std::string(audio_info.driver);
playInfo["inputs"] = audio_info.input_count;
playInfo["outputs"] = audio_info.output_count;
playInfo["id"] = playbackId;
map["playback"] = playInfo;
}
} else {
map["error"] = status;
}
return map;
}
bool BlabbleAPI::SetAudioDevice(int capture, int playback)
{
return PJ_SUCCESS == pjsua_set_snd_dev(capture, playback);
}
FB::VariantMap BlabbleAPI::GetVolume()
{
pjmedia_conf_port_info info;
FB::VariantMap map;
pj_status_t status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &info);
if (status == PJ_SUCCESS)
{
map["outgoingVolume"] = ((float)info.rx_adj_level) / 128 + 1;
map["incomingVolume"] = ((float)info.tx_adj_level) / 128 + 1;
}
else
{
map["error"] = status;
}
return map;
}
void BlabbleAPI::SetVolume(FB::variant outgoingVolume, FB::variant incomingVolume)
{
if (outgoingVolume.is_of_type<double>())
{
pjsua_conf_adjust_rx_level(0, (float)outgoingVolume.cast<double>());
}
if (incomingVolume.is_of_type<double>())
{
pjsua_conf_adjust_tx_level(0, (float)incomingVolume.cast<double>());
}
}
FB::VariantMap BlabbleAPI::GetSignalLevel()
{
unsigned int txLevel, rxLevel;
FB::VariantMap map;
pj_status_t status = pjsua_conf_get_signal_level(0, &txLevel, &rxLevel);
if (status == PJ_SUCCESS)
{
map["outgoingLevel"] = rxLevel;
map["incomingLevel"] = txLevel;
}
else
{
map["error"] = status;
}
return map;
}