Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: content/browser/speech/chunked_byte_buffer.cc

Issue 10123008: Introduced ChunkedByteBuffer class which will be needed by next speech recognition CLs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactored according to Satish indications. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "content/browser/speech/chunked_byte_buffer.h"
6
7 #include <algorithm>
8
9 #include "base/basictypes.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12
13 namespace {
14
15 static const size_t kHeaderLength = sizeof(uint32);
16
17 COMPILE_ASSERT(sizeof(size_t) >= kHeaderLength,
18 ChunkedByteBufferNotSupportedOnThisArchitecture);
19
20 uint32 ReadBigEndian32(const uint8* buffer) {
21 return (static_cast<uint32>(buffer[3])) |
22 (static_cast<uint32>(buffer[2]) << 8) |
23 (static_cast<uint32>(buffer[1]) << 16) |
24 (static_cast<uint32>(buffer[0]) << 24);
25 }
26
27 } // namespace
28
29 namespace speech {
30
31 ChunkedByteBuffer::ChunkedByteBuffer()
32 : partial_chunk_(new ByteBufferChunk()),
33 total_bytes_stored_(0) {
34 }
35
36 ChunkedByteBuffer::~ChunkedByteBuffer() {
37 Clear();
38 }
39
40 void ChunkedByteBuffer::Append(const uint8* start, size_t length) {
41 DCHECK(length > 0);
42 size_t remaining_bytes = length;
43 const uint8* next_data = start;
44
45 while (remaining_bytes > 0) {
46 DCHECK(partial_chunk_ != NULL);
47 size_t insert_length = 0;
48 bool header_completed = false;
49 bool content_completed = false;
50 std::vector<uint8>* insert_target;
51
52 if (partial_chunk_->header.size() < kHeaderLength) {
53 const size_t bytes_to_complete_header =
54 kHeaderLength - partial_chunk_->header.size();
55 insert_length = std::min(bytes_to_complete_header, remaining_bytes);
56 insert_target = &partial_chunk_->header;
57 header_completed = (remaining_bytes >= bytes_to_complete_header);
58 } else {
59 DCHECK_LT(partial_chunk_->content.size(),
60 partial_chunk_->ExpectedContentLength());
61 const size_t bytes_to_complete_chunk =
62 partial_chunk_->ExpectedContentLength() -
63 partial_chunk_->content.size();
64 insert_length = std::min(bytes_to_complete_chunk, remaining_bytes);
65 insert_target = &partial_chunk_->content;
66 content_completed = (remaining_bytes >= bytes_to_complete_chunk);
67 }
68
69 DCHECK_GT(insert_length, 0U);
70 DCHECK_LE(insert_length, remaining_bytes);
71 DCHECK_LE(next_data + insert_length, start + length);
72 insert_target->insert(insert_target->end(),
73 next_data,
74 next_data + insert_length);
75 next_data += insert_length;
76 remaining_bytes -= insert_length;
77
78 if (header_completed) {
79 DCHECK_EQ(partial_chunk_->header.size(), kHeaderLength);
80 DCHECK_NE(partial_chunk_->ExpectedContentLength(), 0U);
81 partial_chunk_->content.reserve(partial_chunk_->ExpectedContentLength());
82 } else if (content_completed) {
83 DCHECK_EQ(partial_chunk_->content.size(),
84 partial_chunk_->ExpectedContentLength());
85 chunks_.push_back(partial_chunk_.release());
86 partial_chunk_.reset(new ByteBufferChunk());
87 }
88 }
89 DCHECK_EQ(next_data, start + length);
90 total_bytes_stored_ += length;
91 }
92
93 void ChunkedByteBuffer::Append(const std::string& string) {
94 Append(reinterpret_cast<const uint8*>(string.data()), string.size());
95 }
96
97 bool ChunkedByteBuffer::HasChunks() const {
98 return !chunks_.empty();
99 }
100
101 const ByteBufferChunk& ChunkedByteBuffer::GetFirstChunk() const {
102 CR_DEFINE_STATIC_LOCAL(ByteBufferChunk, kEmptyChunk, ());
103 if (chunks_.empty())
104 return kEmptyChunk;
105 return **chunks_.begin();
106 }
107
108 scoped_ptr<ByteBufferChunk> ChunkedByteBuffer::PopChunk() {
109 if (chunks_.empty())
110 return scoped_ptr<ByteBufferChunk>();
111 scoped_ptr<ByteBufferChunk> chunk(*chunks_.begin());
112 chunks_.weak_erase(chunks_.begin());
113 DCHECK_EQ(chunk->header.size(), kHeaderLength);
114 DCHECK_EQ(chunk->content.size(), chunk->ExpectedContentLength());
115 total_bytes_stored_ -= chunk->GetLength();
116 total_bytes_stored_ -= kHeaderLength;
117 return chunk.Pass();
118 }
119
120 void ChunkedByteBuffer::Clear() {
121 chunks_.reset();
122 partial_chunk_.reset(new ByteBufferChunk());
123 total_bytes_stored_ = 0;
124 }
125
126 ByteBufferChunk::ByteBufferChunk() {
127 }
128
129 ByteBufferChunk::~ByteBufferChunk() {
130 }
131
132 const uint8* ByteBufferChunk::GetData() const {
133 return &content[0];
134 }
135
136 size_t ByteBufferChunk::GetLength() const {
137 return content.size();
138 }
139
140 size_t ByteBufferChunk::ExpectedContentLength() const {
141 DCHECK_EQ(header.size(), kHeaderLength);
142 return static_cast<size_t>(ReadBigEndian32(&header[0]));
143 }
144
145 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698