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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/webm/webm_tracks_parser.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "media/base/buffers.h"
#include "media/webm/webm_constants.h"
#include "media/webm/webm_content_encodings.h"
namespace media {
// Values for TrackType element.
static const int kWebMTrackTypeVideo = 1;
static const int kWebMTrackTypeAudio = 2;
static const int kWebMTrackTypeSubtitlesOrCaptions = 0x11;
static const int kWebMTrackTypeDescriptionsOrMetadata = 0x21;
WebMTracksParser::WebMTracksParser(const LogCB& log_cb)
: track_type_(-1),
track_num_(-1),
text_track_kind_(kTextNone),
audio_track_num_(-1),
video_track_num_(-1),
log_cb_(log_cb) {
}
WebMTracksParser::~WebMTracksParser() {}
int WebMTracksParser::Parse(const uint8* buf, int size) {
track_type_ =-1;
track_num_ = -1;
text_track_kind_ = kTextNone;
audio_track_num_ = -1;
video_track_num_ = -1;
text_tracks_.clear();
ignored_tracks_.clear();
WebMListParser parser(kWebMIdTracks, this);
int result = parser.Parse(buf, size);
if (result <= 0)
return result;
// For now we do all or nothing parsing.
return parser.IsParsingComplete() ? result : 0;
}
WebMParserClient* WebMTracksParser::OnListStart(int id) {
if (id == kWebMIdContentEncodings) {
DCHECK(!track_content_encodings_client_.get());
track_content_encodings_client_.reset(
new WebMContentEncodingsClient(log_cb_));
return track_content_encodings_client_->OnListStart(id);
}
if (id == kWebMIdTrackEntry) {
track_type_ = -1;
track_num_ = -1;
text_track_kind_ = kTextNone;
return this;
}
return this;
}
bool WebMTracksParser::OnListEnd(int id) {
if (id == kWebMIdContentEncodings) {
DCHECK(track_content_encodings_client_.get());
return track_content_encodings_client_->OnListEnd(id);
}
if (id == kWebMIdTrackEntry) {
if (track_type_ == -1 || track_num_ == -1) {
MEDIA_LOG(log_cb_) << "Missing TrackEntry data for "
<< " TrackType " << track_type_
<< " TrackNum " << track_num_;
return false;
}
if (track_type_ != kWebMTrackTypeAudio &&
track_type_ != kWebMTrackTypeVideo &&
track_type_ != kWebMTrackTypeSubtitlesOrCaptions &&
track_type_ != kWebMTrackTypeDescriptionsOrMetadata) {
MEDIA_LOG(log_cb_) << "Unexpected TrackType " << track_type_;
return false;
}
if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions) {
if (text_track_kind_ == kTextNone) {
MEDIA_LOG(log_cb_) << "Missing TrackEntry CodecID"
<< " TrackNum " << track_num_;
return false;
}
if (text_track_kind_ != kTextSubtitles &&
text_track_kind_ != kTextCaptions) {
MEDIA_LOG(log_cb_) << "Wrong TrackEntry CodecID"
<< " TrackNum " << track_num_;
return false;
}
} else if (track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
if (text_track_kind_ == kTextNone) {
MEDIA_LOG(log_cb_) << "Missing TrackEntry CodecID"
<< " TrackNum " << track_num_;
return false;
}
if (text_track_kind_ != kTextDescriptions &&
text_track_kind_ != kTextMetadata) {
MEDIA_LOG(log_cb_) << "Wrong TrackEntry CodecID"
<< " TrackNum " << track_num_;
return false;
}
}
std::string encryption_key_id;
if (track_content_encodings_client_.get()) {
DCHECK(!track_content_encodings_client_->content_encodings().empty());
// If we have multiple ContentEncoding in one track. Always choose the
// key id in the first ContentEncoding as the key id of the track.
encryption_key_id = track_content_encodings_client_->
content_encodings()[0]->encryption_key_id();
}
if (track_type_ == kWebMTrackTypeAudio) {
if (audio_track_num_ == -1) {
audio_track_num_ = track_num_;
audio_encryption_key_id_ = encryption_key_id;
} else {
MEDIA_LOG(log_cb_) << "Ignoring audio track " << track_num_;
ignored_tracks_.insert(track_num_);
}
} else if (track_type_ == kWebMTrackTypeVideo) {
if (video_track_num_ == -1) {
video_track_num_ = track_num_;
video_encryption_key_id_ = encryption_key_id;
} else {
MEDIA_LOG(log_cb_) << "Ignoring video track " << track_num_;
ignored_tracks_.insert(track_num_);
}
} else if (track_type_ == kWebMTrackTypeSubtitlesOrCaptions ||
track_type_ == kWebMTrackTypeDescriptionsOrMetadata) {
text_tracks_.insert(track_num_);
} else {
MEDIA_LOG(log_cb_) << "Unexpected TrackType " << track_type_;
return false;
}
track_type_ = -1;
track_num_ = -1;
track_content_encodings_client_.reset();
text_track_kind_ = kTextNone;
return true;
}
return true;
}
bool WebMTracksParser::OnUInt(int id, int64 val) {
int64* dst = NULL;
switch (id) {
case kWebMIdTrackNumber:
dst = &track_num_;
break;
case kWebMIdTrackType:
dst = &track_type_;
break;
default:
return true;
}
if (*dst != -1) {
MEDIA_LOG(log_cb_) << "Multiple values for id " << std::hex << id
<< " specified";
return false;
}
*dst = val;
return true;
}
bool WebMTracksParser::OnFloat(int id, double val) {
return true;
}
bool WebMTracksParser::OnBinary(int id, const uint8* data, int size) {
return true;
}
bool WebMTracksParser::OnString(int id, const std::string& str) {
if (id == kWebMIdCodecID) {
if (str == "V_VP8")
return true;
if (str == "A_VORBIS")
return true;
if (str == "D_WEBVTT/SUBTITLES") {
text_track_kind_ = kTextSubtitles;
return true;
}
if (str == "D_WEBVTT/CAPTIONS") {
text_track_kind_ = kTextCaptions;
return true;
}
if (str == "D_WEBVTT/DESCRIPTIONS") {
text_track_kind_ = kTextDescriptions;
return true;
}
if (str == "D_WEBVTT/METADATA") {
text_track_kind_ = kTextMetadata;
return true;
}
MEDIA_LOG(log_cb_) << "Unexpected CodecID " << str;
return false;
}
return true;
}
} // namespace media
|