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_BASE_BYTE_QUEUE_H_
6 #define MEDIA_BASE_BYTE_QUEUE_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 
13 #include "base/macros.h"
14 #include "media/base/media_export.h"
15 
16 namespace media {
17 
18 // Represents a queue of bytes. Data is added to the end of the queue via an
19 // Push() call and removed via Pop(). The contents of the queue can be observed
20 // via the Peek() method.
21 //
22 // This class manages the underlying storage of the queue and tries to minimize
23 // the number of buffer copies when data is appended and removed.
24 class MEDIA_EXPORT ByteQueue {
25  public:
26   ByteQueue();
27   ~ByteQueue();
28 
29   // Reset the queue to the empty state.
30   void Reset();
31 
32   // Appends new bytes onto the end of the queue.
33   void Push(const uint8_t* data, int size);
34 
35   // Get a pointer to the front of the queue and the queue size. These values
36   // are only valid until the next Push() or Pop() call.
37   void Peek(const uint8_t** data, int* size) const;
38 
39   // Remove |count| bytes from the front of the queue.
40   void Pop(int count);
41 
42  private:
43   // Default starting size for the queue.
44   enum { kDefaultQueueSize = 1024 };
45 
46   // Returns a pointer to the front of the queue.
47   uint8_t* Front() const;
48 
49   // Size of |buffer_|.
50   size_t size_ = kDefaultQueueSize;
51 
52   // Offset from the start of |buffer_| that marks the front of the queue.
53   size_t offset_ = 0u;
54 
55   // Number of bytes stored in |buffer_|.
56   int used_ = 0;
57 
58   std::unique_ptr<uint8_t[]> buffer_;
59 
60   DISALLOW_COPY_AND_ASSIGN(ByteQueue);
61 };
62 
63 }  // namespace media
64 
65 #endif  // MEDIA_BASE_BYTE_QUEUE_H_
66