OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "media/base/android/access_unit_queue.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "media/base/demuxer_stream.h" | |
9 | |
10 namespace media { | |
11 | |
12 namespace { | |
13 // Amount of history chunks we keep by default. The zero size means we do not | |
14 // keep chunks before the current one and the history is limited by the size | |
15 // of one chunk. | |
16 const int kDefaultHistoryChunksAmount = 0; | |
17 } | |
18 | |
19 AccessUnitQueue::AccessUnitQueue() | |
20 : index_in_chunk_(0), | |
21 history_chunks_amount_(kDefaultHistoryChunksAmount), | |
wolenetz
2015/06/11 19:21:20
aside: Do we ever need this? If not, always 0? If
| |
22 has_eos_(false) { | |
23 current_chunk_ = chunks_.end(); | |
24 } | |
25 | |
26 AccessUnitQueue::~AccessUnitQueue() { | |
27 } | |
28 | |
29 void AccessUnitQueue::PushBack(const DemuxerData& data) { | |
30 // Media thread | |
31 DCHECK(!data.access_units.empty()); | |
32 | |
33 #if DCHECK_IS_ON() | |
34 // If there is an AU with |kConfigChanged| status, it must me the last | |
wolenetz
2015/06/11 19:21:20
nits: s/ / / and s/me/be/
Tima Vaisburd
2015/06/11 20:54:08
Done.
| |
35 // AU in the chunk and the data should have exactly one correslonging | |
wolenetz
2015/06/11 19:21:20
nit: correslonging :)
Tima Vaisburd
2015/06/11 20:54:08
Done.
| |
36 // DemuxerConfigs. | |
37 for (size_t i = 0; i < data.access_units.size(); ++i) { | |
38 const AccessUnit& unit = data.access_units[i]; | |
39 | |
40 // EOS must be the last unit in the chunk | |
41 if (unit.is_end_of_stream) { | |
42 DCHECK(i == data.access_units.size() - 1); | |
43 } | |
44 | |
45 // kConfigChanged must be the last unit in the chunk. | |
46 if (unit.status == DemuxerStream::kConfigChanged) { | |
47 DCHECK(i == data.access_units.size() - 1); | |
48 DCHECK(data.demuxer_configs.size() == 1); | |
49 } | |
50 } | |
51 #endif | |
52 | |
53 // Create the next chunk and copy data to it. | |
54 scoped_ptr<DemuxerData> chunk(new DemuxerData(data)); | |
55 | |
56 // EOS flag can only be in the last access unit. | |
57 bool has_eos = chunk->access_units.back().is_end_of_stream; | |
58 | |
59 // Append this chunk to the queue. | |
60 base::AutoLock lock(lock_); | |
61 | |
62 bool was_empty = (current_chunk_ == chunks_.end()); | |
63 | |
64 chunks_.push_back(chunk.Pass()); | |
65 | |
66 // Position the current chunk. | |
67 if (was_empty) { | |
68 current_chunk_ = --chunks_.end(); | |
69 index_in_chunk_ = 0; | |
70 } | |
71 | |
72 // We expect that the chunk containing EOS is the last chunk. | |
73 DCHECK(!has_eos_); | |
74 has_eos_ = has_eos; | |
75 } | |
76 | |
77 void AccessUnitQueue::Advance() { | |
78 // Decoder thread | |
79 base::AutoLock lock(lock_); | |
80 | |
81 if (current_chunk_ == chunks_.end()) | |
82 return; | |
83 | |
84 ++index_in_chunk_; | |
85 if ((*current_chunk_)->access_units.size() <= index_in_chunk_) { | |
wolenetz
2015/06/11 19:21:20
nit: change the condition and return early if we'r
Tima Vaisburd
2015/06/11 20:54:09
Done.
| |
86 index_in_chunk_ = 0; | |
87 ++current_chunk_; | |
88 } | |
89 | |
90 // Keep only |history_chunks_amount_| before the current one. | |
91 | |
92 // std::distance() and std::advance() do not work efficiently with std::list, | |
wolenetz
2015/06/11 19:21:21
aside: I suppose we could cache the queue size our
Tima Vaisburd
2015/06/11 20:54:08
Acknowledged.
| |
93 // but the history_size should be small (default is 0). | |
94 size_t num_consumed_chunks = std::distance(chunks_.begin(), current_chunk_); | |
95 if (num_consumed_chunks > history_chunks_amount_) { | |
96 DataChunkQueue::iterator first_to_keep = chunks_.begin(); | |
97 std::advance(first_to_keep, num_consumed_chunks - history_chunks_amount_); | |
98 chunks_.erase(chunks_.begin(), first_to_keep); | |
99 } | |
100 } | |
101 | |
102 void AccessUnitQueue::Flush() { | |
103 // Media thread | |
104 base::AutoLock lock(lock_); | |
105 | |
106 chunks_.clear(); | |
107 | |
108 current_chunk_ = chunks_.end(); | |
109 index_in_chunk_ = 0; | |
110 has_eos_ = false; | |
111 } | |
112 | |
113 bool AccessUnitQueue::RewindToLastKeyFrame() { | |
114 // Media thread | |
115 base::AutoLock lock(lock_); | |
116 | |
117 // Search for the key frame backwards. Start with the current AU. | |
118 | |
119 // Start with current chunk. | |
120 if (current_chunk_ != chunks_.end()) { | |
121 for (int i = (int)index_in_chunk_; i >= 0; --i) { | |
122 if ((*current_chunk_)->access_units[i].is_key_frame) { | |
123 index_in_chunk_ = i; | |
124 return true; | |
125 } | |
126 } | |
127 } | |
128 | |
129 // Position reverse iterator before the current chunk. | |
130 DataChunkQueue::reverse_iterator rchunk(current_chunk_); | |
131 | |
132 for (; rchunk != chunks_.rend(); ++rchunk) { | |
133 int i = (int)(*rchunk)->access_units.size() - 1; | |
134 for (; i >= 0; --i) { | |
135 if ((*rchunk)->access_units[i].is_key_frame) { | |
136 index_in_chunk_ = i; | |
137 current_chunk_ = --rchunk.base(); | |
wolenetz
2015/06/11 19:21:20
would be good to test this.
Tima Vaisburd
2015/06/11 20:54:08
I added a new test KeyFrameWithLongHistory just be
wolenetz
2015/06/12 22:05:19
Acknowledged.
| |
138 return true; | |
139 } | |
140 } | |
141 } | |
142 | |
143 return false; | |
144 } | |
145 | |
146 AccessUnitQueue::Info AccessUnitQueue::GetInfo() const { | |
147 // Media thread, Decoder thread | |
148 | |
149 Info info; | |
150 base::AutoLock lock(lock_); | |
151 | |
152 info.length = GetUnconsumedAccessUnitLength(); | |
153 info.has_eos = has_eos_; | |
154 info.front_unit = nullptr; | |
155 info.configs = nullptr; | |
156 | |
157 if (info.length > 0) { | |
158 DCHECK(current_chunk_ != chunks_.end()); | |
159 DCHECK(index_in_chunk_ < (*current_chunk_)->access_units.size()); | |
160 info.front_unit = &(*current_chunk_)->access_units[index_in_chunk_]; | |
161 | |
162 if (info.front_unit->status == DemuxerStream::kConfigChanged) { | |
163 DCHECK((*current_chunk_)->demuxer_configs.size() == 1); | |
164 info.configs = &(*current_chunk_)->demuxer_configs[0]; | |
165 } | |
166 } | |
167 return info; | |
168 } | |
169 | |
170 void AccessUnitQueue::SetHistorySizeForTesting(size_t history_chunks_amount) { | |
171 history_chunks_amount_ = history_chunks_amount; | |
172 } | |
173 | |
174 int AccessUnitQueue::GetUnconsumedAccessUnitLength() const { | |
175 int result = 0; | |
176 DataChunkQueue::const_iterator chunk; | |
177 for (chunk = current_chunk_; chunk != chunks_.end(); ++chunk) | |
178 result += (*chunk)->access_units.size(); | |
179 | |
180 result -= index_in_chunk_; | |
181 return result; | |
182 } | |
183 | |
184 } // namespace media | |
OLD | NEW |