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