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 } | |
qinmin
2015/06/11 19:12:13
shouldn't we call STLDeleteContainerPointers() her
Tima Vaisburd
2015/06/11 19:25:02
Thank you!
| |
29 | |
30 void AccessUnitQueue::PushBack(const DemuxerData& data) { | |
31 // Media thread | |
32 DCHECK(!data.access_units.empty()); | |
33 | |
34 #if DCHECK_IS_ON() | |
35 // If there is an AU with |kConfigChanged| status, it must me the last | |
36 // AU in the chunk and the data should have exactly one correslonging | |
37 // DemuxerConfigs. | |
38 for (size_t i = 0; i < data.access_units.size(); ++i) { | |
39 const AccessUnit& unit = data.access_units[i]; | |
40 | |
41 // EOS must be the last unit in the chunk | |
42 if (unit.is_end_of_stream) { | |
43 DCHECK(i == data.access_units.size() - 1); | |
44 } | |
45 | |
46 // kConfigChanged must be the last unit in the chunk. | |
47 if (unit.status == DemuxerStream::kConfigChanged) { | |
48 DCHECK(i == data.access_units.size() - 1); | |
49 DCHECK(data.demuxer_configs.size() == 1); | |
50 } | |
51 } | |
52 #endif | |
53 | |
54 // Create the next chunk and copy data to it. | |
55 DemuxerData* chunk = new DemuxerData(data); | |
56 | |
57 // EOS flag can only be in the last access unit. | |
58 bool has_eos = chunk->access_units.back().is_end_of_stream; | |
59 | |
60 // Append this chunk to the queue. | |
61 base::AutoLock lock(lock_); | |
62 | |
63 bool was_empty = (current_chunk_ == chunks_.end()); | |
64 | |
65 // The container |chunks_| will own the chunk. | |
66 chunks_.push_back(chunk); | |
67 | |
68 // Position the current chunk. | |
69 if (was_empty) { | |
70 current_chunk_ = --chunks_.end(); | |
71 index_in_chunk_ = 0; | |
72 } | |
73 | |
74 // We expect that the chunk containing EOS is the last chunk. | |
75 DCHECK(!has_eos_); | |
76 has_eos_ = has_eos; | |
77 } | |
78 | |
79 void AccessUnitQueue::Advance() { | |
80 // Decoder thread | |
81 base::AutoLock lock(lock_); | |
82 | |
83 if (current_chunk_ == chunks_.end()) | |
84 return; | |
85 | |
86 ++index_in_chunk_; | |
87 if ((*current_chunk_)->access_units.size() <= index_in_chunk_) { | |
88 index_in_chunk_ = 0; | |
89 ++current_chunk_; | |
90 } | |
91 | |
92 // Keep only |history_chunks_amount_| before the current one. | |
93 | |
94 // std::distance() and std::advance() do not work efficiently with std::list, | |
95 // but the history_size should be small (default is 0). | |
96 size_t num_consumed_chunks = std::distance(chunks_.begin(), current_chunk_); | |
97 if (num_consumed_chunks > history_chunks_amount_) { | |
98 DataChunkQueue::iterator first_to_keep = chunks_.begin(); | |
99 std::advance(first_to_keep, num_consumed_chunks - history_chunks_amount_); | |
100 STLDeleteContainerPointers(chunks_.begin(), first_to_keep); | |
101 chunks_.erase(chunks_.begin(), first_to_keep); | |
102 } | |
103 } | |
104 | |
105 void AccessUnitQueue::Flush() { | |
106 // Media thread | |
107 base::AutoLock lock(lock_); | |
108 | |
109 STLDeleteContainerPointers(chunks_.begin(), chunks_.end()); | |
110 chunks_.clear(); | |
111 | |
112 current_chunk_ = chunks_.end(); | |
113 index_in_chunk_ = 0; | |
114 has_eos_ = false; | |
115 } | |
116 | |
117 bool AccessUnitQueue::RewindToLastKeyFrame() { | |
118 // Media thread | |
119 base::AutoLock lock(lock_); | |
120 | |
121 // Search for the key frame backwards. Start with the current AU. | |
122 | |
123 // Start with current chunk. | |
124 if (current_chunk_ != chunks_.end()) { | |
125 for (int i = (int)index_in_chunk_; i >= 0; --i) { | |
126 if ((*current_chunk_)->access_units[i].is_key_frame) { | |
127 index_in_chunk_ = i; | |
128 return true; | |
129 } | |
130 } | |
131 } | |
132 | |
133 // Position reverse iterator before the current chunk. | |
134 DataChunkQueue::reverse_iterator rchunk(current_chunk_); | |
135 | |
136 for (; rchunk != chunks_.rend(); ++rchunk) { | |
137 int i = (int)(*rchunk)->access_units.size() - 1; | |
138 for (; i >= 0; --i) { | |
139 if ((*rchunk)->access_units[i].is_key_frame) { | |
140 index_in_chunk_ = i; | |
141 current_chunk_ = --rchunk.base(); | |
142 return true; | |
143 } | |
144 } | |
145 } | |
146 | |
147 return false; | |
148 } | |
149 | |
150 AccessUnitQueue::Info AccessUnitQueue::GetInfo() const { | |
151 // Media thread, Decoder thread | |
152 | |
153 Info info; | |
154 base::AutoLock lock(lock_); | |
155 | |
156 info.length = GetUnconsumedAccessUnitLength(); | |
157 info.has_eos = has_eos_; | |
158 info.front_unit = nullptr; | |
159 info.configs = nullptr; | |
160 | |
161 if (info.length > 0) { | |
162 DCHECK(current_chunk_ != chunks_.end()); | |
163 DCHECK(index_in_chunk_ < (*current_chunk_)->access_units.size()); | |
164 info.front_unit = &(*current_chunk_)->access_units[index_in_chunk_]; | |
165 | |
166 if (info.front_unit->status == DemuxerStream::kConfigChanged) { | |
167 DCHECK((*current_chunk_)->demuxer_configs.size() == 1); | |
168 info.configs = &(*current_chunk_)->demuxer_configs[0]; | |
169 } | |
170 } | |
171 return info; | |
172 } | |
173 | |
174 void AccessUnitQueue::SetHistorySizeForTesting(size_t history_chunks_amount) { | |
175 history_chunks_amount_ = history_chunks_amount; | |
176 } | |
177 | |
178 int AccessUnitQueue::GetUnconsumedAccessUnitLength() const { | |
179 int result = 0; | |
180 DataChunkQueue::const_iterator chunk; | |
181 for (chunk = current_chunk_; chunk != chunks_.end(); ++chunk) | |
182 result += (*chunk)->access_units.size(); | |
183 | |
184 result -= index_in_chunk_; | |
185 return result; | |
186 } | |
187 | |
188 } // namespace media | |
OLD | NEW |