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 "base/stl_util.h" | |
9 #include "media/base/demuxer_stream.h" | |
10 | |
11 namespace media { | |
12 | |
13 namespace { | |
14 // Amount of history chunks we keep by default. The zero size means we do not | |
15 // keep chunks before the current one and the history is limited by the size | |
16 // of one chunk. | |
17 const int kDefaultHistoryChunksAmount = 0; | |
18 } | |
19 | |
20 AccessUnitQueue::AccessUnitQueue() | |
21 : index_in_chunk_(0), | |
22 history_chunks_amount_(kDefaultHistoryChunksAmount), | |
23 has_eos_(false) { | |
24 current_chunk_ = chunks_.end(); | |
25 } | |
26 | |
27 AccessUnitQueue::~AccessUnitQueue() { | |
28 base::STLDeleteContainerPointers(chunks_.begin(), chunks_.end()); | |
29 } | |
30 | |
31 void AccessUnitQueue::PushBack(const DemuxerData& data) { | |
32 // Media thread | |
33 DCHECK(!data.access_units.empty()); | |
34 | |
35 #if DCHECK_IS_ON() | |
36 // If there is an AU with |kConfigChanged| status, it must be the last | |
37 // AU in the chunk and the data should have exactly one corresponding | |
38 // DemuxerConfigs. | |
39 for (size_t i = 0; i < data.access_units.size(); ++i) { | |
40 const AccessUnit& unit = data.access_units[i]; | |
41 | |
42 // EOS must be the last unit in the chunk. | |
43 if (unit.is_end_of_stream) { | |
44 DCHECK(i == data.access_units.size() - 1); | |
45 } | |
46 | |
47 // kConfigChanged must be the last unit in the chunk. | |
48 if (unit.status == DemuxerStream::kConfigChanged) { | |
49 DCHECK(i == data.access_units.size() - 1); | |
50 DCHECK(data.demuxer_configs.size() == 1); | |
51 } | |
52 | |
53 if (unit.status == DemuxerStream::kAborted) { | |
54 DVLOG(1) << "AccessUnitQueue::" << __FUNCTION__ << " kAborted"; | |
55 } | |
56 } | |
57 #endif | |
58 | |
59 // Create the next chunk and copy data to it. | |
60 DemuxerData* chunk = new DemuxerData(data); | |
61 | |
62 // EOS flag can only be in the last access unit. | |
63 bool has_eos = chunk->access_units.back().is_end_of_stream; | |
64 | |
65 // Append this chunk to the queue. | |
66 base::AutoLock lock(lock_); | |
67 | |
68 // Ignore the input after we have received EOS. | |
69 if (has_eos_) { | |
70 delete chunk; | |
71 return; | |
72 } | |
73 | |
74 bool was_empty = (current_chunk_ == chunks_.end()); | |
75 | |
76 // The container |chunks_| will own the chunk. | |
77 chunks_.push_back(chunk); | |
78 | |
79 // Position the current chunk. | |
80 if (was_empty) { | |
81 current_chunk_ = --chunks_.end(); | |
82 index_in_chunk_ = 0; | |
83 } | |
84 | |
85 // We expect that the chunk containing EOS is the last chunk. | |
86 DCHECK(!has_eos_); | |
87 has_eos_ = has_eos; | |
88 } | |
89 | |
90 void AccessUnitQueue::Advance() { | |
91 // Decoder thread | |
92 base::AutoLock lock(lock_); | |
93 | |
94 if (current_chunk_ == chunks_.end()) | |
95 return; | |
96 | |
97 ++index_in_chunk_; | |
98 if (index_in_chunk_ < (*current_chunk_)->access_units.size()) | |
99 return; | |
100 | |
101 index_in_chunk_ = 0; | |
102 ++current_chunk_; | |
103 | |
104 // Keep only |history_chunks_amount_| before the current one. | |
105 // std::distance() and std::advance() do not work efficiently with std::list, | |
106 // but the history_size should be small (default is 0). | |
107 size_t num_consumed_chunks = std::distance(chunks_.begin(), current_chunk_); | |
108 if (num_consumed_chunks > history_chunks_amount_) { | |
109 DataChunkQueue::iterator first_to_keep = chunks_.begin(); | |
110 std::advance(first_to_keep, num_consumed_chunks - history_chunks_amount_); | |
111 base::STLDeleteContainerPointers(chunks_.begin(), first_to_keep); | |
112 chunks_.erase(chunks_.begin(), first_to_keep); | |
113 } | |
114 } | |
115 | |
116 void AccessUnitQueue::Flush() { | |
117 // Media thread | |
118 base::AutoLock lock(lock_); | |
119 | |
120 base::STLDeleteContainerPointers(chunks_.begin(), chunks_.end()); | |
121 chunks_.clear(); | |
122 | |
123 current_chunk_ = chunks_.end(); | |
124 index_in_chunk_ = 0; | |
125 has_eos_ = false; | |
126 } | |
127 | |
128 bool AccessUnitQueue::RewindToLastKeyFrame() { | |
129 // Media thread | |
130 base::AutoLock lock(lock_); | |
131 | |
132 // Search for the key frame backwards. Start with the current AU. | |
133 | |
134 // Start with current chunk. | |
135 if (current_chunk_ != chunks_.end()) { | |
136 for (int i = (int)index_in_chunk_; i >= 0; --i) { | |
137 if ((*current_chunk_)->access_units[i].is_key_frame) { | |
138 index_in_chunk_ = i; | |
139 return true; | |
140 } | |
141 } | |
142 } | |
143 | |
144 // Position reverse iterator before the current chunk. | |
145 DataChunkQueue::reverse_iterator rchunk(current_chunk_); | |
146 | |
147 for (; rchunk != chunks_.rend(); ++rchunk) { | |
148 int i = (int)(*rchunk)->access_units.size() - 1; | |
149 for (; i >= 0; --i) { | |
150 if ((*rchunk)->access_units[i].is_key_frame) { | |
151 index_in_chunk_ = i; | |
152 current_chunk_ = --rchunk.base(); | |
153 return true; | |
154 } | |
155 } | |
156 } | |
157 | |
158 return false; | |
159 } | |
160 | |
161 AccessUnitQueue::Info AccessUnitQueue::GetInfo() const { | |
162 // Media thread, Decoder thread | |
163 | |
164 Info info; | |
165 base::AutoLock lock(lock_); | |
166 | |
167 GetUnconsumedAccessUnitLength(&info.length, &info.data_length); | |
168 | |
169 info.has_eos = has_eos_; | |
170 info.front_unit = nullptr; | |
171 info.configs = nullptr; | |
172 | |
173 if (info.length > 0) { | |
174 DCHECK(current_chunk_ != chunks_.end()); | |
175 DCHECK(index_in_chunk_ < (*current_chunk_)->access_units.size()); | |
176 info.front_unit = &(*current_chunk_)->access_units[index_in_chunk_]; | |
177 | |
178 if (info.front_unit->status == DemuxerStream::kConfigChanged) { | |
179 DCHECK((*current_chunk_)->demuxer_configs.size() == 1); | |
180 info.configs = &(*current_chunk_)->demuxer_configs[0]; | |
181 } | |
182 } | |
183 return info; | |
184 } | |
185 | |
186 void AccessUnitQueue::SetHistorySizeForTesting(size_t history_chunks_amount) { | |
187 history_chunks_amount_ = history_chunks_amount; | |
188 } | |
189 | |
190 void AccessUnitQueue::GetUnconsumedAccessUnitLength(int* total_length, | |
191 int* data_length) const { | |
192 *total_length = *data_length = 0; | |
193 | |
194 DataChunkQueue::const_iterator chunk; | |
195 for (chunk = current_chunk_; chunk != chunks_.end(); ++chunk) { | |
196 size_t chunk_size = (*chunk)->access_units.size(); | |
197 *total_length += chunk_size; | |
198 *data_length += chunk_size; | |
199 | |
200 // Do not count configuration changes for |data_length|. | |
201 if (!(*chunk)->demuxer_configs.empty()) { | |
202 DCHECK((*chunk)->demuxer_configs.size() == 1); | |
203 --(*data_length); | |
204 } | |
205 } | |
206 | |
207 *total_length -= index_in_chunk_; | |
208 *data_length -= index_in_chunk_; | |
209 } | |
210 | |
211 } // namespace media | |
OLD | NEW |