blob: b4a7433fbd7f183050b4626f558d7b32054c877f [file] [log] [blame]
Steve Anton9de3aac2017-10-24 10:08:26 -07001/*
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "rtc_base/testutils.h"
12
13namespace webrtc {
14namespace testing {
15
16StreamSink::StreamSink() = default;
17
18StreamSink::~StreamSink() = default;
19
20StreamSource::StreamSource() {
21 Clear();
22}
23
24StreamSource::~StreamSource() = default;
25
26StreamState StreamSource::GetState() const {
27 return state_;
28}
29
30StreamResult StreamSource::Read(void* buffer,
31 size_t buffer_len,
32 size_t* read,
33 int* error) {
34 if (SS_CLOSED == state_) {
35 if (error)
36 *error = -1;
37 return SR_ERROR;
38 }
39 if ((SS_OPENING == state_) || (readable_data_.size() <= read_block_)) {
40 return SR_BLOCK;
41 }
42 size_t count = std::min(buffer_len, readable_data_.size() - read_block_);
43 memcpy(buffer, &readable_data_[0], count);
44 size_t new_size = readable_data_.size() - count;
45 // Avoid undefined access beyond the last element of the vector.
46 // This only happens when new_size is 0.
47 if (count < readable_data_.size()) {
48 memmove(&readable_data_[0], &readable_data_[count], new_size);
49 }
50 readable_data_.resize(new_size);
51 if (read)
52 *read = count;
53 return SR_SUCCESS;
54}
55
56StreamResult StreamSource::Write(const void* data,
57 size_t data_len,
58 size_t* written,
59 int* error) {
60 if (SS_CLOSED == state_) {
61 if (error)
62 *error = -1;
63 return SR_ERROR;
64 }
65 if (SS_OPENING == state_) {
66 return SR_BLOCK;
67 }
68 if (SIZE_UNKNOWN != write_block_) {
69 if (written_data_.size() >= write_block_) {
70 return SR_BLOCK;
71 }
72 if (data_len > (write_block_ - written_data_.size())) {
73 data_len = write_block_ - written_data_.size();
74 }
75 }
76 if (written)
77 *written = data_len;
78 const char* cdata = static_cast<const char*>(data);
79 written_data_.insert(written_data_.end(), cdata, cdata + data_len);
80 return SR_SUCCESS;
81}
82
83void StreamSource::Close() {
84 state_ = SS_CLOSED;
85}
86
Steve Anton9de3aac2017-10-24 10:08:26 -070087} // namespace testing
88} // namespace webrtc