Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / android / access_unit_queue.h
blobd6ea33d74a99643fc91d6cd99bccd2f490fd240f
1 // Copyright 2015 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.
5 #ifndef MEDIA_BASE_ANDROID_ACCESS_UNIT_QUEUE_H_
6 #define MEDIA_BASE_ANDROID_ACCESS_UNIT_QUEUE_H_
8 #include <list>
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/synchronization/lock.h"
13 #include "media/base/android/demuxer_stream_player_params.h"
15 namespace media {
17 // The queue of incoming data for MediaCodecDecoder.
19 // The data comes in the form of access units. Each access unit has a type.
20 // If the type is |kConfigChanged| the access unit itself has no data, but
21 // is accompanied with DemuxerConfigs.
22 // The queue should be accessed on the Media thread that puts the incoming data
23 // in and on the Decoder thread that gets the next access unit and eventually
24 // removes it from the queue.
25 class AccessUnitQueue {
26 public:
27 // Information about the queue state and the access unit at the front.
28 struct Info {
29 // The unit at front. Null if the queue is empty. This pointer may be
30 // invalidated by the next Advance() or Flush() call and must be used
31 // before the caller calls these methods. The |front_unit| is owned by
32 // the queue itself - never delete it through this pointer.
33 const AccessUnit* front_unit;
35 // Configs for the front unit if it is |kConfigChanged|, null otherwise.
36 // The same validity rule applies: this pointer is only valid till the next
37 // Advance() or Flush() call, and |configs| is owned by the queue itself.
38 const DemuxerConfigs* configs;
40 // Number of access units in the queue.
41 int length;
43 // Number of access units in the queue excluding config units.
44 int data_length;
46 // Whether End Of Stream has been added to the queue. Cleared by Flush().
47 bool has_eos;
49 Info() : front_unit(nullptr), configs(nullptr), length(0), has_eos(false) {}
52 AccessUnitQueue();
53 ~AccessUnitQueue();
55 // Appends the incoming data to the queue.
56 void PushBack(const DemuxerData& frames);
58 // Advances the front position to next unit. Logically the preceding units
59 // do not exist, but they can be physically removed later.
60 void Advance();
62 // Clears the queue, resets the length to zero and clears EOS condition.
63 void Flush();
65 // Looks back for the first key frame starting from the current one (i.e.
66 // the look-back is inclusive of the current front position).
67 // If the key frame exists, sets the current access unit to it and returns
68 // true. Otherwise returns false.
69 bool RewindToLastKeyFrame();
71 // Returns the information about the queue.
72 // The result is invalidated by the following Advance() or Flush call.
73 // There must be only one |Info| consumer at a time.
74 Info GetInfo() const;
76 // For unit tests only. These methods are not thread safe.
77 size_t NumChunksForTesting() const { return chunks_.size(); }
78 void SetHistorySizeForTesting(size_t number_of_history_chunks);
80 private:
81 // Returns the total number of access units (total_length) and the number of
82 // units excluding configiration change requests (data_length). The number is
83 // calculated between the current one and the end, incuding the current.
84 // Logically these are units that have not been consumed.
85 void GetUnconsumedAccessUnitLength(int* total_length, int* data_length) const;
87 // The queue of data chunks. It owns the chunks.
88 typedef std::list<DemuxerData*> DataChunkQueue;
89 DataChunkQueue chunks_;
91 // The chunk that contains the current access unit.
92 DataChunkQueue::iterator current_chunk_;
94 // Index of the current access unit within the current chunk.
95 size_t index_in_chunk_;
97 // Amount of chunks before the |current_chunk_| that's kept for history.
98 size_t history_chunks_amount_;
100 // Indicates that a unit with End Of Stream flag has been appended.
101 bool has_eos_;
103 // The lock protects all fields together.
104 mutable base::Lock lock_;
106 DISALLOW_COPY_AND_ASSIGN(AccessUnitQueue);
109 } // namespace media
111 #endif // MEDIA_BASE_ANDROID_ACCESS_UNIT_QUEUE_H_