Shaka Packager SDK
Loading...
Searching...
No Matches
webm_tracks_parser.cc
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <packager/media/formats/webm/webm_tracks_parser.h>
6
7#include <absl/log/check.h>
8#include <absl/log/log.h>
9#include <absl/strings/str_format.h>
10
11#include <packager/media/base/timestamp.h>
12#include <packager/media/formats/webm/webm_constants.h>
13#include <packager/media/formats/webm/webm_content_encodings.h>
14
15namespace shaka {
16namespace media {
17
18static TextKind CodecIdToTextKind(const std::string& codec_id) {
19 if (codec_id == kWebMCodecSubtitles)
20 return kTextSubtitles;
21
22 if (codec_id == kWebMCodecCaptions)
23 return kTextCaptions;
24
25 if (codec_id == kWebMCodecDescriptions)
26 return kTextDescriptions;
27
28 if (codec_id == kWebMCodecMetadata)
29 return kTextMetadata;
30
31 return kTextNone;
32}
33
34static int64_t PrecisionCappedDefaultDuration(const double timecode_scale_in_us,
35 const int64_t duration_in_ns) {
36 if (duration_in_ns <= 0)
37 return kNoTimestamp;
38
39 int64_t mult = duration_in_ns / 1000;
40 mult /= timecode_scale_in_us;
41 if (mult == 0)
42 return kNoTimestamp;
43
44 mult = static_cast<double>(mult) * timecode_scale_in_us;
45 return mult;
46}
47
48WebMTracksParser::WebMTracksParser(bool ignore_text_tracks)
49 : track_type_(-1),
50 track_num_(-1),
51 seek_preroll_(-1),
52 codec_delay_(-1),
53 default_duration_(-1),
54 audio_track_num_(-1),
55 audio_default_duration_(-1),
56 video_track_num_(-1),
57 video_default_duration_(-1),
58 ignore_text_tracks_(ignore_text_tracks),
59 audio_client_(),
60 video_client_() {}
61
62WebMTracksParser::~WebMTracksParser() {}
63
64int WebMTracksParser::Parse(const uint8_t* buf, int size) {
65 track_type_ = -1;
66 track_num_ = -1;
67 default_duration_ = -1;
68 track_name_.clear();
69 track_language_.clear();
70 audio_track_num_ = -1;
71 audio_default_duration_ = -1;
72 audio_stream_info_ = nullptr;
73 video_track_num_ = -1;
74 video_default_duration_ = -1;
75 video_stream_info_ = nullptr;
76 text_tracks_.clear();
77 ignored_tracks_.clear();
78
79 WebMListParser parser(kWebMIdTracks, this);
80 int result = parser.Parse(buf, size);
81
82 if (result <= 0)
83 return result;
84
85 // For now we do all or nothing parsing.
86 return parser.IsParsingComplete() ? result : 0;
87}
88
89int64_t WebMTracksParser::GetAudioDefaultDuration(
90 const double timecode_scale_in_us) const {
91 return PrecisionCappedDefaultDuration(timecode_scale_in_us,
92 audio_default_duration_);
93}
94
95int64_t WebMTracksParser::GetVideoDefaultDuration(
96 const double timecode_scale_in_us) const {
97 return PrecisionCappedDefaultDuration(timecode_scale_in_us,
98 video_default_duration_);
99}
100
101WebMParserClient* WebMTracksParser::OnListStart(int id) {
102 if (id == kWebMIdContentEncodings) {
103 DCHECK(!track_content_encodings_client_.get());
104 track_content_encodings_client_.reset(new WebMContentEncodingsClient());
105 return track_content_encodings_client_->OnListStart(id);
106 }
107
108 if (id == kWebMIdTrackEntry) {
109 track_type_ = -1;
110 track_num_ = -1;
111 default_duration_ = -1;
112 track_name_.clear();
113 track_language_.clear();
114 codec_id_ = "";
115 codec_private_.clear();
116 audio_client_.Reset();
117 video_client_.Reset();
118 return this;
119 }
120
121 if (id == kWebMIdAudio)
122 return &audio_client_;
123
124 if (id == kWebMIdVideo)
125 return &video_client_;
126
127 return this;
128}
129
130bool WebMTracksParser::OnListEnd(int id) {
131 if (id == kWebMIdContentEncodings) {
132 DCHECK(track_content_encodings_client_.get());
133 return track_content_encodings_client_->OnListEnd(id);
134 }
135
136 if (id == kWebMIdTrackEntry) {
137 if (track_type_ == -1 || track_num_ == -1) {
138 LOG(ERROR) << "Missing TrackEntry data for "
139 << " TrackType " << track_type_ << " TrackNum " << track_num_;
140 return false;
141 }
142
143 if (track_type_ != kWebMTrackTypeAudio &&
144 track_type_ != kWebMTrackTypeVideo &&
145 track_type_ != kWebMTrackTypeSubtitlesOrCaptions &&
146 track_type_ != kWebMTrackTypeDescriptionsOrMetadata) {
147 LOG(ERROR) << "Unexpected TrackType " << track_type_;
148 return false;
149 }
150
151 TextKind text_track_kind = kTextNone;
152 if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions) {
153 text_track_kind = CodecIdToTextKind(codec_id_);
154 if (text_track_kind == kTextNone) {
155 LOG(ERROR) << "Missing TrackEntry CodecID"
156 << " TrackNum " << track_num_;
157 return false;
158 }
159
160 if (text_track_kind != kTextSubtitles &&
161 text_track_kind != kTextCaptions) {
162 LOG(ERROR) << "Wrong TrackEntry CodecID"
163 << " TrackNum " << track_num_;
164 return false;
165 }
166 } else if (track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
167 text_track_kind = CodecIdToTextKind(codec_id_);
168 if (text_track_kind == kTextNone) {
169 LOG(ERROR) << "Missing TrackEntry CodecID"
170 << " TrackNum " << track_num_;
171 return false;
172 }
173
174 if (text_track_kind != kTextDescriptions &&
175 text_track_kind != kTextMetadata) {
176 LOG(ERROR) << "Wrong TrackEntry CodecID"
177 << " TrackNum " << track_num_;
178 return false;
179 }
180 }
181
182 std::string encryption_key_id;
183 if (track_content_encodings_client_) {
184 DCHECK(!track_content_encodings_client_->content_encodings().empty());
185 // If we have multiple ContentEncoding in one track. Always choose the
186 // key id in the first ContentEncoding as the key id of the track.
187 encryption_key_id =
188 track_content_encodings_client_->content_encodings()[0]
189 ->encryption_key_id();
190 }
191
192 if (track_type_ == kWebMTrackTypeAudio) {
193 if (audio_track_num_ == -1) {
194 audio_track_num_ = track_num_;
195 audio_encryption_key_id_ = encryption_key_id;
196
197 if (default_duration_ == 0) {
198 LOG(ERROR) << "Illegal 0ns audio TrackEntry "
199 "DefaultDuration";
200 return false;
201 }
202 audio_default_duration_ = default_duration_;
203
204 DCHECK(!audio_stream_info_);
205 audio_stream_info_ = audio_client_.GetAudioStreamInfo(
206 audio_track_num_, codec_id_, codec_private_, seek_preroll_,
207 codec_delay_, track_language_, !audio_encryption_key_id_.empty());
208 if (!audio_stream_info_)
209 return false;
210 } else {
211 DLOG(INFO) << "Ignoring audio track " << track_num_;
212 ignored_tracks_.insert(track_num_);
213 }
214 } else if (track_type_ == kWebMTrackTypeVideo) {
215 if (video_track_num_ == -1) {
216 video_track_num_ = track_num_;
217 video_encryption_key_id_ = encryption_key_id;
218
219 if (default_duration_ == 0) {
220 LOG(ERROR) << "Illegal 0ns video TrackEntry "
221 "DefaultDuration";
222 return false;
223 }
224 video_default_duration_ = default_duration_;
225
226 DCHECK(!video_stream_info_);
227 video_stream_info_ = video_client_.GetVideoStreamInfo(
228 video_track_num_, codec_id_, codec_private_,
229 !video_encryption_key_id_.empty());
230 if (!video_stream_info_)
231 return false;
232
233 if (codec_id_ == "V_VP8" || codec_id_ == "V_VP9") {
234 vp_config_ = video_client_.GetVpCodecConfig(codec_private_);
235 const double kNanosecondsPerSecond = 1000000000.0;
236 if (codec_id_ == "V_VP9" &&
237 (!vp_config_.is_level_set() || vp_config_.level() == 0)) {
238 vp_config_.SetVP9Level(
239 video_stream_info_->width(), video_stream_info_->height(),
240 video_default_duration_ / kNanosecondsPerSecond);
241 }
242 }
243
244 } else {
245 DLOG(INFO) << "Ignoring video track " << track_num_;
246 ignored_tracks_.insert(track_num_);
247 }
248 } else if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions ||
249 track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
250 if (ignore_text_tracks_) {
251 DLOG(INFO) << "Ignoring text track " << track_num_;
252 ignored_tracks_.insert(track_num_);
253 } else {
254 std::string track_num = absl::StrFormat("%d", track_num_);
255 text_tracks_[track_num_] = TextTrackConfig(text_track_kind, track_name_,
256 track_language_, track_num);
257 }
258 } else {
259 LOG(ERROR) << "Unexpected TrackType " << track_type_;
260 return false;
261 }
262
263 track_type_ = -1;
264 track_num_ = -1;
265 default_duration_ = -1;
266 track_name_.clear();
267 track_language_.clear();
268 codec_id_ = "";
269 codec_private_.clear();
270 track_content_encodings_client_.reset();
271
272 audio_client_.Reset();
273 video_client_.Reset();
274 return true;
275 }
276
277 return true;
278}
279
280bool WebMTracksParser::OnUInt(int id, int64_t val) {
281 int64_t* dst = NULL;
282
283 switch (id) {
284 case kWebMIdTrackNumber:
285 dst = &track_num_;
286 break;
287 case kWebMIdTrackType:
288 dst = &track_type_;
289 break;
290 case kWebMIdSeekPreRoll:
291 dst = &seek_preroll_;
292 break;
293 case kWebMIdCodecDelay:
294 dst = &codec_delay_;
295 break;
296 case kWebMIdDefaultDuration:
297 dst = &default_duration_;
298 break;
299 default:
300 return true;
301 }
302
303 if (*dst != -1) {
304 LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified";
305 return false;
306 }
307
308 *dst = val;
309 return true;
310}
311
312bool WebMTracksParser::OnFloat(int /*id*/, double /*val*/) {
313 return true;
314}
315
316bool WebMTracksParser::OnBinary(int id, const uint8_t* data, int size) {
317 if (id == kWebMIdCodecPrivate) {
318 if (!codec_private_.empty()) {
319 LOG(ERROR) << "Multiple CodecPrivate fields in a track.";
320 return false;
321 }
322 codec_private_.assign(data, data + size);
323 return true;
324 }
325 return true;
326}
327
328bool WebMTracksParser::OnString(int id, const std::string& str) {
329 if (id == kWebMIdCodecID) {
330 if (!codec_id_.empty()) {
331 LOG(ERROR) << "Multiple CodecID fields in a track";
332 return false;
333 }
334
335 codec_id_ = str;
336 return true;
337 }
338
339 if (id == kWebMIdName) {
340 track_name_ = str;
341 return true;
342 }
343
344 if (id == kWebMIdLanguage) {
345 track_language_ = str;
346 return true;
347 }
348
349 return true;
350}
351
352} // namespace media
353} // namespace shaka
int Parse(const uint8_t *buf, int size)
All the methods that are virtual are virtual for mocking.