packet fixes in script-engine and Application

This commit is contained in:
Stephen Birarda 2015-07-08 18:24:42 -07:00
parent 5dc09692b4
commit 9c18277e9c
2 changed files with 63 additions and 60 deletions

View file

@ -2660,15 +2660,17 @@ int Application::sendNackPackets() {
return 0;
}
NLPacketList nackPacketList(PacketType::OctreeDataNack);
// iterates thru all nodes in NodeList
auto nodeList = DependencyManager::get<NodeList>();
int packetsSent = 0;
nodeList->eachNode([&](const SharedNodePointer& node){
if (node->getActiveSocket() && node->getType() == NodeType::EntityServer) {
NLPacketList nackPacketList(PacketType::OctreeDataNack);
QUuid nodeUUID = node->getUUID();
// if there are octree packets from this node that are waiting to be processed,
@ -2698,18 +2700,19 @@ int Application::sendNackPackets() {
auto it = missingSequenceNumbers.constBegin();
while (it != missingSequenceNumbers.constEnd()) {
OCTREE_PACKET_SEQUENCE missingNumber = *it;
nackPacketList->write(&missingNumber, sizeof(OCTREE_PACKET_SEQUENCE));
nackPacketList->writePrimitive(missingNumber);
++it;
}
if (nackPacketList.getNumPackets()) {
packetsSent += nackPacketList.getNumPackets();
// send the packet list
nodeList->sendPacketList(nackPacketList, node);
}
}
});
int packetsSent = nackPacketList.getNumPackets();
if (packetsSent) {
// send the packet list
nodeList->sendPacketList(nackPacketList, node);
}
return packetsSent;
}

View file

@ -587,10 +587,12 @@ void ScriptEngine::run() {
/ (1000 * 1000)) + 0.5);
const int SCRIPT_AUDIO_BUFFER_BYTES = SCRIPT_AUDIO_BUFFER_SAMPLES * sizeof(int16_t);
QByteArray avatarPacket = nodeList->byteArrayWithPopulatedHeader(PacketType::AvatarData);
avatarPacket.append(_avatarData->toByteArray());
QByteArray avatarByteArray = _avatarData->toByteArray();
auto avatarPacket = NLPacket::create(PacketType::AvatarData);
nodeList->broadcastToNodes(avatarPacket, NodeSet() << NodeType::AvatarMixer);
avatarPacket->write(avatarByteArray);
nodeList->broadcastToNodes(std::move(avatarPacket), NodeSet() << NodeType::AvatarMixer);
if (_isListeningToAudioStream || _avatarSound) {
// if we have an avatar audio stream then send it out to our audio-mixer
@ -628,15 +630,12 @@ void ScriptEngine::run() {
}
}
QByteArray audioPacket = nodeList->byteArrayWithPopulatedHeader(silentFrame
auto audioPacket = NLPacket::create(silentFrame
? PacketType::SilentAudioFrame
: PacketType::MicrophoneAudioNoEcho);
QDataStream packetStream(&audioPacket, QIODevice::Append);
// pack a placeholder value for sequence number for now, will be packed when destination node is known
int numPreSequenceNumberBytes = audioPacket.size();
packetStream << (quint16) 0;
// seek past the sequence number, will be packed when destination node is known
audioPacket->seek(sizeof(quint16));
if (silentFrame) {
if (!_isListeningToAudioStream) {
@ -645,37 +644,38 @@ void ScriptEngine::run() {
}
// write the number of silent samples so the audio-mixer can uphold timing
packetStream.writeRawData(reinterpret_cast<const char*>(&SCRIPT_AUDIO_BUFFER_SAMPLES), sizeof(int16_t));
audioPacket->writePrimitive(SCRIPT_AUDIO_BUFFER_SAMPLES);
// use the orientation and position of this avatar for the source of this audio
packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
audioPacket->writePrimitive(_avatarData->getPosition());
glm::quat headOrientation = _avatarData->getHeadOrientation();
packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
audioPacket->writePrimitive(headOrientation);
} else if (nextSoundOutput) {
// assume scripted avatar audio is mono and set channel flag to zero
packetStream << (quint8)0;
audioPacket->writePrimitive((quint8) 0);
// use the orientation and position of this avatar for the source of this audio
packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
audioPacket->writePrimitive(_avatarData->getPosition());
glm::quat headOrientation = _avatarData->getHeadOrientation();
packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
audioPacket->writePrimitive(headOrientation);
// write the raw audio data
packetStream.writeRawData(reinterpret_cast<const char*>(nextSoundOutput), numAvailableSamples * sizeof(int16_t));
audioPacket->write(reinterpret_cast<const char*>(nextSoundOutput), numAvailableSamples * sizeof(int16_t));
}
// write audio packet to AudioMixer nodes
auto nodeList = DependencyManager::get<NodeList>();
nodeList->eachNode([this, &nodeList, &audioPacket, &numPreSequenceNumberBytes](const SharedNodePointer& node){
nodeList->eachNode([this, &nodeList, &audioPacket](const SharedNodePointer& node){
// only send to nodes of type AudioMixer
if (node->getType() == NodeType::AudioMixer) {
// pack sequence number
quint16 sequence = _outgoingScriptAudioSequenceNumbers[node->getUUID()]++;
memcpy(audioPacket.data() + numPreSequenceNumberBytes, &sequence, sizeof(quint16));
audioPacket->seek(0);
audioPacket->writePrimitive(sequence);
// send audio packet
nodeList->writeDatagram(audioPacket, node);
nodeList->sendPacket(std::move(audioPacket), node);
}
});
}