1 | /* $Id: RecordingStream.h 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Recording stream code header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef MAIN_INCLUDED_RecordingStream_h
|
---|
19 | #define MAIN_INCLUDED_RecordingStream_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <map>
|
---|
25 | #include <vector>
|
---|
26 |
|
---|
27 | #include <iprt/critsect.h>
|
---|
28 |
|
---|
29 | #include "RecordingInternals.h"
|
---|
30 |
|
---|
31 | class WebMWriter;
|
---|
32 | class RecordingContext;
|
---|
33 |
|
---|
34 | /** Structure for queuing all blocks bound to a single timecode.
|
---|
35 | * This can happen if multiple tracks are being involved. */
|
---|
36 | struct RecordingBlocks
|
---|
37 | {
|
---|
38 | virtual ~RecordingBlocks()
|
---|
39 | {
|
---|
40 | Clear();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Resets a recording block list by removing (destroying)
|
---|
45 | * all current elements.
|
---|
46 | */
|
---|
47 | void Clear()
|
---|
48 | {
|
---|
49 | while (!List.empty())
|
---|
50 | {
|
---|
51 | RecordingBlock *pBlock = List.front();
|
---|
52 | List.pop_front();
|
---|
53 | delete pBlock;
|
---|
54 | }
|
---|
55 |
|
---|
56 | Assert(List.size() == 0);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** The actual block list for this timecode. */
|
---|
60 | RecordingBlockList List;
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** A block map containing all currently queued blocks.
|
---|
64 | * The key specifies a unique timecode, whereas the value
|
---|
65 | * is a list of blocks which all correlate to the same key (timecode). */
|
---|
66 | typedef std::map<uint64_t, RecordingBlocks *> RecordingBlockMap;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Structure for holding a set of recording (data) blocks.
|
---|
70 | */
|
---|
71 | struct RecordingBlockSet
|
---|
72 | {
|
---|
73 | virtual ~RecordingBlockSet()
|
---|
74 | {
|
---|
75 | Clear();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Resets a recording block set by removing (destroying)
|
---|
80 | * all current elements.
|
---|
81 | */
|
---|
82 | void Clear(void)
|
---|
83 | {
|
---|
84 | RecordingBlockMap::iterator it = Map.begin();
|
---|
85 | while (it != Map.end())
|
---|
86 | {
|
---|
87 | it->second->Clear();
|
---|
88 | delete it->second;
|
---|
89 | Map.erase(it);
|
---|
90 | it = Map.begin();
|
---|
91 | }
|
---|
92 |
|
---|
93 | Assert(Map.size() == 0);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Timestamp (in ms) when this set was last processed. */
|
---|
97 | uint64_t tsLastProcessedMs;
|
---|
98 | /** All blocks related to this block set. */
|
---|
99 | RecordingBlockMap Map;
|
---|
100 | };
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Class for managing a recording stream.
|
---|
104 | */
|
---|
105 | class RecordingStream
|
---|
106 | {
|
---|
107 | public:
|
---|
108 |
|
---|
109 | RecordingStream(RecordingContext *pCtx);
|
---|
110 |
|
---|
111 | RecordingStream(RecordingContext *pCtx, uint32_t uScreen, const settings::RecordingScreenSettings &Settings);
|
---|
112 |
|
---|
113 | virtual ~RecordingStream(void);
|
---|
114 |
|
---|
115 | public:
|
---|
116 |
|
---|
117 | int Init(RecordingContext *pCtx, uint32_t uScreen, const settings::RecordingScreenSettings &Settings);
|
---|
118 | int Uninit(void);
|
---|
119 |
|
---|
120 | int Process(RecordingBlockMap &mapBlocksCommon);
|
---|
121 | int SendVideoFrame(uint32_t x, uint32_t y, uint32_t uPixelFormat, uint32_t uBPP, uint32_t uBytesPerLine,
|
---|
122 | uint32_t uSrcWidth, uint32_t uSrcHeight, uint8_t *puSrcData, uint64_t msTimestamp);
|
---|
123 |
|
---|
124 | const settings::RecordingScreenSettings &GetConfig(void) const;
|
---|
125 | uint16_t GetID(void) const { return this->uScreenID; };
|
---|
126 | bool IsLimitReached(uint64_t msTimestamp) const;
|
---|
127 | bool IsReady(void) const;
|
---|
128 |
|
---|
129 | protected:
|
---|
130 |
|
---|
131 | int open(const settings::RecordingScreenSettings &Settings);
|
---|
132 | int close(void);
|
---|
133 |
|
---|
134 | int initInternal(RecordingContext *pCtx, uint32_t uScreen, const settings::RecordingScreenSettings &Settings);
|
---|
135 | int uninitInternal(void);
|
---|
136 |
|
---|
137 | int initVideo(void);
|
---|
138 | int unitVideo(void);
|
---|
139 |
|
---|
140 | int initAudio(void);
|
---|
141 |
|
---|
142 | bool isLimitReachedInternal(uint64_t msTimestamp) const;
|
---|
143 | int iterateInternal(uint64_t msTimestamp);
|
---|
144 |
|
---|
145 | #ifdef VBOX_WITH_LIBVPX
|
---|
146 | int initVideoVPX(void);
|
---|
147 | int uninitVideoVPX(void);
|
---|
148 | int writeVideoVPX(uint64_t msTimestamp, PRECORDINGVIDEOFRAME pFrame);
|
---|
149 | #endif
|
---|
150 | void lock(void);
|
---|
151 | void unlock(void);
|
---|
152 |
|
---|
153 | int parseOptionsString(const com::Utf8Str &strOptions);
|
---|
154 |
|
---|
155 | protected:
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Enumeration for a recording stream state.
|
---|
159 | */
|
---|
160 | enum RECORDINGSTREAMSTATE
|
---|
161 | {
|
---|
162 | /** Stream not initialized. */
|
---|
163 | RECORDINGSTREAMSTATE_UNINITIALIZED = 0,
|
---|
164 | /** Stream was initialized. */
|
---|
165 | RECORDINGSTREAMSTATE_INITIALIZED = 1,
|
---|
166 | /** The usual 32-bit hack. */
|
---|
167 | RECORDINGSTREAMSTATE_32BIT_HACK = 0x7fffffff
|
---|
168 | };
|
---|
169 |
|
---|
170 | /** Recording context this stream is associated to. */
|
---|
171 | RecordingContext *pCtx;
|
---|
172 | /** The current state. */
|
---|
173 | RECORDINGSTREAMSTATE enmState;
|
---|
174 | struct
|
---|
175 | {
|
---|
176 | /** File handle to use for writing. */
|
---|
177 | RTFILE hFile;
|
---|
178 | /** Pointer to WebM writer instance being used. */
|
---|
179 | WebMWriter *pWEBM;
|
---|
180 | } File;
|
---|
181 | bool fEnabled;
|
---|
182 | #ifdef VBOX_WITH_AUDIO_RECORDING
|
---|
183 | /** Track number of audio stream. */
|
---|
184 | uint8_t uTrackAudio;
|
---|
185 | #endif
|
---|
186 | /** Track number of video stream. */
|
---|
187 | uint8_t uTrackVideo;
|
---|
188 | /** Screen ID. */
|
---|
189 | uint16_t uScreenID;
|
---|
190 | /** Critical section to serialize access. */
|
---|
191 | RTCRITSECT CritSect;
|
---|
192 | /** Timestamp (in ms) of when recording has been start. */
|
---|
193 | uint64_t tsStartMs;
|
---|
194 |
|
---|
195 | struct
|
---|
196 | {
|
---|
197 | /** Minimal delay (in ms) between two video frames.
|
---|
198 | * This value is based on the configured FPS rate. */
|
---|
199 | uint32_t uDelayMs;
|
---|
200 | /** Timestamp (in ms) of the last video frame we encoded. */
|
---|
201 | uint64_t uLastTimeStampMs;
|
---|
202 | /** Number of failed attempts to encode the current video frame in a row. */
|
---|
203 | uint16_t cFailedEncodingFrames;
|
---|
204 | RECORDINGVIDEOCODEC Codec;
|
---|
205 | } Video;
|
---|
206 |
|
---|
207 | settings::RecordingScreenSettings ScreenSettings;
|
---|
208 | /** Common set of recording (data) blocks, needed for
|
---|
209 | * multiplexing to all recording streams. */
|
---|
210 | RecordingBlockSet Blocks;
|
---|
211 | };
|
---|
212 |
|
---|
213 | /** Vector of recording streams. */
|
---|
214 | typedef std::vector <RecordingStream *> RecordingStreams;
|
---|
215 |
|
---|
216 | #endif /* !MAIN_INCLUDED_RecordingStream_h */
|
---|
217 |
|
---|