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

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: Added CONTENT_EXPORT to fix compilation issues on win bots. 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 Chunk()),
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.get();
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 Chunk());
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 scoped_ptr< std::vector<uint8> > ChunkedByteBuffer::PopChunk() {
102 if (chunks_.empty())
103 return scoped_ptr< std::vector<uint8> >();
104 scoped_ptr<Chunk> chunk(*chunks_.begin());
105 chunks_.weak_erase(chunks_.begin());
106 DCHECK_EQ(chunk->header.size(), kHeaderLength);
107 DCHECK_EQ(chunk->content->size(), chunk->ExpectedContentLength());
108 total_bytes_stored_ -= chunk->content->size();
109 total_bytes_stored_ -= kHeaderLength;
110 return chunk->content.Pass();
111 }
112
113 void ChunkedByteBuffer::Clear() {
114 chunks_.reset();
115 partial_chunk_.reset(new Chunk());
116 total_bytes_stored_ = 0;
117 }
118
119 ChunkedByteBuffer::Chunk::Chunk()
120 : content(new std::vector<uint8>()) {
121 }
122
123 ChunkedByteBuffer::Chunk::~Chunk() {
124 }
125
126 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const {
127 DCHECK_EQ(header.size(), kHeaderLength);
128 return static_cast<size_t>(ReadBigEndian32(&header[0]));
129 }
130
131 } // namespace speech
OLDNEW
« no previous file with comments | « content/browser/speech/chunked_byte_buffer.h ('k') | content/browser/speech/chunked_byte_buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698