update audio client packet send to NLPacket

This commit is contained in:
Stephen Birarda 2015-07-07 10:53:55 -07:00
parent e1c098233a
commit edf7a96b58
3 changed files with 33 additions and 40 deletions

View file

@ -724,20 +724,18 @@ void AudioClient::handleLocalEchoAndReverb(QByteArray& inputByteArray) {
} }
void AudioClient::handleAudioInput() { void AudioClient::handleAudioInput() {
static char audioDataPacket[MAX_PACKET_SIZE]; if (!_audioPacket) {
// we don't have an audioPacket yet - set that up now
static int numBytesPacketHeader = numBytesForPacketHeaderGivenPacketType(PacketTypeMicrophoneAudioNoEcho); _audioPacket = NLPacket::create(PacketType::MicrophoneAudioNoEcho);
}
// NOTE: we assume PacketTypeMicrophoneAudioWithEcho has same size headers as
// PacketTypeMicrophoneAudioNoEcho. If not, then networkAudioSamples will be pointing to the wrong place for writing
// audio samples with echo.
static int leadingBytes = numBytesPacketHeader + sizeof(quint16) + sizeof(glm::vec3) + sizeof(glm::quat) + sizeof(quint8);
static int16_t* networkAudioSamples = (int16_t*)(audioDataPacket + leadingBytes);
float inputToNetworkInputRatio = calculateDeviceToNetworkInputRatio(); float inputToNetworkInputRatio = calculateDeviceToNetworkInputRatio();
int inputSamplesRequired = (int)((float)AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL * inputToNetworkInputRatio); int inputSamplesRequired = (int)((float)AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL * inputToNetworkInputRatio);
static int leadingBytes = sizeof(quint16) + sizeof(glm::vec3) + sizeof(glm::quat) + sizeof(quint8);
int16_t* networkAudioSamples = (int16_t*)(_audioPacket->payload() + leadingBytes);
QByteArray inputByteArray = _inputDevice->readAll(); QByteArray inputByteArray = _inputDevice->readAll();
// Add audio source injection if enabled // Add audio source injection if enabled
@ -769,7 +767,7 @@ void AudioClient::handleAudioInput() {
while (_inputRingBuffer.samplesAvailable() >= inputSamplesRequired) { while (_inputRingBuffer.samplesAvailable() >= inputSamplesRequired) {
const int numNetworkBytes = _isStereoInput const int numNetworkBytes = _isStereoInput
? AudioConstants::NETWORK_FRAME_BYTES_STEREO ? AudioConstants::NETWORK_FRAME_BYTES_STEREO`
: AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL; : AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL;
const int numNetworkSamples = _isStereoInput const int numNetworkSamples = _isStereoInput
? AudioConstants::NETWORK_FRAME_SAMPLES_STEREO ? AudioConstants::NETWORK_FRAME_SAMPLES_STEREO
@ -846,57 +844,49 @@ void AudioClient::handleAudioInput() {
PacketType::Value packetType; PacketType::Value packetType;
if (_lastInputLoudness == 0) { if (_lastInputLoudness == 0) {
packetType = PacketTypeSilentAudioFrame; _audioPacket->setType(PacketType::SilentAudioFrame);
} else { } else {
if (_shouldEchoToServer) { if (_shouldEchoToServer) {
packetType = PacketTypeMicrophoneAudioWithEcho; _audioPacket->setType(PacketType::MicrophoneAudioWithEcho);
} else { } else {
packetType = PacketTypeMicrophoneAudioNoEcho; _audioPacket->setType(PacketType::MicrophoneAudioNoEcho);
} }
} }
char* currentPacketPtr = audioDataPacket + nodeList->populatePacketHeader(audioDataPacket, packetType); // seek to the beginning of the audio packet payload
_audioPacket->seek(0);
// pack sequence number // reset the size used in this packet so it will be correct once we are done writing
memcpy(currentPacketPtr, &_outgoingAvatarAudioSequenceNumber, sizeof(quint16)); _audioPacket->setSizeUsed(0);
currentPacketPtr += sizeof(quint16);
if (packetType == PacketTypeSilentAudioFrame) { // write sequence number
_audioPacket->write(&_outgoingAvatarAudioSequenceNumber, sizeof(quint16));
if (packetType == PacketType::SilentAudioFrame) {
// pack num silent samples // pack num silent samples
quint16 numSilentSamples = numNetworkSamples; _audioPacket->write(&numSilentSamples, sizeof(quint16));
memcpy(currentPacketPtr, &numSilentSamples, sizeof(quint16));
currentPacketPtr += sizeof(quint16);
// memcpy the three float positions
memcpy(currentPacketPtr, &headPosition, sizeof(headPosition));
currentPacketPtr += (sizeof(headPosition));
// memcpy our orientation
memcpy(currentPacketPtr, &headOrientation, sizeof(headOrientation));
currentPacketPtr += sizeof(headOrientation);
} else { } else {
// set the mono/stereo byte // set the mono/stereo byte
*currentPacketPtr++ = isStereo; _audioPacket->write(&isStereo, sizeof(isStereo));
}
// memcpy the three float positions // pack the three float positions
memcpy(currentPacketPtr, &headPosition, sizeof(headPosition)); _audioPacket->write(&headPosition, sizeof(headPosition));
currentPacketPtr += (sizeof(headPosition));
// memcpy our orientation // pack the orientation
memcpy(currentPacketPtr, &headOrientation, sizeof(headOrientation)); _audioPacket->write(&headOrientation, sizeof(headOrientation));
currentPacketPtr += sizeof(headOrientation);
if (packetType != PacketType::SilentAudioFrame) {
// audio samples have already been packed (written to networkAudioSamples) // audio samples have already been packed (written to networkAudioSamples)
currentPacketPtr += numNetworkBytes; _audioPacket->setSizeUsed(_audioPacket->getSizeUsed() + numNetworkBytes);
} }
_stats.sentPacket(); _stats.sentPacket();
nodeList->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::SendAudioPacket); nodeList->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::SendAudioPacket);
int packetBytes = currentPacketPtr - audioDataPacket; nodeList->sendUnreliablePacket(_audioPacket, audioMixer);
nodeList->writeDatagram(audioDataPacket, packetBytes, audioMixer);
_outgoingAvatarAudioSequenceNumber++; _outgoingAvatarAudioSequenceNumber++;
} }
} }

View file

@ -308,6 +308,8 @@ private:
void checkDevices(); void checkDevices();
bool _hasReceivedFirstPacket = false; bool _hasReceivedFirstPacket = false;
std::unique_ptr<NLPacket> _audioPacket;
}; };

View file

@ -552,6 +552,7 @@ int NodeList::processDomainServerList(const QByteArray& packet) {
packetStream >> newUUID; packetStream >> newUUID;
setSessionUUID(newUUID); setSessionUUID(newUUID);
// TODO: when fixing this read these are actually chars now, not bools
bool thisNodeCanAdjustLocks; bool thisNodeCanAdjustLocks;
packetStream >> thisNodeCanAdjustLocks; packetStream >> thisNodeCanAdjustLocks;
setThisNodeCanAdjustLocks(thisNodeCanAdjustLocks); setThisNodeCanAdjustLocks(thisNodeCanAdjustLocks);