Skip to content

Commit

Permalink
made av session more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
naxuroqa committed Jul 13, 2014
1 parent e232ce6 commit 5c20f85
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
69 changes: 44 additions & 25 deletions src/core/AudioManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace Venom {
}

public struct CallInfo {
bool shutdown;
bool startup;
bool active;
bool video;
}
Expand Down Expand Up @@ -56,7 +58,6 @@ namespace Venom {

private Thread<int> av_thread = null;
private bool running = false;
private int number_of_calls = 0;

private ToxSession _tox_session = null;
private unowned ToxAV.ToxAV toxav = null;
Expand Down Expand Up @@ -109,6 +110,12 @@ namespace Venom {

((Gst.BaseSrc)audio_source_out).blocksize = (ToxAV.DefaultCodecSettings.audio_frame_duration * ToxAV.DefaultCodecSettings.audio_sample_rate) / 1000 * 2;
}
~AudioManager() {
running = false;
if(av_thread != null) {
av_thread.join();
}
}

public static void audio_receive_callback(ToxAV.ToxAV toxav, int32 call_index, int16[] frames) {
Logger.log(LogLevel.DEBUG, "Got audio frames, of size: %d".printf(frames.length * 2));
Expand Down Expand Up @@ -168,6 +175,7 @@ namespace Venom {
uint8[] enc_buffer = new uint8[perframe*2];
int prep_frame_ret = 0;
ToxAV.AV_Error send_audio_ret;
int number_of_calls = 0;

while(running) {
// read samples from pipeline
Expand All @@ -179,38 +187,56 @@ namespace Venom {
}
// distribute samples across peers
for(int i = 0; i < MAX_CALLS; i++) {
if(calls[i].shutdown && calls[i].active) {
Logger.log(LogLevel.DEBUG, "Shutting down av transmission %i".printf(i));
ToxAV.AV_Error e = toxav.kill_transmission(i);
if(e != ToxAV.AV_Error.NONE) {
Logger.log(LogLevel.FATAL, "Could not shutdown AV transmission: %i".printf((int)e));
}
number_of_calls--;
calls[i].active = false;
calls[i].shutdown = false;
}
if(calls[i].startup && !calls[i].active) {
Logger.log(LogLevel.DEBUG, "Starting av transmission %i".printf(i));
calls[i].startup = false;
ToxAV.CodecSettings t_settings = ToxAV.DefaultCodecSettings;
ToxAV.AV_Error e = toxav.prepare_transmission(i, ref t_settings, ToxAV.CallType.AUDIO);
if(e != ToxAV.AV_Error.NONE) {
Logger.log(LogLevel.FATAL, "Could not prepare AV transmission: %i".printf((int)e));
} else {
number_of_calls++;
calls[i].active = true;
}
}
if(calls[i].active) {

prep_frame_ret = toxav.prepare_audio_frame(i, enc_buffer, buffer, buffer_size);
if(prep_frame_ret <= 0) {
Logger.log(LogLevel.WARNING, "prepare_audio_frame returned an error: %i".printf(prep_frame_ret));
continue;
}

send_audio_ret = toxav.send_audio(i, enc_buffer, prep_frame_ret);
if(send_audio_ret != ToxAV.AV_Error.NONE) {
Logger.log(LogLevel.WARNING, "send_audio returned %d".printf(send_audio_ret));
} else {
send_audio_ret = toxav.send_audio(i, enc_buffer, prep_frame_ret);
if(send_audio_ret != ToxAV.AV_Error.NONE) {
Logger.log(LogLevel.WARNING, "send_audio returned %d".printf(send_audio_ret));
}
}
}
}
if(number_of_calls <= 0) {
number_of_calls = 0;
Logger.log(LogLevel.INFO, "No remaining calls, stopping audio thread.");
break;
}
}

Logger.log(LogLevel.INFO, "stopping av thread...");
Logger.log(LogLevel.INFO, "stopping audio thread...");
set_pipeline_paused();
return 0;
}

public void on_start_call(Contact c) {
ToxAV.CallType call_type = tox_session.get_peer_transmission_type(c);

if(!tox_session.prepare_transmission(c, call_type)) {
Logger.log(LogLevel.ERROR, "Could not prepare AV transmission!");
return;
}

calls[c.call_index].active = true;
calls[c.call_index].startup = true;
calls[c.call_index].video = false;
number_of_calls++;

if(!running) {
running = true;
Expand All @@ -224,14 +250,7 @@ namespace Venom {
return;
}

toxav.kill_transmission(c.call_index);
calls[c.call_index].active = false;
number_of_calls--;

if(number_of_calls == 0) {
Logger.log(LogLevel.INFO, "number of calls is 0, stopping av thread...");
running = false;
}
calls[c.call_index].shutdown = true;
}

}
Expand Down
13 changes: 0 additions & 13 deletions src/core/ToxSession.vala
Original file line number Diff line number Diff line change
Expand Up @@ -898,19 +898,6 @@ namespace Venom {
_toxav_handle.cancel(c.call_index, 0, "do not want");
}

public bool prepare_transmission(Contact c, ToxAV.CallType call_type, ToxAV.CodecSettings settings = ToxAV.DefaultCodecSettings) {
ToxAV.CodecSettings t_settings = settings;
ToxAV.AV_Error e = _toxav_handle.prepare_transmission(c.call_index, ref t_settings, call_type);
if(e != ToxAV.AV_Error.NONE) {
stderr.printf("toxav_prepare_transmission failed: %i\n", e);
}
return e == ToxAV.AV_Error.NONE;
}

public ToxAV.CallType get_peer_transmission_type (Contact c) {
return (ToxAV.CallType)_toxav_handle.get_peer_transmission_type(c.call_index, 0);
}

// TOXAV callbacks
private void on_av_invite_callback(int32 call_index) {
int friend_id = _toxav_handle.get_peer_id(call_index, 0);
Expand Down

0 comments on commit 5c20f85

Please sign in to comment.