forked from zaltar/Blabble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlabbleAudioManager.cpp
186 lines (155 loc) · 4.73 KB
/
BlabbleAudioManager.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
/**********************************************************\
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 "BlabbleAudioManager.h"
BlabbleAudioManager::BlabbleAudioManager(const std::string& wavPath) :
wav_path_(wavPath), wav_player_(-1)
{
pj_str_t name = pj_str(const_cast<char*>("inring"));
pjmedia_tone_desc tone[3];
pj_status_t status;
pool_ = pjsua_pool_create("blabble", 4096, 4096);
if (pool_ == NULL)
throw std::runtime_error("Ran out of memory creating pool!");
tone[0].freq1 = 440;
tone[0].freq2 = 480;
tone[0].on_msec = 2000;
tone[0].off_msec = 1000;
tone[1].freq1 = 440;
tone[1].freq2 = 480;
tone[1].on_msec = 2000;
tone[1].off_msec = 4000;
tone[2].freq1 = 440;
tone[2].freq2 = 480;
tone[2].on_msec = 2000;
tone[2].off_msec = 3000;
try {
in_ring_slot_ = -1;
in_ring_player_ = -1;
std::string path =
//#if WIN32
// wavPath + "\\ringtone.wav";
//#else
wavPath + "/ringtone.wav";
//#endif
pj_str_t ring_file = pj_str(const_cast<char*>(path.c_str()));
if (pjsua_player_create(&ring_file, 0, &in_ring_player_) == PJ_SUCCESS)
{
in_ring_slot_ = pjsua_player_get_conf_port(in_ring_player_);
}
if (in_ring_slot_ < 0)
{
//We don't have the wav ringtone, use a modified tone
status = pjmedia_tonegen_create2(pool_, &name, 8000, 1, 160, 16, PJMEDIA_TONEGEN_LOOP, &in_ring_port_);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed inring pjmedia_tonegen_create2");
status = pjmedia_tonegen_play(in_ring_port_, 1, tone, PJMEDIA_TONEGEN_LOOP);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed inring pjmedia_tonegen_play");
status = pjsua_conf_add_port(pool_, in_ring_port_, &in_ring_slot_);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed inring pjsua_conf_add_port");
}
tone[0].off_msec = 4000;
name = pj_str(const_cast<char*>("ring"));
status = pjmedia_tonegen_create2(pool_, &name, 8000, 1, 160, 16, PJMEDIA_TONEGEN_LOOP, &ring_port_);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed ring pjmedia_tonegen_create2");
status = pjmedia_tonegen_play(ring_port_, 3, tone, PJMEDIA_TONEGEN_LOOP);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed ring pjmedia_tonegen_play");
status = pjsua_conf_add_port(pool_, ring_port_, &ring_slot_);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed ring pjsua_conf_add_port");
tone[0].freq1 = 440;
tone[0].freq2 = 0;
tone[0].on_msec = 500;
tone[0].off_msec = 2000;
tone[1].freq1 = 440;
tone[1].freq2 = 0;
tone[1].on_msec = 500;
tone[1].off_msec = 4000;
name = pj_str(const_cast<char*>("call_wait"));
status = pjmedia_tonegen_create2(pool_, &name, 8000, 1, 160, 16,
PJMEDIA_TONEGEN_LOOP, &call_wait_ring_port_);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed call_wait pjmedia_tonegen_create2");
status = pjmedia_tonegen_play(call_wait_ring_port_, 2, tone, PJMEDIA_TONEGEN_LOOP);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed call_wait pjmedia_tonegen_play");
status = pjsua_conf_add_port(pool_, call_wait_ring_port_, &call_wait_slot_);
if (status != PJ_SUCCESS)
throw std::runtime_error("Failed call_wait pjsua_conf_add_port");
}
catch (std::runtime_error& e)
{
pj_pool_release(pool_);
pool_ = NULL;
throw e;
}
}
void BlabbleAudioManager::StopRings()
{
pjsua_conf_disconnect(ring_slot_, 0);
pjmedia_tonegen_rewind(ring_port_);
if (in_ring_slot_ > -1)
{
pjsua_conf_disconnect(in_ring_slot_, 0);
}
if (in_ring_player_ > -1)
{
pjsua_player_set_pos(in_ring_player_, 0);
}
pjsua_conf_disconnect(call_wait_slot_, 0);
pjmedia_tonegen_rewind(call_wait_ring_port_);
}
void BlabbleAudioManager::StartRing()
{
pjsua_conf_connect(ring_slot_, 0);
}
void BlabbleAudioManager::StartInRing()
{
if (pjsua_call_get_count() > 1)
{
pjsua_conf_connect(call_wait_slot_, 0);
}
else
{
pjsua_conf_connect(in_ring_slot_, 0);
}
}
BlabbleAudioManager::~BlabbleAudioManager()
{
}
bool BlabbleAudioManager::StartWav(const std::string& fileName)
{
if (wav_player_ > 0)
return false;
std::string path =
#if WIN32
wav_path_ + "\\" + fileName;
#else
wav_path_ + "/" + fileName;
#endif
pj_str_t wav_file = pj_str(const_cast<char*>(path.c_str()));
if (pjsua_player_create(&wav_file, 0, &wav_player_) == PJ_SUCCESS)
{
wav_slot_ = pjsua_player_get_conf_port(wav_player_);
pjsua_conf_connect(wav_slot_, 0);
return true;
}
wav_player_ = -1;
return false;
}
void BlabbleAudioManager::StopWav()
{
if (wav_player_ > 0)
{
pjsua_conf_disconnect(wav_slot_, 0);
pjsua_player_destroy(wav_player_);
wav_player_ = -1;
}
}