Overte C++ Documentation
recording/src/recording/Frame.h
1 //
2 // Created by Bradley Austin Davis 2015/11/04
3 // Copyright 2015 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #pragma once
10 #ifndef hifi_Recording_Frame_h
11 #define hifi_Recording_Frame_h
12 
13 #include "Forward.h"
14 
15 #include <functional>
16 
17 #ifdef Q_OS_WIN
18 #include <stdint.h>
19 #else
20 #include <limits>
21 #endif
22 
23 #include <QtCore/QObject>
24 
25 namespace recording {
26 
27 struct FrameHeader {
28  using Time = uint32_t;
29 
30 // until we use a version of visual studio that has constexpr support, we can't use numeric_limits at compile time
31 #ifdef Q_OS_WIN
32  static const Time INVALID_TIME = UINT32_MAX;
33 #else
34  static const Time INVALID_TIME = std::numeric_limits<uint32_t>::max();
35 #endif
36 
37  static const FrameType TYPE_INVALID = 0xFFFF;
38  static const FrameType TYPE_HEADER = 0x0;
39 
40  static Time secondsToFrameTime(float seconds);
41  static float frameTimeToSeconds(Time frameTime);
42 
43  static uint32_t frameTimeToMilliseconds(Time frameTime);
44 
45  static Time frameTimeFromEpoch(quint64 epoch);
46  static quint64 epochForFrameTime(Time frameTime);
47 
48  FrameType type { TYPE_INVALID };
49  Time timeOffset { 0 }; // milliseconds
50 
51  FrameHeader() {}
52  FrameHeader(FrameType type, Time timeOffset)
53  : type(type), timeOffset(timeOffset) { }
54 };
55 
56 struct Frame : public FrameHeader {
57 public:
58  using Pointer = std::shared_ptr<Frame>;
59  using ConstPointer = std::shared_ptr<const Frame>;
60  using Handler = std::function<void(Frame::ConstPointer frame)>;
61 
62  QByteArray data;
63 
64  Frame() {}
65  Frame(FrameType type, float timeOffset, const QByteArray& data)
66  : FrameHeader(type, timeOffset), data(data) { }
67 
68  static FrameType registerFrameType(const QString& frameTypeName);
69  static Handler registerFrameHandler(FrameType type, Handler handler);
70  static Handler registerFrameHandler(const QString& frameTypeName, Handler handler);
71  static void clearFrameHandler(FrameType type);
72  static void clearFrameHandler(const QString& frameTypeName);
73  static QMap<QString, FrameType> getFrameTypes();
74  static QMap<FrameType, QString> getFrameTypeNames();
75  static void handleFrame(const ConstPointer& frame);
76 };
77 
78 }
79 
80 #endif