History

changed class name to AudioStreamStats, reverted ScriptEngine packing back to QDataStream

wangyix committed 12 years ago

Commit de3c1eb1 parent c3f57177 files · +34 −41Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+17−24
@@ -458,54 +458,47 @@ void ScriptEngine::run() { · 457 unmodified lines
458458 _numAvatarSoundSentBytes = 0;
459459 }
460460 }
461+
462+ QByteArray audioPacket = byteArrayWithPopulatedHeader(silentFrame
463+ ? PacketTypeSilentAudioFrame
464+ : PacketTypeMicrophoneAudioNoEcho);
461465
462− char audioPacket[MAX_PACKET_SIZE];
466+ QDataStream packetStream(&audioPacket, QIODevice::Append);
463467
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;
473471
474472 // 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));
478474 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));
481476
482477 if (silentFrame) {
483478 if (!_isListeningToAudioStream) {
484479 // if we have a silent frame and we're not listening then just send nothing and break out of here
485480 break;
486481 }
482+
487483 // 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));
490485 } else if (nextSoundOutput) {
491486 // 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));
495489 }
496490
497491 // write audio packet to AudioMixer nodes
498− int audioPacketSize = dataAt - audioPacket;
499492 NodeList* nodeList = NodeList::getInstance();
500493 foreach(const SharedNodePointer& node, nodeList->getNodeHash()) {
501494 // only send to nodes of type AudioMixer
502495 if (node->getType() == NodeType::AudioMixer) {
503496 // pack sequence number
504497 quint16 sequence = _outgoingScriptAudioSequenceNumbers[node->getUUID()]++;
505− memcpy(sequenceAt, &sequence, sizeof(quint16));
506−
498+ memcpy(audioPacket.data() + numPreSequenceNumberBytes, &sequence, sizeof(quint16));
499+
507500 // send audio packet
508− nodeList->writeDatagram(audioPacket, audioPacketSize, node);
501+ nodeList->writeDatagram(audioPacket, node);
509502 }
510503 }
511504 }