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_MP4_OFFSET_BYTE_QUEUE_H_
6#define MEDIA_MP4_OFFSET_BYTE_QUEUE_H_
7
8#include "base/basictypes.h"
9#include "media/base/byte_queue.h"
10#include "media/base/media_export.h"
11
12namespace media {
13
14// A wrapper around a ByteQueue which maintains a notion of a
15// monotonically-increasing offset. All buffer access is done by passing these
16// offsets into this class, going some way towards preventing the proliferation
17// of many different meanings of "offset", "head", etc.
18class MEDIA_EXPORT OffsetByteQueue {
19 public:
20  OffsetByteQueue();
21  ~OffsetByteQueue();
22
23  // These work like their underlying ByteQueue counterparts.
24  void Reset();
25  void Push(const uint8* buf, int size);
26  void Peek(const uint8** buf, int* size);
27  void Pop(int count);
28
29  // Sets |buf| to point at the first buffered byte corresponding to |offset|,
30  // and |size| to the number of bytes available starting from that offset.
31  //
32  // It is an error if the offset is before the current head. It's not an error
33  // if the current offset is beyond tail(), but you will of course get back
34  // a null |buf| and a |size| of zero.
35  void PeekAt(int64 offset, const uint8** buf, int* size);
36
37  // Marks the bytes up to (but not including) |max_offset| as ready for
38  // deletion. This is relatively inexpensive, but will not necessarily reduce
39  // the resident buffer size right away (or ever).
40  //
41  // Returns true if the full range of bytes were successfully trimmed,
42  // including the case where |max_offset| is less than the current head.
43  // Returns false if |max_offset| > tail() (although all bytes currently
44  // buffered are still cleared).
45  bool Trim(int64 max_offset);
46
47  // The head and tail positions, in terms of the file's absolute offsets.
48  // tail() is an exclusive bound.
49  int64 head() { return head_; }
50  int64 tail() { return head_ + size_; }
51
52 private:
53  // Synchronize |buf_| and |size_| with |queue_|.
54  void Sync();
55
56  ByteQueue queue_;
57  const uint8* buf_;
58  int size_;
59  int64 head_;
60
61  DISALLOW_COPY_AND_ASSIGN(OffsetByteQueue);
62};
63
64}  // namespace media
65
66#endif  // MEDIA_MP4_MP4_STREAM_PARSER_H_
67