1 | /* $Id: VideoRec.cpp 65263 2017-01-12 15:25:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Video capturing utility routines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2017 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 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
19 |
|
---|
20 | #include <vector>
|
---|
21 |
|
---|
22 | #include <VBox/log.h>
|
---|
23 | #include <iprt/asm.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/semaphore.h>
|
---|
26 | #include <iprt/thread.h>
|
---|
27 | #include <iprt/time.h>
|
---|
28 |
|
---|
29 | #include <VBox/com/VirtualBox.h>
|
---|
30 | #include <VBox/com/com.h>
|
---|
31 | #include <VBox/com/string.h>
|
---|
32 |
|
---|
33 | #include "EbmlWriter.h"
|
---|
34 | #include "VideoRec.h"
|
---|
35 |
|
---|
36 | #ifdef VBOX_WITH_LIBVPX
|
---|
37 | # define VPX_CODEC_DISABLE_COMPAT 1
|
---|
38 | # include <vpx/vp8cx.h>
|
---|
39 | # include <vpx/vpx_image.h>
|
---|
40 |
|
---|
41 | /** Default VPX codec to use. */
|
---|
42 | # define DEFAULTCODEC (vpx_codec_vp8_cx())
|
---|
43 | #endif /* VBOX_WITH_LIBVPX */
|
---|
44 |
|
---|
45 | static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStrm);
|
---|
46 | static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm);
|
---|
47 |
|
---|
48 | using namespace com;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Enumeration for a video recording state.
|
---|
52 | */
|
---|
53 | enum VIDEORECSTS
|
---|
54 | {
|
---|
55 | /** Not initialized. */
|
---|
56 | VIDEORECSTS_UNINITIALIZED = 0,
|
---|
57 | /** Initialized, idle. */
|
---|
58 | VIDEORECSTS_IDLE = 1,
|
---|
59 | /** Currently in VideoRecCopyToIntBuf(), delay termination. */
|
---|
60 | VIDEORECSTS_COPYING = 2,
|
---|
61 | /** Signal that we are terminating. */
|
---|
62 | VIDEORECSTS_TERMINATING = 3
|
---|
63 | };
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Enumeration for supported pixel formats.
|
---|
67 | */
|
---|
68 | enum VIDEORECPIXELFMT
|
---|
69 | {
|
---|
70 | /** Unknown pixel format. */
|
---|
71 | VIDEORECPIXELFMT_UNKNOWN = 0,
|
---|
72 | /** RGB 24. */
|
---|
73 | VIDEORECPIXELFMT_RGB24 = 1,
|
---|
74 | /** RGB 24. */
|
---|
75 | VIDEORECPIXELFMT_RGB32 = 2,
|
---|
76 | /** RGB 565. */
|
---|
77 | VIDEORECPIXELFMT_RGB565 = 3
|
---|
78 | };
|
---|
79 |
|
---|
80 | /* Must be always accessible and therefore cannot be part of VIDEORECCONTEXT */
|
---|
81 | static uint32_t g_enmState = VIDEORECSTS_UNINITIALIZED;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Structure for keeping specific video recording codec data.
|
---|
85 | */
|
---|
86 | typedef struct VIDEORECCODEC
|
---|
87 | {
|
---|
88 | union
|
---|
89 | {
|
---|
90 | #ifdef VBOX_WITH_LIBVPX
|
---|
91 | struct
|
---|
92 | {
|
---|
93 | /** VPX codec context. */
|
---|
94 | vpx_codec_ctx_t CodecCtx;
|
---|
95 | /** VPX codec configuration. */
|
---|
96 | vpx_codec_enc_cfg_t Config;
|
---|
97 | /** VPX image context. */
|
---|
98 | vpx_image_t RawImage;
|
---|
99 | } VPX;
|
---|
100 | #endif /* VBOX_WITH_LIBVPX */
|
---|
101 | };
|
---|
102 | } VIDEORECCODEC, *PVIDEORECCODEC;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Strucutre for maintaining a video recording stream.
|
---|
106 | */
|
---|
107 | typedef struct VIDEORECSTREAM
|
---|
108 | {
|
---|
109 | /** Container context. */
|
---|
110 | WebMWriter *pEBML;
|
---|
111 | /** Codec data. */
|
---|
112 | VIDEORECCODEC Codec;
|
---|
113 | /** Target X resolution (in pixels). */
|
---|
114 | uint32_t uTargetWidth;
|
---|
115 | /** Target Y resolution (in pixels). */
|
---|
116 | uint32_t uTargetHeight;
|
---|
117 | /** X resolution of the last encoded frame. */
|
---|
118 | uint32_t uLastSourceWidth;
|
---|
119 | /** Y resolution of the last encoded frame. */
|
---|
120 | uint32_t uLastSourceHeight;
|
---|
121 | /** Current frame number. */
|
---|
122 | uint64_t cFrame;
|
---|
123 | /** RGB buffer containing the most recent frame of the framebuffer. */
|
---|
124 | uint8_t *pu8RgbBuf;
|
---|
125 | /** YUV buffer the encode function fetches the frame from. */
|
---|
126 | uint8_t *pu8YuvBuf;
|
---|
127 | /** Whether video recording is enabled or not. */
|
---|
128 | bool fEnabled;
|
---|
129 | /** Whether the RGB buffer is filled or not. */
|
---|
130 | bool fRgbFilled;
|
---|
131 | /** Pixel format of the current frame. */
|
---|
132 | uint32_t u32PixelFormat;
|
---|
133 | /** Minimal delay between two frames. */
|
---|
134 | uint32_t uDelay;
|
---|
135 | /** Time stamp of the last frame we encoded. */
|
---|
136 | uint64_t u64LastTimeStamp;
|
---|
137 | /** Time stamp of the current frame. */
|
---|
138 | uint64_t u64TimeStamp;
|
---|
139 | /** Encoder deadline. */
|
---|
140 | unsigned int uEncoderDeadline;
|
---|
141 | } VIDEORECSTREAM, *PVIDEORECSTREAM;
|
---|
142 |
|
---|
143 | /** Vector of video recording streams. */
|
---|
144 | typedef std::vector <PVIDEORECSTREAM> VideoRecStreams;
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Structure for keeping a video recording context.
|
---|
148 | */
|
---|
149 | typedef struct VIDEORECCONTEXT
|
---|
150 | {
|
---|
151 | /** Semaphore to signal the encoding worker thread. */
|
---|
152 | RTSEMEVENT WaitEvent;
|
---|
153 | /** Semaphore required during termination. */
|
---|
154 | RTSEMEVENT TermEvent;
|
---|
155 | /** Whether video recording is enabled or not. */
|
---|
156 | bool fEnabled;
|
---|
157 | /** Worker thread. */
|
---|
158 | RTTHREAD Thread;
|
---|
159 | /** Maximal time stamp. */
|
---|
160 | uint64_t u64MaxTimeStamp;
|
---|
161 | /** Maximal file size in MB. */
|
---|
162 | uint32_t uMaxFileSize;
|
---|
163 | /** Vector of current video recording stream contexts. */
|
---|
164 | VideoRecStreams vecStreams;
|
---|
165 | } VIDEORECCONTEXT, *PVIDEORECCONTEXT;
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Iterator class for running through a BGRA32 image buffer and converting
|
---|
170 | * it to RGB.
|
---|
171 | */
|
---|
172 | class ColorConvBGRA32Iter
|
---|
173 | {
|
---|
174 | private:
|
---|
175 | enum { PIX_SIZE = 4 };
|
---|
176 | public:
|
---|
177 | ColorConvBGRA32Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
|
---|
178 | {
|
---|
179 | LogFlow(("width = %d height=%d aBuf=%lx\n", aWidth, aHeight, aBuf));
|
---|
180 | mPos = 0;
|
---|
181 | mSize = aWidth * aHeight * PIX_SIZE;
|
---|
182 | mBuf = aBuf;
|
---|
183 | }
|
---|
184 | /**
|
---|
185 | * Convert the next pixel to RGB.
|
---|
186 | * @returns true on success, false if we have reached the end of the buffer
|
---|
187 | * @param aRed where to store the red value
|
---|
188 | * @param aGreen where to store the green value
|
---|
189 | * @param aBlue where to store the blue value
|
---|
190 | */
|
---|
191 | bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
|
---|
192 | {
|
---|
193 | bool rc = false;
|
---|
194 | if (mPos + PIX_SIZE <= mSize)
|
---|
195 | {
|
---|
196 | *aRed = mBuf[mPos + 2];
|
---|
197 | *aGreen = mBuf[mPos + 1];
|
---|
198 | *aBlue = mBuf[mPos ];
|
---|
199 | mPos += PIX_SIZE;
|
---|
200 | rc = true;
|
---|
201 | }
|
---|
202 | return rc;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Skip forward by a certain number of pixels
|
---|
207 | * @param aPixels how many pixels to skip
|
---|
208 | */
|
---|
209 | void skip(unsigned aPixels)
|
---|
210 | {
|
---|
211 | mPos += PIX_SIZE * aPixels;
|
---|
212 | }
|
---|
213 | private:
|
---|
214 | /** Size of the picture buffer */
|
---|
215 | unsigned mSize;
|
---|
216 | /** Current position in the picture buffer */
|
---|
217 | unsigned mPos;
|
---|
218 | /** Address of the picture buffer */
|
---|
219 | uint8_t *mBuf;
|
---|
220 | };
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Iterator class for running through an BGR24 image buffer and converting
|
---|
224 | * it to RGB.
|
---|
225 | */
|
---|
226 | class ColorConvBGR24Iter
|
---|
227 | {
|
---|
228 | private:
|
---|
229 | enum { PIX_SIZE = 3 };
|
---|
230 | public:
|
---|
231 | ColorConvBGR24Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
|
---|
232 | {
|
---|
233 | mPos = 0;
|
---|
234 | mSize = aWidth * aHeight * PIX_SIZE;
|
---|
235 | mBuf = aBuf;
|
---|
236 | }
|
---|
237 | /**
|
---|
238 | * Convert the next pixel to RGB.
|
---|
239 | * @returns true on success, false if we have reached the end of the buffer
|
---|
240 | * @param aRed where to store the red value
|
---|
241 | * @param aGreen where to store the green value
|
---|
242 | * @param aBlue where to store the blue value
|
---|
243 | */
|
---|
244 | bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
|
---|
245 | {
|
---|
246 | bool rc = false;
|
---|
247 | if (mPos + PIX_SIZE <= mSize)
|
---|
248 | {
|
---|
249 | *aRed = mBuf[mPos + 2];
|
---|
250 | *aGreen = mBuf[mPos + 1];
|
---|
251 | *aBlue = mBuf[mPos ];
|
---|
252 | mPos += PIX_SIZE;
|
---|
253 | rc = true;
|
---|
254 | }
|
---|
255 | return rc;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Skip forward by a certain number of pixels
|
---|
260 | * @param aPixels how many pixels to skip
|
---|
261 | */
|
---|
262 | void skip(unsigned aPixels)
|
---|
263 | {
|
---|
264 | mPos += PIX_SIZE * aPixels;
|
---|
265 | }
|
---|
266 | private:
|
---|
267 | /** Size of the picture buffer */
|
---|
268 | unsigned mSize;
|
---|
269 | /** Current position in the picture buffer */
|
---|
270 | unsigned mPos;
|
---|
271 | /** Address of the picture buffer */
|
---|
272 | uint8_t *mBuf;
|
---|
273 | };
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Iterator class for running through an BGR565 image buffer and converting
|
---|
277 | * it to RGB.
|
---|
278 | */
|
---|
279 | class ColorConvBGR565Iter
|
---|
280 | {
|
---|
281 | private:
|
---|
282 | enum { PIX_SIZE = 2 };
|
---|
283 | public:
|
---|
284 | ColorConvBGR565Iter(unsigned aWidth, unsigned aHeight, uint8_t *aBuf)
|
---|
285 | {
|
---|
286 | mPos = 0;
|
---|
287 | mSize = aWidth * aHeight * PIX_SIZE;
|
---|
288 | mBuf = aBuf;
|
---|
289 | }
|
---|
290 | /**
|
---|
291 | * Convert the next pixel to RGB.
|
---|
292 | * @returns true on success, false if we have reached the end of the buffer
|
---|
293 | * @param aRed where to store the red value
|
---|
294 | * @param aGreen where to store the green value
|
---|
295 | * @param aBlue where to store the blue value
|
---|
296 | */
|
---|
297 | bool getRGB(unsigned *aRed, unsigned *aGreen, unsigned *aBlue)
|
---|
298 | {
|
---|
299 | bool rc = false;
|
---|
300 | if (mPos + PIX_SIZE <= mSize)
|
---|
301 | {
|
---|
302 | unsigned uFull = (((unsigned) mBuf[mPos + 1]) << 8)
|
---|
303 | | ((unsigned) mBuf[mPos]);
|
---|
304 | *aRed = (uFull >> 8) & ~7;
|
---|
305 | *aGreen = (uFull >> 3) & ~3 & 0xff;
|
---|
306 | *aBlue = (uFull << 3) & ~7 & 0xff;
|
---|
307 | mPos += PIX_SIZE;
|
---|
308 | rc = true;
|
---|
309 | }
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Skip forward by a certain number of pixels
|
---|
315 | * @param aPixels how many pixels to skip
|
---|
316 | */
|
---|
317 | void skip(unsigned aPixels)
|
---|
318 | {
|
---|
319 | mPos += PIX_SIZE * aPixels;
|
---|
320 | }
|
---|
321 | private:
|
---|
322 | /** Size of the picture buffer */
|
---|
323 | unsigned mSize;
|
---|
324 | /** Current position in the picture buffer */
|
---|
325 | unsigned mPos;
|
---|
326 | /** Address of the picture buffer */
|
---|
327 | uint8_t *mBuf;
|
---|
328 | };
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Convert an image to YUV420p format
|
---|
332 | * @returns true on success, false on failure
|
---|
333 | * @param aWidth width of image
|
---|
334 | * @param aHeight height of image
|
---|
335 | * @param aDestBuf an allocated memory buffer large enough to hold the
|
---|
336 | * destination image (i.e. width * height * 12bits)
|
---|
337 | * @param aSrcBuf the source image as an array of bytes
|
---|
338 | */
|
---|
339 | template <class T>
|
---|
340 | inline bool colorConvWriteYUV420p(unsigned aWidth, unsigned aHeight, uint8_t *aDestBuf, uint8_t *aSrcBuf)
|
---|
341 | {
|
---|
342 | AssertReturn(!(aWidth & 1), false);
|
---|
343 | AssertReturn(!(aHeight & 1), false);
|
---|
344 | bool fRc = true;
|
---|
345 | T iter1(aWidth, aHeight, aSrcBuf);
|
---|
346 | T iter2 = iter1;
|
---|
347 | iter2.skip(aWidth);
|
---|
348 | unsigned cPixels = aWidth * aHeight;
|
---|
349 | unsigned offY = 0;
|
---|
350 | unsigned offU = cPixels;
|
---|
351 | unsigned offV = cPixels + cPixels / 4;
|
---|
352 | unsigned const cyHalf = aHeight / 2;
|
---|
353 | unsigned const cxHalf = aWidth / 2;
|
---|
354 | for (unsigned i = 0; i < cyHalf && fRc; ++i)
|
---|
355 | {
|
---|
356 | for (unsigned j = 0; j < cxHalf; ++j)
|
---|
357 | {
|
---|
358 | unsigned red, green, blue;
|
---|
359 | fRc = iter1.getRGB(&red, &green, &blue);
|
---|
360 | AssertReturn(fRc, false);
|
---|
361 | aDestBuf[offY] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
362 | unsigned u = (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
363 | unsigned v = (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
364 |
|
---|
365 | fRc = iter1.getRGB(&red, &green, &blue);
|
---|
366 | AssertReturn(fRc, false);
|
---|
367 | aDestBuf[offY + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
368 | u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
369 | v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
370 |
|
---|
371 | fRc = iter2.getRGB(&red, &green, &blue);
|
---|
372 | AssertReturn(fRc, false);
|
---|
373 | aDestBuf[offY + aWidth] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
374 | u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
375 | v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
376 |
|
---|
377 | fRc = iter2.getRGB(&red, &green, &blue);
|
---|
378 | AssertReturn(fRc, false);
|
---|
379 | aDestBuf[offY + aWidth + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
380 | u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
381 | v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
382 |
|
---|
383 | aDestBuf[offU] = u;
|
---|
384 | aDestBuf[offV] = v;
|
---|
385 | offY += 2;
|
---|
386 | ++offU;
|
---|
387 | ++offV;
|
---|
388 | }
|
---|
389 |
|
---|
390 | iter1.skip(aWidth);
|
---|
391 | iter2.skip(aWidth);
|
---|
392 | offY += aWidth;
|
---|
393 | }
|
---|
394 |
|
---|
395 | return true;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Convert an image to RGB24 format
|
---|
400 | * @returns true on success, false on failure
|
---|
401 | * @param aWidth width of image
|
---|
402 | * @param aHeight height of image
|
---|
403 | * @param aDestBuf an allocated memory buffer large enough to hold the
|
---|
404 | * destination image (i.e. width * height * 12bits)
|
---|
405 | * @param aSrcBuf the source image as an array of bytes
|
---|
406 | */
|
---|
407 | template <class T>
|
---|
408 | inline bool colorConvWriteRGB24(unsigned aWidth, unsigned aHeight,
|
---|
409 | uint8_t *aDestBuf, uint8_t *aSrcBuf)
|
---|
410 | {
|
---|
411 | enum { PIX_SIZE = 3 };
|
---|
412 | bool rc = true;
|
---|
413 | AssertReturn(0 == (aWidth & 1), false);
|
---|
414 | AssertReturn(0 == (aHeight & 1), false);
|
---|
415 | T iter(aWidth, aHeight, aSrcBuf);
|
---|
416 | unsigned cPixels = aWidth * aHeight;
|
---|
417 | for (unsigned i = 0; i < cPixels && rc; ++i)
|
---|
418 | {
|
---|
419 | unsigned red, green, blue;
|
---|
420 | rc = iter.getRGB(&red, &green, &blue);
|
---|
421 | if (rc)
|
---|
422 | {
|
---|
423 | aDestBuf[i * PIX_SIZE ] = red;
|
---|
424 | aDestBuf[i * PIX_SIZE + 1] = green;
|
---|
425 | aDestBuf[i * PIX_SIZE + 2] = blue;
|
---|
426 | }
|
---|
427 | }
|
---|
428 | return rc;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Worker thread for all streams of a video recording context.
|
---|
433 | *
|
---|
434 | * Does RGB/YUV conversion and encoding.
|
---|
435 | */
|
---|
436 | static DECLCALLBACK(int) videoRecThread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
437 | {
|
---|
438 | RT_NOREF(hThreadSelf);
|
---|
439 | PVIDEORECCONTEXT pCtx = (PVIDEORECCONTEXT)pvUser;
|
---|
440 | for (;;)
|
---|
441 | {
|
---|
442 | int rc = RTSemEventWait(pCtx->WaitEvent, RT_INDEFINITE_WAIT);
|
---|
443 | AssertRCBreak(rc);
|
---|
444 |
|
---|
445 | if (ASMAtomicReadU32(&g_enmState) == VIDEORECSTS_TERMINATING)
|
---|
446 | break;
|
---|
447 |
|
---|
448 | for (VideoRecStreams::iterator it = pCtx->vecStreams.begin(); it != pCtx->vecStreams.end(); it++)
|
---|
449 | {
|
---|
450 | PVIDEORECSTREAM pStream = (*it);
|
---|
451 |
|
---|
452 | if ( pStream->fEnabled
|
---|
453 | && ASMAtomicReadBool(&pStream->fRgbFilled))
|
---|
454 | {
|
---|
455 | rc = videoRecRGBToYUV(pStream);
|
---|
456 |
|
---|
457 | ASMAtomicWriteBool(&pStream->fRgbFilled, false);
|
---|
458 |
|
---|
459 | if (RT_SUCCESS(rc))
|
---|
460 | rc = videoRecEncodeAndWrite(pStream);
|
---|
461 |
|
---|
462 | if (RT_FAILURE(rc))
|
---|
463 | {
|
---|
464 | static unsigned cErrors = 100;
|
---|
465 | if (cErrors > 0)
|
---|
466 | {
|
---|
467 | LogRel(("VideoRec: Error %Rrc encoding / writing video frame\n", rc));
|
---|
468 | cErrors--;
|
---|
469 | }
|
---|
470 | }
|
---|
471 | }
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | return VINF_SUCCESS;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * Creates a video recording context.
|
---|
480 | *
|
---|
481 | * @returns IPRT status code.
|
---|
482 | * @param cScreens Number of screens to create context for.
|
---|
483 | * @param ppCtx Pointer to created video recording context on success.
|
---|
484 | */
|
---|
485 | int VideoRecContextCreate(uint32_t cScreens, PVIDEORECCONTEXT *ppCtx)
|
---|
486 | {
|
---|
487 | AssertReturn(cScreens, VERR_INVALID_PARAMETER);
|
---|
488 | AssertPtrReturn(ppCtx, VERR_INVALID_POINTER);
|
---|
489 |
|
---|
490 | Assert(ASMAtomicReadU32(&g_enmState) == VIDEORECSTS_UNINITIALIZED);
|
---|
491 |
|
---|
492 | int rc = VINF_SUCCESS;
|
---|
493 |
|
---|
494 | PVIDEORECCONTEXT pCtx = (PVIDEORECCONTEXT)RTMemAllocZ(sizeof(VIDEORECCONTEXT));
|
---|
495 | if (!pCtx)
|
---|
496 | return VERR_NO_MEMORY;
|
---|
497 |
|
---|
498 | for (uint32_t uScreen = 0; uScreen < cScreens; uScreen++)
|
---|
499 | {
|
---|
500 | PVIDEORECSTREAM pStream = (PVIDEORECSTREAM)RTMemAllocZ(sizeof(VIDEORECSTREAM));
|
---|
501 | if (!pStream)
|
---|
502 | {
|
---|
503 | rc = VERR_NO_MEMORY;
|
---|
504 | break;
|
---|
505 | }
|
---|
506 |
|
---|
507 | try
|
---|
508 | {
|
---|
509 | pCtx->vecStreams.push_back(pStream);
|
---|
510 |
|
---|
511 | pStream->pEBML = new WebMWriter();
|
---|
512 | }
|
---|
513 | catch (std::bad_alloc)
|
---|
514 | {
|
---|
515 | rc = VERR_NO_MEMORY;
|
---|
516 | break;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | if (RT_SUCCESS(rc))
|
---|
521 | {
|
---|
522 | rc = RTSemEventCreate(&pCtx->WaitEvent);
|
---|
523 | AssertRCReturn(rc, rc);
|
---|
524 |
|
---|
525 | rc = RTSemEventCreate(&pCtx->TermEvent);
|
---|
526 | AssertRCReturn(rc, rc);
|
---|
527 |
|
---|
528 | rc = RTThreadCreate(&pCtx->Thread, videoRecThread, (void*)pCtx, 0,
|
---|
529 | RTTHREADTYPE_MAIN_WORKER, RTTHREADFLAGS_WAITABLE, "VideoRec");
|
---|
530 | AssertRCReturn(rc, rc);
|
---|
531 |
|
---|
532 | ASMAtomicWriteU32(&g_enmState, VIDEORECSTS_IDLE);
|
---|
533 |
|
---|
534 | if (ppCtx)
|
---|
535 | *ppCtx = pCtx;
|
---|
536 | }
|
---|
537 | else
|
---|
538 | {
|
---|
539 | /* Roll back allocations on error. */
|
---|
540 | VideoRecStreams::iterator it = pCtx->vecStreams.begin();
|
---|
541 | while (it != pCtx->vecStreams.end())
|
---|
542 | {
|
---|
543 | PVIDEORECSTREAM pStream = (*it);
|
---|
544 |
|
---|
545 | if (pStream->pEBML)
|
---|
546 | delete pStream->pEBML;
|
---|
547 |
|
---|
548 | it = pCtx->vecStreams.erase(it);
|
---|
549 |
|
---|
550 | RTMemFree(pStream);
|
---|
551 | pStream = NULL;
|
---|
552 | }
|
---|
553 |
|
---|
554 | Assert(pCtx->vecStreams.empty());
|
---|
555 | }
|
---|
556 |
|
---|
557 | return rc;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Destroys a video recording context.
|
---|
562 | *
|
---|
563 | * @param pCtx Video recording context to destroy.
|
---|
564 | */
|
---|
565 | void VideoRecContextDestroy(PVIDEORECCONTEXT pCtx)
|
---|
566 | {
|
---|
567 | if (!pCtx)
|
---|
568 | return;
|
---|
569 |
|
---|
570 | uint32_t enmState = VIDEORECSTS_IDLE;
|
---|
571 |
|
---|
572 | for (;;) /** @todo r=andy Remove busy waiting! */
|
---|
573 | {
|
---|
574 | if (ASMAtomicCmpXchgExU32(&g_enmState, VIDEORECSTS_TERMINATING, enmState, &enmState))
|
---|
575 | break;
|
---|
576 | if (enmState == VIDEORECSTS_UNINITIALIZED)
|
---|
577 | return;
|
---|
578 | }
|
---|
579 |
|
---|
580 | if (enmState == VIDEORECSTS_COPYING)
|
---|
581 | {
|
---|
582 | int rc = RTSemEventWait(pCtx->TermEvent, RT_INDEFINITE_WAIT);
|
---|
583 | AssertRC(rc);
|
---|
584 | }
|
---|
585 |
|
---|
586 | RTSemEventSignal(pCtx->WaitEvent);
|
---|
587 | RTThreadWait(pCtx->Thread, 10 * 1000, NULL);
|
---|
588 | RTSemEventDestroy(pCtx->WaitEvent);
|
---|
589 | RTSemEventDestroy(pCtx->TermEvent);
|
---|
590 |
|
---|
591 | VideoRecStreams::iterator it = pCtx->vecStreams.begin();
|
---|
592 | while (it != pCtx->vecStreams.end())
|
---|
593 | {
|
---|
594 | PVIDEORECSTREAM pStream = (*it);
|
---|
595 |
|
---|
596 | if (pStream->fEnabled)
|
---|
597 | {
|
---|
598 | AssertPtr(pStream->pEBML);
|
---|
599 | pStream->pEBML->Close();
|
---|
600 |
|
---|
601 | vpx_img_free(&pStream->Codec.VPX.RawImage);
|
---|
602 | vpx_codec_err_t rcv = vpx_codec_destroy(&pStream->Codec.VPX.CodecCtx);
|
---|
603 | Assert(rcv == VPX_CODEC_OK); RT_NOREF(rcv);
|
---|
604 |
|
---|
605 | if (pStream->pu8RgbBuf)
|
---|
606 | {
|
---|
607 | RTMemFree(pStream->pu8RgbBuf);
|
---|
608 | pStream->pu8RgbBuf = NULL;
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | if (pStream->pEBML)
|
---|
613 | {
|
---|
614 | delete pStream->pEBML;
|
---|
615 | pStream->pEBML = NULL;
|
---|
616 | }
|
---|
617 |
|
---|
618 | it = pCtx->vecStreams.erase(it);
|
---|
619 |
|
---|
620 | RTMemFree(pStream);
|
---|
621 | pStream = NULL;
|
---|
622 | }
|
---|
623 |
|
---|
624 | Assert(pCtx->vecStreams.empty());
|
---|
625 | RTMemFree(pCtx);
|
---|
626 |
|
---|
627 | ASMAtomicWriteU32(&g_enmState, VIDEORECSTS_UNINITIALIZED);
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * VideoRec utility function to initialize video recording context.
|
---|
632 | *
|
---|
633 | * @returns IPRT status code.
|
---|
634 | * @param pCtx Pointer to video recording context to initialize Framebuffer width.
|
---|
635 | * @param uScreen Screen number.
|
---|
636 | * @param pszFile File to save the recorded data
|
---|
637 | * @param uWidth Width of the target image in the video recoriding file (movie)
|
---|
638 | * @param uHeight Height of the target image in video recording file.
|
---|
639 | * @param uRate Rate.
|
---|
640 | * @param uFps FPS.
|
---|
641 | * @param uMaxTime
|
---|
642 | * @param uMaxFileSize
|
---|
643 | * @param pszOptions
|
---|
644 | */
|
---|
645 | int VideoRecStreamInit(PVIDEORECCONTEXT pCtx, uint32_t uScreen, const char *pszFile,
|
---|
646 | uint32_t uWidth, uint32_t uHeight, uint32_t uRate, uint32_t uFps,
|
---|
647 | uint32_t uMaxTime, uint32_t uMaxFileSize, const char *pszOptions)
|
---|
648 | {
|
---|
649 | AssertPtrReturn(pCtx, VERR_INVALID_PARAMETER);
|
---|
650 | AssertReturn(uScreen < pCtx->vecStreams.size(), VERR_INVALID_PARAMETER);
|
---|
651 |
|
---|
652 | pCtx->u64MaxTimeStamp = (uMaxTime > 0 ? RTTimeProgramMilliTS() + uMaxTime * 1000 : 0);
|
---|
653 | pCtx->uMaxFileSize = uMaxFileSize;
|
---|
654 |
|
---|
655 | PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
|
---|
656 |
|
---|
657 | pStream->uTargetWidth = uWidth;
|
---|
658 | pStream->uTargetHeight = uHeight;
|
---|
659 | pStream->pu8RgbBuf = (uint8_t *)RTMemAllocZ(uWidth * uHeight * 4);
|
---|
660 | AssertReturn(pStream->pu8RgbBuf, VERR_NO_MEMORY);
|
---|
661 |
|
---|
662 | /* Play safe: the file must not exist, overwriting is potentially
|
---|
663 | * hazardous as nothing prevents the user from picking a file name of some
|
---|
664 | * other important file, causing unintentional data loss. */
|
---|
665 |
|
---|
666 | #ifdef VBOX_WITH_LIBVPX
|
---|
667 | pStream->uEncoderDeadline = VPX_DL_REALTIME;
|
---|
668 |
|
---|
669 | vpx_codec_err_t rcv = vpx_codec_enc_config_default(DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
|
---|
670 | if (rcv != VPX_CODEC_OK)
|
---|
671 | {
|
---|
672 | LogRel(("VideoRec: Failed to get default configuration for VPX codec: %s\n", vpx_codec_err_to_string(rcv)));
|
---|
673 | return VERR_INVALID_PARAMETER;
|
---|
674 | }
|
---|
675 | #endif
|
---|
676 |
|
---|
677 | com::Utf8Str options(pszOptions);
|
---|
678 | size_t pos = 0;
|
---|
679 |
|
---|
680 | /* By default we enable everything (if available). */
|
---|
681 | bool fHasVideoTrack = true;
|
---|
682 | #ifdef VBOX_WITH_AUDIO_VIDEOREC
|
---|
683 | bool fHasAudioTrack = true;
|
---|
684 | #endif
|
---|
685 |
|
---|
686 | com::Utf8Str key, value;
|
---|
687 | while ((pos = options.parseKeyValue(key, value, pos)) != com::Utf8Str::npos)
|
---|
688 | {
|
---|
689 | if (key.compare("vc_quality", Utf8Str::CaseInsensitive) == 0)
|
---|
690 | {
|
---|
691 | if (value.compare("realtime", Utf8Str::CaseInsensitive) == 0)
|
---|
692 | {
|
---|
693 | #ifdef VBOX_WITH_LIBVPX
|
---|
694 | pStream->uEncoderDeadline = VPX_DL_REALTIME;
|
---|
695 | #endif
|
---|
696 | }
|
---|
697 | else if (value.compare("good", Utf8Str::CaseInsensitive) == 0)
|
---|
698 | {
|
---|
699 | pStream->uEncoderDeadline = 1000000 / uFps;
|
---|
700 | }
|
---|
701 | else if (value.compare("best", Utf8Str::CaseInsensitive) == 0)
|
---|
702 | {
|
---|
703 | #ifdef VBOX_WITH_LIBVPX
|
---|
704 | pStream->uEncoderDeadline = VPX_DL_BEST_QUALITY;
|
---|
705 | #endif
|
---|
706 | }
|
---|
707 | else
|
---|
708 | {
|
---|
709 | LogRel(("VideoRec: Setting quality deadline to '%s'\n", value.c_str()));
|
---|
710 | pStream->uEncoderDeadline = value.toUInt32();
|
---|
711 | }
|
---|
712 | }
|
---|
713 | else if (key.compare("vc_enabled", Utf8Str::CaseInsensitive) == 0)
|
---|
714 | {
|
---|
715 | #ifdef VBOX_WITH_AUDIO_VIDEOREC
|
---|
716 | if (value.compare("false", Utf8Str::CaseInsensitive) == 0) /* Disable audio. */
|
---|
717 | {
|
---|
718 | fHasVideoTrack = false;
|
---|
719 | LogRel(("VideoRec: Only audio will be recorded\n"));
|
---|
720 | }
|
---|
721 | #endif
|
---|
722 | }
|
---|
723 | else if (key.compare("ac_enabled", Utf8Str::CaseInsensitive) == 0)
|
---|
724 | {
|
---|
725 | #ifdef VBOX_WITH_AUDIO_VIDEOREC
|
---|
726 | if (value.compare("false", Utf8Str::CaseInsensitive)) /* Disable audio. */
|
---|
727 | {
|
---|
728 | fHasAudioTrack = false;
|
---|
729 | LogRel(("VideoRec: Only video will be recorded\n"));
|
---|
730 | }
|
---|
731 | #endif
|
---|
732 | }
|
---|
733 | else
|
---|
734 | LogRel(("VideoRec: Unknown option '%s' (value '%s'), skipping\n", key.c_str(), value.c_str()));
|
---|
735 |
|
---|
736 | } /* while */
|
---|
737 |
|
---|
738 | uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_WRITE;
|
---|
739 | #ifdef DEBUG
|
---|
740 | fOpen |= RTFILE_O_CREATE_REPLACE;
|
---|
741 | #else
|
---|
742 | fOpen |= RTFILE_O_CREATE;
|
---|
743 | #endif
|
---|
744 |
|
---|
745 | int rc = pStream->pEBML->Create(pszFile, fOpen, WebMWriter::AudioCodec_Opus, WebMWriter::VideoCodec_VP8);
|
---|
746 | if (RT_FAILURE(rc))
|
---|
747 | {
|
---|
748 | LogRel(("VideoRec: Failed to create the video capture output file '%s' (%Rrc)\n", pszFile, rc));
|
---|
749 | return rc;
|
---|
750 | }
|
---|
751 |
|
---|
752 | pStream->uDelay = 1000 / uFps;
|
---|
753 |
|
---|
754 | if (fHasVideoTrack)
|
---|
755 | {
|
---|
756 | rc = pStream->pEBML->AddVideoTrack(uWidth, uHeight, uFps);
|
---|
757 | if (RT_FAILURE(rc))
|
---|
758 | {
|
---|
759 | LogRel(("VideoRec: Failed to add video track to output file '%s' (%Rrc)\n", pszFile, rc));
|
---|
760 | return rc;
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 | #ifdef VBOX_WITH_LIBVPX
|
---|
765 | /* target bitrate in kilobits per second */
|
---|
766 | pStream->Codec.VPX.Config.rc_target_bitrate = uRate;
|
---|
767 | /* frame width */
|
---|
768 | pStream->Codec.VPX.Config.g_w = uWidth;
|
---|
769 | /* frame height */
|
---|
770 | pStream->Codec.VPX.Config.g_h = uHeight;
|
---|
771 | /* 1ms per frame */
|
---|
772 | pStream->Codec.VPX.Config.g_timebase.num = 1;
|
---|
773 | pStream->Codec.VPX.Config.g_timebase.den = 1000;
|
---|
774 | /* disable multithreading */
|
---|
775 | pStream->Codec.VPX.Config.g_threads = 0;
|
---|
776 |
|
---|
777 | /* Initialize codec. */
|
---|
778 | rcv = vpx_codec_enc_init(&pStream->Codec.VPX.CodecCtx, DEFAULTCODEC, &pStream->Codec.VPX.Config, 0);
|
---|
779 | if (rcv != VPX_CODEC_OK)
|
---|
780 | {
|
---|
781 | LogFlow(("Failed to initialize VP8 encoder %s", vpx_codec_err_to_string(rcv)));
|
---|
782 | return VERR_INVALID_PARAMETER;
|
---|
783 | }
|
---|
784 |
|
---|
785 | if (!vpx_img_alloc(&pStream->Codec.VPX.RawImage, VPX_IMG_FMT_I420, uWidth, uHeight, 1))
|
---|
786 | {
|
---|
787 | LogFlow(("Failed to allocate image %dx%d", uWidth, uHeight));
|
---|
788 | return VERR_NO_MEMORY;
|
---|
789 | }
|
---|
790 |
|
---|
791 | pStream->pu8YuvBuf = pStream->Codec.VPX.RawImage.planes[0];
|
---|
792 | #endif
|
---|
793 |
|
---|
794 | pCtx->fEnabled = true;
|
---|
795 | pStream->fEnabled = true;
|
---|
796 |
|
---|
797 | return VINF_SUCCESS;
|
---|
798 | }
|
---|
799 |
|
---|
800 | /**
|
---|
801 | * VideoRec utility function to check if recording is enabled.
|
---|
802 | *
|
---|
803 | * @returns true if recording is enabled
|
---|
804 | * @param pCtx Pointer to video recording context.
|
---|
805 | */
|
---|
806 | bool VideoRecIsEnabled(PVIDEORECCONTEXT pCtx)
|
---|
807 | {
|
---|
808 | RT_NOREF(pCtx);
|
---|
809 | uint32_t enmState = ASMAtomicReadU32(&g_enmState);
|
---|
810 | return ( enmState == VIDEORECSTS_IDLE
|
---|
811 | || enmState == VIDEORECSTS_COPYING);
|
---|
812 | }
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * VideoRec utility function to check if recording engine is ready to accept a new frame
|
---|
816 | * for the given screen.
|
---|
817 | *
|
---|
818 | * @returns true if recording engine is ready
|
---|
819 | * @param pCtx Pointer to video recording context.
|
---|
820 | * @param uScreen Screen ID.
|
---|
821 | * @param u64TimeStamp Current time stamp.
|
---|
822 | */
|
---|
823 | bool VideoRecIsReady(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint64_t u64TimeStamp)
|
---|
824 | {
|
---|
825 | uint32_t enmState = ASMAtomicReadU32(&g_enmState);
|
---|
826 | if (enmState != VIDEORECSTS_IDLE)
|
---|
827 | return false;
|
---|
828 |
|
---|
829 | PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
|
---|
830 | if (!pStream->fEnabled)
|
---|
831 | return false;
|
---|
832 |
|
---|
833 | if (u64TimeStamp < pStream->u64LastTimeStamp + pStream->uDelay)
|
---|
834 | return false;
|
---|
835 |
|
---|
836 | if (ASMAtomicReadBool(&pStream->fRgbFilled))
|
---|
837 | return false;
|
---|
838 |
|
---|
839 | return true;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /**
|
---|
843 | * VideoRec utility function to check if the file size has reached
|
---|
844 | * specified limits (if any).
|
---|
845 | *
|
---|
846 | * @returns true if any limit has been reached.
|
---|
847 | * @param pCtx Pointer to video recording context.
|
---|
848 | * @param uScreen Screen ID.
|
---|
849 | * @param u64TimeStamp Current time stamp.
|
---|
850 | */
|
---|
851 |
|
---|
852 | bool VideoRecIsFull(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint64_t u64TimeStamp)
|
---|
853 | {
|
---|
854 | PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
|
---|
855 | if(!pStream->fEnabled)
|
---|
856 | return false;
|
---|
857 |
|
---|
858 | if(pCtx->u64MaxTimeStamp > 0 && u64TimeStamp >= pCtx->u64MaxTimeStamp)
|
---|
859 | return true;
|
---|
860 |
|
---|
861 | if (pCtx->uMaxFileSize > 0)
|
---|
862 | {
|
---|
863 | uint64_t sizeInMB = pStream->pEBML->GetFileSize() / (1024 * 1024);
|
---|
864 | if(sizeInMB >= pCtx->uMaxFileSize)
|
---|
865 | return true;
|
---|
866 | }
|
---|
867 | /* Check for available free disk space */
|
---|
868 | if (pStream->pEBML->GetAvailableSpace() < 0x100000)
|
---|
869 | {
|
---|
870 | LogRel(("VideoRec: Not enough free storage space available, stopping video capture\n"));
|
---|
871 | return true;
|
---|
872 | }
|
---|
873 |
|
---|
874 | return false;
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * VideoRec utility function to encode the source image and write the encoded
|
---|
879 | * image to target file.
|
---|
880 | *
|
---|
881 | * @returns IPRT status code.
|
---|
882 | * @param pStream Stream to encode and write.
|
---|
883 | */
|
---|
884 | static int videoRecEncodeAndWrite(PVIDEORECSTREAM pStream)
|
---|
885 | {
|
---|
886 | int rc;
|
---|
887 |
|
---|
888 | #ifdef VBOX_WITH_LIBVPX
|
---|
889 | /* presentation time stamp */
|
---|
890 | vpx_codec_pts_t pts = pStream->u64TimeStamp;
|
---|
891 | vpx_codec_err_t rcv = vpx_codec_encode(&pStream->Codec.VPX.CodecCtx,
|
---|
892 | &pStream->Codec.VPX.RawImage,
|
---|
893 | pts /* time stamp */,
|
---|
894 | pStream->uDelay /* how long to show this frame */,
|
---|
895 | 0 /* flags */,
|
---|
896 | pStream->uEncoderDeadline /* quality setting */);
|
---|
897 | if (rcv != VPX_CODEC_OK)
|
---|
898 | {
|
---|
899 | LogFlow(("Failed to encode:%s\n", vpx_codec_err_to_string(rcv)));
|
---|
900 | return VERR_GENERAL_FAILURE;
|
---|
901 | }
|
---|
902 |
|
---|
903 | vpx_codec_iter_t iter = NULL;
|
---|
904 | rc = VERR_NO_DATA;
|
---|
905 | for (;;)
|
---|
906 | {
|
---|
907 | const vpx_codec_cx_pkt_t *pPacket = vpx_codec_get_cx_data(&pStream->Codec.VPX.CodecCtx, &iter);
|
---|
908 | if (!pPacket)
|
---|
909 | break;
|
---|
910 |
|
---|
911 | switch (pPacket->kind)
|
---|
912 | {
|
---|
913 | case VPX_CODEC_CX_FRAME_PKT:
|
---|
914 | {
|
---|
915 | WebMWriter::BlockData_VP8 blockData = { &pStream->Codec.VPX.Config, pPacket };
|
---|
916 | rc = pStream->pEBML->WriteBlock(WebMWriter::BlockType_Video, &blockData, sizeof(blockData));
|
---|
917 | break;
|
---|
918 | }
|
---|
919 |
|
---|
920 | default:
|
---|
921 | LogFlow(("Unexpected CODEC packet kind %ld\n", pPacket->kind));
|
---|
922 | break;
|
---|
923 | }
|
---|
924 | }
|
---|
925 |
|
---|
926 | pStream->cFrame++;
|
---|
927 | #else
|
---|
928 | RT_NOREF(pStream);
|
---|
929 | rc = VERR_NOT_SUPPORTED;
|
---|
930 | #endif /* VBOX_WITH_LIBVPX */
|
---|
931 | return rc;
|
---|
932 | }
|
---|
933 |
|
---|
934 | /**
|
---|
935 | * VideoRec utility function to convert RGB to YUV.
|
---|
936 | *
|
---|
937 | * @returns IPRT status code.
|
---|
938 | * @param pStrm Strm.
|
---|
939 | */
|
---|
940 | static int videoRecRGBToYUV(PVIDEORECSTREAM pStrm)
|
---|
941 | {
|
---|
942 | switch (pStrm->u32PixelFormat)
|
---|
943 | {
|
---|
944 | case VIDEORECPIXELFMT_RGB32:
|
---|
945 | LogFlow(("32 bit\n"));
|
---|
946 | if (!colorConvWriteYUV420p<ColorConvBGRA32Iter>(pStrm->uTargetWidth,
|
---|
947 | pStrm->uTargetHeight,
|
---|
948 | pStrm->pu8YuvBuf,
|
---|
949 | pStrm->pu8RgbBuf))
|
---|
950 | return VERR_INVALID_PARAMETER;
|
---|
951 | break;
|
---|
952 | case VIDEORECPIXELFMT_RGB24:
|
---|
953 | LogFlow(("24 bit\n"));
|
---|
954 | if (!colorConvWriteYUV420p<ColorConvBGR24Iter>(pStrm->uTargetWidth,
|
---|
955 | pStrm->uTargetHeight,
|
---|
956 | pStrm->pu8YuvBuf,
|
---|
957 | pStrm->pu8RgbBuf))
|
---|
958 | return VERR_INVALID_PARAMETER;
|
---|
959 | break;
|
---|
960 | case VIDEORECPIXELFMT_RGB565:
|
---|
961 | LogFlow(("565 bit\n"));
|
---|
962 | if (!colorConvWriteYUV420p<ColorConvBGR565Iter>(pStrm->uTargetWidth,
|
---|
963 | pStrm->uTargetHeight,
|
---|
964 | pStrm->pu8YuvBuf,
|
---|
965 | pStrm->pu8RgbBuf))
|
---|
966 | return VERR_INVALID_PARAMETER;
|
---|
967 | break;
|
---|
968 | default:
|
---|
969 | return VERR_NOT_SUPPORTED;
|
---|
970 | }
|
---|
971 | return VINF_SUCCESS;
|
---|
972 | }
|
---|
973 |
|
---|
974 | /**
|
---|
975 | * VideoRec utility function to copy a source image (FrameBuf) to the intermediate
|
---|
976 | * RGB buffer. This function is executed only once per time.
|
---|
977 | *
|
---|
978 | * @thread EMT
|
---|
979 | *
|
---|
980 | * @returns IPRT status code.
|
---|
981 | * @param pCtx Pointer to the video recording context.
|
---|
982 | * @param uScreen Screen number.
|
---|
983 | * @param x Starting x coordinate of the source buffer (Framebuffer).
|
---|
984 | * @param y Starting y coordinate of the source buffer (Framebuffer).
|
---|
985 | * @param uPixelFormat Pixel Format.
|
---|
986 | * @param uBitsPerPixel Bits Per Pixel
|
---|
987 | * @param uBytesPerLine Bytes per source scanlineName.
|
---|
988 | * @param uSourceWidth Width of the source image (framebuffer).
|
---|
989 | * @param uSourceHeight Height of the source image (framebuffer).
|
---|
990 | * @param pu8BufAddr Pointer to source image(framebuffer).
|
---|
991 | * @param u64TimeStamp Time stamp (milliseconds).
|
---|
992 | */
|
---|
993 | int VideoRecCopyToIntBuf(PVIDEORECCONTEXT pCtx, uint32_t uScreen, uint32_t x, uint32_t y,
|
---|
994 | uint32_t uPixelFormat, uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
|
---|
995 | uint32_t uSourceWidth, uint32_t uSourceHeight, uint8_t *pu8BufAddr,
|
---|
996 | uint64_t u64TimeStamp)
|
---|
997 | {
|
---|
998 | /* Do not execute during termination and guard against termination */
|
---|
999 | if (!ASMAtomicCmpXchgU32(&g_enmState, VIDEORECSTS_COPYING, VIDEORECSTS_IDLE))
|
---|
1000 | return VINF_TRY_AGAIN;
|
---|
1001 |
|
---|
1002 | int rc = VINF_SUCCESS;
|
---|
1003 | do
|
---|
1004 | {
|
---|
1005 | AssertPtrBreakStmt(pu8BufAddr, rc = VERR_INVALID_PARAMETER);
|
---|
1006 | AssertBreakStmt(uSourceWidth, rc = VERR_INVALID_PARAMETER);
|
---|
1007 | AssertBreakStmt(uSourceHeight, rc = VERR_INVALID_PARAMETER);
|
---|
1008 | AssertBreakStmt(uScreen < pCtx->vecStreams.size(), rc = VERR_INVALID_PARAMETER);
|
---|
1009 |
|
---|
1010 | PVIDEORECSTREAM pStream = pCtx->vecStreams.at(uScreen);
|
---|
1011 | if (!pStream->fEnabled)
|
---|
1012 | {
|
---|
1013 | rc = VINF_TRY_AGAIN; /* not (yet) enabled */
|
---|
1014 | break;
|
---|
1015 | }
|
---|
1016 | if (u64TimeStamp < pStream->u64LastTimeStamp + pStream->uDelay)
|
---|
1017 | {
|
---|
1018 | rc = VINF_TRY_AGAIN; /* respect maximum frames per second */
|
---|
1019 | break;
|
---|
1020 | }
|
---|
1021 | if (ASMAtomicReadBool(&pStream->fRgbFilled))
|
---|
1022 | {
|
---|
1023 | rc = VERR_TRY_AGAIN; /* previous frame not yet encoded */
|
---|
1024 | break;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | pStream->u64LastTimeStamp = u64TimeStamp;
|
---|
1028 |
|
---|
1029 | int xDiff = ((int)pStream->uTargetWidth - (int)uSourceWidth) / 2;
|
---|
1030 | uint32_t w = uSourceWidth;
|
---|
1031 | if ((int)w + xDiff + (int)x <= 0) /* nothing visible */
|
---|
1032 | {
|
---|
1033 | rc = VERR_INVALID_PARAMETER;
|
---|
1034 | break;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | uint32_t destX;
|
---|
1038 | if ((int)x < -xDiff)
|
---|
1039 | {
|
---|
1040 | w += xDiff + x;
|
---|
1041 | x = -xDiff;
|
---|
1042 | destX = 0;
|
---|
1043 | }
|
---|
1044 | else
|
---|
1045 | destX = x + xDiff;
|
---|
1046 |
|
---|
1047 | uint32_t h = uSourceHeight;
|
---|
1048 | int yDiff = ((int)pStream->uTargetHeight - (int)uSourceHeight) / 2;
|
---|
1049 | if ((int)h + yDiff + (int)y <= 0) /* nothing visible */
|
---|
1050 | {
|
---|
1051 | rc = VERR_INVALID_PARAMETER;
|
---|
1052 | break;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | uint32_t destY;
|
---|
1056 | if ((int)y < -yDiff)
|
---|
1057 | {
|
---|
1058 | h += yDiff + (int)y;
|
---|
1059 | y = -yDiff;
|
---|
1060 | destY = 0;
|
---|
1061 | }
|
---|
1062 | else
|
---|
1063 | destY = y + yDiff;
|
---|
1064 |
|
---|
1065 | if ( destX > pStream->uTargetWidth
|
---|
1066 | || destY > pStream->uTargetHeight)
|
---|
1067 | {
|
---|
1068 | rc = VERR_INVALID_PARAMETER; /* nothing visible */
|
---|
1069 | break;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | if (destX + w > pStream->uTargetWidth)
|
---|
1073 | w = pStream->uTargetWidth - destX;
|
---|
1074 |
|
---|
1075 | if (destY + h > pStream->uTargetHeight)
|
---|
1076 | h = pStream->uTargetHeight - destY;
|
---|
1077 |
|
---|
1078 | /* Calculate bytes per pixel */
|
---|
1079 | uint32_t bpp = 1;
|
---|
1080 | if (uPixelFormat == BitmapFormat_BGR)
|
---|
1081 | {
|
---|
1082 | switch (uBitsPerPixel)
|
---|
1083 | {
|
---|
1084 | case 32:
|
---|
1085 | pStream->u32PixelFormat = VIDEORECPIXELFMT_RGB32;
|
---|
1086 | bpp = 4;
|
---|
1087 | break;
|
---|
1088 | case 24:
|
---|
1089 | pStream->u32PixelFormat = VIDEORECPIXELFMT_RGB24;
|
---|
1090 | bpp = 3;
|
---|
1091 | break;
|
---|
1092 | case 16:
|
---|
1093 | pStream->u32PixelFormat = VIDEORECPIXELFMT_RGB565;
|
---|
1094 | bpp = 2;
|
---|
1095 | break;
|
---|
1096 | default:
|
---|
1097 | AssertMsgFailed(("Unknown color depth! mBitsPerPixel=%d\n", uBitsPerPixel));
|
---|
1098 | break;
|
---|
1099 | }
|
---|
1100 | }
|
---|
1101 | else
|
---|
1102 | AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", uPixelFormat));
|
---|
1103 |
|
---|
1104 | /* One of the dimensions of the current frame is smaller than before so
|
---|
1105 | * clear the entire buffer to prevent artifacts from the previous frame */
|
---|
1106 | if ( uSourceWidth < pStream->uLastSourceWidth
|
---|
1107 | || uSourceHeight < pStream->uLastSourceHeight)
|
---|
1108 | memset(pStream->pu8RgbBuf, 0, pStream->uTargetWidth * pStream->uTargetHeight * 4);
|
---|
1109 |
|
---|
1110 | pStream->uLastSourceWidth = uSourceWidth;
|
---|
1111 | pStream->uLastSourceHeight = uSourceHeight;
|
---|
1112 |
|
---|
1113 | /* Calculate start offset in source and destination buffers */
|
---|
1114 | uint32_t offSrc = y * uBytesPerLine + x * bpp;
|
---|
1115 | uint32_t offDst = (destY * pStream->uTargetWidth + destX) * bpp;
|
---|
1116 | /* do the copy */
|
---|
1117 | for (unsigned int i = 0; i < h; i++)
|
---|
1118 | {
|
---|
1119 | /* Overflow check */
|
---|
1120 | Assert(offSrc + w * bpp <= uSourceHeight * uBytesPerLine);
|
---|
1121 | Assert(offDst + w * bpp <= pStream->uTargetHeight * pStream->uTargetWidth * bpp);
|
---|
1122 | memcpy(pStream->pu8RgbBuf + offDst, pu8BufAddr + offSrc, w * bpp);
|
---|
1123 | offSrc += uBytesPerLine;
|
---|
1124 | offDst += pStream->uTargetWidth * bpp;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | pStream->u64TimeStamp = u64TimeStamp;
|
---|
1128 |
|
---|
1129 | ASMAtomicWriteBool(&pStream->fRgbFilled, true);
|
---|
1130 | RTSemEventSignal(pCtx->WaitEvent);
|
---|
1131 | } while (0);
|
---|
1132 |
|
---|
1133 | if (!ASMAtomicCmpXchgU32(&g_enmState, VIDEORECSTS_IDLE, VIDEORECSTS_COPYING))
|
---|
1134 | {
|
---|
1135 | rc = RTSemEventSignal(pCtx->TermEvent);
|
---|
1136 | AssertRC(rc);
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | return rc;
|
---|
1140 | }
|
---|