mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 17:00:13 +02:00
add some debug for audio injector wrap case
This commit is contained in:
parent
81b04f2e75
commit
b6f478e087
1 changed files with 60 additions and 55 deletions
|
@ -36,7 +36,7 @@ AudioInjector::AudioInjector(Sound* sound, const AudioInjectorOptions& injectorO
|
||||||
_audioData(sound->getByteArray()),
|
_audioData(sound->getByteArray()),
|
||||||
_options(injectorOptions)
|
_options(injectorOptions)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioInjector::AudioInjector(const QByteArray& audioData, const AudioInjectorOptions& injectorOptions) :
|
AudioInjector::AudioInjector(const QByteArray& audioData, const AudioInjectorOptions& injectorOptions) :
|
||||||
|
@ -49,15 +49,15 @@ AudioInjector::AudioInjector(const QByteArray& audioData, const AudioInjectorOpt
|
||||||
void AudioInjector::finish() {
|
void AudioInjector::finish() {
|
||||||
bool shouldDelete = (_state == State::NotFinishedWithPendingDelete);
|
bool shouldDelete = (_state == State::NotFinishedWithPendingDelete);
|
||||||
_state = State::Finished;
|
_state = State::Finished;
|
||||||
|
|
||||||
emit finished();
|
emit finished();
|
||||||
|
|
||||||
if (_localBuffer) {
|
if (_localBuffer) {
|
||||||
_localBuffer->stop();
|
_localBuffer->stop();
|
||||||
_localBuffer->deleteLater();
|
_localBuffer->deleteLater();
|
||||||
_localBuffer = NULL;
|
_localBuffer = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldDelete) {
|
if (shouldDelete) {
|
||||||
// we've been asked to delete after finishing, trigger a deleteLater here
|
// we've been asked to delete after finishing, trigger a deleteLater here
|
||||||
deleteLater();
|
deleteLater();
|
||||||
|
@ -67,7 +67,7 @@ void AudioInjector::finish() {
|
||||||
void AudioInjector::setupInjection() {
|
void AudioInjector::setupInjection() {
|
||||||
if (!_hasSetup) {
|
if (!_hasSetup) {
|
||||||
_hasSetup = true;
|
_hasSetup = true;
|
||||||
|
|
||||||
// check if we need to offset the sound by some number of seconds
|
// check if we need to offset the sound by some number of seconds
|
||||||
if (_options.secondOffset > 0.0f) {
|
if (_options.secondOffset > 0.0f) {
|
||||||
|
|
||||||
|
@ -87,31 +87,31 @@ void AudioInjector::setupInjection() {
|
||||||
void AudioInjector::restart() {
|
void AudioInjector::restart() {
|
||||||
// grab the AudioInjectorManager
|
// grab the AudioInjectorManager
|
||||||
auto injectorManager = DependencyManager::get<AudioInjectorManager>();
|
auto injectorManager = DependencyManager::get<AudioInjectorManager>();
|
||||||
|
|
||||||
if (thread() != QThread::currentThread()) {
|
if (thread() != QThread::currentThread()) {
|
||||||
QMetaObject::invokeMethod(this, "restart");
|
QMetaObject::invokeMethod(this, "restart");
|
||||||
|
|
||||||
if (!_options.localOnly) {
|
if (!_options.localOnly) {
|
||||||
// notify the AudioInjectorManager to wake up in case it's waiting for new injectors
|
// notify the AudioInjectorManager to wake up in case it's waiting for new injectors
|
||||||
injectorManager->notifyInjectorReadyCondition();
|
injectorManager->notifyInjectorReadyCondition();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset the current send offset to zero
|
// reset the current send offset to zero
|
||||||
_currentSendOffset = 0;
|
_currentSendOffset = 0;
|
||||||
|
|
||||||
// check our state to decide if we need extra handling for the restart request
|
// check our state to decide if we need extra handling for the restart request
|
||||||
if (_state == State::Finished) {
|
if (_state == State::Finished) {
|
||||||
// we finished playing, need to reset state so we can get going again
|
// we finished playing, need to reset state so we can get going again
|
||||||
_hasSetup = false;
|
_hasSetup = false;
|
||||||
_shouldStop = false;
|
_shouldStop = false;
|
||||||
_state = State::NotFinished;
|
_state = State::NotFinished;
|
||||||
|
|
||||||
// call inject audio to start injection over again
|
// call inject audio to start injection over again
|
||||||
setupInjection();
|
setupInjection();
|
||||||
|
|
||||||
// if we're a local injector call inject locally to start injecting again
|
// if we're a local injector call inject locally to start injecting again
|
||||||
if (_options.localOnly) {
|
if (_options.localOnly) {
|
||||||
injectLocally();
|
injectLocally();
|
||||||
|
@ -153,7 +153,7 @@ bool AudioInjector::injectLocally() {
|
||||||
// we never started so we are finished, call our stop method
|
// we never started so we are finished, call our stop method
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,85 +162,85 @@ static const uint64_t NEXT_FRAME_DELTA_ERROR_OR_FINISHED = 0;
|
||||||
static const uint64_t NEXT_FRAME_DELTA_IMMEDIATELY = 1;
|
static const uint64_t NEXT_FRAME_DELTA_IMMEDIATELY = 1;
|
||||||
|
|
||||||
uint64_t AudioInjector::injectNextFrame() {
|
uint64_t AudioInjector::injectNextFrame() {
|
||||||
|
|
||||||
if (_state == AudioInjector::State::Finished) {
|
if (_state == AudioInjector::State::Finished) {
|
||||||
qDebug() << "AudioInjector::injectNextFrame called but AudioInjector has finished and was not restarted. Returning.";
|
qDebug() << "AudioInjector::injectNextFrame called but AudioInjector has finished and was not restarted. Returning.";
|
||||||
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we haven't setup the packet to send then do so now
|
// if we haven't setup the packet to send then do so now
|
||||||
static int positionOptionOffset = -1;
|
static int positionOptionOffset = -1;
|
||||||
static int volumeOptionOffset = -1;
|
static int volumeOptionOffset = -1;
|
||||||
static int audioDataOffset = -1;
|
static int audioDataOffset = -1;
|
||||||
|
|
||||||
if (!_currentPacket) {
|
if (!_currentPacket) {
|
||||||
if (_currentSendOffset < 0 ||
|
if (_currentSendOffset < 0 ||
|
||||||
_currentSendOffset >= _audioData.size()) {
|
_currentSendOffset >= _audioData.size()) {
|
||||||
_currentSendOffset = 0;
|
_currentSendOffset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure we actually have samples downloaded to inject
|
// make sure we actually have samples downloaded to inject
|
||||||
if (_audioData.size()) {
|
if (_audioData.size()) {
|
||||||
|
|
||||||
_outgoingSequenceNumber = 0;
|
_outgoingSequenceNumber = 0;
|
||||||
_nextFrame = 0;
|
_nextFrame = 0;
|
||||||
|
|
||||||
if (!_frameTimer) {
|
if (!_frameTimer) {
|
||||||
_frameTimer = std::unique_ptr<QElapsedTimer>(new QElapsedTimer);
|
_frameTimer = std::unique_ptr<QElapsedTimer>(new QElapsedTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
_frameTimer->restart();
|
_frameTimer->restart();
|
||||||
|
|
||||||
_currentPacket = NLPacket::create(PacketType::InjectAudio);
|
_currentPacket = NLPacket::create(PacketType::InjectAudio);
|
||||||
|
|
||||||
// setup the packet for injected audio
|
// setup the packet for injected audio
|
||||||
QDataStream audioPacketStream(_currentPacket.get());
|
QDataStream audioPacketStream(_currentPacket.get());
|
||||||
|
|
||||||
// pack some placeholder sequence number for now
|
// pack some placeholder sequence number for now
|
||||||
audioPacketStream << (quint16) 0;
|
audioPacketStream << (quint16) 0;
|
||||||
|
|
||||||
// pack stream identifier (a generated UUID)
|
// pack stream identifier (a generated UUID)
|
||||||
audioPacketStream << QUuid::createUuid();
|
audioPacketStream << QUuid::createUuid();
|
||||||
|
|
||||||
// pack the stereo/mono type of the stream
|
// pack the stereo/mono type of the stream
|
||||||
audioPacketStream << _options.stereo;
|
audioPacketStream << _options.stereo;
|
||||||
|
|
||||||
// pack the flag for loopback
|
// pack the flag for loopback
|
||||||
uchar loopbackFlag = (uchar) true;
|
uchar loopbackFlag = (uchar) true;
|
||||||
audioPacketStream << loopbackFlag;
|
audioPacketStream << loopbackFlag;
|
||||||
|
|
||||||
// pack the position for injected audio
|
// pack the position for injected audio
|
||||||
positionOptionOffset = _currentPacket->pos();
|
positionOptionOffset = _currentPacket->pos();
|
||||||
audioPacketStream.writeRawData(reinterpret_cast<const char*>(&_options.position),
|
audioPacketStream.writeRawData(reinterpret_cast<const char*>(&_options.position),
|
||||||
sizeof(_options.position));
|
sizeof(_options.position));
|
||||||
|
|
||||||
// pack our orientation for injected audio
|
// pack our orientation for injected audio
|
||||||
audioPacketStream.writeRawData(reinterpret_cast<const char*>(&_options.orientation),
|
audioPacketStream.writeRawData(reinterpret_cast<const char*>(&_options.orientation),
|
||||||
sizeof(_options.orientation));
|
sizeof(_options.orientation));
|
||||||
|
|
||||||
// pack zero for radius
|
// pack zero for radius
|
||||||
float radius = 0;
|
float radius = 0;
|
||||||
audioPacketStream << radius;
|
audioPacketStream << radius;
|
||||||
|
|
||||||
// pack 255 for attenuation byte
|
// pack 255 for attenuation byte
|
||||||
volumeOptionOffset = _currentPacket->pos();
|
volumeOptionOffset = _currentPacket->pos();
|
||||||
quint8 volume = MAX_INJECTOR_VOLUME;
|
quint8 volume = MAX_INJECTOR_VOLUME;
|
||||||
audioPacketStream << volume;
|
audioPacketStream << volume;
|
||||||
|
|
||||||
audioPacketStream << _options.ignorePenumbra;
|
audioPacketStream << _options.ignorePenumbra;
|
||||||
|
|
||||||
audioDataOffset = _currentPacket->pos();
|
audioDataOffset = _currentPacket->pos();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// no samples to inject, return immediately
|
// no samples to inject, return immediately
|
||||||
qDebug() << "AudioInjector::injectNextFrame() called with no samples to inject. Returning.";
|
qDebug() << "AudioInjector::injectNextFrame() called with no samples to inject. Returning.";
|
||||||
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int bytesToCopy = std::min((_options.stereo ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL,
|
int bytesToCopy = std::min((_options.stereo ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL,
|
||||||
_audioData.size() - _currentSendOffset);
|
_audioData.size() - _currentSendOffset);
|
||||||
|
|
||||||
// Measure the loudness of this frame
|
// Measure the loudness of this frame
|
||||||
_loudness = 0.0f;
|
_loudness = 0.0f;
|
||||||
for (int i = 0; i < bytesToCopy; i += sizeof(int16_t)) {
|
for (int i = 0; i < bytesToCopy; i += sizeof(int16_t)) {
|
||||||
|
@ -248,40 +248,40 @@ uint64_t AudioInjector::injectNextFrame() {
|
||||||
(AudioConstants::MAX_SAMPLE_VALUE / 2.0f);
|
(AudioConstants::MAX_SAMPLE_VALUE / 2.0f);
|
||||||
}
|
}
|
||||||
_loudness /= (float)(bytesToCopy / sizeof(int16_t));
|
_loudness /= (float)(bytesToCopy / sizeof(int16_t));
|
||||||
|
|
||||||
_currentPacket->seek(0);
|
_currentPacket->seek(0);
|
||||||
|
|
||||||
// pack the sequence number
|
// pack the sequence number
|
||||||
_currentPacket->writePrimitive(_outgoingSequenceNumber);
|
_currentPacket->writePrimitive(_outgoingSequenceNumber);
|
||||||
|
|
||||||
_currentPacket->seek(positionOptionOffset);
|
_currentPacket->seek(positionOptionOffset);
|
||||||
_currentPacket->writePrimitive(_options.position);
|
_currentPacket->writePrimitive(_options.position);
|
||||||
_currentPacket->writePrimitive(_options.orientation);
|
_currentPacket->writePrimitive(_options.orientation);
|
||||||
|
|
||||||
quint8 volume = MAX_INJECTOR_VOLUME * _options.volume;
|
quint8 volume = MAX_INJECTOR_VOLUME * _options.volume;
|
||||||
_currentPacket->seek(volumeOptionOffset);
|
_currentPacket->seek(volumeOptionOffset);
|
||||||
_currentPacket->writePrimitive(volume);
|
_currentPacket->writePrimitive(volume);
|
||||||
|
|
||||||
_currentPacket->seek(audioDataOffset);
|
_currentPacket->seek(audioDataOffset);
|
||||||
|
|
||||||
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
||||||
_currentPacket->write(_audioData.data() + _currentSendOffset, bytesToCopy);
|
_currentPacket->write(_audioData.data() + _currentSendOffset, bytesToCopy);
|
||||||
|
|
||||||
// set the correct size used for this packet
|
// set the correct size used for this packet
|
||||||
_currentPacket->setPayloadSize(_currentPacket->pos());
|
_currentPacket->setPayloadSize(_currentPacket->pos());
|
||||||
|
|
||||||
// grab our audio mixer from the NodeList, if it exists
|
// grab our audio mixer from the NodeList, if it exists
|
||||||
auto nodeList = DependencyManager::get<NodeList>();
|
auto nodeList = DependencyManager::get<NodeList>();
|
||||||
SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer);
|
SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer);
|
||||||
|
|
||||||
if (audioMixer) {
|
if (audioMixer) {
|
||||||
// send off this audio packet
|
// send off this audio packet
|
||||||
nodeList->sendUnreliablePacket(*_currentPacket, *audioMixer);
|
nodeList->sendUnreliablePacket(*_currentPacket, *audioMixer);
|
||||||
_outgoingSequenceNumber++;
|
_outgoingSequenceNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
_currentSendOffset += bytesToCopy;
|
_currentSendOffset += bytesToCopy;
|
||||||
|
|
||||||
if (_currentSendOffset >= _audioData.size()) {
|
if (_currentSendOffset >= _audioData.size()) {
|
||||||
// we're at the end of the audio data to send
|
// we're at the end of the audio data to send
|
||||||
if (_options.loop) {
|
if (_options.loop) {
|
||||||
|
@ -293,15 +293,20 @@ uint64_t AudioInjector::injectNextFrame() {
|
||||||
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_currentSendOffset == bytesToCopy) {
|
if (_currentSendOffset == bytesToCopy) {
|
||||||
// ask AudioInjectorManager to call us right away again to
|
// ask AudioInjectorManager to call us right away again to
|
||||||
// immediately send the first two frames so the mixer can start using the audio right away
|
// immediately send the first two frames so the mixer can start using the audio right away
|
||||||
return NEXT_FRAME_DELTA_IMMEDIATELY;
|
return NEXT_FRAME_DELTA_IMMEDIATELY;
|
||||||
} else {
|
} else {
|
||||||
return (++_nextFrame * AudioConstants::NETWORK_FRAME_USECS) - _frameTimer->nsecsElapsed() / 1000;
|
uint64_t untilNextFrame = (++_nextFrame * AudioConstants::NETWORK_FRAME_USECS) - _frameTimer->nsecsElapsed() / 1000;
|
||||||
|
if (untilNextFrame >= 10 * AudioConstants::NETWORK_FRAME_USECS) {
|
||||||
|
qDebug() << "AudioInjector returning delta to next frame that is very large -" << untilNextFrame;
|
||||||
|
qDebug() << "Next Frame is " << _nextFrame << "frameTimer nsecsElapsed is" << _frameTimer->nsecsElapsed();
|
||||||
|
}
|
||||||
|
return untilNextFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioInjector::stop() {
|
void AudioInjector::stop() {
|
||||||
|
@ -315,7 +320,7 @@ void AudioInjector::triggerDeleteAfterFinish() {
|
||||||
QMetaObject::invokeMethod(this, "triggerDeleteAfterFinish", Qt::QueuedConnection);
|
QMetaObject::invokeMethod(this, "triggerDeleteAfterFinish", Qt::QueuedConnection);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_state == State::Finished) {
|
if (_state == State::Finished) {
|
||||||
stopAndDeleteLater();
|
stopAndDeleteLater();
|
||||||
} else {
|
} else {
|
||||||
|
@ -371,11 +376,11 @@ AudioInjector* AudioInjector::playSound(const QString& soundUrl, const float vol
|
||||||
|
|
||||||
AudioInjector* AudioInjector::playSoundAndDelete(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface) {
|
AudioInjector* AudioInjector::playSoundAndDelete(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface) {
|
||||||
AudioInjector* sound = playSound(buffer, options, localInterface);
|
AudioInjector* sound = playSound(buffer, options, localInterface);
|
||||||
|
|
||||||
if (sound) {
|
if (sound) {
|
||||||
sound->_state = AudioInjector::State::NotFinishedWithPendingDelete;
|
sound->_state = AudioInjector::State::NotFinishedWithPendingDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sound;
|
return sound;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -383,13 +388,13 @@ AudioInjector* AudioInjector::playSoundAndDelete(const QByteArray& buffer, const
|
||||||
AudioInjector* AudioInjector::playSound(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface) {
|
AudioInjector* AudioInjector::playSound(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface) {
|
||||||
AudioInjector* injector = new AudioInjector(buffer, options);
|
AudioInjector* injector = new AudioInjector(buffer, options);
|
||||||
injector->setLocalAudioInterface(localInterface);
|
injector->setLocalAudioInterface(localInterface);
|
||||||
|
|
||||||
// grab the AudioInjectorManager
|
// grab the AudioInjectorManager
|
||||||
auto injectorManager = DependencyManager::get<AudioInjectorManager>();
|
auto injectorManager = DependencyManager::get<AudioInjectorManager>();
|
||||||
|
|
||||||
// setup parameters required for injection
|
// setup parameters required for injection
|
||||||
injector->setupInjection();
|
injector->setupInjection();
|
||||||
|
|
||||||
if (options.localOnly) {
|
if (options.localOnly) {
|
||||||
if (injector->injectLocally()) {
|
if (injector->injectLocally()) {
|
||||||
// local injection succeeded, return the pointer to injector
|
// local injection succeeded, return the pointer to injector
|
||||||
|
|
Loading…
Reference in a new issue