Chromium Code Reviews| Index: media/base/android/access_unit_queue.cc |
| diff --git a/media/base/android/access_unit_queue.cc b/media/base/android/access_unit_queue.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..236cd81a0d07ccfe4bdb57a217afefef1a26e6c4 |
| --- /dev/null |
| +++ b/media/base/android/access_unit_queue.cc |
| @@ -0,0 +1,151 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/android/access_unit_queue.h" |
| + |
| +#include "base/logging.h" |
| +#include "media/base/demuxer_stream.h" |
| + |
| +namespace media { |
| + |
| +AccessUnitQueue::AccessUnitQueue() |
| + : index_in_chunk_(0), |
| + has_eos_(false) { |
| + current_chunk_ = chunks_.end(); |
| +} |
| + |
| +AccessUnitQueue::~AccessUnitQueue() {} |
| + |
| +void AccessUnitQueue::PushBack(const DemuxerData& data) { |
| + // Media thread |
| + DCHECK(!data.access_units.empty()); |
| + |
| +#if DCHECK_IS_ON() |
| + // Verify the rule "one DemuxerConfigs for one AU with |kConfigChanged|". |
|
wolenetz
2015/06/08 21:37:08
Like EOS (and more simple vs the current verificat
Tima Vaisburd
2015/06/09 21:29:21
Changed the comment and simplified the check.
|
| + size_t num_config_placeholders = 0; |
| + |
| + for (size_t i = 0; i < data.access_units.size(); ++i) { |
| + const AccessUnit& unit = data.access_units[i]; |
| + if (unit.status == DemuxerStream::kConfigChanged) |
| + ++num_config_placeholders; |
| + |
| + // EOS must be the last unit in the chunk |
| + if (unit.is_end_of_stream) { |
| + DCHECK(i == data.access_units.size() - 1); |
| + } |
| + } |
| +#endif |
| + |
| + DCHECK(data.demuxer_configs.size() == num_config_placeholders); |
| + |
| + // Also verify that there is no more than one |kConfigChanged| per chunk. |
| + DCHECK(num_config_placeholders <= 1); |
|
wolenetz
2015/06/08 21:37:09
num_config_placeholders is undefined here in non-D
Tima Vaisburd
2015/06/09 21:29:20
Removed since the checking code is simplified.
|
| + |
| + // Create the next chunk and copy data to it. |
| + scoped_ptr<DemuxerData> chunk(new DemuxerData(data)); |
| + |
| + // EOS flag can only be in the last access unit. |
| + bool has_eos = chunk->access_units.back().is_end_of_stream; |
| + |
| + // Append this chunk to the queue. |
| + base::AutoLock lock(lock_); |
| + |
| + bool was_empty = (current_chunk_ == chunks_.end()); |
| + |
| + chunks_.push_back(chunk.Pass()); |
| + |
| + // Position the current chunk. |
| + if (was_empty) { |
| + current_chunk_ = --chunks_.end(); |
| + index_in_chunk_ = 0; |
| + } |
| + |
| + // We expect that the chunk containing EOS is the last chunk. |
| + DCHECK(!has_eos_); |
| + has_eos_ = has_eos; |
| +} |
| + |
| +void AccessUnitQueue::Advance() { |
| + // Decoder thread |
| + base::AutoLock lock(lock_); |
| + |
| + if (current_chunk_ == chunks_.end()) |
| + return; |
| + |
| + ++index_in_chunk_; |
| + if ((*current_chunk_)->access_units.size() <= index_in_chunk_) { |
| + index_in_chunk_ = 0; |
| + ++current_chunk_; |
| + } |
| + |
| + // Remove all chunks before the current |
| + DataChunkQueue::iterator first_to_keep = current_chunk_; |
| + chunks_.erase(chunks_.begin(), first_to_keep); |
| +} |
| + |
| +void AccessUnitQueue::Flush() { |
| + // Media thread |
| + base::AutoLock lock(lock_); |
| + |
| + chunks_.clear(); |
| + |
| + current_chunk_ = chunks_.end(); |
| + index_in_chunk_ = 0; |
| + has_eos_ = false; |
| +} |
| + |
| +bool AccessUnitQueue::SkipToKeyFrame() { |
| + // Media thread |
| + base::AutoLock lock(lock_); |
| + |
| + // Search backwards in the current chunk only. |
|
wolenetz
2015/06/08 21:37:08
This isn't clear from the .h comment. Also, why is
Tima Vaisburd
2015/06/09 21:29:21
Updated the comment in .h.
I searched in the curr
wolenetz
2015/06/11 19:21:20
I tend to agree that increased complexity might no
Tima Vaisburd
2015/06/11 20:54:08
That's no problem in general, and I went forward w
|
| + |
| + if (current_chunk_ == chunks_.end()) |
| + return false; |
| + |
| + for (int i = index_in_chunk_; i >= 0; --i) { |
|
wolenetz
2015/06/08 21:37:09
minor minor nit: On some platforms (like Windows),
Tima Vaisburd
2015/06/09 21:29:21
I did not find an elegant solution, just put a cas
|
| + if ((*current_chunk_)->access_units[i].is_key_frame) { |
| + index_in_chunk_ = i; |
| + return true; |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +AccessUnitQueue::Info AccessUnitQueue::GetInfo() const { |
| + // Media thread, Decoder thread |
| + |
| + Info info; |
| + base::AutoLock lock(lock_); |
| + |
| + info.length = GetUndecodedAccessUnitLength(); |
| + info.has_eos = has_eos_; |
| + info.front_unit = nullptr; |
| + info.configs = nullptr; |
| + |
| + if (info.length > 0) { |
| + DCHECK(current_chunk_ != chunks_.end()); |
| + DCHECK(index_in_chunk_ < (*current_chunk_)->access_units.size()); |
| + info.front_unit = &(*current_chunk_)->access_units[index_in_chunk_]; |
|
wolenetz
2015/06/08 21:37:08
Who owns the DemuxerData scoped pointer at this po
Tima Vaisburd
2015/06/09 21:29:21
The container remains to be the owner. I wanted to
wolenetz
2015/06/11 19:21:20
See my other comments.
|
| + |
| + if (info.front_unit->status == DemuxerStream::kConfigChanged) { |
| + DCHECK((*current_chunk_)->demuxer_configs.size() == 1); |
| + info.configs = &(*current_chunk_)->demuxer_configs[0]; |
|
wolenetz
2015/06/08 21:37:09
Likewise: ownership?
Tima Vaisburd
2015/06/09 21:29:20
Same as above
|
| + } |
| + } |
| + return info; |
| +} |
| + |
| +int AccessUnitQueue::GetUndecodedAccessUnitLength() const { |
| + int result = 0; |
| + DataChunkQueue::const_iterator chunk; |
| + for (chunk = current_chunk_; chunk != chunks_.end(); ++chunk) |
| + result += (*chunk)->access_units.size(); |
| + |
| + result -= index_in_chunk_; |
| + return result; |
| +} |
| + |
| +} // namespace media |