Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 7 files). Show all
Unified · Split
| @@ -458,54 +458,47 @@ void ScriptEngine::run() { · 457 unmodified lines | ||
| 458 | 458 | _numAvatarSoundSentBytes = 0; |
| 459 | 459 | } |
| 460 | 460 | } |
| 461 | + | |
| 462 | + QByteArray audioPacket = byteArrayWithPopulatedHeader(silentFrame | |
| 463 | + ? PacketTypeSilentAudioFrame | |
| 464 | + : PacketTypeMicrophoneAudioNoEcho); | |
| 461 | 465 | |
| 462 | − char audioPacket[MAX_PACKET_SIZE]; | |
| 466 | + QDataStream packetStream(&audioPacket, QIODevice::Append); | |
| 463 | 467 | |
| 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); | |
| 468 | + // pack a placeholder value for sequence number for now, will be packed when destination node is known | |
| 469 | + int numPreSequenceNumberBytes = audioPacket.size(); | |
| 470 | + packetStream << (quint16)0; | |
| 473 | 471 | |
| 474 | 472 | // use the orientation and position of this avatar for the source of this audio |
| 475 | − memcpy(dataAt, &_avatarData->getPosition(), sizeof(glm::vec3)); | |
| 476 | − dataAt += sizeof(glm::vec3); | |
| 477 | − | |
| 473 | + packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3)); | |
| 478 | 474 | glm::quat headOrientation = _avatarData->getHeadOrientation(); |
| 479 | − memcpy(dataAt, &headOrientation, sizeof(glm::quat)); | |
| 480 | − dataAt += sizeof(glm::quat); | |
| 475 | + packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat)); | |
| 481 | 476 | |
| 482 | 477 | if (silentFrame) { |
| 483 | 478 | if (!_isListeningToAudioStream) { |
| 484 | 479 | // if we have a silent frame and we're not listening then just send nothing and break out of here |
| 485 | 480 | break; |
| 486 | 481 | } |
| 482 | + | |
| 487 | 483 | // write the number of silent samples so the audio-mixer can uphold timing |
| 488 | − memcpy(dataAt, &SCRIPT_AUDIO_BUFFER_SAMPLES, sizeof(int16_t)); | |
| 489 | − dataAt += sizeof(int16_t); | |
| 484 | + packetStream.writeRawData(reinterpret_cast<const char*>(&SCRIPT_AUDIO_BUFFER_SAMPLES), sizeof(int16_t)); | |
| 490 | 485 | } else if (nextSoundOutput) { |
| 491 | 486 | // write the raw audio data |
| 492 | − int numAvailableBytes = numAvailableSamples * sizeof(int16_t); | |
| 493 | − memcpy(dataAt, nextSoundOutput, numAvailableBytes); | |
| 494 | − dataAt += numAvailableBytes; | |
| 487 | + packetStream.writeRawData(reinterpret_cast<const char*>(nextSoundOutput), | |
| 488 | + numAvailableSamples * sizeof(int16_t)); | |
| 495 | 489 | } |
| 496 | 490 | |
| 497 | 491 | // write audio packet to AudioMixer nodes |
| 498 | − int audioPacketSize = dataAt - audioPacket; | |
| 499 | 492 | NodeList* nodeList = NodeList::getInstance(); |
| 500 | 493 | foreach(const SharedNodePointer& node, nodeList->getNodeHash()) { |
| 501 | 494 | // only send to nodes of type AudioMixer |
| 502 | 495 | if (node->getType() == NodeType::AudioMixer) { |
| 503 | 496 | // pack sequence number |
| 504 | 497 | quint16 sequence = _outgoingScriptAudioSequenceNumbers[node->getUUID()]++; |
| 505 | − memcpy(sequenceAt, &sequence, sizeof(quint16)); | |
| 506 | − | |
| 498 | + memcpy(audioPacket.data() + numPreSequenceNumberBytes, &sequence, sizeof(quint16)); | |
| 499 | + | |
| 507 | 500 | // send audio packet |
| 508 | − nodeList->writeDatagram(audioPacket, audioPacketSize, node); | |
| 501 | + nodeList->writeDatagram(audioPacket, node); | |
| 509 | 502 | } |
| 510 | 503 | } |
| 511 | 504 | } |