1 | /* $Id: Recording.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Recording code header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef MAIN_INCLUDED_Recording_h
|
---|
29 | #define MAIN_INCLUDED_Recording_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/settings.h>
|
---|
36 |
|
---|
37 | #include "RecordingStream.h"
|
---|
38 |
|
---|
39 | class Console;
|
---|
40 | class Progress;
|
---|
41 |
|
---|
42 | /** No flags specified. */
|
---|
43 | #define VBOX_RECORDING_CURSOR_F_NONE 0
|
---|
44 | /** Cursor is visible. */
|
---|
45 | #define VBOX_RECORDING_CURSOR_F_VISIBLE RT_BIT(0)
|
---|
46 | /** Cursor shape contains an alpha mask. */
|
---|
47 | #define VBOX_RECORDING_CURSOR_F_ALPHA RT_BIT(1)
|
---|
48 | /** Cursor state flags valid mask. */
|
---|
49 | #define VBOX_RECORDING_CURSOR_F_VALID_MASK 0x3
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Class for keeping a recording cursor state.
|
---|
53 | */
|
---|
54 | class RecordingCursorState
|
---|
55 | {
|
---|
56 | public:
|
---|
57 |
|
---|
58 | RecordingCursorState();
|
---|
59 | virtual ~RecordingCursorState();
|
---|
60 |
|
---|
61 | void Destroy();
|
---|
62 |
|
---|
63 | int CreateOrUpdate(bool fAlpha, uint32_t uWidth, uint32_t uHeight, const uint8_t *pu8Shape, size_t cbShape);
|
---|
64 |
|
---|
65 | int Move(int32_t iX, int32_t iY);
|
---|
66 |
|
---|
67 | /** Cursor state flags. */
|
---|
68 | uint32_t m_fFlags;
|
---|
69 | /** The current cursor shape. */
|
---|
70 | RECORDINGVIDEOFRAME m_Shape;
|
---|
71 | };
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Enumeration for a recording context state.
|
---|
75 | */
|
---|
76 | enum RECORDINGSTS
|
---|
77 | {
|
---|
78 | /** Recording not initialized. */
|
---|
79 | RECORDINGSTS_UNINITIALIZED = 0,
|
---|
80 | /** Recording was created. */
|
---|
81 | RECORDINGSTS_CREATED = 1,
|
---|
82 | /** Recording was started. */
|
---|
83 | RECORDINGSTS_STARTED = 2,
|
---|
84 | /** Recording was stopped. */
|
---|
85 | RECORDINGSTS_STOPPED = 3,
|
---|
86 | /** Limit has been reached. */
|
---|
87 | RECORDINGSTS_LIMIT_REACHED = 4,
|
---|
88 | /** Recording experienced an error. */
|
---|
89 | RECORDINGSTS_FAILURE = 5,
|
---|
90 | /** The usual 32-bit hack. */
|
---|
91 | RECORDINGSTS_32BIT_HACK = 0x7fffffff
|
---|
92 | };
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Class for managing a recording context.
|
---|
96 | */
|
---|
97 | class RecordingContext
|
---|
98 | {
|
---|
99 | friend RecordingStream;
|
---|
100 |
|
---|
101 | public:
|
---|
102 |
|
---|
103 | /** Recording context callback table. */
|
---|
104 | struct CALLBACKS
|
---|
105 | {
|
---|
106 | /**
|
---|
107 | * Recording state got changed. Optional.
|
---|
108 | *
|
---|
109 | * @param pCtx Recording context.
|
---|
110 | * @param enmSts New status.
|
---|
111 | * @param uScreen Screen ID.
|
---|
112 | * Set to UINT32_MAX if the limit of all streams was reached.
|
---|
113 | * @param vrc Result code of state change.
|
---|
114 | * @param pvUser User-supplied pointer. Might be NULL.
|
---|
115 | */
|
---|
116 | DECLCALLBACKMEMBER(void, pfnStateChanged, (RecordingContext *pCtx, RECORDINGSTS enmSts, uint32_t uScreen, int vrc, void *pvUser));
|
---|
117 |
|
---|
118 | /** User-supplied pointer. Might be NULL. */
|
---|
119 | void *pvUser;
|
---|
120 | };
|
---|
121 |
|
---|
122 | public:
|
---|
123 |
|
---|
124 | RecordingContext();
|
---|
125 |
|
---|
126 | virtual ~RecordingContext(void);
|
---|
127 |
|
---|
128 | public:
|
---|
129 |
|
---|
130 | const settings::Recording &GetConfig(void) const;
|
---|
131 | RecordingStream *GetStream(unsigned uScreen) const;
|
---|
132 | size_t GetStreamCount(void) const;
|
---|
133 | #ifdef VBOX_WITH_AUDIO_RECORDING
|
---|
134 | PRECORDINGCODEC GetCodecAudio(void) { return &this->m_CodecAudio; }
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | int Create(Console *pConsole, const settings::Recording &Settings, ComPtr<IProgress> &pProgress);
|
---|
138 | void Destroy(void);
|
---|
139 |
|
---|
140 | int Start(void);
|
---|
141 | int Stop(void);
|
---|
142 |
|
---|
143 | int SetError(int rc, const com::Utf8Str &strText);
|
---|
144 |
|
---|
145 | int SendAudioFrame(const void *pvData, size_t cbData, uint64_t uTimestampMs);
|
---|
146 | int SendVideoFrame(uint32_t uScreen, PRECORDINGVIDEOFRAME pFrame, uint64_t msTimestamp);
|
---|
147 | int SendCursorPositionChange(uint32_t uScreen, int32_t x, int32_t y, uint64_t msTimestamp);
|
---|
148 | int SendCursorShapeChange(bool fVisible, bool fAlpha, uint32_t xHot, uint32_t yHot, uint32_t uWidth, uint32_t uHeight, const uint8_t *pu8Shape, size_t cbShape, uint64_t msTimestamp);
|
---|
149 | int SendScreenChange(uint32_t uScreen, PRECORDINGSURFACEINFO pInfo, uint64_t uTimestampMs);
|
---|
150 |
|
---|
151 | public:
|
---|
152 |
|
---|
153 | uint64_t GetCurrentPTS(void) const;
|
---|
154 | bool IsFeatureEnabled(RecordingFeature_T enmFeature);
|
---|
155 | bool IsFeatureEnabled(uint32_t uScreen, RecordingFeature_T enmFeature);
|
---|
156 | bool IsReady(void);
|
---|
157 | bool IsStarted(void);
|
---|
158 | bool IsLimitReached(void);
|
---|
159 | bool IsLimitReached(uint32_t uScreen, uint64_t msTimestamp);
|
---|
160 | bool NeedsUpdate(uint32_t uScreen, uint64_t msTimestamp);
|
---|
161 | void SetCallbacks(RecordingContext::CALLBACKS *pCallbacks, void *pvUser);
|
---|
162 |
|
---|
163 | /** The state mouse cursor state.
|
---|
164 | * We currently only support one mouse cursor at a time. */
|
---|
165 | RecordingCursorState m_Cursor;
|
---|
166 |
|
---|
167 |
|
---|
168 | protected:
|
---|
169 |
|
---|
170 | int createInternal(Console *ptrConsole, const settings::Recording &Settings, ComPtr<IProgress> &pProgress);
|
---|
171 | void reset(void);
|
---|
172 | int startInternal(void);
|
---|
173 | int stopInternal(void);
|
---|
174 |
|
---|
175 | void destroyInternal(void);
|
---|
176 |
|
---|
177 | RecordingStream *getStreamInternal(unsigned uScreen) const;
|
---|
178 |
|
---|
179 | int processCommonData(RecordingBlockMap &mapCommon, RTMSINTERVAL msTimeout);
|
---|
180 | int writeCommonData(RecordingBlockMap &mapCommon, PRECORDINGCODEC pCodec, const void *pvData, size_t cbData, uint64_t msAbsPTS, uint32_t uFlags);
|
---|
181 |
|
---|
182 | int lock(void);
|
---|
183 | int unlock(void);
|
---|
184 |
|
---|
185 | int onLimitReached(uint32_t uScreen, int vrc);
|
---|
186 |
|
---|
187 | bool progressIsCanceled(void) const;
|
---|
188 | bool progressIsCompleted(void) const;
|
---|
189 | int progressCreate(const settings::Recording &Settings, ComObjPtr<Progress> &pProgress);
|
---|
190 | int progressNotifyComplete(HRESULT hrc = S_OK, IVirtualBoxErrorInfo *pErrorInfo = NULL);
|
---|
191 | int progressSet(uint32_t uOp, const com::Bstr &strDesc);
|
---|
192 | int progressSet(uint64_t msTimestamp);
|
---|
193 |
|
---|
194 | static DECLCALLBACK(int) threadMain(RTTHREAD hThreadSelf, void *pvUser);
|
---|
195 |
|
---|
196 | int threadNotify(void);
|
---|
197 |
|
---|
198 | protected:
|
---|
199 |
|
---|
200 | int audioInit(const settings::RecordingScreen &screenSettings);
|
---|
201 |
|
---|
202 | protected:
|
---|
203 |
|
---|
204 | static void s_progressCancelCallback(void *pvUser);
|
---|
205 |
|
---|
206 | static DECLCALLBACK(void) s_recordingStateChangedCallback(RecordingContext *pCtx, RECORDINGSTS enmSts, uint32_t uScreen, int vrc, void *pvUser);
|
---|
207 |
|
---|
208 | static DECLCALLBACK(int) s_audioCodecWriteDataCallback(PRECORDINGCODEC pCodec, const void *pvData, size_t cbData, uint64_t msAbsPTS, uint32_t uFlags, void *pvUser);
|
---|
209 |
|
---|
210 | protected:
|
---|
211 |
|
---|
212 | /** Pointer to the console object. */
|
---|
213 | Console *m_pConsole;
|
---|
214 | /** Used recording settings. */
|
---|
215 | settings::Recording m_Settings;
|
---|
216 | /** The current state. */
|
---|
217 | RECORDINGSTS m_enmState;
|
---|
218 | /** Callback table. */
|
---|
219 | CALLBACKS m_Callbacks;
|
---|
220 | /** Critical section to serialize access. */
|
---|
221 | RTCRITSECT m_CritSect;
|
---|
222 | /** Semaphore to signal the encoding worker thread. */
|
---|
223 | RTSEMEVENT m_WaitEvent;
|
---|
224 | /** Current operation of progress. Set to 0 if not started yet, >= 1 if started. */
|
---|
225 | ULONG m_ulCurOp;
|
---|
226 | /** Number of progress operations. Always >= 1. */
|
---|
227 | ULONG m_cOps;
|
---|
228 | /** The progress object assigned to this context.
|
---|
229 | * Might be NULL if not being used. */
|
---|
230 | const ComObjPtr<Progress> m_pProgress;
|
---|
231 | /** Shutdown indicator. */
|
---|
232 | bool m_fShutdown;
|
---|
233 | /** Encoding worker thread. */
|
---|
234 | RTTHREAD m_Thread;
|
---|
235 | /** Vector of current recording streams.
|
---|
236 | * Per VM screen (display) one recording stream is being used. */
|
---|
237 | RecordingStreams m_vecStreams;
|
---|
238 | /** Number of streams in vecStreams which currently are enabled for recording. */
|
---|
239 | uint16_t m_cStreamsEnabled;
|
---|
240 | /** Timestamp (in ms) of when recording has been started.
|
---|
241 | * Set to 0 if not started (yet). */
|
---|
242 | uint64_t m_tsStartMs;
|
---|
243 | #ifdef VBOX_WITH_AUDIO_RECORDING
|
---|
244 | /** Audio codec to use.
|
---|
245 | *
|
---|
246 | * We multiplex audio data from this recording context to all streams,
|
---|
247 | * to avoid encoding the same audio data for each stream. We ASSUME that
|
---|
248 | * all audio data of a VM will be the same for each stream at a given
|
---|
249 | * point in time. */
|
---|
250 | RECORDINGCODEC m_CodecAudio;
|
---|
251 | #endif /* VBOX_WITH_AUDIO_RECORDING */
|
---|
252 | /** Block map of raw common data blocks which need to get encoded first. */
|
---|
253 | RecordingBlockMap m_mapBlocksRaw;
|
---|
254 | /** Block map of encoded common blocks.
|
---|
255 | *
|
---|
256 | * Only do the encoding of common data blocks only once and then multiplex
|
---|
257 | * the encoded data to all affected recording streams.
|
---|
258 | *
|
---|
259 | * This avoids doing the (expensive) encoding + multiplexing work in other
|
---|
260 | * threads like EMT / audio async I/O.
|
---|
261 | *
|
---|
262 | * For now this only affects audio, e.g. all recording streams
|
---|
263 | * need to have the same audio data at a specific point in time. */
|
---|
264 | RecordingBlockMap m_mapBlocksEncoded;
|
---|
265 | };
|
---|
266 | #endif /* !MAIN_INCLUDED_Recording_h */
|
---|
267 |
|
---|