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