1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
media / muxers / mp4_fragment_box_writer.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/muxers/mp4_fragment_box_writer.h"
#include <memory>
#include <type_traits>
#include <vector>
#include "media/formats/mp4/fourccs.h"
#include "media/muxers/box_byte_stream.h"
#include "media/muxers/mp4_muxer_context.h"
#include "media/muxers/mp4_type_conversion.h"
#include "media/muxers/output_position_tracker.h"
namespace media {
namespace {
void ValidateSampleFlags(uint32_t flags) {
uint32_t allowed_sample_flags =
static_cast<uint32_t>(
mp4::writable_boxes::FragmentSampleFlags::kSampleFlagIsNonSync) |
static_cast<uint32_t>(
mp4::writable_boxes::FragmentSampleFlags::kSampleFlagDependsYes) |
static_cast<uint32_t>(
mp4::writable_boxes::FragmentSampleFlags::kSampleFlagDependsNo);
CHECK_EQ(flags & ~(allowed_sample_flags), 0u);
}
bool IsVideoTrackBox(const Mp4MuxerContext& context, uint32_t track_id) {
// The Mp4MuxerDelegate sets the track id with plus 1 over track index,
// which is 0 based on the internal fragments list.
if (auto video_track = context.GetVideoTrack()) {
return (video_track.value().index + 1) == track_id;
}
return false;
}
} // namespace
// Mp4MovieFragmentBoxWriter (`moof`) class.
Mp4MovieFragmentBoxWriter::Mp4MovieFragmentBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::MovieFragment& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddChildBox(
std::make_unique<Mp4MovieFragmentHeaderBoxWriter>(context, box_->header));
for (const auto& fragment : box_->track_fragments) {
AddChildBox(std::make_unique<Mp4TrackFragmentBoxWriter>(context, fragment));
}
}
Mp4MovieFragmentBoxWriter::~Mp4MovieFragmentBoxWriter() = default;
void Mp4MovieFragmentBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartBox(mp4::FOURCC_MOOF);
WriteChildren(writer);
writer.EndBox();
}
// Mp4MovieFragmentHeaderBoxWriter (`mfhd`) class.
Mp4MovieFragmentHeaderBoxWriter::Mp4MovieFragmentHeaderBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::MovieFragmentHeader& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4MovieFragmentHeaderBoxWriter::~Mp4MovieFragmentHeaderBoxWriter() = default;
void Mp4MovieFragmentHeaderBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartFullBox(mp4::FOURCC_MFHD, /*flags=*/0);
writer.WriteU32(box_->sequence_number);
writer.EndBox();
}
// Mp4TrackFragmentBoxWriter (`traf`) class.
Mp4TrackFragmentBoxWriter::Mp4TrackFragmentBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::TrackFragment& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
AddChildBox(
std::make_unique<Mp4TrackFragmentHeaderBoxWriter>(context, box_->header));
AddChildBox(std::make_unique<Mp4TrackFragmentDecodeTimeBoxWriter>(
context, box_->decode_time));
AddChildBox(
std::make_unique<Mp4TrackFragmentRunBoxWriter>(context, box_->run));
}
Mp4TrackFragmentBoxWriter::~Mp4TrackFragmentBoxWriter() = default;
void Mp4TrackFragmentBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartBox(mp4::FOURCC_TRAF);
WriteChildren(writer);
writer.EndBox();
}
// Mp4TrackFragmentHeaderBoxWriter (`tfhd`) class.
Mp4TrackFragmentHeaderBoxWriter::Mp4TrackFragmentHeaderBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::TrackFragmentHeader& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4TrackFragmentHeaderBoxWriter::~Mp4TrackFragmentHeaderBoxWriter() = default;
void Mp4TrackFragmentHeaderBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartFullBox(mp4::FOURCC_TFHD, box_->flags);
writer.WriteU32(box_->track_id);
CHECK(box_->flags &
static_cast<uint32_t>(
mp4::writable_boxes::TrackFragmentHeaderFlags::kDefaultBaseIsMoof));
if (box_->flags &
static_cast<uint32_t>(mp4::writable_boxes::TrackFragmentHeaderFlags::
kSampleDescriptionIndexPresent)) {
NOTREACHED();
}
if (box_->flags &
static_cast<uint32_t>(mp4::writable_boxes::TrackFragmentHeaderFlags::
kDefaultSampleDurationPresent)) {
writer.WriteU32(box_->default_sample_duration.InMilliseconds());
}
if (box_->flags &
static_cast<uint32_t>(mp4::writable_boxes::TrackFragmentHeaderFlags::
kDefaultSampleSizePresent)) {
writer.WriteU32(box_->default_sample_size);
}
if (box_->flags &
static_cast<uint32_t>(mp4::writable_boxes::TrackFragmentHeaderFlags::
kkDefaultSampleFlagsPresent)) {
ValidateSampleFlags(box_->default_sample_flags);
writer.WriteU32(box_->default_sample_flags);
}
writer.EndBox();
}
// Mp4TrackFragmentDecodeTimeBoxWriter (`tfdt`) class.
Mp4TrackFragmentDecodeTimeBoxWriter::Mp4TrackFragmentDecodeTimeBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::TrackFragmentDecodeTime& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4TrackFragmentDecodeTimeBoxWriter::~Mp4TrackFragmentDecodeTimeBoxWriter() =
default;
void Mp4TrackFragmentDecodeTimeBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartFullBox(mp4::FOURCC_TFDT, /*flags=*/0, /*version=*/1);
uint32_t timescale = 0;
if (IsVideoTrackBox(context(), box_->track_id)) {
timescale = context().GetVideoTrack().value().timescale;
} else {
timescale = context().GetAudioTrack().value().timescale;
}
writer.WriteU64(ConvertToTimescale(box_->base_media_decode_time, timescale));
writer.EndBox();
}
// Mp4TrackFragmentRunBoxWriter (`trun`) class.
Mp4TrackFragmentRunBoxWriter::Mp4TrackFragmentRunBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::TrackFragmentRun& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4TrackFragmentRunBoxWriter::~Mp4TrackFragmentRunBoxWriter() = default;
void Mp4TrackFragmentRunBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartFullBox(mp4::FOURCC_TRUN, box_->flags, /*version=*/1);
writer.WriteU32(box_->sample_count);
{
// `data_offset`.
// `movie-fragment relative addressing` should exist by
// `https://www.w3.org/TR/mse-byte-stream-format-isobmff/`.
CHECK(box_->flags &
static_cast<uint16_t>(
mp4::writable_boxes::TrackFragmentRunFlags::kDataOffsetPresent));
writer.WriteOffsetPlaceholder();
}
uint32_t timescale = 0;
if (box_->flags &
static_cast<uint16_t>(mp4::writable_boxes::TrackFragmentRunFlags::
kFirstSampleFlagsPresent)) {
ValidateSampleFlags(box_->first_sample_flags);
writer.WriteU32(box_->first_sample_flags);
// `kFirstSampleFlagsPresent` exists for only Video track.
timescale = context().GetVideoTrack().value().timescale;
} else {
timescale = context().GetAudioTrack().value().timescale;
}
bool duration_exists =
(box_->flags &
static_cast<uint16_t>(
mp4::writable_boxes::TrackFragmentRunFlags::kSampleDurationPresent));
bool size_exists =
(box_->flags &
static_cast<uint16_t>(
mp4::writable_boxes::TrackFragmentRunFlags::kSampleSizePresent));
bool flags_exists =
(box_->flags &
static_cast<uint16_t>(
mp4::writable_boxes::TrackFragmentRunFlags::kSampleFlagsPresent));
if (duration_exists) {
// fragment, if not last, has an additional timestamp entry for last
// item duration calculation.
if (box_->sample_count == 0) {
CHECK_EQ(box_->sample_count, box_->sample_timestamps.size());
} else {
CHECK_EQ(box_->sample_count + 1, box_->sample_timestamps.size());
}
}
if (size_exists) {
CHECK_EQ(box_->sample_count, box_->sample_sizes.size());
}
if (flags_exists) {
CHECK_EQ(box_->sample_count, box_->sample_flags.size());
}
for (uint32_t i = 0; i < box_->sample_count; ++i) {
if (duration_exists) {
base::TimeDelta time_diff =
box_->sample_timestamps[i + 1] - box_->sample_timestamps[i];
writer.WriteU32(
static_cast<uint32_t>(ConvertToTimescale(time_diff, timescale)));
}
if (size_exists) {
writer.WriteU32(box_->sample_sizes[i]);
}
if (flags_exists) {
writer.WriteU32(box_->sample_flags[i]);
}
}
writer.EndBox();
}
// Mp4MediaDataBoxWriter (`mdat`) class.
Mp4MediaDataBoxWriter::Mp4MediaDataBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::MediaData& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4MediaDataBoxWriter::~Mp4MediaDataBoxWriter() = default;
void Mp4MediaDataBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartBox(mp4::FOURCC_MDAT);
for (const auto& data : box_->track_data) {
// Write base data offset to the entry of `base_data_offset` of the `trun`,
// which was set with placeholder during its write.
writer.FlushCurrentOffset();
writer.WriteBytes(data.data(), data.size());
}
writer.EndBox();
}
// Mp4FragmentRandomAccessBoxWriter (`mfra`) class.
Mp4FragmentRandomAccessBoxWriter::Mp4FragmentRandomAccessBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::FragmentRandomAccess& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// It will add video track only because every sample are sync samples in the
// audio.
if (auto video_track = context.GetVideoTrack()) {
AddChildBox(std::make_unique<Mp4TrackFragmentRandomAccessBoxWriter>(
context, box_->tracks[video_track.value().index]));
}
AddChildBox(
std::make_unique<Mp4FragmentRandomAccessOffsetBoxBoxWriter>(context));
}
Mp4FragmentRandomAccessBoxWriter::~Mp4FragmentRandomAccessBoxWriter() = default;
void Mp4FragmentRandomAccessBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartBox(mp4::FOURCC_MFRA);
WriteChildren(writer);
writer.EndBox();
}
// Mp4TrackFragmentRandomAccessBoxWriter (`tfra`) class.
Mp4TrackFragmentRandomAccessBoxWriter::Mp4TrackFragmentRandomAccessBoxWriter(
const Mp4MuxerContext& context,
const mp4::writable_boxes::TrackFragmentRandomAccess& box)
: Mp4BoxWriter(context), box_(box) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4TrackFragmentRandomAccessBoxWriter::
~Mp4TrackFragmentRandomAccessBoxWriter() = default;
void Mp4TrackFragmentRandomAccessBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartFullBox(mp4::FOURCC_TFRA, /*flags=*/0, /*version=*/1);
// `length_size_of_traf_num`, `length_size_of_trun_num` and
// `length_size_of_sample_num` size is 4 (uint32_t size).
constexpr uint32_t kLengthSizeOfEntries = 0x0000003F;
writer.WriteU32(box_->track_id);
writer.WriteU32(kLengthSizeOfEntries);
writer.WriteU32(box_->entries.size());
for (const mp4::writable_boxes::TrackFragmentRandomAccessEntry& entry :
box_->entries) {
// TODO(crbug.com://1471314): convert the presentation time based on
// the track's timescale.
uint32_t timescale = context().GetVideoTrack().value().timescale;
writer.WriteU64(ConvertToTimescale(entry.time, timescale));
writer.WriteU64(entry.moof_offset);
writer.WriteU32(entry.traf_number);
writer.WriteU32(entry.trun_number);
writer.WriteU32(entry.sample_number);
}
writer.EndBox();
}
// Mp4FragmentRandomAccessOffsetBoxBoxWriter (`mfro`) class.
Mp4FragmentRandomAccessOffsetBoxBoxWriter::
Mp4FragmentRandomAccessOffsetBoxBoxWriter(const Mp4MuxerContext& context)
: Mp4BoxWriter(context) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Mp4FragmentRandomAccessOffsetBoxBoxWriter::
~Mp4FragmentRandomAccessOffsetBoxBoxWriter() = default;
void Mp4FragmentRandomAccessOffsetBoxBoxWriter::Write(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.StartFullBox(mp4::FOURCC_MFRO, /*flags=*/0, /*version=*/1);
// `size` property of the `mfro` box is the total size of the `mfra` box.
writer.WriteU32(writer.size() + 4);
writer.EndBox();
}
} // namespace media