Replace vector_as_array(...) with vector::data()

vector::data() introduced in C++11 does exactly what vector_as_array does.

Change-Id: Iab436b6f1ce7db7678bd240256d9ae80208be07b
This commit is contained in:
KongQun Yang 2016-01-29 13:23:12 -08:00
parent 583f1f3352
commit cef1729652
15 changed files with 51 additions and 77 deletions

View File

@ -6,6 +6,7 @@
#include "packager/media/base/buffer_writer.h"
#include "packager/base/logging.h"
#include "packager/base/sys_byteorder.h"
#include "packager/media/file/file.h"

View File

@ -9,7 +9,7 @@
#include <vector>
#include "packager/base/stl_util.h"
#include "packager/base/macros.h"
#include "packager/media/base/status.h"
namespace edash_packager {
@ -56,7 +56,7 @@ class BufferWriter {
void Clear() { buf_.clear(); }
size_t Size() const { return buf_.size(); }
/// @return Underlying buffer. Behavior is undefined if the buffer size is 0.
const uint8_t* Buffer() const { return vector_as_array(&buf_); }
const uint8_t* Buffer() const { return buf_.data(); }
/// Write the buffer to file. The internal buffer will be cleared after
/// writing.

View File

@ -5,7 +5,6 @@
#include <gtest/gtest.h>
#include "packager/base/files/file_util.h"
#include "packager/base/stl_util.h"
#include "packager/media/base/container_names.h"
#include "packager/media/test/test_data_util.h"
@ -186,9 +185,8 @@ TEST(ContainerNamesTest, WebVtt) {
webvtt_with_utf8_byte_order_mark.end(), kWebVtt,
kWebVtt + arraysize(kWebVtt));
EXPECT_EQ(
CONTAINER_WEBVTT,
DetermineContainer(vector_as_array(&webvtt_with_utf8_byte_order_mark),
EXPECT_EQ(CONTAINER_WEBVTT,
DetermineContainer(webvtt_with_utf8_byte_order_mark.data(),
webvtt_with_utf8_byte_order_mark.size()));
}

View File

@ -8,8 +8,6 @@
#include <gtest/gtest.h>
#include "packager/base/stl_util.h"
namespace edash_packager {
namespace media {
@ -31,7 +29,7 @@ scoped_refptr<StreamInfo> CreateVideoStreamInfo(
param.pixel_height,
0, // trick_play_rate
param.nalu_length_size,
vector_as_array(&param.extra_data),
param.extra_data.data(),
param.extra_data.size(),
param.is_encrypted));
}

View File

@ -11,7 +11,6 @@
#include <algorithm>
#include "packager/base/logging.h"
#include "packager/base/stl_util.h"
namespace edash_packager {
@ -28,8 +27,8 @@ IoCache::IoCache(uint64_t cache_size)
// condition r_ptr == w_ptr is unambiguous (buffer empty).
circular_buffer_(cache_size + 1),
end_ptr_(&circular_buffer_[0] + cache_size + 1),
r_ptr_(vector_as_array(&circular_buffer_)),
w_ptr_(vector_as_array(&circular_buffer_)),
r_ptr_(circular_buffer_.data()),
w_ptr_(circular_buffer_.data()),
closed_(false) {}
IoCache::~IoCache() {
@ -102,7 +101,7 @@ uint64_t IoCache::Write(const void* buffer, uint64_t size) {
void IoCache::Clear() {
AutoLock lock(lock_);
r_ptr_ = w_ptr_ = vector_as_array(&circular_buffer_);
r_ptr_ = w_ptr_ = circular_buffer_.data();
// Let any writers know that there is room in the cache.
read_event_.Signal();
}
@ -117,7 +116,7 @@ void IoCache::Close() {
void IoCache::Reopen() {
AutoLock lock(lock_);
CHECK(closed_);
r_ptr_ = w_ptr_ = vector_as_array(&circular_buffer_);
r_ptr_ = w_ptr_ = circular_buffer_.data();
closed_ = false;
read_event_.Reset();
write_event_.Reset();
@ -134,9 +133,9 @@ uint64_t IoCache::BytesFree() {
}
uint64_t IoCache::BytesCachedInternal() {
return (r_ptr_ <= w_ptr_) ?
w_ptr_ - r_ptr_ :
(end_ptr_ - r_ptr_) + (w_ptr_ - vector_as_array(&circular_buffer_));
return (r_ptr_ <= w_ptr_)
? w_ptr_ - r_ptr_
: (end_ptr_ - r_ptr_) + (w_ptr_ - circular_buffer_.data());
}
uint64_t IoCache::BytesFreeInternal() {

View File

@ -9,7 +9,6 @@
#include <algorithm>
#include "packager/base/bind.h"
#include "packager/base/bind_helpers.h"
#include "packager/base/stl_util.h"
#include "packager/base/threading/platform_thread.h"
#include "packager/media/base/closure_thread.h"
#include "packager/media/file/io_cache.h"
@ -29,8 +28,8 @@ class IoCacheTest : public testing::Test {
int sleep_between_writes,
bool close_when_done) {
for (uint64_t write_idx = 0; write_idx < num_writes; ++write_idx) {
uint64_t write_result = cache_->Write(vector_as_array(&test_buffer),
test_buffer.size());
uint64_t write_result =
cache_->Write(test_buffer.data(), test_buffer.size());
if (!write_result) {
// Cache was closed.
cache_closed_ = true;
@ -60,7 +59,7 @@ class IoCacheTest : public testing::Test {
void GenerateTestBuffer(uint64_t size, std::vector<uint8_t>* test_buffer) {
test_buffer->resize(size);
uint8_t* w_ptr(vector_as_array(test_buffer));
uint8_t* w_ptr(test_buffer->data());
while (size) {
uint64_t copy_size(std::min(size, kBlockSize));
memcpy(w_ptr, reference_block_, copy_size);
@ -106,8 +105,7 @@ TEST_F(IoCacheTest, VerySmallWrite) {
WriteToCacheThreaded(write_buffer, 1, 0, false);
std::vector<uint8_t> read_buffer(kTestBytes);
EXPECT_EQ(kTestBytes, cache_->Read(vector_as_array(&read_buffer),
kTestBytes));
EXPECT_EQ(kTestBytes, cache_->Read(read_buffer.data(), kTestBytes));
EXPECT_EQ(write_buffer, read_buffer);
}
@ -119,8 +117,7 @@ TEST_F(IoCacheTest, LotsOfAlignedBlocks) {
WriteToCacheThreaded(write_buffer, kNumWrites, 0, false);
for (uint64_t num_reads = 0; num_reads < kNumWrites; ++num_reads) {
std::vector<uint8_t> read_buffer(kBlockSize);
EXPECT_EQ(kBlockSize, cache_->Read(vector_as_array(&read_buffer),
kBlockSize));
EXPECT_EQ(kBlockSize, cache_->Read(read_buffer.data(), kBlockSize));
EXPECT_EQ(write_buffer, read_buffer);
}
}
@ -138,8 +135,8 @@ TEST_F(IoCacheTest, LotsOfUnalignedBlocks) {
WriteToCacheThreaded(write_buffer2, kNumWrites, 0, false);
std::vector<uint8_t> read_buffer1(kUnalignBlockSize);
EXPECT_EQ(kUnalignBlockSize, cache_->Read(vector_as_array(&read_buffer1),
kUnalignBlockSize));
EXPECT_EQ(kUnalignBlockSize,
cache_->Read(read_buffer1.data(), kUnalignBlockSize));
EXPECT_EQ(write_buffer1, read_buffer1);
std::vector<uint8> verify_buffer;
for (uint64_t idx = 0; idx < kNumWrites; ++idx)
@ -149,12 +146,10 @@ TEST_F(IoCacheTest, LotsOfUnalignedBlocks) {
uint64_t verify_index(0);
while (verify_index < verify_buffer.size()) {
std::vector<uint8_t> read_buffer2(kBlockSize);
uint64_t bytes_read = cache_->Read(vector_as_array(&read_buffer2),
kBlockSize);
uint64_t bytes_read = cache_->Read(read_buffer2.data(), kBlockSize);
EXPECT_NE(0U, bytes_read);
EXPECT_FALSE(memcmp(&verify_buffer[verify_index],
vector_as_array(&read_buffer2),
bytes_read));
EXPECT_FALSE(
memcmp(&verify_buffer[verify_index], read_buffer2.data(), bytes_read));
verify_index += bytes_read;
}
}
@ -168,8 +163,7 @@ TEST_F(IoCacheTest, SlowWrite) {
WriteToCacheThreaded(write_buffer, kNumWrites, kWriteDelayMs, false);
for (uint64_t num_reads = 0; num_reads < kNumWrites; ++num_reads) {
std::vector<uint8_t> read_buffer(kBlockSize);
EXPECT_EQ(kBlockSize, cache_->Read(vector_as_array(&read_buffer),
kBlockSize));
EXPECT_EQ(kBlockSize, cache_->Read(read_buffer.data(), kBlockSize));
EXPECT_EQ(write_buffer, read_buffer);
}
}
@ -183,8 +177,7 @@ TEST_F(IoCacheTest, SlowRead) {
WriteToCacheThreaded(write_buffer, kNumWrites, 0, false);
for (uint64_t num_reads = 0; num_reads < kNumWrites; ++num_reads) {
std::vector<uint8_t> read_buffer(kBlockSize);
EXPECT_EQ(kBlockSize, cache_->Read(vector_as_array(&read_buffer),
kBlockSize));
EXPECT_EQ(kBlockSize, cache_->Read(read_buffer.data(), kBlockSize));
EXPECT_EQ(write_buffer, read_buffer);
base::PlatformThread::Sleep(
base::TimeDelta::FromMilliseconds(kReadDelayMs));
@ -223,8 +216,7 @@ TEST_F(IoCacheTest, Reopen) {
WriteToCacheThreaded(write_buffer, 1, 0, true);
std::vector<uint8_t> read_buffer(kTestBytes1);
EXPECT_EQ(kTestBytes1, cache_->Read(vector_as_array(&read_buffer),
kTestBytes1));
EXPECT_EQ(kTestBytes1, cache_->Read(read_buffer.data(), kTestBytes1));
EXPECT_EQ(write_buffer, read_buffer);
WaitForWriterThread();
@ -235,8 +227,7 @@ TEST_F(IoCacheTest, Reopen) {
GenerateTestBuffer(kTestBytes2, &write_buffer);
WriteToCacheThreaded(write_buffer, 1, 0, false);
read_buffer.resize(kTestBytes2);
EXPECT_EQ(kTestBytes2, cache_->Read(vector_as_array(&read_buffer),
kTestBytes2));
EXPECT_EQ(kTestBytes2, cache_->Read(read_buffer.data(), kTestBytes2));
EXPECT_EQ(write_buffer, read_buffer);
}
@ -272,8 +263,7 @@ TEST_F(IoCacheTest, LargeRead) {
base::TimeDelta::FromMilliseconds(10));
}
std::vector<uint8_t> read_buffer(kCacheSize);
EXPECT_EQ(kCacheSize, cache_->Read(vector_as_array(&read_buffer),
kCacheSize));
EXPECT_EQ(kCacheSize, cache_->Read(read_buffer.data(), kCacheSize));
EXPECT_EQ(verify_buffer, read_buffer);
cache_->Close();
}

View File

@ -6,7 +6,6 @@
#include "packager/media/filters/avc_decoder_configuration.h"
#include "packager/base/stl_util.h"
#include "packager/base/strings/string_number_conversions.h"
#include "packager/base/strings/string_util.h"
#include "packager/media/base/buffer_reader.h"
@ -26,7 +25,7 @@ AVCDecoderConfiguration::AVCDecoderConfiguration()
AVCDecoderConfiguration::~AVCDecoderConfiguration() {}
bool AVCDecoderConfiguration::Parse(const std::vector<uint8_t>& data) {
BufferReader reader(vector_as_array(&data), data.size());
BufferReader reader(data.data(), data.size());
RCHECK(reader.Read1(&version_) && version_ == 1 &&
reader.Read1(&profile_indication_) &&
reader.Read1(&profile_compatibility_) && reader.Read1(&avc_level_));

View File

@ -5,7 +5,6 @@
#include <gtest/gtest.h>
#include "packager/base/logging.h"
#include "packager/base/stl_util.h"
#include "packager/media/test/test_data_util.h"
#include "packager/media/filters/h264_parser.h"
@ -19,8 +18,7 @@ TEST(H264ParserTest, StreamFileParsing) {
int num_nalus = 759;
H264Parser parser;
NaluReader reader(kIsAnnexbByteStream, vector_as_array(&buffer),
buffer.size());
NaluReader reader(kIsAnnexbByteStream, buffer.data(), buffer.size());
// Parse until the end of stream/unsupported stream/error in stream is found.
int num_parsed_nalus = 0;

View File

@ -6,7 +6,6 @@
#include "packager/media/filters/hevc_decoder_configuration.h"
#include "packager/base/stl_util.h"
#include "packager/base/strings/string_number_conversions.h"
#include "packager/base/strings/string_util.h"
#include "packager/media/base/buffer_reader.h"
@ -86,7 +85,7 @@ HEVCDecoderConfiguration::HEVCDecoderConfiguration()
HEVCDecoderConfiguration::~HEVCDecoderConfiguration() {}
bool HEVCDecoderConfiguration::Parse(const std::vector<uint8_t>& data) {
BufferReader reader(vector_as_array(&data), data.size());
BufferReader reader(data.data(), data.size());
uint8_t profile_indication = 0;
uint8_t length_size_minus_one = 0;

View File

@ -63,7 +63,7 @@ VPCodecConfiguration::VPCodecConfiguration(
VPCodecConfiguration::~VPCodecConfiguration(){};
bool VPCodecConfiguration::Parse(const std::vector<uint8_t>& data) {
BitReader reader(vector_as_array(&data), data.size());
BitReader reader(data.data(), data.size());
RCHECK(reader.ReadBits(8, &profile_));
RCHECK(reader.ReadBits(8, &level_));
RCHECK(reader.ReadBits(4, &bit_depth_));

View File

@ -9,7 +9,6 @@
#include "packager/base/bind.h"
#include "packager/base/logging.h"
#include "packager/base/stl_util.h"
#include "packager/media/base/media_sample.h"
#include "packager/media/base/timestamp.h"
#include "packager/media/base/video_stream_info.h"
@ -157,12 +156,9 @@ void EsParserH264Test::LoadStream(const char* filename) {
// The input file does not have AUDs.
std::vector<Packet> access_units_without_aud =
GetAccessUnits(vector_as_array(&buffer), buffer.size());
GetAccessUnits(buffer.data(), buffer.size());
ASSERT_GT(access_units_without_aud.size(), 0u);
AppendAUD(vector_as_array(&buffer),
buffer.size(),
access_units_without_aud,
stream_,
AppendAUD(buffer.data(), buffer.size(), access_units_without_aud, stream_,
access_units_);
}

View File

@ -130,7 +130,7 @@ class BoxBuffer {
if (reader_)
return reader_->ReadToVector(vector, count);
DCHECK_EQ(vector->size(), count);
writer_->AppendArray(vector_as_array(vector), count);
writer_->AppendArray(vector->data(), count);
return true;
}

View File

@ -366,7 +366,7 @@ bool SampleEncryption::ParseFromSampleEncryptionData(
std::vector<SampleEncryptionEntry>* sample_encryption_entries) const {
DCHECK(IsIvSizeValid(iv_size));
BufferReader reader(vector_as_array(&sample_encryption_data),
BufferReader reader(sample_encryption_data.data(),
sample_encryption_data.size());
uint32_t sample_count = 0;
RCHECK(reader.Read4(&sample_count));

View File

@ -445,7 +445,7 @@ bool MP4MediaParser::ParseMoov(BoxReader* reader) {
sampling_frequency,
max_bitrate,
avg_bitrate,
vector_as_array(&extra_data),
extra_data.data(),
extra_data.size(),
is_encrypted));
}
@ -541,7 +541,7 @@ bool MP4MediaParser::ParseMoov(BoxReader* reader) {
codec_string, track->media.header.language.code, coded_width,
coded_height, pixel_width, pixel_height,
0, // trick_play_rate
nalu_length_size, vector_as_array(&entry.codec_config_record.data),
nalu_length_size, entry.codec_config_record.data.data(),
entry.codec_config_record.data.size(), is_encrypted));
}
}

View File

@ -8,7 +8,6 @@
#include <sstream>
#include <vector>
#include "packager/base/stl_util.h"
#include "packager/base/strings/string_number_conversions.h"
#include "packager/media/base/aes_encryptor.h"
#include "packager/media/base/audio_stream_info.h"
@ -550,7 +549,7 @@ bool WvmMediaParser::ParseIndexEntry() {
return false;
}
const uint8_t* read_ptr = vector_as_array(&index_data_);
const uint8_t* read_ptr = index_data_.data();
if (ntohlFromBuffer(read_ptr) != kIndexMagic) {
index_data_.clear();
return false;
@ -737,16 +736,15 @@ bool WvmMediaParser::ParseIndexEntry() {
index_metadata_max_size -= length;
}
// End Index metadata
index_size = read_ptr - vector_as_array(&index_data_);
index_size = read_ptr - index_data_.data();
if (has_video) {
VideoCodec video_codec = kCodecH264;
stream_infos_.push_back(new VideoStreamInfo(
stream_id_count_, time_scale, track_duration, video_codec,
std::string(), std::string(), video_width, video_height,
pixel_width, pixel_height, trick_play_rate, nalu_length_size,
vector_as_array(&video_codec_config), video_codec_config.size(),
true));
std::string(), std::string(), video_width, video_height, pixel_width,
pixel_height, trick_play_rate, nalu_length_size,
video_codec_config.data(), video_codec_config.size(), true));
program_demux_stream_map_[base::UintToString(index_program_id_) + ":" +
base::UintToString(video_pes_stream_id ?
video_pes_stream_id :
@ -759,7 +757,7 @@ bool WvmMediaParser::ParseIndexEntry() {
stream_infos_.push_back(new AudioStreamInfo(
stream_id_count_, time_scale, track_duration, audio_codec,
std::string(), std::string(), kAacSampleSizeBits, num_channels,
sampling_frequency, 0, 0, vector_as_array(&audio_codec_config),
sampling_frequency, 0, 0, audio_codec_config.data(),
audio_codec_config.size(), true));
program_demux_stream_map_[base::UintToString(index_program_id_) + ":" +
base::UintToString(audio_pes_stream_id ?
@ -813,16 +811,14 @@ void WvmMediaParser::StartMediaSampleDemux() {
bool WvmMediaParser::Output(bool output_encrypted_sample) {
if (output_encrypted_sample) {
media_sample_->set_data(vector_as_array(&sample_data_),
sample_data_.size());
media_sample_->set_data(sample_data_.data(), sample_data_.size());
media_sample_->set_is_encrypted(true);
} else {
if ((prev_pes_stream_id_ & kPesStreamIdVideoMask) == kPesStreamIdVideo) {
// Convert video stream to unit stream and get config.
std::vector<uint8_t> nal_unit_stream;
if (!byte_to_unit_stream_converter_.ConvertByteStreamToNalUnitStream(
vector_as_array(&sample_data_), sample_data_.size(),
&nal_unit_stream)) {
sample_data_.data(), sample_data_.size(), &nal_unit_stream)) {
LOG(ERROR) << "Could not convert h.264 byte stream sample";
return false;
}
@ -898,9 +894,9 @@ bool WvmMediaParser::Output(bool output_encrypted_sample) {
kPesStreamIdAudio) {
// Set data on the audio stream.
int frame_size = media::mp2t::AdtsHeader::GetAdtsFrameSize(
vector_as_array(&sample_data_), kAdtsHeaderMinSize);
sample_data_.data(), kAdtsHeaderMinSize);
media::mp2t::AdtsHeader adts_header;
const uint8_t* frame_ptr = vector_as_array(&sample_data_);
const uint8_t* frame_ptr = sample_data_.data();
if (!adts_header.Parse(frame_ptr, frame_size)) {
LOG(ERROR) << "Could not parse ADTS header";
return false;
@ -1128,8 +1124,8 @@ bool WvmMediaParser::ProcessEcm() {
kEcmFlagsSizeBytes + kEcmContentKeySizeBytes +
kEcmPaddingSizeBytes; // flags + contentKey + padding.
std::vector<uint8_t> content_key_buffer(content_key_buffer_size);
asset_decryptor.Decrypt(
ecm_data, content_key_buffer_size, vector_as_array(&content_key_buffer));
asset_decryptor.Decrypt(ecm_data, content_key_buffer_size,
content_key_buffer.data());
std::vector<uint8_t> decrypted_content_key_vec(
content_key_buffer.begin() + 4,