1// Copyright (c) 2012 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 MEDIA_WEBM_WEBM_STREAM_PARSER_H_
6#define MEDIA_WEBM_WEBM_STREAM_PARSER_H_
7
8#include "base/callback_forward.h"
9#include "base/memory/ref_counted.h"
10#include "media/base/audio_decoder_config.h"
11#include "media/base/buffers.h"
12#include "media/base/byte_queue.h"
13#include "media/base/stream_parser.h"
14#include "media/base/video_decoder_config.h"
15
16namespace media {
17
18class WebMClusterParser;
19
20class WebMStreamParser : public StreamParser {
21 public:
22  WebMStreamParser();
23  virtual ~WebMStreamParser();
24
25  // StreamParser implementation.
26  virtual void Init(const InitCB& init_cb, const NewConfigCB& config_cb,
27                    const NewBuffersCB& new_buffers_cb,
28                    const NewTextBuffersCB& text_cb,
29                    const NeedKeyCB& need_key_cb,
30                    const NewMediaSegmentCB& new_segment_cb,
31                    const base::Closure& end_of_segment_cb,
32                    const LogCB& log_cb) OVERRIDE;
33  virtual void Flush() OVERRIDE;
34  virtual bool Parse(const uint8* buf, int size) OVERRIDE;
35
36 private:
37  enum State {
38    kWaitingForInit,
39    kParsingHeaders,
40    kParsingClusters,
41    kError
42  };
43
44  void ChangeState(State new_state);
45
46  // Parses WebM Header, Info, Tracks elements. It also skips other level 1
47  // elements that are not used right now. Once the Info & Tracks elements have
48  // been parsed, this method will transition the parser from PARSING_HEADERS to
49  // PARSING_CLUSTERS.
50  //
51  // Returns < 0 if the parse fails.
52  // Returns 0 if more data is needed.
53  // Returning > 0 indicates success & the number of bytes parsed.
54  int ParseInfoAndTracks(const uint8* data, int size);
55
56  // Incrementally parses WebM cluster elements. This method also skips
57  // CUES elements if they are encountered since we currently don't use the
58  // data in these elements.
59  //
60  // Returns < 0 if the parse fails.
61  // Returns 0 if more data is needed.
62  // Returning > 0 indicates success & the number of bytes parsed.
63  int ParseCluster(const uint8* data, int size);
64
65  // Fire needkey event through the |need_key_cb_|.
66  void FireNeedKey(const std::string& key_id);
67
68  State state_;
69  InitCB init_cb_;
70  NewConfigCB config_cb_;
71  NewBuffersCB new_buffers_cb_;
72  NewTextBuffersCB text_cb_;
73  NeedKeyCB need_key_cb_;
74
75  NewMediaSegmentCB new_segment_cb_;
76  base::Closure end_of_segment_cb_;
77  LogCB log_cb_;
78
79  // True if a new cluster id has been seen, but no audio or video buffers have
80  // been parsed yet.
81  bool waiting_for_buffers_;
82
83  scoped_ptr<WebMClusterParser> cluster_parser_;
84  ByteQueue byte_queue_;
85
86  DISALLOW_COPY_AND_ASSIGN(WebMStreamParser);
87};
88
89}  // namespace media
90
91#endif  // MEDIA_WEBM_WEBM_STREAM_PARSER_H_
92