Skip to content

Commit

Permalink
Merge branch 'release/0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverepper committed Aug 18, 2023
2 parents 6c106a2 + 76985d4 commit e468176
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cmake-build-debug
TODO.md
cmake/credentials.cmake
installed
cmake-build-debug-event-trace
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.3)

project(phone VERSION 0.7.0 LANGUAGES C CXX)
project(phone VERSION 0.8.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)

Expand Down
2 changes: 1 addition & 1 deletion scripts/build-pjproject-darwin-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ then
fi

export PREFIX="$1/pjproject"
PJPROJECT_VERSION=2.13
PJPROJECT_VERSION=2.13.1

#IOS_ARM64_INSTALL_PREFIX="${PREFIX}/ios-arm64"
#
Expand Down
27 changes: 26 additions & 1 deletion src/c_cli/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <float.h>

void clear_input_buffer(void) {
int c;
Expand Down Expand Up @@ -42,12 +43,36 @@ int read_int(int *in) {
if (value <= INT_MAX && value >= INT_MIN) {
*in = (int)value;
} else {
fprintf(stderr, "invalid input.\n");
fprintf(stderr, "value out of range.\n");
return 1;
}
return 0;
}

int read_float(float *in) {
char input[11];
double value;
char *endptr;

if (read_string(input, sizeof(input)) != 0) return 1;

value = strtod(input, &endptr);

if (*input == '\0' || *endptr != '\0') {
fprintf(stderr, "invalid input.\n");
return 1;
}

if (value <= FLT_MAX && value >= -FLT_MAX) {
*in = (float)value;
} else {
fprintf(stderr, "value out of range.\n");
return 1;
}

return 0;
}

void die(phone_t instance) {
phone_destroy(instance);
fprintf(stderr, "%s\n", phone_last_error());
Expand Down
66 changes: 61 additions & 5 deletions src/c_cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ void on_incoming_call_with_id_cb(const char *call_id, __attribute__((unused)) vo

printf("Incoming call id: %s, index: %d\n", call_id, s->last_call_index);

int answer_after;
phone_call_answer_after_id(s->phone, call_id, &answer_after);
int answer_after = -1;
if (phone_call_answer_after_id(s->phone, call_id, &answer_after) != PHONE_STATUS_SUCCESS)
fprintf(stderr, "%s\n", phone_last_error());

// call is user initiated if >= 0
if (answer_after >= 0) {
sleep(answer_after);
phone_answer_call_id(s->phone, call_id);
Expand Down Expand Up @@ -204,8 +207,10 @@ int main() {
{
phone_refresh_audio_devices();
size_t count = phone_get_audio_devices_count();
size_t max_driver_name_length = phone_get_audio_device_driver_name_length() + 1; // +1 for zero termination
size_t max_device_name_length = phone_get_audio_device_info_name_length() + 1; // +1 for zero termination
size_t max_driver_name_length =
phone_get_audio_device_driver_name_length() + 1; // +1 for zero termination
size_t max_device_name_length =
phone_get_audio_device_info_name_length() + 1; // +1 for zero termination

audio_device_info_t devices[count];
char driver_names[count][max_driver_name_length];
Expand All @@ -222,7 +227,8 @@ int main() {
fprintf(stderr, "%s\n", phone_last_error());

for (i = 0; i < count; i++) {
printf("%d - %s/%s (%d/%d)\n", devices[i].id, devices[i].driver, devices[i].name, devices[i].input_count, devices[i].output_count);
printf("%d - %s/%s (%d/%d)\n", devices[i].id, devices[i].driver, devices[i].name,
devices[i].input_count, devices[i].output_count);
}
}
break;
Expand Down Expand Up @@ -262,6 +268,56 @@ int main() {
printf("handle ip change\n");
phone_handle_ip_change();
break;
case 'm':
clear_input_buffer();
{
int call_index;
unsigned level;
printf("please enter call index: ");
if (read_int(&call_index) != 0) break;
if (phone_get_rx_level_call_index(state->phone, call_index, &level) != PHONE_STATUS_SUCCESS)
fprintf(stderr, "%s\n", phone_last_error());
printf("last rx level for call: %d\n", level);
}
break;
case 'M':
clear_input_buffer();
{
char call_id[128];
unsigned level;
printf("please enter call id: ");
if (read_string(call_id, sizeof(call_id)) != 0) break;
if (phone_get_rx_level_call_id(state->phone, call_id, &level) != PHONE_STATUS_SUCCESS)
fprintf(stderr, "%s\n", phone_last_error());
printf("last rx level for call: %d\n", level);
}
break;
case '0':
clear_input_buffer();
{
int call_index;
float level;
printf("please enter call index: ");
if (read_int(&call_index) != 0) break;
printf("please enter desired level: ");
if (read_float(&level) != 0) break;
if (phone_set_rx_level_call_index(state->phone, call_index, level) != PHONE_STATUS_SUCCESS)
fprintf(stderr, "%s\n", phone_last_error());
}
break;
case '=':
clear_input_buffer();
{
char call_id[128];
float level;
printf("please enter call id: ");
if (read_string(call_id, sizeof(call_id)) != 0) break;
printf("please enter desired level: ");
if (read_float(&level) != 0) break;
if (phone_set_rx_level_call_id(state->phone, call_id, level) != PHONE_STATUS_SUCCESS)
fprintf(stderr, "%s\n", phone_last_error());
}
break;
default:
clear_input_buffer();
break;
Expand Down
26 changes: 26 additions & 0 deletions src/cpp_cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ auto main() -> int {
} else if (command == 'i') {
std::cout << "handle ip change" << std::endl;
phone_instance_t::handle_ip_change();
} else if (command == 'm') {
int call_index;
std::cout << "please enter call index: ";
std::cin >> call_index;
std::cout << "tx level for call: " << state.phone.get_rx_level_for_call(call_index) << std::endl;
} else if (command == 'M') {
std::string call_id;
std::cout << "please enter call id: ";
std::cin >> call_id;
std::cout << "tx level for call: " << state.phone.get_rx_level_for_call(call_id) << std::endl;
} else if (command == '0') {
int call_index;
std::cout << "please enter call index: ";
std::cin >> call_index;
float level;
std::cout << "please enter desired level 0-2: ";
std::cin >> level;
state.phone.set_rx_level_for_call(call_index, level);
} else if (command == '=') {
int call_id;
std::cout << "please enter call id: ";
std::cin >> call_id;
float level;
std::cout << "please enter desired level 0-2: ";
std::cin >> level;
state.phone.set_rx_level_for_call(call_id, level);
}
} catch (const phone::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/cpp_cli/simple_task_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class simple_task_system {
std::vector<std::thread> m_threads;
simple_notification_queue m_queue;

phone_instance_t * m_phone_ptr;
phone_instance_t *m_phone_ptr;

void run(unsigned i) {
m_phone_ptr->register_thread("worker_" + std::to_string(i));
Expand Down
15 changes: 12 additions & 3 deletions src/phone/include/phone.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ PHONE_DEPRECATED_EXPORT phone_status_t phone_answer_call(phone_t instance, int c
PHONE_EXPORT phone_status_t phone_answer_call_index(phone_t instance, int call_index);
PHONE_EXPORT phone_status_t phone_answer_call_id(phone_t instance, const char *call_id);

PHONE_EXPORT phone_status_t phone_start_ringing_call_index(phone_t instance, int call_index);
PHONE_EXPORT phone_status_t phone_start_ringing_call_id(phone_t instance, const char *call_id);
PHONE_DEPRECATED_EXPORT phone_status_t phone_start_ringing_call_index(phone_t instance, int call_index);
PHONE_DEPRECATED_EXPORT phone_status_t phone_start_ringing_call_id(phone_t instance, const char *call_id);

PHONE_EXPORT phone_status_t phone_answer_ringing_call_index(phone_t instance, int call_index);
PHONE_EXPORT phone_status_t phone_answer_ringing_call_id(phone_t instance, const char *call_id);

PHONE_DEPRECATED_EXPORT phone_status_t phone_hangup_call(phone_t instance, int call_id);
PHONE_EXPORT phone_status_t phone_hangup_call_index(phone_t instance, int call_index);
Expand Down Expand Up @@ -102,7 +105,7 @@ PHONE_EXPORT phone_status_t phone_set_audio_devices(int capture_device, int play
* <pre>
* Call-Info: \<sip:SERVER\>;answer-after=0
* </pre>
* If so the value will be returned via the out parameter \p answer-after.
* If so this value will be returned via the out parameter \p answer-after, -1 otherwise.
*
* @param instance phone instance
* @param call_index the call index
Expand Down Expand Up @@ -157,6 +160,12 @@ PHONE_EXPORT unsigned phone_get_call_count(phone_t instance);

PHONE_EXPORT phone_status_t phone_handle_ip_change(void);

PHONE_EXPORT phone_status_t phone_get_rx_level_call_index(phone_t instance, int call_index, unsigned int *level);
PHONE_EXPORT phone_status_t phone_get_rx_level_call_id(phone_t instance, const char *call_id, unsigned int *level);

PHONE_EXPORT phone_status_t phone_set_rx_level_call_index(phone_t instance, int call_index, float level);
PHONE_EXPORT phone_status_t phone_set_rx_level_call_id(phone_t instance, const char *call_id, float level);

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 7 additions & 1 deletion src/phone/include/phone_instance_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ class phone_instance_t {
PHONE_EXPORT void play_call_waiting() const;
PHONE_EXPORT void stop_call_waiting() const;

[[nodiscard]] PHONE_EXPORT int get_call_count();
[[nodiscard]] PHONE_EXPORT unsigned int get_call_count();

PHONE_EXPORT static void handle_ip_change();

[[nodiscard]] PHONE_EXPORT unsigned int get_rx_level_for_call(int call_index) const;
[[nodiscard]] PHONE_EXPORT unsigned int get_rx_level_for_call(const std::string& call_id) const;

PHONE_EXPORT void set_rx_level_for_call(int call_index, float level) const;
PHONE_EXPORT void set_rx_level_for_call(const std::string& call_id, float level) const;

private:
std::unique_ptr<pj::Endpoint> m_ep;
std::unique_ptr<account_t> m_account;
Expand Down
58 changes: 53 additions & 5 deletions src/phone/include/private/account_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,17 @@ class account_t : public pj::Account {

m_calls.back()->on_call_state_with_index = on_call_state_with_index;
m_calls.back()->on_call_state_with_id = on_call_state_with_id;
if (on_incoming_call_with_index.has_value())
on_incoming_call_with_index.value()(static_cast<int>(*m_calls.back()));
if (on_incoming_call_with_id.has_value())
on_incoming_call_with_id.value()(static_cast<std::string>(*m_calls.back()));

if (on_incoming_call_with_index.has_value()) {
auto index = static_cast<int>(*m_calls.back());
PJ_LOG(6, (__BASE_FILE__, "calling on_incoming_call with index: %d", index));
on_incoming_call_with_index.value()(index);
}
if (on_incoming_call_with_id.has_value()) {
auto id = static_cast<std::string>(*m_calls.back());
PJ_LOG(6, (__BASE_FILE__, "calling on_incoming_call with id: %s", index));
on_incoming_call_with_id.value()(id);
}
}

void make_call(const std::string &uri) {
Expand Down Expand Up @@ -149,7 +156,7 @@ class account_t : public pj::Account {
return static_cast<int>(*find_call(call_id));
}

int get_call_count() {
unsigned int get_call_count() {
unsigned call_count = pjsua_call_get_count();
assert(call_count == m_calls.size());
return call_count;
Expand All @@ -165,6 +172,47 @@ class account_t : public pj::Account {
m_calls.clear();
}

unsigned int get_rx_level_for_call(phone::CallID auto id) {
auto call = find_call(id);
auto media = call->getInfo().media;
auto it = std::find_if(
std::begin(media),
std::end(media),
[](const auto& info) {
return info.type == PJMEDIA_TYPE_AUDIO;
});
if (it != std::end(media)) {
auto index = it->index;
if (index <= std::numeric_limits<int>::max()) {
return call->getAudioMedia(static_cast<int>(index)).getRxLevel();
}
}
if constexpr (std::is_same_v<decltype(id), int>) {
throw phone::exception{"no audio media for call: " + std::to_string(id)};
} else {
throw phone::exception{"no audio media for call: " + id};
}
}

void set_rx_level_for_call(phone::CallID auto id, float level) {
auto call = find_call(id);
auto media = call->getInfo().media;
auto it = std::find_if(
std::begin(media),
std::end(media),
[](const auto& info){
return info.type == PJMEDIA_TYPE_AUDIO;
});
if (it != std::end(media)) {
auto index = it->index;
if (index <= std::numeric_limits<int>::max()) {
call->getAudioMedia(static_cast<int>(index)).adjustRxLevel(level);
} else {
throw phone::exception("media index out of range");
}
}
}

private:
std::vector<std::unique_ptr<call_t>> m_calls{};
};
Expand Down
14 changes: 11 additions & 3 deletions src/phone/include/private/call_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ class call_t : public pj::Call {

void onCallState(pj::OnCallStateParam &prm) override {
auto info = getInfo();
if (on_call_state_with_index.has_value()) on_call_state_with_index.value()(info.id, info.state);
if (on_call_state_with_id.has_value()) on_call_state_with_id.value()(info.callIdString, info.state);
if (on_call_state_with_index.has_value()) {
PJ_LOG(6, (__BASE_FILE__, "calling on_call_state with index: %d and state: %s(%d)",
info.id, phone::call_state_name(info.state), info.state));
on_call_state_with_index.value()(info.id, info.state);
}
if (on_call_state_with_id.has_value()) {
PJ_LOG(6, (__BASE_FILE__, "calling on_call_state with id: %s and state: %s(%d)",
info.callIdString.c_str(), phone::call_state_name(info.state), info.state));
on_call_state_with_id.value()(info.callIdString, info.state);
}
if (info.state == PJSIP_INV_STATE_DISCONNECTED) {
PJ_LOG(3, (__BASE_FILE__, "calling delete function for call: %d with id: %s", info.id, info.callIdString.c_str()));
m_delete_call_from_account(info.id);
Expand All @@ -32,7 +40,7 @@ class call_t : public pj::Call {
for (const auto& media : info.media) {
if (media.type == PJMEDIA_TYPE_AUDIO) {
auto& manager = pj::Endpoint::instance().audDevManager();
if (media.index <= static_cast<unsigned int>(std::numeric_limits<int>::max())) {
if (media.index <= std::numeric_limits<int>::max()) {
auto audio_media = getAudioMedia(static_cast<int>(media.index));
audio_media.startTransmit(manager.getPlaybackDevMedia());
manager.getCaptureDevMedia().startTransmit(audio_media);
Expand Down
6 changes: 5 additions & 1 deletion src/phone/include/private/log_writer_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
struct log_writer_t : public pj::LogWriter {
std::function<void(int, std::string_view, long thread_id, std::string_view thread_name)> log_function =
[](int level, std::string_view msg, long thread_id, std::string_view thread_name){
pj_log_write(level, msg.data(), msg.length());
if (msg.length() <= std::numeric_limits<int>::max()) {
pj_log_write(level, msg.data(), static_cast<int>(msg.length()));
} else {
throw std::invalid_argument{"message too long"};
}
};

void write(const pj::LogEntry &entry) override {
Expand Down
Loading

0 comments on commit e468176

Please sign in to comment.