Shaka Packager SDK
webm_media_parser.h
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 #ifndef PACKAGER_MEDIA_FORMATS_WEBM_WEBM_MEDIA_PARSER_H_
6 #define PACKAGER_MEDIA_FORMATS_WEBM_WEBM_MEDIA_PARSER_H_
7 
8 #include "packager/base/callback_forward.h"
9 #include "packager/base/compiler_specific.h"
10 #include "packager/media/base/byte_queue.h"
11 #include "packager/media/base/media_parser.h"
12 
13 namespace shaka {
14 namespace media {
15 
16 class WebMClusterParser;
17 
18 class WebMMediaParser : public MediaParser {
19  public:
21  ~WebMMediaParser() override;
22 
25  void Init(const InitCB& init_cb,
26  const NewMediaSampleCB& new_media_sample_cb,
27  const NewTextSampleCB& new_text_sample_cb,
28  KeySource* decryption_key_source) override;
29  bool Flush() override WARN_UNUSED_RESULT;
30  bool Parse(const uint8_t* buf, int size) override WARN_UNUSED_RESULT;
32 
33  private:
34  enum State {
35  kWaitingForInit,
36  kParsingHeaders,
37  kParsingClusters,
38  kError
39  };
40 
41  void ChangeState(State new_state);
42 
43  // Parses WebM Header, Info, Tracks elements. It also skips other level 1
44  // elements that are not used right now. Once the Info & Tracks elements have
45  // been parsed, this method will transition the parser from PARSING_HEADERS to
46  // PARSING_CLUSTERS.
47  //
48  // Returns < 0 if the parse fails.
49  // Returns 0 if more data is needed.
50  // Returning > 0 indicates success & the number of bytes parsed.
51  int ParseInfoAndTracks(const uint8_t* data, int size);
52 
53  // Incrementally parses WebM cluster elements. This method also skips
54  // CUES elements if they are encountered since we currently don't use the
55  // data in these elements.
56  //
57  // Returns < 0 if the parse fails.
58  // Returns 0 if more data is needed.
59  // Returning > 0 indicates success & the number of bytes parsed.
60  int ParseCluster(const uint8_t* data, int size);
61 
62  // Fetch keys for the input key ids. Returns true on success, false otherwise.
63  bool FetchKeysIfNecessary(const std::string& audio_encryption_key_id,
64  const std::string& video_encryption_key_id);
65 
66  State state_;
67  InitCB init_cb_;
68  NewMediaSampleCB new_sample_cb_;
69  KeySource* decryption_key_source_;
70  bool ignore_text_tracks_;
71 
72  bool unknown_segment_size_;
73 
74  std::unique_ptr<WebMClusterParser> cluster_parser_;
75  ByteQueue byte_queue_;
76 
77  DISALLOW_COPY_AND_ASSIGN(WebMMediaParser);
78 };
79 
80 } // namespace media
81 } // namespace shaka
82 
83 #endif // PACKAGER_MEDIA_FORMATS_WEBM_WEBM_MEDIA_PARSER_H_
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:51
base::Callback< bool(uint32_t track_id, std::shared_ptr< TextSample > text_sample)> NewTextSampleCB
Definition: media_parser.h:53
base::Callback< bool(uint32_t track_id, std::shared_ptr< MediaSample > media_sample)> NewMediaSampleCB
Definition: media_parser.h:44
base::Callback< void(const std::vector< std::shared_ptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:35
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
bool Flush() override WARN_UNUSED_RESULT
void Init(const InitCB &init_cb, const NewMediaSampleCB &new_media_sample_cb, const NewTextSampleCB &new_text_sample_cb, KeySource *decryption_key_source) override
All the methods that are virtual are virtual for mocking.