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