DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
webm_stream_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_stream_parser.h"
6 
7 #include <string>
8 
9 #include "packager/base/callback.h"
10 #include "packager/base/callback_helpers.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/timestamp.h"
13 #include "packager/media/formats/webm/webm_cluster_parser.h"
14 #include "packager/media/formats/webm/webm_constants.h"
15 #include "packager/media/formats/webm/webm_content_encodings.h"
16 #include "packager/media/formats/webm/webm_info_parser.h"
17 #include "packager/media/formats/webm/webm_tracks_parser.h"
18 
19 namespace edash_packager {
20 namespace media {
21 
22 WebMStreamParser::WebMStreamParser()
23  : state_(kWaitingForInit),
24  unknown_segment_size_(false) {
25 }
26 
27 WebMStreamParser::~WebMStreamParser() {
28 }
29 
31  const InitCB& init_cb,
32  const NewSampleCB& new_sample_cb,
33  KeySource* decryption_key_source) {
34  DCHECK_EQ(state_, kWaitingForInit);
35  DCHECK(init_cb_.is_null());
36  DCHECK(!init_cb.is_null());
37  DCHECK(!new_sample_cb.is_null());
38 
39  ChangeState(kParsingHeaders);
40  init_cb_ = init_cb;
41  new_sample_cb_ = new_sample_cb;
42  decryption_key_source_ = decryption_key_source;
43  ignore_text_tracks_ = true;
44 }
45 
47  DCHECK_NE(state_, kWaitingForInit);
48 
49  byte_queue_.Reset();
50  if (cluster_parser_)
51  cluster_parser_->Reset();
52  if (state_ == kParsingClusters) {
53  ChangeState(kParsingHeaders);
54  }
55 }
56 
57 bool WebMStreamParser::Parse(const uint8_t* buf, int size) {
58  DCHECK_NE(state_, kWaitingForInit);
59 
60  if (state_ == kError)
61  return false;
62 
63  byte_queue_.Push(buf, size);
64 
65  int result = 0;
66  int bytes_parsed = 0;
67  const uint8_t* cur = NULL;
68  int cur_size = 0;
69 
70  byte_queue_.Peek(&cur, &cur_size);
71  while (cur_size > 0) {
72  State oldState = state_;
73  switch (state_) {
74  case kParsingHeaders:
75  result = ParseInfoAndTracks(cur, cur_size);
76  break;
77 
78  case kParsingClusters:
79  result = ParseCluster(cur, cur_size);
80  break;
81 
82  case kWaitingForInit:
83  case kError:
84  return false;
85  }
86 
87  if (result < 0) {
88  ChangeState(kError);
89  return false;
90  }
91 
92  if (state_ == oldState && result == 0)
93  break;
94 
95  DCHECK_GE(result, 0);
96  cur += result;
97  cur_size -= result;
98  bytes_parsed += result;
99  }
100 
101  byte_queue_.Pop(bytes_parsed);
102  return true;
103 }
104 
105 void WebMStreamParser::ChangeState(State new_state) {
106  DVLOG(1) << "ChangeState() : " << state_ << " -> " << new_state;
107  state_ = new_state;
108 }
109 
110 int WebMStreamParser::ParseInfoAndTracks(const uint8_t* data, int size) {
111  DVLOG(2) << "ParseInfoAndTracks()";
112  DCHECK(data);
113  DCHECK_GT(size, 0);
114 
115  const uint8_t* cur = data;
116  int cur_size = size;
117  int bytes_parsed = 0;
118 
119  int id;
120  int64_t element_size;
121  int result = WebMParseElementHeader(cur, cur_size, &id, &element_size);
122 
123  if (result <= 0)
124  return result;
125 
126  switch (id) {
127  case kWebMIdEBMLHeader:
128  case kWebMIdSeekHead:
129  case kWebMIdVoid:
130  case kWebMIdCRC32:
131  case kWebMIdCues:
132  case kWebMIdChapters:
133  case kWebMIdTags:
134  case kWebMIdAttachments:
135  // TODO(matthewjheaney): Implement support for chapters.
136  if (cur_size < (result + element_size)) {
137  // We don't have the whole element yet. Signal we need more data.
138  return 0;
139  }
140  // Skip the element.
141  return result + element_size;
142  break;
143  case kWebMIdCluster:
144  if (!cluster_parser_) {
145  LOG(ERROR) << "Found Cluster element before Info.";
146  return -1;
147  }
148  ChangeState(kParsingClusters);
149  return 0;
150  break;
151  case kWebMIdSegment:
152  // Segment of unknown size indicates live stream.
153  if (element_size == kWebMUnknownSize)
154  unknown_segment_size_ = true;
155  // Just consume the segment header.
156  return result;
157  break;
158  case kWebMIdInfo:
159  // We've found the element we are looking for.
160  break;
161  default: {
162  LOG(ERROR) << "Unexpected element ID 0x" << std::hex << id;
163  return -1;
164  }
165  }
166 
167  WebMInfoParser info_parser;
168  result = info_parser.Parse(cur, cur_size);
169 
170  if (result <= 0)
171  return result;
172 
173  cur += result;
174  cur_size -= result;
175  bytes_parsed += result;
176 
177  WebMTracksParser tracks_parser(ignore_text_tracks_);
178  result = tracks_parser.Parse(cur, cur_size);
179 
180  if (result <= 0)
181  return result;
182 
183  bytes_parsed += result;
184 
185  double timecode_scale_in_us = info_parser.timecode_scale() / 1000.0;
186  int64_t duration_in_us = info_parser.duration() * timecode_scale_in_us;
187 
188  std::vector<scoped_refptr<StreamInfo>> streams;
189  scoped_refptr<AudioStreamInfo> audio_stream_info =
190  tracks_parser.audio_stream_info();
191  streams.push_back(tracks_parser.audio_stream_info());
192  streams.back()->set_duration(duration_in_us);
193  if (streams.back()->is_encrypted())
194  OnEncryptedMediaInitData(tracks_parser.audio_encryption_key_id());
195 
196  streams.push_back(tracks_parser.video_stream_info());
197  streams.back()->set_duration(duration_in_us);
198  if (streams.back()->is_encrypted())
199  OnEncryptedMediaInitData(tracks_parser.video_encryption_key_id());
200 
201  init_cb_.Run(streams);
202 
203  cluster_parser_.reset(new WebMClusterParser(
204  info_parser.timecode_scale(), tracks_parser.audio_track_num(),
205  tracks_parser.GetAudioDefaultDuration(timecode_scale_in_us),
206  tracks_parser.video_track_num(),
207  tracks_parser.GetVideoDefaultDuration(timecode_scale_in_us),
208  tracks_parser.text_tracks(), tracks_parser.ignored_tracks(),
209  tracks_parser.audio_encryption_key_id(),
210  tracks_parser.video_encryption_key_id(), audio_stream_info->codec(),
211  new_sample_cb_));
212 
213  return bytes_parsed;
214 }
215 
216 int WebMStreamParser::ParseCluster(const uint8_t* data, int size) {
217  if (!cluster_parser_)
218  return -1;
219 
220  int bytes_parsed = cluster_parser_->Parse(data, size);
221  if (bytes_parsed < 0)
222  return bytes_parsed;
223 
224  bool cluster_ended = cluster_parser_->cluster_ended();
225  if (cluster_ended) {
226  ChangeState(kParsingHeaders);
227  }
228 
229  return bytes_parsed;
230 }
231 
232 void WebMStreamParser::OnEncryptedMediaInitData(const std::string& key_id) {
233  NOTIMPLEMENTED() << "WebM decryption is not implemented yet.";
234 }
235 
236 } // namespace media
237 } // namespace edash_packager
void Push(const uint8_t *data, int size)
Append new bytes to the end of the queue.
Definition: byte_queue.cc:29
bool Parse(const uint8_t *buf, int size) override
base::Callback< bool(uint32_t track_id, const scoped_refptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:43
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
void Reset()
Reset the queue to the empty state.
Definition: byte_queue.cc:24
void Peek(const uint8_t **data, int *size) const
Definition: byte_queue.cc:63