Overte C++ Documentation
AvatarMixer.h
1 //
2 // AvatarMixer.h
3 // assignment-client/src/avatars
4 //
5 // Created by Stephen Birarda on 9/5/13.
6 // Copyright 2013 High Fidelity, Inc.
7 // Copyright 2021 Vircadia contributors.
8 //
9 // The avatar mixer receives head, hand and positional data from all connected
10 // nodes, and broadcasts that data back to them, every BROADCAST_INTERVAL ms.
11 //
12 // Distributed under the Apache License, Version 2.0.
13 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
14 //
15 
16 #ifndef hifi_AvatarMixer_h
17 #define hifi_AvatarMixer_h
18 
19 #include <QtCore/QSharedPointer>
20 
21 #include <set>
22 #include <shared/RateCounter.h>
23 #include <PortableHighResolutionClock.h>
24 
25 #include <ThreadedAssignment.h>
26 #include "../entities/EntityTreeHeadlessViewer.h"
27 #include "AvatarMixerClientData.h"
28 
29 #include "AvatarMixerSlavePool.h"
30 
32 class AvatarMixer : public ThreadedAssignment {
33  Q_OBJECT
34 public:
35  AvatarMixer(ReceivedMessage& message);
36  virtual void aboutToFinish() override;
37 
38  static bool shouldReplicateTo(const Node& from, const Node& to) {
39  return to.getType() == NodeType::DownstreamAvatarMixer &&
40  to.getPublicSocket() != from.getPublicSocket() &&
41  to.getLocalSocket() != from.getLocalSocket();
42  }
43 
44 public slots:
46  void run() override;
47 
48  void handleAvatarKilled(SharedNodePointer killedNode);
49 
50  void sendStatsPacket() override;
51 
52  // Avatar zone possibly changed
53  void entityAdded(EntityItem* entity);
54  void entityRemoved(EntityItem* entity);
55  void entityChange();
56 
57 private slots:
58  void queueIncomingPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer node);
59  void handleAdjustAvatarSorting(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
60  void handleAvatarQueryPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
61  void handleAvatarIdentityPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
62  void handleKillAvatarPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
63  void handleNodeIgnoreRequestPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
64  void handleRadiusIgnoreRequestPacket(QSharedPointer<ReceivedMessage> packet, SharedNodePointer sendingNode);
65  void handleRequestsDomainListDataPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
66  void handleReplicatedPacket(QSharedPointer<ReceivedMessage> message);
67  void handleReplicatedBulkAvatarPacket(QSharedPointer<ReceivedMessage> message);
68  void domainSettingsRequestComplete();
69  void handlePacketVersionMismatch(PacketType type, const SockAddr& senderSockAddr, const QUuid& senderUUID);
70  void handleOctreePacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
71  void start();
72 
73 private:
74  AvatarMixerClientData* getOrCreateClientData(SharedNodePointer node);
75  std::chrono::microseconds timeFrame(p_high_resolution_clock::time_point& timestamp);
76  void throttle(std::chrono::microseconds duration, int frame);
77 
78  void parseDomainServerSettings(const QJsonObject& domainSettings);
79  void sendIdentityPacket(AvatarMixerClientData* nodeData, const SharedNodePointer& destinationNode);
80 
81  void manageIdentityData(const SharedNodePointer& node);
82 
83  void optionallyReplicatePacket(ReceivedMessage& message, const Node& node);
84 
85  void setupEntityQuery();
86 
87  p_high_resolution_clock::time_point _lastFrameTimestamp;
88 
89  // Attach to entity tree for avatar-priority zone info.
90  EntityTreeHeadlessViewer _entityViewer;
91  bool _dirtyHeroStatus { true }; // Dirty the needs-hero-update
92 
93  // FIXME - new throttling - use these values somehow
94  float _trailingMixRatio { 0.0f };
95  float _throttlingRatio { 0.0f };
96 
97  int _sumListeners { 0 };
98  int _numStatFrames { 0 };
99  int _numTightLoopFrames { 0 };
100  int _sumIdentityPackets { 0 };
101 
102  float _maxKbpsPerNode = 0.0f;
103 
104  float _domainMinimumHeight { MIN_AVATAR_HEIGHT };
105  float _domainMaximumHeight { MAX_AVATAR_HEIGHT };
106 
107  RateCounter<> _broadcastRate;
108  p_high_resolution_clock::time_point _lastDebugMessage;
109 
110  // Pair of basename + uniquifying integer suffix.
111  struct SessionDisplayName {
112  explicit SessionDisplayName(QString baseName = QString(), int suffix = 0) :
113  _baseName(baseName),
114  _suffix(suffix) { }
115  // Does lexicographic ordering:
116  bool operator<(const SessionDisplayName& rhs) const;
117  bool operator==(const SessionDisplayName& rhs) const {
118  return _baseName == rhs._baseName && _suffix == rhs._suffix;
119  }
120 
121  QString _baseName;
122  int _suffix;
123  };
124  static const QRegularExpression suffixedNamePattern;
125 
126  std::set<SessionDisplayName> _sessionDisplayNames;
127 
128  quint64 _displayNameManagementElapsedTime { 0 }; // total time spent in broadcastAvatarData/display name management... since last stats window
129  quint64 _ignoreCalculationElapsedTime { 0 };
130  quint64 _avatarDataPackingElapsedTime { 0 };
131  quint64 _packetSendingElapsedTime { 0 };
132 
133  quint64 _broadcastAvatarDataElapsedTime { 0 }; // total time spent in broadcastAvatarData since last stats window
134  quint64 _broadcastAvatarDataInner { 0 };
135  quint64 _broadcastAvatarDataLockWait { 0 };
136  quint64 _broadcastAvatarDataNodeTransform { 0 };
137  quint64 _broadcastAvatarDataNodeFunctor { 0 };
138 
139  quint64 _handleAdjustAvatarSortingElapsedTime { 0 };
140  quint64 _handleViewFrustumPacketElapsedTime { 0 };
141  quint64 _handleAvatarIdentityPacketElapsedTime { 0 };
142  quint64 _handleKillAvatarPacketElapsedTime { 0 };
143  quint64 _handleNodeIgnoreRequestPacketElapsedTime { 0 };
144  quint64 _handleRadiusIgnoreRequestPacketElapsedTime { 0 };
145  quint64 _handleRequestsDomainListDataPacketElapsedTime { 0 };
146  quint64 _processQueuedAvatarDataPacketsElapsedTime { 0 };
147  quint64 _processQueuedAvatarDataPacketsLockWaitElapsedTime { 0 };
148 
149  quint64 _processEventsElapsedTime { 0 };
150  quint64 _sendStatsElapsedTime { 0 };
151  quint64 _queueIncomingPacketElapsedTime { 0 };
152  quint64 _lastStatsTime { usecTimestampNow() };
153 
154  RateCounter<> _loopRate; // this is the rate that the main thread tight loop runs
155 
156  AvatarMixerSlavePool _slavePool;
157  SlaveSharedData _slaveSharedData;
158 };
159 
160 #endif // hifi_AvatarMixer_h
Handles assignments of type AvatarMixer - distribution of avatar data to various clients.
Definition: AvatarMixer.h:32
void run() override
runs the avatar mixer
Definition: AvatarMixer.cpp:935
Definition: EntityItem.h:82