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 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 #endif | |
54 | |
55 // Ignore the input after we have received EOS. | |
56 if (has_eos_) | |
57 return; | |
58 | |
59 // Create the next chunk and copy data to it. | |
60 DemuxerData* chunk = new DemuxerData(data); | |
wolenetz
2015/06/12 22:05:19
aside: Is it always necessary to copy data? This c
Tima Vaisburd
2015/06/12 22:44:01
Acknowledged.
| |
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 bool was_empty = (current_chunk_ == chunks_.end()); | |
69 | |
70 // The container |chunks_| will own the chunk. | |
71 chunks_.push_back(chunk); | |
72 | |
73 // Position the current chunk. | |
74 if (was_empty) { | |
75 current_chunk_ = --chunks_.end(); | |
76 index_in_chunk_ = 0; | |
77 } | |
78 | |
79 // We expect that the chunk containing EOS is the last chunk. | |
80 DCHECK(!has_eos_); | |
81 has_eos_ = has_eos; | |
82 } | |
83 | |
84 void AccessUnitQueue::Advance() { | |
85 // Decoder thread | |
86 base::AutoLock lock(lock_); | |
87 | |
88 if (current_chunk_ == chunks_.end()) | |
89 return; | |
90 | |
91 ++index_in_chunk_; | |
92 if (index_in_chunk_ < (*current_chunk_)->access_units.size()) | |
93 return; | |
94 | |
95 index_in_chunk_ = 0; | |
96 ++current_chunk_; | |
97 | |
98 // Keep only |history_chunks_amount_| before the current one. | |
99 | |
wolenetz
2015/06/12 22:05:19
nit: remove empty line
Tima Vaisburd
2015/06/12 22:44:01
Done.
| |
100 // std::distance() and std::advance() do not work efficiently with std::list, | |
101 // but the history_size should be small (default is 0). | |
102 size_t num_consumed_chunks = std::distance(chunks_.begin(), current_chunk_); | |
103 if (num_consumed_chunks > history_chunks_amount_) { | |
104 DataChunkQueue::iterator first_to_keep = chunks_.begin(); | |
105 std::advance(first_to_keep, num_consumed_chunks - history_chunks_amount_); | |
106 STLDeleteContainerPointers(chunks_.begin(), first_to_keep); | |
wolenetz
2015/06/12 22:05:19
my stl-fu is failing me: does this and the followi
wolenetz
2015/06/12 22:17:15
No double-free. Ignore my fu failure :)
| |
107 chunks_.erase(chunks_.begin(), first_to_keep); | |
108 } | |
109 } | |
110 | |
111 void AccessUnitQueue::Flush() { | |
112 // Media thread | |
113 base::AutoLock lock(lock_); | |
114 | |
115 STLDeleteContainerPointers(chunks_.begin(), chunks_.end()); | |
116 chunks_.clear(); | |
117 | |
118 current_chunk_ = chunks_.end(); | |
119 index_in_chunk_ = 0; | |
120 has_eos_ = false; | |
121 } | |
122 | |
123 bool AccessUnitQueue::RewindToLastKeyFrame() { | |
124 // Media thread | |
125 base::AutoLock lock(lock_); | |
126 | |
127 // Search for the key frame backwards. Start with the current AU. | |
128 | |
129 // Start with current chunk. | |
130 if (current_chunk_ != chunks_.end()) { | |
131 for (int i = (int)index_in_chunk_; i >= 0; --i) { | |
132 if ((*current_chunk_)->access_units[i].is_key_frame) { | |
133 index_in_chunk_ = i; | |
134 return true; | |
135 } | |
136 } | |
137 } | |
138 | |
139 // Position reverse iterator before the current chunk. | |
140 DataChunkQueue::reverse_iterator rchunk(current_chunk_); | |
141 | |
142 for (; rchunk != chunks_.rend(); ++rchunk) { | |
143 int i = (int)(*rchunk)->access_units.size() - 1; | |
144 for (; i >= 0; --i) { | |
145 if ((*rchunk)->access_units[i].is_key_frame) { | |
146 index_in_chunk_ = i; | |
147 current_chunk_ = --rchunk.base(); | |
148 return true; | |
149 } | |
150 } | |
151 } | |
152 | |
153 return false; | |
154 } | |
155 | |
156 AccessUnitQueue::Info AccessUnitQueue::GetInfo() const { | |
157 // Media thread, Decoder thread | |
158 | |
159 Info info; | |
160 base::AutoLock lock(lock_); | |
161 | |
162 info.length = GetUnconsumedAccessUnitLength(); | |
163 info.has_eos = has_eos_; | |
164 info.front_unit = nullptr; | |
165 info.configs = nullptr; | |
166 | |
167 if (info.length > 0) { | |
168 DCHECK(current_chunk_ != chunks_.end()); | |
169 DCHECK(index_in_chunk_ < (*current_chunk_)->access_units.size()); | |
170 info.front_unit = &(*current_chunk_)->access_units[index_in_chunk_]; | |
171 | |
172 if (info.front_unit->status == DemuxerStream::kConfigChanged) { | |
173 DCHECK((*current_chunk_)->demuxer_configs.size() == 1); | |
174 info.configs = &(*current_chunk_)->demuxer_configs[0]; | |
175 } | |
176 } | |
177 return info; | |
178 } | |
179 | |
180 void AccessUnitQueue::SetHistorySizeForTesting(size_t history_chunks_amount) { | |
181 history_chunks_amount_ = history_chunks_amount; | |
182 } | |
183 | |
184 int AccessUnitQueue::GetUnconsumedAccessUnitLength() const { | |
185 int result = 0; | |
186 DataChunkQueue::const_iterator chunk; | |
187 for (chunk = current_chunk_; chunk != chunks_.end(); ++chunk) | |
188 result += (*chunk)->access_units.size(); | |
189 | |
190 result -= index_in_chunk_; | |
191 return result; | |
192 } | |
193 | |
194 } // namespace media | |
OLD | NEW |