update signature of callbacks for avatar packets

This commit is contained in:
Stephen Birarda 2015-07-13 13:32:46 -07:00
parent d990420565
commit 859122db5d
12 changed files with 53 additions and 72 deletions

View file

@ -988,7 +988,7 @@ void Avatar::setBillboard(const QByteArray& billboard) {
_billboardTexture.reset(); _billboardTexture.reset();
} }
int Avatar::parseDataAtOffset(const QByteArray& packet, int offset) { int Avatar::parseDataFromBuffer(const QByteArray& buffer) {
if (!_initialized) { if (!_initialized) {
// now that we have data for this Avatar we are go for init // now that we have data for this Avatar we are go for init
init(); init();
@ -997,7 +997,7 @@ int Avatar::parseDataAtOffset(const QByteArray& packet, int offset) {
// change in position implies movement // change in position implies movement
glm::vec3 oldPosition = _position; glm::vec3 oldPosition = _position;
int bytesRead = AvatarData::parseDataAtOffset(packet, offset); int bytesRead = AvatarData::parseDataFromBuffer(buffer);
const float MOVE_DISTANCE_THRESHOLD = 0.001f; const float MOVE_DISTANCE_THRESHOLD = 0.001f;
_moving = glm::distance(oldPosition, _position) > MOVE_DISTANCE_THRESHOLD; _moving = glm::distance(oldPosition, _position) > MOVE_DISTANCE_THRESHOLD;
@ -1123,7 +1123,7 @@ void Avatar::setShowDisplayName(bool showDisplayName) {
} }
} }
// virtual // virtual
void Avatar::computeShapeInfo(ShapeInfo& shapeInfo) { void Avatar::computeShapeInfo(ShapeInfo& shapeInfo) {
const CapsuleShape& capsule = _skeletonModel.getBoundingShape(); const CapsuleShape& capsule = _skeletonModel.getBoundingShape();
shapeInfo.setCapsuleY(capsule.getRadius(), capsule.getHalfHeight()); shapeInfo.setCapsuleY(capsule.getRadius(), capsule.getHalfHeight());

View file

@ -85,10 +85,10 @@ public:
virtual void render(RenderArgs* renderArgs, const glm::vec3& cameraPosition, virtual void render(RenderArgs* renderArgs, const glm::vec3& cameraPosition,
bool postLighting = false); bool postLighting = false);
bool addToScene(AvatarSharedPointer self, std::shared_ptr<render::Scene> scene, bool addToScene(AvatarSharedPointer self, std::shared_ptr<render::Scene> scene,
render::PendingChanges& pendingChanges); render::PendingChanges& pendingChanges);
void removeFromScene(AvatarSharedPointer self, std::shared_ptr<render::Scene> scene, void removeFromScene(AvatarSharedPointer self, std::shared_ptr<render::Scene> scene,
render::PendingChanges& pendingChanges); render::PendingChanges& pendingChanges);
//setters //setters
@ -146,7 +146,7 @@ public:
void setShowDisplayName(bool showDisplayName); void setShowDisplayName(bool showDisplayName);
virtual int parseDataAtOffset(const QByteArray& packet, int offset); virtual int parseDataFromBuffer(const QByteArray& buffer);
static void renderJointConnectingCone( gpu::Batch& batch, glm::vec3 position1, glm::vec3 position2, static void renderJointConnectingCone( gpu::Batch& batch, glm::vec3 position1, glm::vec3 position2,
float radius1, float radius2, const glm::vec4& color); float radius1, float radius2, const glm::vec4& color);
@ -163,7 +163,7 @@ public:
Q_INVOKABLE glm::quat getJointCombinedRotation(const QString& name) const; Q_INVOKABLE glm::quat getJointCombinedRotation(const QString& name) const;
Q_INVOKABLE void setJointModelPositionAndOrientation(int index, const glm::vec3 position, const glm::quat& rotation); Q_INVOKABLE void setJointModelPositionAndOrientation(int index, const glm::vec3 position, const glm::quat& rotation);
Q_INVOKABLE void setJointModelPositionAndOrientation(const QString& name, const glm::vec3 position, Q_INVOKABLE void setJointModelPositionAndOrientation(const QString& name, const glm::vec3 position,
const glm::quat& rotation); const glm::quat& rotation);
Q_INVOKABLE glm::vec3 getNeckPosition() const; Q_INVOKABLE glm::vec3 getNeckPosition() const;
@ -179,8 +179,8 @@ public:
void slamPosition(const glm::vec3& position); void slamPosition(const glm::vec3& position);
// Call this when updating Avatar position with a delta. This will allow us to // Call this when updating Avatar position with a delta. This will allow us to
// _accurately_ measure position changes and compute the resulting velocity // _accurately_ measure position changes and compute the resulting velocity
// (otherwise floating point error will cause problems at large positions). // (otherwise floating point error will cause problems at large positions).
void applyPositionDelta(const glm::vec3& delta); void applyPositionDelta(const glm::vec3& delta);
@ -203,9 +203,9 @@ protected:
// These position histories and derivatives are in the world-frame. // These position histories and derivatives are in the world-frame.
// The derivatives are the MEASURED results of all external and internal forces // The derivatives are the MEASURED results of all external and internal forces
// and are therefore READ-ONLY --> motion control of the Avatar is NOT obtained // and are therefore READ-ONLY --> motion control of the Avatar is NOT obtained
// by setting these values. // by setting these values.
// Floating point error prevents us from accurately measuring velocity using a naive approach // Floating point error prevents us from accurately measuring velocity using a naive approach
// (e.g. vel = (pos - lastPos)/dt) so instead we use _positionDeltaAccumulator. // (e.g. vel = (pos - lastPos)/dt) so instead we use _positionDeltaAccumulator.
glm::vec3 _positionDeltaAccumulator; glm::vec3 _positionDeltaAccumulator;
glm::vec3 _lastVelocity; glm::vec3 _lastVelocity;

View file

@ -56,6 +56,10 @@ static void localLightFromScriptValue(const QScriptValue& value, AvatarManager::
vec3FromScriptValue(value.property("color"), light.color); vec3FromScriptValue(value.property("color"), light.color);
} }
AvatarManager::AvatarManager() {
}
void AvatarManager::registerMetaTypes(QScriptEngine* engine) { void AvatarManager::registerMetaTypes(QScriptEngine* engine) {
qScriptRegisterMetaType(engine, localLightToScriptValue, localLightFromScriptValue); qScriptRegisterMetaType(engine, localLightToScriptValue, localLightFromScriptValue);
qScriptRegisterSequenceMetaType<QVector<AvatarManager::LocalLight> >(engine); qScriptRegisterSequenceMetaType<QVector<AvatarManager::LocalLight> >(engine);

View file

@ -29,6 +29,7 @@ class AvatarManager : public AvatarHashMap {
SINGLETON_DEPENDENCY SINGLETON_DEPENDENCY
public: public:
AvatarManager();
/// Registers the script types associated with the avatar manager. /// Registers the script types associated with the avatar manager.
static void registerMetaTypes(QScriptEngine* engine); static void registerMetaTypes(QScriptEngine* engine);

View file

@ -870,12 +870,11 @@ AttachmentData MyAvatar::loadAttachmentData(const QUrl& modelURL, const QString&
return attachment; return attachment;
} }
int MyAvatar::parseDataAtOffset(const QByteArray& packet, int offset) { int MyAvatar::parseDataFromBuffer(const QByteArray& buffer) {
qCDebug(interfaceapp) << "Error: ignoring update packet for MyAvatar" qCDebug(interfaceapp) << "Error: ignoring update packet for MyAvatar"
<< " packetLength = " << packet.size() << " packetLength = " << buffer.size()
<< " offset = " << offset;
// this packet is just bad, so we pretend that we unpacked it ALL // this packet is just bad, so we pretend that we unpacked it ALL
return packet.size() - offset; return buffer.size();
} }
void MyAvatar::sendKillAvatar() { void MyAvatar::sendKillAvatar() {

View file

@ -102,7 +102,7 @@ public:
eyeContactTarget getEyeContactTarget(); eyeContactTarget getEyeContactTarget();
virtual int parseDataAtOffset(const QByteArray& packet, int offset); virtual int parseDataFromBuffer(const QByteArray& buffer);
static void sendKillAvatar(); static void sendKillAvatar();

View file

@ -265,7 +265,7 @@ bool AvatarData::shouldLogError(const quint64& now) {
} }
// read data in packet starting at byte offset and return number of bytes parsed // read data in packet starting at byte offset and return number of bytes parsed
int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { int AvatarData::parseDataFromBuffer(const QByteArray& buffer) {
// lazily allocate memory for HeadData in case we're not an Avatar instance // lazily allocate memory for HeadData in case we're not an Avatar instance
if (!_headData) { if (!_headData) {
@ -277,7 +277,7 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) {
_handData = new HandData(this); _handData = new HandData(this);
} }
const unsigned char* startPosition = reinterpret_cast<const unsigned char*>(packet.data()) + offset; const unsigned char* startPosition = reinterpret_cast<const unsigned char*>(buffer.data());
const unsigned char* sourceBuffer = startPosition; const unsigned char* sourceBuffer = startPosition;
quint64 now = usecTimestampNow(); quint64 now = usecTimestampNow();
@ -299,7 +299,7 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) {
// = 45 bytes // = 45 bytes
int minPossibleSize = 45; int minPossibleSize = 45;
int maxAvailableSize = packet.size() - offset; int maxAvailableSize = buffer.size();
if (minPossibleSize > maxAvailableSize) { if (minPossibleSize > maxAvailableSize) {
if (shouldLogError(now)) { if (shouldLogError(now)) {
qCDebug(avatars) << "Malformed AvatarData packet at the start; " qCDebug(avatars) << "Malformed AvatarData packet at the start; "

View file

@ -66,12 +66,12 @@ typedef QHash<QUuid, AvatarSharedPointer> AvatarHash;
const quint32 AVATAR_MOTION_KEYBOARD_MOTOR_ENABLED = 1U << 0; const quint32 AVATAR_MOTION_KEYBOARD_MOTOR_ENABLED = 1U << 0;
const quint32 AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED = 1U << 1; const quint32 AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED = 1U << 1;
const quint32 AVATAR_MOTION_DEFAULTS = const quint32 AVATAR_MOTION_DEFAULTS =
AVATAR_MOTION_KEYBOARD_MOTOR_ENABLED | AVATAR_MOTION_KEYBOARD_MOTOR_ENABLED |
AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED; AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED;
// these bits will be expanded as features are exposed // these bits will be expanded as features are exposed
const quint32 AVATAR_MOTION_SCRIPTABLE_BITS = const quint32 AVATAR_MOTION_SCRIPTABLE_BITS =
AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED; AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED;
const qint64 AVATAR_SILENCE_THRESHOLD_USECS = 5 * USECS_PER_SECOND; const qint64 AVATAR_SILENCE_THRESHOLD_USECS = 5 * USECS_PER_SECOND;
@ -181,7 +181,7 @@ public:
/// \param packet byte array of data /// \param packet byte array of data
/// \param offset number of bytes into packet where data starts /// \param offset number of bytes into packet where data starts
/// \return number of bytes parsed /// \return number of bytes parsed
virtual int parseDataAtOffset(const QByteArray& packet, int offset); virtual int parseDataFromBuffer(const QByteArray& buffer);
// Body Rotation (degrees) // Body Rotation (degrees)
float getBodyYaw() const { return _bodyYaw; } float getBodyYaw() const { return _bodyYaw; }
@ -241,7 +241,7 @@ public:
Q_INVOKABLE virtual void clearJointsData(); Q_INVOKABLE virtual void clearJointsData();
/// Returns the index of the joint with the specified name, or -1 if not found/unknown. /// Returns the index of the joint with the specified name, or -1 if not found/unknown.
Q_INVOKABLE virtual int getJointIndex(const QString& name) const { return _jointIndices.value(name) - 1; } Q_INVOKABLE virtual int getJointIndex(const QString& name) const { return _jointIndices.value(name) - 1; }
Q_INVOKABLE virtual QStringList getJointNames() const { return _jointNames; } Q_INVOKABLE virtual QStringList getJointNames() const { return _jointNames; }

View file

@ -52,48 +52,37 @@ AvatarSharedPointer AvatarHashMap::addAvatar(const QUuid& sessionUUID, const QWe
return avatar; return avatar;
} }
void AvatarHashMap::processAvatarDataPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr) { void AvatarHashMap::processAvatarDataPacket(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode) {
const auto data = QByteArray::fromRawData(packet->getPayload(), packet->size());
int bytesRead = 0;
SharedNodePointer avatarMixer = DependencyManager::get<NodeList>()->nodeWithUUID(packet->getSourceID());
if (avatarMixer) {
avatarMixer->setLastHeardMicrostamp(usecTimestampNow());
}
// enumerate over all of the avatars in this packet // enumerate over all of the avatars in this packet
// only add them if mixerWeakPointer points to something (meaning that mixer is still around) // only add them if mixerWeakPointer points to something (meaning that mixer is still around)
while (bytesRead < data.size() && avatarMixer.data()) { while (packet->bytesAvailable()) {
QUuid sessionUUID = QUuid::fromRfc4122(data.mid(bytesRead, NUM_BYTES_RFC4122_UUID)); QUuid sessionUUID = QUuid::fromRfc4122(packet->read(NUM_BYTES_RFC4122_UUID));
bytesRead += NUM_BYTES_RFC4122_UUID;
if (sessionUUID != _lastOwnerSessionUUID) { if (sessionUUID != _lastOwnerSessionUUID) {
AvatarSharedPointer avatar = _avatarHash.value(sessionUUID); AvatarSharedPointer avatar = _avatarHash.value(sessionUUID);
if (!avatar) { if (!avatar) {
avatar = addAvatar(sessionUUID, avatarMixer); avatar = addAvatar(sessionUUID, sendingNode);
} }
// have the matching (or new) avatar parse the data from the packet // have the matching (or new) avatar parse the data from the packet
bytesRead += avatar->parseDataAtOffset(data, bytesRead); int bytesRead = avatar->parseDataFromBuffer(QByteArray::fromRawData(packet->getPayload(), packet->pos()));
packet->seek(packet->pos() + bytesRead);
} else { } else {
// create a dummy AvatarData class to throw this data on the ground // create a dummy AvatarData class to throw this data on the ground
AvatarData dummyData; AvatarData dummyData;
bytesRead += dummyData.parseDataAtOffset(data, bytesRead); int bytesRead = dummyData.parseDataFromBuffer(QByteArray::fromRawData(packet->getPayload(), packet->pos()));
packet->seek(packet->pos() + bytesRead);
} }
} }
} }
void AvatarHashMap::processAvatarIdentityPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr) { void AvatarHashMap::processAvatarIdentityPacket(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode) {
// setup a data stream to parse the packet // setup a data stream to parse the packet
QDataStream identityStream { packet.data() }; QDataStream identityStream(packet.data());
QUuid sessionUUID; QUuid sessionUUID;
SharedNodePointer avatarMixer = DependencyManager::get<NodeList>()->nodeWithUUID(packet->getSourceID());
if (avatarMixer) {
avatarMixer->setLastHeardMicrostamp(usecTimestampNow());
}
while (!identityStream.atEnd()) { while (!identityStream.atEnd()) {
QUrl faceMeshURL, skeletonURL; QUrl faceMeshURL, skeletonURL;
@ -104,7 +93,7 @@ void AvatarHashMap::processAvatarIdentityPacket(QSharedPointer<NLPacket> packet,
// mesh URL for a UUID, find avatar in our list // mesh URL for a UUID, find avatar in our list
AvatarSharedPointer avatar = _avatarHash.value(sessionUUID); AvatarSharedPointer avatar = _avatarHash.value(sessionUUID);
if (!avatar) { if (!avatar) {
avatar = addAvatar(sessionUUID, avatarMixer); avatar = addAvatar(sessionUUID, sendingNode);
} }
if (avatar->getFaceModelURL() != faceMeshURL) { if (avatar->getFaceModelURL() != faceMeshURL) {
avatar->setFaceModelURL(faceMeshURL); avatar->setFaceModelURL(faceMeshURL);
@ -124,34 +113,23 @@ void AvatarHashMap::processAvatarIdentityPacket(QSharedPointer<NLPacket> packet,
} }
} }
void AvatarHashMap::processAvatarBillboardPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr) { void AvatarHashMap::processAvatarBillboardPacket(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode) {
const auto data = QByteArray::fromRawData(packet->getPayload(), packet->size()); QUuid sessionUUID = QUuid::fromRfc4122(packet->read(NUM_BYTES_RFC4122_UUID));
QUuid sessionUUID = QUuid::fromRfc4122(QByteArray::fromRawData(data, NUM_BYTES_RFC4122_UUID));
SharedNodePointer avatarMixer = DependencyManager::get<NodeList>()->nodeWithUUID(packet->getSourceID());
if (avatarMixer) {
avatarMixer->setLastHeardMicrostamp(usecTimestampNow());
}
AvatarSharedPointer avatar = _avatarHash.value(sessionUUID); AvatarSharedPointer avatar = _avatarHash.value(sessionUUID);
if (!avatar) { if (!avatar) {
avatar = addAvatar(sessionUUID, avatarMixer); avatar = addAvatar(sessionUUID, sendingNode);
} }
QByteArray billboard = data.mid(NUM_BYTES_RFC4122_UUID); QByteArray billboard = packet->read(packet->bytesAvailable());
if (avatar->getBillboard() != billboard) { if (avatar->getBillboard() != billboard) {
avatar->setBillboard(billboard); avatar->setBillboard(billboard);
} }
} }
void AvatarHashMap::processKillAvatar(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr) { void AvatarHashMap::processKillAvatar(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode) {
SharedNodePointer avatarMixer = DependencyManager::get<NodeList>()->nodeWithUUID(packet->getSourceID());
if (avatarMixer) {
avatarMixer->setLastHeardMicrostamp(usecTimestampNow());
}
// read the node id // read the node id
QUuid sessionUUID = QUuid::fromRfc4122(QByteArray(packet->getPayload(), NUM_BYTES_RFC4122_UUID)); QUuid sessionUUID = QUuid::fromRfc4122(packet->read(NUM_BYTES_RFC4122_UUID));
removeAvatar(sessionUUID); removeAvatar(sessionUUID);
} }

View file

@ -38,10 +38,10 @@ public slots:
private slots: private slots:
void sessionUUIDChanged(const QUuid& sessionUUID, const QUuid& oldUUID); void sessionUUIDChanged(const QUuid& sessionUUID, const QUuid& oldUUID);
void processAvatarDataPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr); void processAvatarDataPacket(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode);
void processAvatarIdentityPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr); void processAvatarIdentityPacket(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode);
void processAvatarBillboardPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr); void processAvatarBillboardPacket(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode);
void processKillAvatar(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr); void processKillAvatar(QSharedPointer<NLPacket> packet, SharedNodePointer sendingNode);
protected: protected:
AvatarHashMap(); AvatarHashMap();

View file

@ -27,7 +27,6 @@
#include <QtCore/QReadWriteLock> #include <QtCore/QReadWriteLock>
#include <QtCore/QSet> #include <QtCore/QSet>
#include <QtCore/QSharedMemory> #include <QtCore/QSharedMemory>
#include <QtCore/QSharedPointer>
#include <QtNetwork/QUdpSocket> #include <QtNetwork/QUdpSocket>
#include <QtNetwork/QHostAddress> #include <QtNetwork/QHostAddress>
@ -64,9 +63,6 @@ const QString USERNAME_UUID_REPLACEMENT_STATS_KEY = "$username";
class HifiSockAddr; class HifiSockAddr;
typedef QSharedPointer<Node> SharedNodePointer;
Q_DECLARE_METATYPE(SharedNodePointer)
using namespace tbb; using namespace tbb;
typedef std::pair<QUuid, SharedNodePointer> UUIDNodePair; typedef std::pair<QUuid, SharedNodePointer> UUIDNodePair;
typedef concurrent_unordered_map<QUuid, SharedNodePointer, UUIDHasher> NodeHash; typedef concurrent_unordered_map<QUuid, SharedNodePointer, UUIDHasher> NodeHash;

View file

@ -17,8 +17,8 @@
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QMutex> #include <QtCore/QMutex>
#include <QtCore/QSharedPointer>
#include <QtCore/QUuid> #include <QtCore/QUuid>
#include <QMutex>
#include "HifiSockAddr.h" #include "HifiSockAddr.h"
#include "NetworkPeer.h" #include "NetworkPeer.h"
@ -92,6 +92,9 @@ private:
PacketTypeSequenceMap _lastSequenceNumbers; PacketTypeSequenceMap _lastSequenceNumbers;
}; };
typedef QSharedPointer<Node> SharedNodePointer;
Q_DECLARE_METATYPE(SharedNodePointer)
QDebug operator<<(QDebug debug, const Node &message); QDebug operator<<(QDebug debug, const Node &message);
#endif // hifi_Node_h #endif // hifi_Node_h