/* * Call with audio and video on both sides. Alice calls Bob. */ CALL_AND_START_LOOP(TypeVideo, TypeVideo) { /* Both send */ toxav_send_audio(status_control.Alice.av, sample_payload, 10); toxav_send_video(status_control.Alice.av, sample_image); toxav_send_audio(status_control.Bob.av, sample_payload, 10); toxav_send_video(status_control.Bob.av, sample_image); /* Both receive */ int16_t storage[10]; vpx_image_t *video_storage; int recved; /* Payload from Bob */ recved = toxav_recv_audio(status_control.Alice.av, 10, storage); if ( recved ) { /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ memset(storage, 0, 10); } /* Video payload */ toxav_recv_video(status_control.Alice.av, &video_storage); if ( video_storage ) { /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 || memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 || memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/ } /* Payload from Alice */ recved = toxav_recv_audio(status_control.Bob.av, 10, storage); if ( recved ) { /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ } /* Video payload */ toxav_recv_video(status_control.Bob.av, &video_storage); if ( video_storage ) { /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 || memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 || memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Alice is invalid");*/ } if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */ step++; /* This terminates the loop */ toxav_kill_transmission(status_control.Alice.av); toxav_kill_transmission(status_control.Bob.av); /* Call over Alice hangs up */ toxav_hangup(status_control.Alice.av); } }
void *one_threaded_audio(void *arg) { INFO("Started audio thread!"); av_session_t *_phone = arg; _phone->running_decaud = 1; //int recved_size; //uint8_t dest [RTP_PAYLOAD_SIZE]; int frame_size = AUDIO_FRAME_SIZE; int16_t frame[4096]; ALint sample = 0; alcCaptureStart((ALCdevice *)_phone->audio_capture_device); ALCdevice *dev; ALCcontext *ctx; ALuint source, *buffers; dev = alcOpenDevice(NULL); ctx = alcCreateContext(dev, NULL); alcMakeContextCurrent(ctx); int openal_buffers = 5; buffers = calloc(sizeof(ALuint) * openal_buffers, 1); alGenBuffers(openal_buffers, buffers); alGenSources((ALuint)1, &source); alSourcei(source, AL_LOOPING, AL_FALSE); ALuint buffer; ALint ready; uint16_t zeros[frame_size]; memset(zeros, 0, frame_size); int16_t PCM[frame_size]; int i; for (i = 0; i < openal_buffers; ++i) { alBufferData(buffers[i], AL_FORMAT_MONO16, zeros, frame_size, 48000); } alSourceQueueBuffers(source, openal_buffers, buffers); alSourcePlay(source); if (alGetError() != AL_NO_ERROR) { fprintf(stderr, "Error starting audio\n"); goto ending; } int dec_frame_len; while (_phone->running_decaud) { // combo alcGetIntegerv((ALCdevice *)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample); // record and send if (sample >= frame_size) { alcCaptureSamples((ALCdevice *)_phone->audio_capture_device, frame, frame_size); if (toxav_send_audio(_phone->av, frame, frame_size) < 0) printf("Could not encode or send audio packet\n"); } else { usleep(5000); } // play received alGetSourcei(source, AL_BUFFERS_PROCESSED, &ready); if (ready <= 0) continue; dec_frame_len = toxav_recv_audio(_phone->av, frame_size, PCM); /* Play the packet */ if (dec_frame_len > 0) { alSourceUnqueueBuffers(source, 1, &buffer); alBufferData(buffer, AL_FORMAT_MONO16, PCM, dec_frame_len * 2 * 1, 48000); int error = alGetError(); if (error != AL_NO_ERROR) { fprintf(stderr, "Error setting buffer %d\n", error); break; } alSourceQueueBuffers(source, 1, &buffer); if (alGetError() != AL_NO_ERROR) { fprintf(stderr, "Error: could not buffer audio\n"); break; } alGetSourcei(source, AL_SOURCE_STATE, &ready); if (ready != AL_PLAYING) alSourcePlay(source); } usleep(1000); } ending: _phone->running_decaud = -1; pthread_exit ( NULL ); }
void *decode_audio_thread(void *arg) { INFO("Started decode audio thread!"); av_session_t *_phone = arg; _phone->running_decaud = 1; //int recved_size; //uint8_t dest [RTP_PAYLOAD_SIZE]; int frame_size = AUDIO_FRAME_SIZE; //int data_size; ALCdevice *dev; ALCcontext *ctx; ALuint source, *buffers; dev = alcOpenDevice(NULL); ctx = alcCreateContext(dev, NULL); alcMakeContextCurrent(ctx); int openal_buffers = 5; buffers = calloc(sizeof(ALuint) * openal_buffers, 1); alGenBuffers(openal_buffers, buffers); alGenSources((ALuint)1, &source); alSourcei(source, AL_LOOPING, AL_FALSE); ALuint buffer; ALint ready; uint16_t zeros[frame_size]; memset(zeros, 0, frame_size); int16_t PCM[frame_size]; int i; for (i = 0; i < openal_buffers; ++i) { alBufferData(buffers[i], AL_FORMAT_MONO16, zeros, frame_size, 48000); } alSourceQueueBuffers(source, openal_buffers, buffers); alSourcePlay(source); if (alGetError() != AL_NO_ERROR) { fprintf(stderr, "Error starting audio\n"); goto ending; } int dec_frame_len = 0; while (_phone->running_decaud) { alGetSourcei(source, AL_BUFFERS_PROCESSED, &ready); if (ready <= 0) continue; dec_frame_len = toxav_recv_audio(_phone->av, frame_size, PCM); /* Play the packet */ if (dec_frame_len > 0) { alSourceUnqueueBuffers(source, 1, &buffer); alBufferData(buffer, AL_FORMAT_MONO16, PCM, dec_frame_len * 2 * 1, 48000); int error = alGetError(); if (error != AL_NO_ERROR) { fprintf(stderr, "Error setting buffer %d\n", error); break; } alSourceQueueBuffers(source, 1, &buffer); if (alGetError() != AL_NO_ERROR) { fprintf(stderr, "Error: could not buffer audio\n"); break; } alGetSourcei(source, AL_SOURCE_STATE, &ready); if (ready != AL_PLAYING) alSourcePlay(source); } usleep(1000); } ending: /* clean up codecs */ //pthread_mutex_lock(&cs->ctrl_mutex); /* alDeleteSources(1, &source); alDeleteBuffers(openal_buffers, buffers); alcMakeContextCurrent(NULL); alcDestroyContext(ctx); alcCloseDevice(dev); */ //pthread_mutex_unlock(&cs->ctrl_mutex); _phone->running_decaud = -1; pthread_exit ( NULL ); }
/* * Call with audio and video on both sides. Alice calls Bob. */ CALL_AND_START_LOOP(TypeVideo, TypeVideo) { /* Both send */ payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size); if ( payload_size < 0 ) { ck_assert_msg ( 0, "Failed to encode payload" ); } toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size); if ( payload_size < 0 ) { ck_assert_msg ( 0, "Failed to encode payload" ); } toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); // toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image); // toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image); /* Both receive */ int16_t storage[frame_size]; vpx_image_t *video_storage; int recved; /* Payload from Bob */ recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage); if ( recved ) { /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ } /* Video payload */ // toxav_recv_video(status_control.Alice.av, status_control.Alice.call_index, &video_storage); // // if ( video_storage ) { // /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 || // memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 || // memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/ // vpx_img_free(video_storage); // } /* Payload from Alice */ recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage); if ( recved ) { /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ } /* Video payload */ // toxav_recv_video(status_control.Bob.av, status_control.Bob.call_index, &video_storage); // // if ( video_storage ) { // /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 || // memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 || // memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Alice is invalid");*/ // vpx_img_free(video_storage); // } if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */ step++; /* This terminates the loop */ toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index); toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index); /* Call over Alice hangs up */ toxav_hangup(status_control.Alice.av, status_control.Alice.call_index); } }
void *transmission(void *arg) { #define lock pthread_mutex_lock(&this_call->mutex) #define unlock pthread_mutex_unlock(&this_call->mutex) ToxWindow* self = arg; int32_t call_index = self->call_idx; /* Missing audio support */ if ( !ASettins.av ) _cbend; Call* this_call = &ASettins.calls[call_index]; int32_t dec_frame_len; int16_t PCM[frame_size]; this_call->has_output = 1; if ( open_primary_device(input, &this_call->in_idx) != de_None ) line_info_add(self, NULL, NULL, NULL, "Failed to open input device!", SYS_MSG, 0, 0); if ( register_device_callback(call_index, this_call->in_idx, read_device_callback, &call_index, _True) != de_None) /* Set VAD as true for all; TODO: Make it more dynamic */ line_info_add(self, NULL, NULL, NULL, "Failed to register input handler!", SYS_MSG, 0, 0); if ( open_primary_device(output, &this_call->out_idx) != de_None ) { line_info_add(self, NULL, NULL, NULL, "Failed to open output device!", SYS_MSG, 0, 0); this_call->has_output = 0; } /* Start transmission */ while (this_call->ttas) { lock; if ( this_call->has_output ) { if (playback_device_ready(this_call->out_idx) == de_Busy) { unlock; continue; } dec_frame_len = toxav_recv_audio(ASettins.av, call_index, frame_size, PCM); /* Play the packet */ if (dec_frame_len > 0) { write_out(this_call->out_idx, PCM, dec_frame_len, av_DefaultSettings.audio_channels); } else if (dec_frame_len != 0) { /* >implying it'll ever get an error */ } } unlock; usleep(1000); } cleanup: if ( this_call->in_idx != -1 ) if ( close_device(input, this_call->in_idx) != de_None ) line_info_add(self, NULL, NULL, NULL, "Failed to close input device!", SYS_MSG, 0, 0); if ( this_call->out_idx != -1 ) if ( close_device(output, this_call->out_idx) != de_None ) line_info_add(self, NULL, NULL, NULL, "Failed to close output device!", SYS_MSG, 0, 0); set_call(this_call, _False); _cbend; }