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_
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"
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
{
27 // Information about the queue state and the access unit at the front.
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.
43 // Number of access units in the queue excluding config units.
46 // Whether End Of Stream has been added to the queue. Cleared by Flush().
49 Info() : front_unit(nullptr), configs(nullptr), length(0), has_eos(false) {}
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.
62 // Clears the queue, resets the length to zero and clears EOS condition.
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.
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
);
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.
103 // The lock protects all fields together.
104 mutable base::Lock lock_
;
106 DISALLOW_COPY_AND_ASSIGN(AccessUnitQueue
);
111 #endif // MEDIA_BASE_ANDROID_ACCESS_UNIT_QUEUE_H_