Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / base / android / access_unit_queue.h
blob9fae42000f4924ae59e3f532ac92abd49fe31e2a
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 // Whether End Of Stream has been added to the queue. Cleared by Flush().
44 bool has_eos;
46 Info() : front_unit(nullptr), configs(nullptr), length(0), has_eos(false) {}
49 AccessUnitQueue();
50 ~AccessUnitQueue();
52 // Appends the incoming data to the queue.
53 void PushBack(const DemuxerData& frames);
55 // Advances the front position to next unit. Logically the preceding units
56 // do not exist, but they can be physically removed later.
57 void Advance();
59 // Clears the queue, resets the length to zero and clears EOS condition.
60 void Flush();
62 // Looks back for the first key frame starting from the current one (i.e.
63 // the look-back is inclusive of the current front position).
64 // If the key frame exists, sets the current access unit to it and returns
65 // true. Otherwise returns false.
66 bool RewindToLastKeyFrame();
68 // Returns the information about the queue.
69 // The result is invalidated by the following Advance() or Flush call.
70 // There must be only one |Info| consumer at a time.
71 Info GetInfo() const;
73 // For unit tests only. These methods are not thread safe.
74 size_t NumChunksForTesting() const { return chunks_.size(); }
75 void SetHistorySizeForTesting(size_t number_of_history_chunks);
77 private:
78 // Returns the amount of access units between the current one and the end,
79 // incuding current. Logically these are units that have not been consumed.
80 int GetUnconsumedAccessUnitLength() const;
82 // The queue of data chunks. It owns the chunks.
83 typedef std::list<DemuxerData*> DataChunkQueue;
84 DataChunkQueue chunks_;
86 // The chunk that contains the current access unit.
87 DataChunkQueue::iterator current_chunk_;
89 // Index of the current access unit within the current chunk.
90 size_t index_in_chunk_;
92 // Amount of chunks before the |current_chunk_| that's kept for history.
93 size_t history_chunks_amount_;
95 // Indicates that a unit with End Of Stream flag has been appended.
96 bool has_eos_;
98 // The lock protects all fields together.
99 mutable base::Lock lock_;
101 DISALLOW_COPY_AND_ASSIGN(AccessUnitQueue);
104 } // namespace media
106 #endif // MEDIA_BASE_ANDROID_ACCESS_UNIT_QUEUE_H_