summaryrefslogtreecommitdiffstats
path: root/media/base/buffer_queue.h
blob: 39e1976602cbfcbae69f75fa22cccb5db1a9652d (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
// Copyright (c) 2009 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.

// BufferQueue is a simple Buffer manager that handles requests for data
// while hiding Buffer boundaries, treating its internal queue of Buffers
// as a contiguous region.
//
// This class is not threadsafe and requires external locking.

#ifndef MEDIA_BASE_BUFFER_QUEUE_H_
#define MEDIA_BASE_BUFFER_QUEUE_H_

#include <deque>

#include "base/ref_counted.h"
#include "base/time.h"

namespace media {

class Buffer;

class BufferQueue {
 public:
  BufferQueue();
  ~BufferQueue();

  // Clears |queue_|.
  void Clear();

  // Advances front pointer |bytes_to_be_consumed| bytes and discards
  // "consumed" buffers.
  void Consume(size_t bytes_to_be_consumed);

  // Tries to copy |bytes| bytes from our data to |dest|. Returns the number
  // of bytes successfully copied.
  size_t Copy(uint8* dest, size_t bytes);

  // Enqueues |buffer_in| and adds a reference.
  void Enqueue(Buffer* buffer_in);

  // Returns the timestamp of the first buffer plus |data_offset_| in
  // microseconds, calculated using the conversion |bytes_to_sec|.
  base::TimeDelta GetTime(double bytes_to_sec);

  // Returns true if the |queue_| is empty.
  bool IsEmpty();

  // Returns the number of bytes in the |queue_|.
  size_t SizeInBytes();

 private:
  // Queued audio data.
  std::deque< scoped_refptr<Buffer> > queue_;

  // Remembers the amount of remaining audio data in the front buffer.
  size_t data_offset_;

  // Keeps track of the |queue_| size in bytes.
  size_t size_in_bytes_;

  DISALLOW_COPY_AND_ASSIGN(BufferQueue);
};

}  // namespace media

#endif  // MEDIA_BASE_BUFFER_QUEUE_H_