summaryrefslogtreecommitdiffstats
path: root/media/webm/webm_cluster_parser.h
blob: c702d1013fe6b638321079990552a67c5d9a6228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_
#define MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_

#include <deque>
#include <string>

#include "base/memory/scoped_ptr.h"
#include "media/base/media_export.h"
#include "media/base/stream_parser_buffer.h"
#include "media/webm/webm_parser.h"

namespace media {

class MEDIA_EXPORT WebMClusterParser : public WebMParserClient {
 public:
  typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;

  WebMClusterParser(int64 timecode_scale,
                    int audio_track_num,
                    int video_track_num,
                    const uint8* video_encryption_key_id,
                    int video_encryption_key_id_size);
  virtual ~WebMClusterParser();

  // Resets the parser state so it can accept a new cluster.
  void Reset();

  // Parses a WebM cluster element in |buf|.
  //
  // Returns -1 if the parse fails.
  // Returns 0 if more data is needed.
  // Returns the number of bytes parsed on success.
  int Parse(const uint8* buf, int size);

  const BufferQueue& audio_buffers() const { return audio_.buffers(); }
  const BufferQueue& video_buffers() const { return video_.buffers(); }

 private:
  // Helper class that manages per-track state.
  class Track {
   public:
    explicit Track(int track_num);
    ~Track();

    int track_num() const { return track_num_; }
    const BufferQueue& buffers() const { return buffers_; }

    bool AddBuffer(const scoped_refptr<StreamParserBuffer>& buffer);

    // Clears all buffer state.
    void Reset();

    // Clears only the |buffers_|.
    void ClearBufferQueue();

   private:
    // Sets the duration of all the buffers in |delayed_buffers_|
    // and then moves these buffers into |buffers_|. |delayed_buffers_|
    // is empty when this call returns.
    void SetDelayedBufferDurations(base::TimeDelta duration);

    // Adds the buffer to |buffers_|. |buffer| must have its duration set.
    void AddToBufferQueue(const scoped_refptr<StreamParserBuffer>& buffer);

    int track_num_;
    BufferQueue buffers_;
    BufferQueue delayed_buffers_;
  };

  // WebMParserClient methods.
  virtual WebMParserClient* OnListStart(int id) OVERRIDE;
  virtual bool OnListEnd(int id) OVERRIDE;
  virtual bool OnUInt(int id, int64 val) OVERRIDE;
  virtual bool OnBinary(int id, const uint8* data, int size) OVERRIDE;

  bool ParseBlock(const uint8* buf, int size, int duration);
  bool OnBlock(int track_num, int timecode, int duration, int flags,
               const uint8* data, int size);

  double timecode_multiplier_;  // Multiplier used to convert timecodes into
                                // microseconds.
  scoped_array<uint8> video_encryption_key_id_;
  int video_encryption_key_id_size_;

  WebMListParser parser_;

  int64 last_block_timecode_;
  scoped_array<uint8> block_data_;
  int block_data_size_;
  int64 block_duration_;

  int64 cluster_timecode_;

  Track audio_;
  Track video_;

  DISALLOW_IMPLICIT_CONSTRUCTORS(WebMClusterParser);
};

}  // namespace media

#endif  // MEDIA_WEBM_WEBM_CLUSTER_PARSER_H_