#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <time.h>
#include <errno.h>
#endif
#include <opus/opusfile.h>
#define OPUS_DECODE_SAMPLERATE 48000
#define BUFFER_SIZE 960
static const char* account_uuid = TOXMOD_ACCOUNT_UUID;
static const char* api_key = TOXMOD_API_KEY;
static const char* player_name = "player1";
static const char* session_name = "chatroom123";
static const char* single_tenant_prefix = "";
void custom_logging_callback(const char* message) { printf("[libtox] %s\n", message); }
void custom_error_callback(const char* message) { fprintf(stderr, "[libtox] %s\n", message); }
int sleepms(long msec) {
#ifdef _WIN32
Sleep(msec);
return 0;
#else
struct timespec ts;
int res;
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while(res && errno == EINTR);
return res;
#endif
}
int main(int argc, char* argv[]) {
if(argc != 2) {
fprintf(stderr, "Usage: tox_example.out <file.opus>\n");
return 1;
}
const char* filepath = argv[1];
if(strcmp(account_uuid, "your account uuid here") == 0) {
fprintf(stderr, "Please replace the account uuid value with your account id.\n");
return 1;
}
if(strcmp(api_key, "your api key here") == 0) {
fprintf(stderr, "Please replace the api key value with your key.\n");
return 1;
}
if(tox_error) {
return tox_error;
}
if(tox_error) {
return tox_error;
}
if(tox_error) {
return tox_error;
}
.api_key = api_key,
.player_name_unique = player_name,
.verbose = 1,
if(strlen(single_tenant_prefix) > 0)
if(tox_error) {
return tox_error;
}
if(tox_error) {
return tox_error;
}
int opus_error;
OggOpusFile* opus_file = op_open_file(filepath, &opus_error);
if(opus_error != 0) {
fprintf(stderr, "Failed to open file %s\n", filepath);
return 1;
}
printf("Processing simulated chat audio in realtime; uploads occur every ~15s\n");
int num_samples_read = 0;
int counter = 0;
float samples_buffer[BUFFER_SIZE];
do {
int link_index;
num_samples_read = op_read_float(opus_file, samples_buffer, BUFFER_SIZE, &link_index);
const int num_channels = op_channel_count(opus_file, link_index);
if(num_samples_read > 0) {
OPUS_DECODE_SAMPLERATE, num_channels);
if(tox_error)
const unsigned int milliseconds_read =
(unsigned int)(((double)num_samples_read / OPUS_DECODE_SAMPLERATE) * 1000);
counter += num_samples_read;
if(counter >= OPUS_DECODE_SAMPLERATE) {
counter -= OPUS_DECODE_SAMPLERATE;
printf(".");
fflush(stdout);
}
sleepms(milliseconds_read);
}
} while(num_samples_read > 0);
if(num_samples_read < 0) {
fprintf(stderr, "Opus error %d reading file %s\n", num_samples_read, filepath);
return 1;
}
op_free(opus_file);
if(tox_error) {
return tox_error;
}
if(tox_error) {
return tox_error;
}
return 0;
}
const char * single_tenant_prefix
Definition tox.h:137
TOX_ERROR
Definition tox.h:177
TOX_DEF_PREFIX TOX_ERROR tox_join_session(tox_instance_t *tox_instance_ptr, const char *session_name_unique)
TOX_DEF_PREFIX TOX_ERROR tox_set_log_error_callback(void(*log_error_callback)(const char *))
TOX_DEF_PREFIX TOX_ERROR tox_create_instance(tox_instance_t *tox_instance_ptr, const tox_config_t *tox_config_ptr)
TOX_DEF_PREFIX TOX_ERROR tox_destroy_instance(tox_instance_t *tox_instance_ptr)
TOX_DEF_PREFIX TOX_ERROR tox_global_cleanup(void)
TOX_DEF_PREFIX TOX_ERROR tox_set_log_info_callback(void(*log_info_callback)(const char *))
@ TOX_NONE
Definition tox.h:68
TOX_DEF_PREFIX const char * tox_error_name(TOX_ERROR error)
TOX_DEF_PREFIX TOX_ERROR tox_leave_session(tox_instance_t *tox_instance_ptr)
TOX_DEF_PREFIX TOX_ERROR tox_add_audio_float(tox_instance_t *tox_instance_ptr, const float *audio, unsigned int num_samples, unsigned int sample_rate, unsigned int num_channels)
TOX_DEF_PREFIX TOX_ERROR tox_global_init(void)