Overte C++ Documentation
Agent.h
1 //
2 // Agent.h
3 // assignment-client/src
4 //
5 // Created by Stephen Birarda on 7/1/13.
6 // Copyright 2013 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
14 #ifndef hifi_Agent_h
15 #define hifi_Agent_h
16 
17 #include <memory>
18 #include <vector>
19 
20 #include <QtCore/QObject>
21 #include <QtCore/QSharedPointer>
22 #include <QtCore/QUrl>
23 #include <QtCore/QTimer>
24 #include <QUuid>
25 #include <QtCore/QSharedPointer>
26 
27 #include <EntityEditPacketSender.h>
28 #include <EntityTree.h>
29 #include <ScriptEngine.h>
30 #include <ThreadedAssignment.h>
31 
32 #include <plugins/CodecPlugin.h>
33 
34 #include <Sound.h>
35 #include "AudioGate.h"
36 #include "MixedAudioStream.h"
37 #include "entities/EntityTreeHeadlessViewer.h"
38 #include "avatars/ScriptableAvatar.h"
39 
40 class ScriptEngine;
41 class ScriptManager;
42 using ScriptEnginePointer = std::shared_ptr<ScriptEngine>;
43 using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
44 
45 class Agent : public ThreadedAssignment {
46  Q_OBJECT
47 
48  Q_PROPERTY(bool isAvatar READ isAvatar WRITE setIsAvatar)
49  Q_PROPERTY(bool isPlayingAvatarSound READ isPlayingAvatarSound)
50  Q_PROPERTY(bool isListeningToAudioStream READ isListeningToAudioStream WRITE setIsListeningToAudioStream)
51  Q_PROPERTY(bool isNoiseGateEnabled READ isNoiseGateEnabled WRITE setIsNoiseGateEnabled)
52  Q_PROPERTY(float lastReceivedAudioLoudness READ getLastReceivedAudioLoudness)
53  Q_PROPERTY(QUuid sessionUUID READ getSessionUUID)
54 
55 public:
56  Agent(ReceivedMessage& message);
57 
58  bool isPlayingAvatarSound() const { return _avatarSound != NULL; }
59 
60  bool isListeningToAudioStream() const { return _isListeningToAudioStream; }
61  void setIsListeningToAudioStream(bool isListeningToAudioStream);
62 
63  bool isNoiseGateEnabled() const { return _isNoiseGateEnabled; }
64  void setIsNoiseGateEnabled(bool isNoiseGateEnabled);
65 
66  float getLastReceivedAudioLoudness() const { return _lastReceivedAudioLoudness; }
67  QUuid getSessionUUID() const;
68 
69  virtual void aboutToFinish() override;
70 
71 public slots:
72  void run() override;
73 
74  void playAvatarSound(SharedSoundPointer avatarSound);
75 
76  void setIsAvatar(bool isAvatar);
77  bool isAvatar() const { return _isAvatar; }
78 
79  Q_INVOKABLE virtual void stop() override;
80 
81 private slots:
82  void requestScript();
83  void scriptRequestFinished();
84  void executeScript();
85 
86  void handleAudioPacket(QSharedPointer<ReceivedMessage> message);
87  void handleOctreePacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
88  void handleSelectedAudioFormat(QSharedPointer<ReceivedMessage> message);
89 
90  void nodeActivated(SharedNodePointer activatedNode);
91  void nodeKilled(SharedNodePointer killedNode);
92 
93  void processAgentAvatarAudio();
94 
95 private:
96  void negotiateAudioFormat();
97  void selectAudioFormat(const QString& selectedCodecName);
98  void encodeFrameOfZeros(QByteArray& encodedZeros);
99  void computeLoudness(const QByteArray* decodedBuffer, QSharedPointer<ScriptableAvatar>);
100 
101  ScriptManagerPointer _scriptManager;
102  EntityEditPacketSender _entityEditSender;
103  EntityTreeHeadlessViewer _entityViewer;
104 
105  MixedAudioStream _receivedAudioStream;
106  float _lastReceivedAudioLoudness;
107 
108  void setAvatarSound(SharedSoundPointer avatarSound) { _avatarSound = avatarSound; }
109 
110  void queryAvatars();
111 
112  QString _scriptContents;
113  QTimer* _scriptRequestTimeout { nullptr };
114  ResourceRequest* _pendingScriptRequest { nullptr };
115  bool _isListeningToAudioStream = false;
116  SharedSoundPointer _avatarSound;
117  bool _shouldMuteRecordingAudio { false };
118  int _numAvatarSoundSentBytes = 0;
119  bool _isAvatar = false;
120  QTimer* _avatarQueryTimer = nullptr;
121  QHash<QUuid, quint16> _outgoingScriptAudioSequenceNumbers;
122 
123  AudioGate _audioGate;
124  bool _audioGateOpen { true };
125  bool _isNoiseGateEnabled { false };
126 
127  CodecPluginPointer _codec;
128  QString _selectedCodecName;
129  Encoder* _encoder { nullptr };
130  QTimer _avatarAudioTimer;
131  bool _flushEncoder { false };
132 };
133 
134 #endif // hifi_Agent_h
Utility for processing, packing, queueing and sending of outbound edit voxel messages.
Definition: EntityEditPacketSender.h:25
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
Manages a single scripting engine.
Definition: ScriptManager.h:281