History

added seq number to all Audio types, untested

wangyix committed 12 years ago

Commit cb488251 parent 97139b014 files · +117 −29Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 14 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+38−11
@@ -459,32 +459,55 @@ void ScriptEngine::run() { · 458 unmodified lines
459459 }
460460 }
461461
462− QByteArray audioPacket = byteArrayWithPopulatedHeader(silentFrame
463− ? PacketTypeSilentAudioFrame
464− : PacketTypeMicrophoneAudioNoEcho);
462+ char audioPacket[MAX_PACKET_SIZE];
465463
466− QDataStream packetStream(&audioPacket, QIODevice::Append);
464+ // pack header
465+ int numBytesPacketHeader = populatePacketHeader(audioPacket, silentFrame
466+ ? PacketTypeSilentAudioFrame
467+ : PacketTypeMicrophoneAudioNoEcho);
468+ char* dataAt = audioPacket + numBytesPacketHeader;
469+
470+ // skip over sequence number for now; will be packed when destination node is known
471+ char* sequenceAt = dataAt;
472+ dataAt += sizeof(quint16);
467473
468474 // use the orientation and position of this avatar for the source of this audio
469− packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
475+ memcpy(dataAt, &_avatarData->getPosition(), sizeof(glm::vec3));
476+ dataAt += sizeof(glm::vec3);
477+
470478 glm::quat headOrientation = _avatarData->getHeadOrientation();
471− packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
479+ memcpy(dataAt, &headOrientation, sizeof(glm::quat));
480+ dataAt += sizeof(glm::quat);
472481
473482 if (silentFrame) {
474483 if (!_isListeningToAudioStream) {
475484 // if we have a silent frame and we're not listening then just send nothing and break out of here
476485 break;
477486 }
478−
479487 // write the number of silent samples so the audio-mixer can uphold timing
480− packetStream.writeRawData(reinterpret_cast<const char*>(&SCRIPT_AUDIO_BUFFER_SAMPLES), sizeof(int16_t));
488+ memcpy(dataAt, &SCRIPT_AUDIO_BUFFER_SAMPLES, sizeof(int16_t));
489+ dataAt += sizeof(int16_t);
481490 } else if (nextSoundOutput) {
482491 // write the raw audio data
483− packetStream.writeRawData(reinterpret_cast<const char*>(nextSoundOutput),
484− numAvailableSamples * sizeof(int16_t));
492+ int numAvailableBytes = numAvailableSamples * sizeof(int16_t);
493+ memcpy(dataAt, nextSoundOutput, numAvailableBytes);
494+ dataAt += numAvailableBytes;
485495 }
486496
487− nodeList->broadcastToNodes(audioPacket, NodeSet() << NodeType::AudioMixer);
497+ // write audio packet to AudioMixer nodes
498+ int audioPacketSize = dataAt - audioPacket;
499+ NodeList* nodeList = NodeList::getInstance();
500+ foreach(const SharedNodePointer& node, nodeList->getNodeHash()) {
501+ // only send to nodes of type AudioMixer
502+ if (node->getType() == NodeType::AudioMixer) {
503+ // pack sequence number
504+ quint16 sequence = _outgoingSequenceNumbers[node->getUUID()]++;
505+ memcpy(sequenceAt, &sequence, sizeof(quint16));
506+
507+ // send audio packet
508+ nodeList->writeDatagram(audioPacket, audioPacketSize, node);
509+ }
510+ }
488511 }
489512 }
490513
@@ -658,3 +681,7 @@ void ScriptEngine::include(const QString& includeFile) { · 167 unmodified lines
658681 emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString());
659682 }
660683 }
684+
685+void ScriptEngine::nodeKilled(SharedNodePointer node) {
686+ _outgoingSequenceNumbers.remove(node->getUUID());
687+}