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