277 lines
7.9 KiB
C++
277 lines
7.9 KiB
C++
|
|
#include "modules/ImguiModule/ImguiModule.h"
|
|
|
|
#include "modules/ClientModule/ClientModule.h"
|
|
|
|
#include "ChatClientVoice.h"
|
|
|
|
|
|
ChatClientVoice::ChatClientVoice(Archimedes::App* a, void* h) : Module(a, h) {
|
|
|
|
name = "ChatClientVoice";
|
|
|
|
ClientModule* cm = new ClientModule(a, h);
|
|
deps[*cm] = cm;
|
|
cm->shouldHandleEvents(ClientModule::CMEventEnum::ConnectionStatusChanged | ClientModule::CMEventEnum::DataSent);
|
|
|
|
ImguiModule* im = new ImguiModule(a, h);
|
|
|
|
deps[*im] = im;
|
|
|
|
}
|
|
|
|
ChatClientVoice::~ChatClientVoice() {
|
|
if(app) {
|
|
|
|
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
|
|
|
|
im->releaseContext(ImGui::GetCurrentContext());
|
|
|
|
//if(buf)
|
|
// delete [] buf;
|
|
}
|
|
}
|
|
|
|
void ChatClientVoice::onLoad() {
|
|
|
|
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
|
|
|
|
if(!im) {
|
|
std::cout << "No ImguiModule for ChatClientVoice!\n";
|
|
std::abort();
|
|
}
|
|
|
|
ImGui::SetCurrentContext(im->aquireContext());
|
|
|
|
ClientModule* cm; { cm = (ClientModule*) moduleInstances[ClientModule()]; }
|
|
|
|
if(!cm) {
|
|
std::cout << "No ClientModule for ChatClientVoice!\n";
|
|
std::abort();
|
|
}
|
|
|
|
if(!SDL_Init(SDL_INIT_AUDIO)) {
|
|
std::cout << "Audio init failed!\n";
|
|
std::abort();
|
|
}
|
|
|
|
mic = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, NULL, NULL, NULL);
|
|
SDL_GetAudioStreamFormat(mic, &spec, NULL);
|
|
speaker = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL);
|
|
|
|
//buf = new unsigned char[len];
|
|
|
|
}
|
|
|
|
void ChatClientVoice::run() {
|
|
|
|
static ClientModule* cm; { cm = (ClientModule*) moduleInstances[ClientModule()]; }
|
|
|
|
static unsigned char buf[10 * 1024];
|
|
|
|
if(!cm) {
|
|
std::cout << "No ClientModule for ChatClientVoice!\n";
|
|
std::abort();
|
|
}
|
|
|
|
if(open) {
|
|
static std::string s, addr = "127.0.0.1:9932";
|
|
|
|
ImGui::Begin("ChatClientVoice Module", &open);
|
|
|
|
ImGui::InputText("Server Address: ", &addr);
|
|
|
|
if(cm->isRunning() && cm->isConnected()) {
|
|
|
|
if(ImGui::Button("Disconnect") && cm->isRunning()) {
|
|
cm->sendReliable("srvcmd1", strlen("srvcmd1"));
|
|
//cm->stopClient();
|
|
}
|
|
|
|
} else {
|
|
|
|
if(ImGui::Button("Connect") && !cm->isRunning()) {
|
|
static SteamNetworkingIPAddr serverAddr;
|
|
serverAddr.ParseString(addr.c_str());
|
|
cm->startClient(serverAddr);
|
|
}
|
|
}
|
|
|
|
|
|
ImGui::Text("%s", messages.c_str());
|
|
|
|
ImGui::InputText("Message: ", &s);
|
|
|
|
if(cm->isConnected()) {
|
|
ImGui::SameLine();
|
|
|
|
if(ImGui::Button("send")) {
|
|
if(s == "/quit") {
|
|
cm->sendReliable("srvcmd1", strlen("srvcmd1"));
|
|
cm->stopClient();
|
|
} else if(s == "/shutdown") {
|
|
cm->sendReliable("srvcmd2", strlen("srvcmd2"));
|
|
//cm->stopClient();
|
|
} else {
|
|
s.insert(0, "client");
|
|
cm->sendReliable(s.c_str(), (uint32) s.length());
|
|
}
|
|
s.clear();
|
|
}
|
|
}
|
|
|
|
static float micVol = 1.0f;
|
|
static float speakerVol = 1.0f;
|
|
|
|
static bool micTest = false;
|
|
|
|
ImGui::SliderFloat("Mic Volume", &micVol, 0.0f, 1.0f);
|
|
ImGui::SliderFloat("Speaker Volume", &speakerVol, 0.0f, 1.0f);
|
|
|
|
ImGui::Checkbox("Mic test", &micTest);
|
|
|
|
if(!SDL_AudioStreamDevicePaused(mic)) {
|
|
SDL_SetAudioStreamGain(mic, micVol);
|
|
} else if(micTest) {
|
|
SDL_ClearAudioStream(mic);
|
|
SDL_ResumeAudioStreamDevice(mic);
|
|
SDL_SetAudioStreamGain(mic, micVol);
|
|
}
|
|
|
|
if(!SDL_AudioStreamDevicePaused(speaker)) {
|
|
SDL_SetAudioStreamGain(speaker, speakerVol);
|
|
} else if(micTest) {
|
|
SDL_ClearAudioStream(speaker);
|
|
SDL_ResumeAudioStreamDevice(speaker);
|
|
SDL_SetAudioStreamGain(speaker, speakerVol);
|
|
}
|
|
|
|
static int avail = 0;
|
|
|
|
if(micTest) {
|
|
|
|
avail = SDL_min(len, SDL_GetAudioStreamAvailable(mic));
|
|
if(avail > 10) {
|
|
avail = SDL_GetAudioStreamData(mic, buf, avail);
|
|
SDL_PutAudioStreamData(speaker, buf, avail);
|
|
}
|
|
|
|
} else if(cm->isConnected()){
|
|
// if not testing, send to server.
|
|
|
|
avail = SDL_min(len, SDL_GetAudioStreamAvailable(mic));
|
|
if(avail > 10) {
|
|
avail = SDL_GetAudioStreamData(mic, buf, avail);
|
|
cm->sendReliable(buf, avail);
|
|
}
|
|
}
|
|
|
|
|
|
ImGui::End();
|
|
} else {
|
|
app->emitEvent(new Archimedes::DoUnloadModuleEvent(*cm));
|
|
}
|
|
}
|
|
|
|
bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
|
|
|
|
unsigned int type = app->getEventType(event);
|
|
static ClientModule* cm;
|
|
|
|
/*if(type == app->getEventType("DataSentEvent")) {
|
|
//we did this?
|
|
return true;
|
|
} else */
|
|
|
|
if(type == app->getEventType(Archimedes::DataRecievedEvent())) {
|
|
|
|
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
|
|
|
|
{ cm = (ClientModule*) moduleInstances[ClientModule()]; }
|
|
|
|
static std::string s;
|
|
|
|
if(std::string((const char*)e.msg->m_pData, 6) == "client") {
|
|
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
|
|
|
|
messages += "\n\n" + s;
|
|
} else if(std::string((const char*)e.msg->m_pData, 6) == "server") {
|
|
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
|
|
|
|
int serverCode = stoi(s);
|
|
|
|
switch(serverCode) {
|
|
case 0:
|
|
//start voice
|
|
SDL_ClearAudioStream(mic);
|
|
SDL_ClearAudioStream(speaker);
|
|
SDL_ResumeAudioStreamDevice(mic);
|
|
SDL_ResumeAudioStreamDevice(speaker);
|
|
break;
|
|
case 1:
|
|
//stop voice and clear
|
|
SDL_PauseAudioStreamDevice(mic);
|
|
SDL_PauseAudioStreamDevice(speaker);
|
|
SDL_ClearAudioStream(mic);
|
|
SDL_ClearAudioStream(speaker);
|
|
break;
|
|
case 2:
|
|
//disconnect
|
|
cm->stopClient();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
} else /*if(std::string((const char*)e.msg->m_pData, 6) == "audio:")*/ {
|
|
s = "audio data";
|
|
|
|
SDL_PutAudioStreamData(speaker, e.msg->m_pData, e.msg->m_cbSize);
|
|
}
|
|
|
|
//std::cerr << "Client Recieved: " << s << std::endl;
|
|
|
|
return true;
|
|
} else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
|
|
|
Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event;
|
|
|
|
switch(e.info->m_info.m_eState) {
|
|
|
|
case k_ESteamNetworkingConnectionState_None:
|
|
// NOTE: We will get callbacks here when we destroy connections. You can ignore these.
|
|
break;
|
|
|
|
case k_ESteamNetworkingConnectionState_ClosedByPeer:
|
|
case k_ESteamNetworkingConnectionState_ProblemDetectedLocally:
|
|
{
|
|
|
|
SDL_PauseAudioStreamDevice(mic);
|
|
SDL_PauseAudioStreamDevice(speaker);
|
|
SDL_ClearAudioStream(mic);
|
|
SDL_ClearAudioStream(speaker);
|
|
|
|
break;
|
|
}
|
|
|
|
case k_ESteamNetworkingConnectionState_Connecting:
|
|
{
|
|
break;
|
|
}
|
|
|
|
case k_ESteamNetworkingConnectionState_Connected:
|
|
//OnConnect
|
|
break;
|
|
|
|
default:
|
|
// Silences -Wswitch
|
|
break;
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
}
|