DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
webm_media_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_media_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/buffer_writer.h"
13 #include "packager/media/base/timestamp.h"
14 #include "packager/media/base/widevine_pssh_data.pb.h"
15 #include "packager/media/formats/webm/webm_cluster_parser.h"
16 #include "packager/media/formats/webm/webm_constants.h"
17 #include "packager/media/formats/webm/webm_content_encodings.h"
18 #include "packager/media/formats/webm/webm_info_parser.h"
19 #include "packager/media/formats/webm/webm_tracks_parser.h"
20 
21 namespace edash_packager {
22 namespace media {
23 
24 WebMMediaParser::WebMMediaParser()
25  : state_(kWaitingForInit), unknown_segment_size_(false) {}
26 
27 WebMMediaParser::~WebMMediaParser() {}
28 
29 void WebMMediaParser::Init(const InitCB& init_cb,
30  const NewSampleCB& new_sample_cb,
31  KeySource* decryption_key_source) {
32  DCHECK_EQ(state_, kWaitingForInit);
33  DCHECK(init_cb_.is_null());
34  DCHECK(!init_cb.is_null());
35  DCHECK(!new_sample_cb.is_null());
36 
37  ChangeState(kParsingHeaders);
38  init_cb_ = init_cb;
39  new_sample_cb_ = new_sample_cb;
40  decryption_key_source_ = decryption_key_source;
41  ignore_text_tracks_ = true;
42 }
43 
45  DCHECK_NE(state_, kWaitingForInit);
46 
47  byte_queue_.Reset();
48  bool result = true;
49  if (cluster_parser_)
50  result = cluster_parser_->Flush();
51  if (state_ == kParsingClusters) {
52  ChangeState(kParsingHeaders);
53  }
54  return result;
55 }
56 
57 bool WebMMediaParser::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 WebMMediaParser::ChangeState(State new_state) {
106  DVLOG(1) << "ChangeState() : " << state_ << " -> " << new_state;
107  state_ = new_state;
108 }
109 
110 int WebMMediaParser::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: 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  scoped_refptr<AudioStreamInfo> audio_stream_info =
189  tracks_parser.audio_stream_info();
190  if (audio_stream_info) {
191  audio_stream_info->set_duration(duration_in_us);
192  } else {
193  VLOG(1) << "No audio track info found.";
194  }
195 
196  scoped_refptr<VideoStreamInfo> video_stream_info =
197  tracks_parser.video_stream_info();
198  if (video_stream_info) {
199  video_stream_info->set_duration(duration_in_us);
200  } else {
201  VLOG(1) << "No video track info found.";
202  }
203 
204  if (!FetchKeysIfNecessary(tracks_parser.audio_encryption_key_id(),
205  tracks_parser.video_encryption_key_id())) {
206  return -1;
207  }
208 
209  cluster_parser_.reset(new WebMClusterParser(
210  info_parser.timecode_scale(), audio_stream_info, video_stream_info,
211  tracks_parser.GetAudioDefaultDuration(timecode_scale_in_us),
212  tracks_parser.GetVideoDefaultDuration(timecode_scale_in_us),
213  tracks_parser.text_tracks(), tracks_parser.ignored_tracks(),
214  tracks_parser.audio_encryption_key_id(),
215  tracks_parser.video_encryption_key_id(), new_sample_cb_, init_cb_,
216  decryption_key_source_));
217 
218  return bytes_parsed;
219 }
220 
221 int WebMMediaParser::ParseCluster(const uint8_t* data, int size) {
222  if (!cluster_parser_)
223  return -1;
224 
225  int bytes_parsed = cluster_parser_->Parse(data, size);
226  if (bytes_parsed < 0)
227  return bytes_parsed;
228 
229  bool cluster_ended = cluster_parser_->cluster_ended();
230  if (cluster_ended) {
231  ChangeState(kParsingHeaders);
232  }
233 
234  return bytes_parsed;
235 }
236 
237 bool WebMMediaParser::FetchKeysIfNecessary(
238  const std::string& audio_encryption_key_id,
239  const std::string& video_encryption_key_id) {
240  if (audio_encryption_key_id.empty() && video_encryption_key_id.empty())
241  return true;
242  // An error will be returned later if the samples need to be derypted.
243  if (!decryption_key_source_)
244  return true;
245 
246  // Generate WidevinePsshData from key_id.
247  WidevinePsshData widevine_pssh_data;
248  if (!audio_encryption_key_id.empty())
249  widevine_pssh_data.add_key_id(audio_encryption_key_id);
250  if (!video_encryption_key_id.empty())
251  widevine_pssh_data.add_key_id(video_encryption_key_id);
252 
253  const std::string serialized_string = widevine_pssh_data.SerializeAsString();
254  Status status = decryption_key_source_->FetchKeys(
255  std::vector<uint8_t>(serialized_string.begin(), serialized_string.end()));
256  if (!status.ok()) {
257  LOG(ERROR) << "Error fetching decryption keys: " << status;
258  return false;
259  }
260  return true;
261 }
262 
263 } // namespace media
264 } // namespace edash_packager
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
void Push(const uint8_t *data, int size)
Append new bytes to the end of the queue.
Definition: byte_queue.cc:29
virtual Status FetchKeys(const std::vector< uint8_t > &content_id, const std::string &policy)
Definition: key_source.cc:30
base::Callback< bool(uint32_t track_id, const scoped_refptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:44
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
void Reset()
Reset the queue to the empty state.
Definition: byte_queue.cc:24
bool Flush() override WARN_UNUSED_RESULT
void Peek(const uint8_t **data, int *size) const
Definition: byte_queue.cc:63