VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/WebMWriter.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 21 months ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.0 KB
Line 
1/* $Id: WebMWriter.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * WebMWriter.cpp - WebM container handling.
4 */
5
6/*
7 * Copyright (C) 2013-2022 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/**
29 * For more information, see:
30 * - https://w3c.github.io/media-source/webm-byte-stream-format.html
31 * - https://www.webmproject.org/docs/container/#muxer-guidelines
32 */
33
34#define LOG_GROUP LOG_GROUP_RECORDING
35#include "LoggingNew.h"
36
37#include <iprt/buildconfig.h>
38#include <iprt/errcore.h>
39
40#include <VBox/version.h>
41
42#include "RecordingInternals.h"
43#include "WebMWriter.h"
44
45
46WebMWriter::WebMWriter(void)
47{
48 /* Size (in bytes) of time code to write. We use 2 bytes (16 bit) by default. */
49 m_cbTimecode = 2;
50 m_uTimecodeMax = UINT16_MAX;
51
52 m_fInTracksSection = false;
53}
54
55WebMWriter::~WebMWriter(void)
56{
57 Close();
58}
59
60/**
61 * Opens (creates) an output file using an already open file handle.
62 *
63 * @returns VBox status code.
64 * @param a_pszFilename Name of the file the file handle points at.
65 * @param a_phFile Pointer to open file handle to use.
66 * @param a_enmAudioCodec Audio codec to use.
67 * @param a_enmVideoCodec Video codec to use.
68 */
69int WebMWriter::OpenEx(const char *a_pszFilename, PRTFILE a_phFile,
70 RecordingAudioCodec_T a_enmAudioCodec, RecordingVideoCodec_T a_enmVideoCodec)
71{
72 try
73 {
74 LogFunc(("Creating '%s'\n", a_pszFilename));
75
76 int vrc = createEx(a_pszFilename, a_phFile);
77 if (RT_SUCCESS(vrc))
78 {
79 vrc = init(a_enmAudioCodec, a_enmVideoCodec);
80 if (RT_SUCCESS(vrc))
81 vrc = writeHeader();
82 }
83 }
84 catch(int vrc)
85 {
86 return vrc;
87 }
88 return VINF_SUCCESS;
89}
90
91/**
92 * Opens an output file.
93 *
94 * @returns VBox status code.
95 * @param a_pszFilename Name of the file to create.
96 * @param a_fOpen File open mode of type RTFILE_O_.
97 * @param a_enmAudioCodec Audio codec to use.
98 * @param a_enmVideoCodec Video codec to use.
99 */
100int WebMWriter::Open(const char *a_pszFilename, uint64_t a_fOpen,
101 RecordingAudioCodec_T a_enmAudioCodec, RecordingVideoCodec_T a_enmVideoCodec)
102{
103 try
104 {
105 LogFunc(("Creating '%s'\n", a_pszFilename));
106
107 int vrc = create(a_pszFilename, a_fOpen);
108 if (RT_SUCCESS(vrc))
109 {
110 vrc = init(a_enmAudioCodec, a_enmVideoCodec);
111 if (RT_SUCCESS(vrc))
112 vrc = writeHeader();
113 }
114 }
115 catch(int vrc)
116 {
117 return vrc;
118 }
119 return VINF_SUCCESS;
120}
121
122/**
123 * Closes the WebM file and drains all queues.
124 *
125 * @returns VBox status code.
126 */
127int WebMWriter::Close(void)
128{
129 LogFlowFuncEnter();
130
131 if (!isOpen())
132 return VINF_SUCCESS;
133
134 /* Make sure to drain all queues. */
135 processQueue(&m_CurSeg.m_queueBlocks, true /* fForce */);
136
137 writeFooter();
138
139 WebMTracks::iterator itTrack = m_CurSeg.m_mapTracks.begin();
140 while (itTrack != m_CurSeg.m_mapTracks.end())
141 {
142 WebMTrack *pTrack = itTrack->second;
143 if (pTrack) /* Paranoia. */
144 delete pTrack;
145
146 m_CurSeg.m_mapTracks.erase(itTrack);
147
148 itTrack = m_CurSeg.m_mapTracks.begin();
149 }
150
151 Assert(m_CurSeg.m_queueBlocks.Map.size() == 0);
152 Assert(m_CurSeg.m_mapTracks.size() == 0);
153
154 com::Utf8Str strFileName = getFileName().c_str();
155
156 close();
157
158 return VINF_SUCCESS;
159}
160
161/**
162 * Adds an audio track.
163 *
164 * @returns VBox status code.
165 * @param pCodec Audio codec to use.
166 * @param uHz Input sampling rate.
167 * Must be supported by the selected audio codec.
168 * @param cChannels Number of input audio channels.
169 * @param cBits Number of input bits per channel.
170 * @param puTrack Track number on successful creation. Optional.
171 */
172int WebMWriter::AddAudioTrack(PRECORDINGCODEC pCodec, uint16_t uHz, uint8_t cChannels, uint8_t cBits, uint8_t *puTrack)
173{
174#if defined(VBOX_WITH_LIBVORBIS)
175 AssertReturn(uHz, VERR_INVALID_PARAMETER);
176 AssertReturn(cBits, VERR_INVALID_PARAMETER);
177 AssertReturn(cChannels, VERR_INVALID_PARAMETER);
178
179 /* Some players (e.g. Firefox with Nestegg) rely on track numbers starting at 1.
180 * Using a track number 0 will show those files as being corrupted. */
181 const uint8_t uTrack = (uint8_t)m_CurSeg.m_mapTracks.size() + 1;
182
183 subStart(MkvElem_TrackEntry);
184
185 serializeUnsignedInteger(MkvElem_TrackNumber, (uint8_t)uTrack);
186 serializeString (MkvElem_Language, "und" /* "Undefined"; see ISO-639-2. */);
187 serializeUnsignedInteger(MkvElem_FlagLacing, (uint8_t)0);
188
189 int vrc = VINF_SUCCESS;
190
191 WebMTrack *pTrack = NULL;
192 try
193 {
194 pTrack = new WebMTrack(WebMTrackType_Audio, pCodec, uTrack, RTFileTell(getFile()));
195 }
196 catch (std::bad_alloc &)
197 {
198 vrc = VERR_NO_MEMORY;
199 }
200
201 if (RT_SUCCESS(vrc))
202 {
203 serializeUnsignedInteger(MkvElem_TrackUID, pTrack->uUUID, 4)
204 .serializeUnsignedInteger(MkvElem_TrackType, 2 /* Audio */);
205
206 switch (m_enmAudioCodec)
207 {
208# ifdef VBOX_WITH_LIBVORBIS
209 case RecordingAudioCodec_OggVorbis:
210 {
211 #if 0 /* Own header code -- more compact and does not require libvorbis. */
212 WEBMOGGVORBISPRIVDATA vorbisPrivData;
213
214 /* Vorbis I allows the following block sizes (bytes):
215 * 64, 128, 256, 512, 1024, 2048, 4096 and 8192. */
216 pTrack->Audio.msPerBlock = msBlockSize;
217 if (!pTrack->Audio.msPerBlock) /* No ms per frame defined? Use default. */
218 pTrack->Audio.msPerBlock = VBOX_RECORDING_VORBIS_FRAME_MS_DEFAULT;
219
220 serializeString(MkvElem_CodecID, "A_VORBIS");
221 serializeData(MkvElem_CodecPrivate, &vorbisPrivData, sizeof(vorbisPrivData));
222 #else
223 pTrack->Audio.msPerBlock = 0; /** @todo */
224 if (!pTrack->Audio.msPerBlock) /* No ms per frame defined? Use default. */
225 pTrack->Audio.msPerBlock = VBOX_RECORDING_VORBIS_FRAME_MS_DEFAULT;
226
227 #if 0
228 vorbis_info m_vi;
229 /** Encoder state. */
230 vorbis_dsp_state m_vd;
231 /** Current block being worked on. */
232 vorbis_block m_vb;
233 vorbis_comment m_vc;
234 #endif
235
236
237 #if 0 /* Must be done by the caller. */
238 vorbis_info_init(&pCodec->Audio.Vorbis.info);
239 int bitrate = 0;
240 if (bitrate)
241 vorbis_encode_init(&pCodec->Audio.Vorbis.info, cChannels, uHz, -1, 128000, -1);
242 else
243 vorbis_encode_init_vbr(&pCodec->Audio.Vorbis.info, cChannels, uHz, (float).4);
244 vorbis_analysis_init(&pCodec->Audio.Vorbis.dsp_state, &pCodec->Audio.Vorbis.info);
245 vorbis_block_init(&pCodec->Audio.Vorbis.dsp_state, &pCodec->Audio.Vorbis.block_cur);
246 #endif
247
248 vorbis_comment vc;
249 vorbis_comment_init(&vc);
250 vorbis_comment_add_tag(&vc,"ENCODER", vorbis_version_string());
251
252 ogg_packet pkt_ident;
253 ogg_packet pkt_comments;
254 ogg_packet pkt_setup;
255 vorbis_analysis_headerout(&pCodec->Audio.Vorbis.dsp_state, &vc, &pkt_ident, &pkt_comments, &pkt_setup);
256 AssertMsgBreakStmt(pkt_ident.bytes <= 255 && pkt_comments.bytes <= 255,
257 ("Too long header / comment packets\n"), vrc = VERR_INVALID_PARAMETER);
258
259 WEBMOGGVORBISPRIVDATA vorbisPrivData(pkt_ident.bytes, pkt_comments.bytes, pkt_setup.bytes);
260
261 uint8_t *pabHdr = &vorbisPrivData.abHdr[0];
262 memcpy(pabHdr, pkt_ident.packet, pkt_ident.bytes);
263 pabHdr += pkt_ident.bytes;
264 memcpy(pabHdr, pkt_comments.packet, pkt_comments.bytes);
265 pabHdr += pkt_comments.bytes;
266 memcpy(pabHdr, pkt_setup.packet, pkt_setup.bytes);
267 pabHdr += pkt_setup.bytes;
268
269 vorbis_comment_clear(&vc);
270
271 size_t const offHeaders = RT_OFFSETOF(WEBMOGGVORBISPRIVDATA, abHdr);
272
273 serializeString(MkvElem_CodecID, "A_VORBIS");
274 serializeData(MkvElem_CodecPrivate, &vorbisPrivData,
275 offHeaders + pkt_ident.bytes + pkt_comments.bytes + pkt_setup.bytes);
276 #endif
277 break;
278 }
279# endif /* VBOX_WITH_LIBVORBIS */
280 default:
281 AssertFailed(); /* Shouldn't ever happen (tm). */
282 break;
283 }
284 serializeUnsignedInteger(MkvElem_CodecDelay, 0)
285 .serializeUnsignedInteger(MkvElem_SeekPreRoll, 80 * 1000000) /* 80ms in ns. */
286 .subStart(MkvElem_Audio)
287 .serializeFloat(MkvElem_SamplingFrequency, (float)uHz)
288 .serializeUnsignedInteger(MkvElem_Channels, cChannels)
289 .serializeUnsignedInteger(MkvElem_BitDepth, cBits)
290 .subEnd(MkvElem_Audio)
291 .subEnd(MkvElem_TrackEntry);
292
293 pTrack->Audio.uHz = uHz;
294 pTrack->Audio.framesPerBlock = uHz / (1000 /* s in ms */ / pTrack->Audio.msPerBlock);
295
296 LogRel2(("Recording: WebM track #%RU8: Audio codec @ %RU16Hz (%RU16ms, %RU16 frames per block)\n",
297 pTrack->uTrack, pTrack->Audio.uHz, pTrack->Audio.msPerBlock, pTrack->Audio.framesPerBlock));
298
299 m_CurSeg.m_mapTracks[uTrack] = pTrack;
300
301 if (puTrack)
302 *puTrack = uTrack;
303
304 return VINF_SUCCESS;
305 }
306
307 if (pTrack)
308 delete pTrack;
309 return vrc;
310#else /* !defined(VBOX_WITH_LIBVORBIS) */
311 RT_NOREF(pCodec, uHz, cChannels, cBits, puTrack);
312 return VERR_NOT_SUPPORTED;
313#endif
314}
315
316/**
317 * Adds a video track.
318 *
319 * @returns VBox status code.
320 * @param pCodec Codec data to use.
321 * @param uWidth Width (in pixels) of the video track.
322 * @param uHeight Height (in pixels) of the video track.
323 * @param uFPS FPS (Frames Per Second) of the video track.
324 * @param puTrack Track number of the added video track on success. Optional.
325 */
326int WebMWriter::AddVideoTrack(PRECORDINGCODEC pCodec, uint16_t uWidth, uint16_t uHeight, uint32_t uFPS, uint8_t *puTrack)
327{
328#ifdef VBOX_WITH_LIBVPX
329 /* Some players (e.g. Firefox with Nestegg) rely on track numbers starting at 1.
330 * Using a track number 0 will show those files as being corrupted. */
331 const uint8_t uTrack = (uint8_t)m_CurSeg.m_mapTracks.size() + 1;
332
333 subStart(MkvElem_TrackEntry);
334
335 serializeUnsignedInteger(MkvElem_TrackNumber, (uint8_t)uTrack);
336 serializeString (MkvElem_Language, "und" /* "Undefined"; see ISO-639-2. */);
337 serializeUnsignedInteger(MkvElem_FlagLacing, (uint8_t)0);
338
339 WebMTrack *pTrack = new WebMTrack(WebMTrackType_Video, pCodec, uTrack, RTFileTell(getFile()));
340
341 /** @todo Resolve codec type. */
342 serializeUnsignedInteger(MkvElem_TrackUID, pTrack->uUUID /* UID */, 4)
343 .serializeUnsignedInteger(MkvElem_TrackType, 1 /* Video */)
344 .serializeString(MkvElem_CodecID, "V_VP8")
345 .subStart(MkvElem_Video)
346 .serializeUnsignedInteger(MkvElem_PixelWidth, uWidth)
347 .serializeUnsignedInteger(MkvElem_PixelHeight, uHeight)
348 /* Some players rely on the FPS rate for timing calculations.
349 * So make sure to *always* include that. */
350 .serializeFloat (MkvElem_FrameRate, (float)uFPS)
351 .subEnd(MkvElem_Video);
352
353 subEnd(MkvElem_TrackEntry);
354
355 LogRel2(("Recording: WebM track #%RU8: Video\n", pTrack->uTrack));
356
357 m_CurSeg.m_mapTracks[uTrack] = pTrack;
358
359 if (puTrack)
360 *puTrack = uTrack;
361
362 return VINF_SUCCESS;
363#else
364 RT_NOREF(pCodec, uWidth, uHeight, uFPS, puTrack);
365 return VERR_NOT_SUPPORTED;
366#endif
367}
368
369/**
370 * Gets file name.
371 *
372 * @returns File name as UTF-8 string.
373 */
374const com::Utf8Str& WebMWriter::GetFileName(void)
375{
376 return getFileName();
377}
378
379/**
380 * Gets current output file size.
381 *
382 * @returns File size in bytes.
383 */
384uint64_t WebMWriter::GetFileSize(void)
385{
386 return getFileSize();
387}
388
389/**
390 * Gets current free storage space available for the file.
391 *
392 * @returns Available storage free space.
393 */
394uint64_t WebMWriter::GetAvailableSpace(void)
395{
396 return getAvailableSpace();
397}
398
399/**
400 * Takes care of the initialization of the instance.
401 *
402 * @returns VBox status code.
403 * @retval VERR_NOT_SUPPORTED if a given codec is not supported.
404 * @param enmAudioCodec Audio codec to use.
405 * @param enmVideoCodec Video codec to use.
406 */
407int WebMWriter::init(RecordingAudioCodec_T enmAudioCodec, RecordingVideoCodec_T enmVideoCodec)
408{
409#ifndef VBOX_WITH_LIBVORBIS
410 AssertReturn(enmAudioCodec != RecordingAudioCodec_OggVorbis, VERR_NOT_SUPPORTED);
411#endif
412 AssertReturn( enmVideoCodec == RecordingVideoCodec_None
413 || enmVideoCodec == RecordingVideoCodec_VP8, VERR_NOT_SUPPORTED);
414
415 m_enmAudioCodec = enmAudioCodec;
416 m_enmVideoCodec = enmVideoCodec;
417
418 return m_CurSeg.init();
419}
420
421/**
422 * Takes care of the destruction of the instance.
423 */
424void WebMWriter::destroy(void)
425{
426 m_CurSeg.uninit();
427}
428
429/**
430 * Writes the WebM file header.
431 *
432 * @returns VBox status code.
433 */
434int WebMWriter::writeHeader(void)
435{
436 LogFunc(("Header @ %RU64\n", RTFileTell(getFile())));
437
438 subStart(MkvElem_EBML)
439 .serializeUnsignedInteger(MkvElem_EBMLVersion, 1)
440 .serializeUnsignedInteger(MkvElem_EBMLReadVersion, 1)
441 .serializeUnsignedInteger(MkvElem_EBMLMaxIDLength, 4)
442 .serializeUnsignedInteger(MkvElem_EBMLMaxSizeLength, 8)
443 .serializeString(MkvElem_DocType, "webm")
444 .serializeUnsignedInteger(MkvElem_DocTypeVersion, 2)
445 .serializeUnsignedInteger(MkvElem_DocTypeReadVersion, 2)
446 .subEnd(MkvElem_EBML);
447
448 subStart(MkvElem_Segment);
449
450 /* Save offset of current segment. */
451 m_CurSeg.m_offStart = RTFileTell(getFile());
452
453 writeSeekHeader();
454
455 /* Save offset of upcoming tracks segment. */
456 m_CurSeg.m_offTracks = RTFileTell(getFile());
457
458 /* The tracks segment starts right after this header. */
459 subStart(MkvElem_Tracks);
460 m_fInTracksSection = true;
461
462 return VINF_SUCCESS;
463}
464
465/**
466 * Writes a simple block into the EBML structure.
467 *
468 * @returns VBox status code.
469 * @param a_pTrack Track the simple block is assigned to.
470 * @param a_pBlock Simple block to write.
471 */
472int WebMWriter::writeSimpleBlockEBML(WebMTrack *a_pTrack, WebMSimpleBlock *a_pBlock)
473{
474#ifdef LOG_ENABLED
475 WebMCluster &Cluster = m_CurSeg.m_CurCluster;
476
477 Log3Func(("[T%RU8C%RU64] Off=%RU64, AbsPTSMs=%RU64, RelToClusterMs=%RU16, %zu bytes\n",
478 a_pTrack->uTrack, Cluster.uID, RTFileTell(getFile()),
479 a_pBlock->Data.tcAbsPTSMs, a_pBlock->Data.tcRelToClusterMs, a_pBlock->Data.cb));
480#endif
481 /*
482 * Write a "Simple Block".
483 */
484 writeClassId(MkvElem_SimpleBlock);
485 /* Block size. */
486 writeUnsignedInteger(0x10000000u | ( 1 /* Track number size. */
487 + m_cbTimecode /* Timecode size .*/
488 + 1 /* Flags size. */
489 + a_pBlock->Data.cb /* Actual frame data size. */), 4);
490 /* Track number. */
491 writeSize(a_pTrack->uTrack);
492 /* Timecode (relative to cluster opening timecode). */
493 writeUnsignedInteger(a_pBlock->Data.tcRelToClusterMs, m_cbTimecode);
494 /* Flags. */
495 writeUnsignedInteger(a_pBlock->Data.fFlags, 1);
496 /* Frame data. */
497 write(a_pBlock->Data.pv, a_pBlock->Data.cb);
498
499 return VINF_SUCCESS;
500}
501
502/**
503 * Writes a simple block and enqueues it into the segment's render queue.
504 *
505 * @returns VBox status code.
506 * @param a_pTrack Track the simple block is assigned to.
507 * @param a_pBlock Simple block to write and enqueue.
508 */
509int WebMWriter::writeSimpleBlockQueued(WebMTrack *a_pTrack, WebMSimpleBlock *a_pBlock)
510{
511 RT_NOREF(a_pTrack);
512
513 int vrc = VINF_SUCCESS;
514
515 try
516 {
517 const WebMTimecodeAbs tcAbsPTS = a_pBlock->Data.tcAbsPTSMs;
518
519 /* See if we already have an entry for the specified timecode in our queue. */
520 WebMBlockMap::iterator itQueue = m_CurSeg.m_queueBlocks.Map.find(tcAbsPTS);
521 if (itQueue != m_CurSeg.m_queueBlocks.Map.end()) /* Use existing queue. */
522 {
523 WebMTimecodeBlocks &Blocks = itQueue->second;
524 Blocks.Enqueue(a_pBlock);
525 }
526 else /* Create a new timecode entry. */
527 {
528 WebMTimecodeBlocks Blocks;
529 Blocks.Enqueue(a_pBlock);
530
531 m_CurSeg.m_queueBlocks.Map[tcAbsPTS] = Blocks;
532 }
533
534 vrc = processQueue(&m_CurSeg.m_queueBlocks, false /* fForce */);
535 }
536 catch(...)
537 {
538 delete a_pBlock;
539 a_pBlock = NULL;
540
541 vrc = VERR_NO_MEMORY;
542 }
543
544 return vrc;
545}
546
547/**
548 * Writes a data block to the specified track.
549 *
550 * @returns VBox status code.
551 * @param uTrack Track ID to write data to.
552 * @param pvData Pointer to data block to write.
553 * @param cbData Size (in bytes) of data block to write.
554 * @param tcAbsPTSMs Absolute PTS of simple data block.
555 * @param uFlags WebM block flags to use for this block.
556 */
557int WebMWriter::WriteBlock(uint8_t uTrack, const void *pvData, size_t cbData, WebMTimecodeAbs tcAbsPTSMs, WebMBlockFlags uFlags)
558{
559 int vrc = RTCritSectEnter(&m_CurSeg.m_CritSect);
560 AssertRC(vrc);
561
562 WebMTracks::iterator itTrack = m_CurSeg.m_mapTracks.find(uTrack);
563 if (itTrack == m_CurSeg.m_mapTracks.end())
564 {
565 RTCritSectLeave(&m_CurSeg.m_CritSect);
566 return VERR_NOT_FOUND;
567 }
568
569 WebMTrack *pTrack = itTrack->second;
570 AssertPtr(pTrack);
571
572 if (m_fInTracksSection)
573 {
574 subEnd(MkvElem_Tracks);
575 m_fInTracksSection = false;
576 }
577
578 try
579 {
580 vrc = writeSimpleBlockQueued(pTrack,
581 new WebMSimpleBlock(pTrack,
582 tcAbsPTSMs, pvData, cbData, uFlags));
583 }
584 catch (std::bad_alloc &)
585 {
586 vrc = VERR_NO_MEMORY;
587 }
588
589 int vrc2 = RTCritSectLeave(&m_CurSeg.m_CritSect);
590 AssertRC(vrc2);
591
592 return vrc;
593}
594
595/**
596 * Processes a render queue.
597 *
598 * @returns VBox status code.
599 * @param pQueue Queue to process.
600 * @param fForce Whether forcing to process the render queue or not.
601 * Needed to drain the queues when terminating.
602 */
603int WebMWriter::processQueue(WebMQueue *pQueue, bool fForce)
604{
605 if (pQueue->tsLastProcessedMs == 0)
606 pQueue->tsLastProcessedMs = RTTimeMilliTS();
607
608 if (!fForce)
609 {
610 /* Only process when we reached a certain threshold. */
611 if (RTTimeMilliTS() - pQueue->tsLastProcessedMs < 5000 /* ms */ /** @todo Make this configurable */)
612 return VINF_SUCCESS;
613 }
614
615 WebMCluster &Cluster = m_CurSeg.m_CurCluster;
616
617 /* Iterate through the block map. */
618 WebMBlockMap::iterator it = pQueue->Map.begin();
619 while (it != m_CurSeg.m_queueBlocks.Map.end())
620 {
621 WebMTimecodeAbs mapAbsPTSMs = it->first;
622 WebMTimecodeBlocks mapBlocks = it->second;
623
624 /* Whether to start a new cluster or not. */
625 bool fClusterStart = false;
626
627 /* If the current segment does not have any clusters (yet),
628 * take the first absolute PTS as the starting point for that segment. */
629 if (m_CurSeg.m_cClusters == 0)
630 {
631 m_CurSeg.m_tcAbsStartMs = mapAbsPTSMs;
632 fClusterStart = true;
633 }
634
635 /* Determine if we need to start a new cluster. */
636 /* No blocks written yet? Start a new cluster. */
637 if ( Cluster.cBlocks == 0
638 /* Did we reach the maximum a cluster can hold? Use a new cluster then. */
639 || mapAbsPTSMs - Cluster.tcAbsStartMs > VBOX_WEBM_CLUSTER_MAX_LEN_MS
640 /* If the block map indicates that a cluster is needed for this timecode, create one. */
641 || mapBlocks.fClusterNeeded)
642 {
643 fClusterStart = true;
644 }
645
646 if ( fClusterStart
647 && !mapBlocks.fClusterStarted)
648 {
649 /* Last written timecode of the current cluster. */
650 uint64_t tcAbsClusterLastWrittenMs;
651
652 if (Cluster.fOpen) /* Close current cluster first. */
653 {
654 Log2Func(("[C%RU64] End @ %RU64ms (duration = %RU64ms)\n",
655 Cluster.uID, Cluster.tcAbsLastWrittenMs, Cluster.tcAbsLastWrittenMs - Cluster.tcAbsStartMs));
656
657 /* Make sure that the current cluster contained some data. */
658 Assert(Cluster.offStart);
659 Assert(Cluster.cBlocks);
660
661 /* Save the last written timecode of the current cluster before closing it. */
662 tcAbsClusterLastWrittenMs = Cluster.tcAbsLastWrittenMs;
663
664 subEnd(MkvElem_Cluster);
665 Cluster.fOpen = false;
666 }
667 else /* First cluster ever? Use the segment's starting timecode. */
668 tcAbsClusterLastWrittenMs = m_CurSeg.m_tcAbsStartMs;
669
670 Cluster.fOpen = true;
671 Cluster.uID = m_CurSeg.m_cClusters;
672 /* Use the block map's currently processed TC as the cluster's starting TC. */
673 Cluster.tcAbsStartMs = mapAbsPTSMs;
674 Cluster.tcAbsLastWrittenMs = Cluster.tcAbsStartMs;
675 Cluster.offStart = RTFileTell(getFile());
676 Cluster.cBlocks = 0;
677
678 AssertMsg(Cluster.tcAbsStartMs <= mapAbsPTSMs,
679 ("Cluster #%RU64 start TC (%RU64) must not bigger than the block map's currently processed TC (%RU64)\n",
680 Cluster.uID, Cluster.tcAbsStartMs, mapAbsPTSMs));
681
682 Log2Func(("[C%RU64] Start @ %RU64ms (map TC is %RU64) / %RU64 bytes\n",
683 Cluster.uID, Cluster.tcAbsStartMs, mapAbsPTSMs, Cluster.offStart));
684
685 /* Insert cue points for all tracks if a new cluster has been started. */
686 WebMCuePoint *pCuePoint = new WebMCuePoint(Cluster.tcAbsStartMs);
687
688 WebMTracks::iterator itTrack = m_CurSeg.m_mapTracks.begin();
689 while (itTrack != m_CurSeg.m_mapTracks.end())
690 {
691 pCuePoint->Pos[itTrack->first] = new WebMCueTrackPosEntry(Cluster.offStart);
692 ++itTrack;
693 }
694
695 m_CurSeg.m_lstCuePoints.push_back(pCuePoint);
696
697 subStart(MkvElem_Cluster)
698 .serializeUnsignedInteger(MkvElem_Timecode, Cluster.tcAbsStartMs - m_CurSeg.m_tcAbsStartMs);
699
700 m_CurSeg.m_cClusters++;
701
702 mapBlocks.fClusterStarted = true;
703 }
704
705 Log2Func(("[C%RU64] SegTcAbsStartMs=%RU64, ClusterTcAbsStartMs=%RU64, ClusterTcAbsLastWrittenMs=%RU64, mapAbsPTSMs=%RU64\n",
706 Cluster.uID, m_CurSeg.m_tcAbsStartMs, Cluster.tcAbsStartMs, Cluster.tcAbsLastWrittenMs, mapAbsPTSMs));
707
708 /* Iterate through all blocks related to the current timecode. */
709 while (!mapBlocks.Queue.empty())
710 {
711 WebMSimpleBlock *pBlock = mapBlocks.Queue.front();
712 AssertPtr(pBlock);
713
714 WebMTrack *pTrack = pBlock->pTrack;
715 AssertPtr(pTrack);
716
717 /* Calculate the block's relative time code to the current cluster's starting time code. */
718 Assert(pBlock->Data.tcAbsPTSMs >= Cluster.tcAbsStartMs);
719 pBlock->Data.tcRelToClusterMs = pBlock->Data.tcAbsPTSMs - Cluster.tcAbsStartMs;
720
721 int vrc2 = writeSimpleBlockEBML(pTrack, pBlock);
722 AssertRC(vrc2);
723
724 Cluster.cBlocks++;
725 Cluster.tcAbsLastWrittenMs = pBlock->Data.tcAbsPTSMs;
726
727 pTrack->cTotalBlocks++;
728 pTrack->tcAbsLastWrittenMs = Cluster.tcAbsLastWrittenMs;
729
730 if (m_CurSeg.m_tcAbsLastWrittenMs < pTrack->tcAbsLastWrittenMs)
731 m_CurSeg.m_tcAbsLastWrittenMs = pTrack->tcAbsLastWrittenMs;
732
733 /* Save a cue point if this is a keyframe (if no new cluster has been started,
734 * as this implies that a cue point already is present. */
735 if ( !fClusterStart
736 && (pBlock->Data.fFlags & VBOX_WEBM_BLOCK_FLAG_KEY_FRAME))
737 {
738 /* Insert cue points for all tracks if a new cluster has been started. */
739 WebMCuePoint *pCuePoint = new WebMCuePoint(Cluster.tcAbsLastWrittenMs);
740
741 WebMTracks::iterator itTrack = m_CurSeg.m_mapTracks.begin();
742 while (itTrack != m_CurSeg.m_mapTracks.end())
743 {
744 pCuePoint->Pos[itTrack->first] = new WebMCueTrackPosEntry(Cluster.offStart);
745 ++itTrack;
746 }
747
748 m_CurSeg.m_lstCuePoints.push_back(pCuePoint);
749 }
750
751 delete pBlock;
752 pBlock = NULL;
753
754 mapBlocks.Queue.pop();
755 }
756
757 Assert(mapBlocks.Queue.empty());
758
759 m_CurSeg.m_queueBlocks.Map.erase(it);
760
761 it = m_CurSeg.m_queueBlocks.Map.begin();
762 }
763
764 Assert(m_CurSeg.m_queueBlocks.Map.empty());
765
766 pQueue->tsLastProcessedMs = RTTimeMilliTS();
767
768 return VINF_SUCCESS;
769}
770
771/**
772 * Writes the WebM footer.
773 *
774 * @returns VBox status code.
775 */
776int WebMWriter::writeFooter(void)
777{
778 AssertReturn(isOpen(), VERR_WRONG_ORDER);
779
780 if (m_fInTracksSection)
781 {
782 subEnd(MkvElem_Tracks);
783 m_fInTracksSection = false;
784 }
785
786 if (m_CurSeg.m_CurCluster.fOpen)
787 {
788 subEnd(MkvElem_Cluster);
789 m_CurSeg.m_CurCluster.fOpen = false;
790 }
791
792 /*
793 * Write Cues element.
794 */
795 m_CurSeg.m_offCues = RTFileTell(getFile());
796 LogFunc(("Cues @ %RU64\n", m_CurSeg.m_offCues));
797
798 subStart(MkvElem_Cues);
799
800 WebMCuePointList::iterator itCuePoint = m_CurSeg.m_lstCuePoints.begin();
801 while (itCuePoint != m_CurSeg.m_lstCuePoints.end())
802 {
803 WebMCuePoint *pCuePoint = (*itCuePoint);
804 AssertPtr(pCuePoint);
805
806 LogFunc(("CuePoint @ %RU64: %zu tracks, tcAbs=%RU64)\n",
807 RTFileTell(getFile()), pCuePoint->Pos.size(), pCuePoint->tcAbs));
808
809 subStart(MkvElem_CuePoint)
810 .serializeUnsignedInteger(MkvElem_CueTime, pCuePoint->tcAbs);
811
812 WebMCueTrackPosMap::iterator itTrackPos = pCuePoint->Pos.begin();
813 while (itTrackPos != pCuePoint->Pos.end())
814 {
815 WebMCueTrackPosEntry *pTrackPos = itTrackPos->second;
816 AssertPtr(pTrackPos);
817
818 LogFunc(("TrackPos (track #%RU32) @ %RU64, offCluster=%RU64)\n",
819 itTrackPos->first, RTFileTell(getFile()), pTrackPos->offCluster));
820
821 subStart(MkvElem_CueTrackPositions)
822 .serializeUnsignedInteger(MkvElem_CueTrack, itTrackPos->first)
823 .serializeUnsignedInteger(MkvElem_CueClusterPosition, pTrackPos->offCluster - m_CurSeg.m_offStart, 8)
824 .subEnd(MkvElem_CueTrackPositions);
825
826 ++itTrackPos;
827 }
828
829 subEnd(MkvElem_CuePoint);
830
831 ++itCuePoint;
832 }
833
834 subEnd(MkvElem_Cues);
835 subEnd(MkvElem_Segment);
836
837 /*
838 * Re-Update seek header with final information.
839 */
840
841 writeSeekHeader();
842
843 return RTFileSeek(getFile(), 0, RTFILE_SEEK_END, NULL);
844}
845
846/**
847 * Writes the segment's seek header.
848 */
849void WebMWriter::writeSeekHeader(void)
850{
851 if (m_CurSeg.m_offSeekInfo)
852 RTFileSeek(getFile(), m_CurSeg.m_offSeekInfo, RTFILE_SEEK_BEGIN, NULL);
853 else
854 m_CurSeg.m_offSeekInfo = RTFileTell(getFile());
855
856 LogFunc(("Seek Header @ %RU64\n", m_CurSeg.m_offSeekInfo));
857
858 subStart(MkvElem_SeekHead);
859
860 subStart(MkvElem_Seek)
861 .serializeUnsignedInteger(MkvElem_SeekID, MkvElem_Tracks)
862 .serializeUnsignedInteger(MkvElem_SeekPosition, m_CurSeg.m_offTracks - m_CurSeg.m_offStart, 8)
863 .subEnd(MkvElem_Seek);
864
865 if (m_CurSeg.m_offCues)
866 LogFunc(("Updating Cues @ %RU64\n", m_CurSeg.m_offCues));
867
868 subStart(MkvElem_Seek)
869 .serializeUnsignedInteger(MkvElem_SeekID, MkvElem_Cues)
870 .serializeUnsignedInteger(MkvElem_SeekPosition, m_CurSeg.m_offCues - m_CurSeg.m_offStart, 8)
871 .subEnd(MkvElem_Seek);
872
873 subStart(MkvElem_Seek)
874 .serializeUnsignedInteger(MkvElem_SeekID, MkvElem_Info)
875 .serializeUnsignedInteger(MkvElem_SeekPosition, m_CurSeg.m_offInfo - m_CurSeg.m_offStart, 8)
876 .subEnd(MkvElem_Seek);
877
878 subEnd(MkvElem_SeekHead);
879
880 /*
881 * Write the segment's info element.
882 */
883
884 /* Save offset of the segment's info element. */
885 m_CurSeg.m_offInfo = RTFileTell(getFile());
886
887 LogFunc(("Info @ %RU64\n", m_CurSeg.m_offInfo));
888
889 char szMux[64];
890 RTStrPrintf(szMux, sizeof(szMux),
891#ifdef VBOX_WITH_LIBVPX
892 "vpxenc%s", vpx_codec_version_str()
893#else
894 "unknown"
895#endif
896 );
897 char szApp[64];
898 RTStrPrintf(szApp, sizeof(szApp), VBOX_PRODUCT " %sr%u", VBOX_VERSION_STRING, RTBldCfgRevision());
899
900 const WebMTimecodeAbs tcAbsDurationMs = m_CurSeg.m_tcAbsLastWrittenMs - m_CurSeg.m_tcAbsStartMs;
901
902 if (!m_CurSeg.m_lstCuePoints.empty())
903 {
904 LogFunc(("tcAbsDurationMs=%RU64\n", tcAbsDurationMs));
905 AssertMsg(tcAbsDurationMs, ("Segment seems to be empty (duration is 0)\n"));
906 }
907
908 subStart(MkvElem_Info)
909 .serializeUnsignedInteger(MkvElem_TimecodeScale, m_CurSeg.m_uTimecodeScaleFactor)
910 .serializeFloat(MkvElem_Segment_Duration, tcAbsDurationMs)
911 .serializeString(MkvElem_MuxingApp, szMux)
912 .serializeString(MkvElem_WritingApp, szApp)
913 .subEnd(MkvElem_Info);
914}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use