| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 delete stream_; | 231 delete stream_; |
| 232 } | 232 } |
| 233 | 233 |
| 234 void StreamAdapterInterface::OnEvent(StreamInterface* stream, | 234 void StreamAdapterInterface::OnEvent(StreamInterface* stream, |
| 235 int events, | 235 int events, |
| 236 int err) { | 236 int err) { |
| 237 SignalEvent(this, events, err); | 237 SignalEvent(this, events, err); |
| 238 } | 238 } |
| 239 | 239 |
| 240 /////////////////////////////////////////////////////////////////////////////// | 240 /////////////////////////////////////////////////////////////////////////////// |
| 241 // StreamTap |
| 242 /////////////////////////////////////////////////////////////////////////////// |
| 243 |
| 244 StreamTap::StreamTap(StreamInterface* stream, StreamInterface* tap) |
| 245 : StreamAdapterInterface(stream), tap_(), tap_result_(SR_SUCCESS), |
| 246 tap_error_(0) { |
| 247 AttachTap(tap); |
| 248 } |
| 249 |
| 250 StreamTap::~StreamTap() = default; |
| 251 |
| 252 void StreamTap::AttachTap(StreamInterface* tap) { |
| 253 tap_.reset(tap); |
| 254 } |
| 255 |
| 256 StreamInterface* StreamTap::DetachTap() { |
| 257 return tap_.release(); |
| 258 } |
| 259 |
| 260 StreamResult StreamTap::GetTapResult(int* error) { |
| 261 if (error) { |
| 262 *error = tap_error_; |
| 263 } |
| 264 return tap_result_; |
| 265 } |
| 266 |
| 267 StreamResult StreamTap::Read(void* buffer, size_t buffer_len, |
| 268 size_t* read, int* error) { |
| 269 size_t backup_read; |
| 270 if (!read) { |
| 271 read = &backup_read; |
| 272 } |
| 273 StreamResult res = StreamAdapterInterface::Read(buffer, buffer_len, |
| 274 read, error); |
| 275 if ((res == SR_SUCCESS) && (tap_result_ == SR_SUCCESS)) { |
| 276 tap_result_ = tap_->WriteAll(buffer, *read, nullptr, &tap_error_); |
| 277 } |
| 278 return res; |
| 279 } |
| 280 |
| 281 StreamResult StreamTap::Write(const void* data, size_t data_len, |
| 282 size_t* written, int* error) { |
| 283 size_t backup_written; |
| 284 if (!written) { |
| 285 written = &backup_written; |
| 286 } |
| 287 StreamResult res = StreamAdapterInterface::Write(data, data_len, |
| 288 written, error); |
| 289 if ((res == SR_SUCCESS) && (tap_result_ == SR_SUCCESS)) { |
| 290 tap_result_ = tap_->WriteAll(data, *written, nullptr, &tap_error_); |
| 291 } |
| 292 return res; |
| 293 } |
| 294 |
| 295 /////////////////////////////////////////////////////////////////////////////// |
| 241 // NullStream | 296 // NullStream |
| 242 /////////////////////////////////////////////////////////////////////////////// | 297 /////////////////////////////////////////////////////////////////////////////// |
| 243 | 298 |
| 244 NullStream::NullStream() { | 299 NullStream::NullStream() { |
| 245 } | 300 } |
| 246 | 301 |
| 247 NullStream::~NullStream() { | 302 NullStream::~NullStream() { |
| 248 } | 303 } |
| 249 | 304 |
| 250 StreamState NullStream::GetState() const { | 305 StreamState NullStream::GetState() const { |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 return SR_SUCCESS; | 655 return SR_SUCCESS; |
| 601 } | 656 } |
| 602 | 657 |
| 603 if (error) { | 658 if (error) { |
| 604 *error = ENOMEM; | 659 *error = ENOMEM; |
| 605 } | 660 } |
| 606 return SR_ERROR; | 661 return SR_ERROR; |
| 607 } | 662 } |
| 608 | 663 |
| 609 /////////////////////////////////////////////////////////////////////////////// | 664 /////////////////////////////////////////////////////////////////////////////// |
| 665 |
| 666 ExternalMemoryStream::ExternalMemoryStream() { |
| 667 } |
| 668 |
| 669 ExternalMemoryStream::ExternalMemoryStream(void* data, size_t length) { |
| 670 SetData(data, length); |
| 671 } |
| 672 |
| 673 ExternalMemoryStream::~ExternalMemoryStream() { |
| 674 } |
| 675 |
| 676 void ExternalMemoryStream::SetData(void* data, size_t length) { |
| 677 data_length_ = buffer_length_ = length; |
| 678 buffer_ = static_cast<char*>(data); |
| 679 seek_position_ = 0; |
| 680 } |
| 681 |
| 682 /////////////////////////////////////////////////////////////////////////////// |
| 610 // FifoBuffer | 683 // FifoBuffer |
| 611 /////////////////////////////////////////////////////////////////////////////// | 684 /////////////////////////////////////////////////////////////////////////////// |
| 612 | 685 |
| 613 FifoBuffer::FifoBuffer(size_t size) | 686 FifoBuffer::FifoBuffer(size_t size) |
| 614 : state_(SS_OPEN), buffer_(new char[size]), buffer_length_(size), | 687 : state_(SS_OPEN), buffer_(new char[size]), buffer_length_(size), |
| 615 data_length_(0), read_position_(0), owner_(Thread::Current()) { | 688 data_length_(0), read_position_(0), owner_(Thread::Current()) { |
| 616 // all events are done on the owner_ thread | 689 // all events are done on the owner_ thread |
| 617 } | 690 } |
| 618 | 691 |
| 619 FifoBuffer::FifoBuffer(size_t size, Thread* owner) | 692 FifoBuffer::FifoBuffer(size_t size, Thread* owner) |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 const char* const p = static_cast<const char*>(buffer); | 887 const char* const p = static_cast<const char*>(buffer); |
| 815 memcpy(&buffer_[write_position], p, tail_copy); | 888 memcpy(&buffer_[write_position], p, tail_copy); |
| 816 memcpy(&buffer_[0], p + tail_copy, copy - tail_copy); | 889 memcpy(&buffer_[0], p + tail_copy, copy - tail_copy); |
| 817 | 890 |
| 818 if (bytes_written) { | 891 if (bytes_written) { |
| 819 *bytes_written = copy; | 892 *bytes_written = copy; |
| 820 } | 893 } |
| 821 return SR_SUCCESS; | 894 return SR_SUCCESS; |
| 822 } | 895 } |
| 823 | 896 |
| 897 |
| 898 |
| 899 /////////////////////////////////////////////////////////////////////////////// |
| 900 // LoggingAdapter |
| 901 /////////////////////////////////////////////////////////////////////////////// |
| 902 |
| 903 LoggingAdapter::LoggingAdapter(StreamInterface* stream, LoggingSeverity level, |
| 904 const std::string& label, bool hex_mode) |
| 905 : StreamAdapterInterface(stream), level_(level), hex_mode_(hex_mode) { |
| 906 set_label(label); |
| 907 } |
| 908 |
| 909 void LoggingAdapter::set_label(const std::string& label) { |
| 910 label_.assign("["); |
| 911 label_.append(label); |
| 912 label_.append("]"); |
| 913 } |
| 914 |
| 915 StreamResult LoggingAdapter::Read(void* buffer, size_t buffer_len, |
| 916 size_t* read, int* error) { |
| 917 size_t local_read; if (!read) read = &local_read; |
| 918 StreamResult result = StreamAdapterInterface::Read(buffer, buffer_len, read, |
| 919 error); |
| 920 if (result == SR_SUCCESS) { |
| 921 LogMultiline(level_, label_.c_str(), true, buffer, *read, hex_mode_, &lms_); |
| 922 } |
| 923 return result; |
| 924 } |
| 925 |
| 926 StreamResult LoggingAdapter::Write(const void* data, size_t data_len, |
| 927 size_t* written, int* error) { |
| 928 size_t local_written; |
| 929 if (!written) written = &local_written; |
| 930 StreamResult result = StreamAdapterInterface::Write(data, data_len, written, |
| 931 error); |
| 932 if (result == SR_SUCCESS) { |
| 933 LogMultiline(level_, label_.c_str(), false, data, *written, hex_mode_, |
| 934 &lms_); |
| 935 } |
| 936 return result; |
| 937 } |
| 938 |
| 939 void LoggingAdapter::Close() { |
| 940 LogMultiline(level_, label_.c_str(), false, nullptr, 0, hex_mode_, &lms_); |
| 941 LogMultiline(level_, label_.c_str(), true, nullptr, 0, hex_mode_, &lms_); |
| 942 LOG_V(level_) << label_ << " Closed locally"; |
| 943 StreamAdapterInterface::Close(); |
| 944 } |
| 945 |
| 946 void LoggingAdapter::OnEvent(StreamInterface* stream, int events, int err) { |
| 947 if (events & SE_OPEN) { |
| 948 LOG_V(level_) << label_ << " Open"; |
| 949 } else if (events & SE_CLOSE) { |
| 950 LogMultiline(level_, label_.c_str(), false, nullptr, 0, hex_mode_, &lms_); |
| 951 LogMultiline(level_, label_.c_str(), true, nullptr, 0, hex_mode_, &lms_); |
| 952 LOG_V(level_) << label_ << " Closed with error: " << err; |
| 953 } |
| 954 StreamAdapterInterface::OnEvent(stream, events, err); |
| 955 } |
| 956 |
| 824 /////////////////////////////////////////////////////////////////////////////// | 957 /////////////////////////////////////////////////////////////////////////////// |
| 825 // StringStream - Reads/Writes to an external std::string | 958 // StringStream - Reads/Writes to an external std::string |
| 826 /////////////////////////////////////////////////////////////////////////////// | 959 /////////////////////////////////////////////////////////////////////////////// |
| 827 | 960 |
| 828 StringStream::StringStream(std::string* str) | 961 StringStream::StringStream(std::string* str) |
| 829 : str_(*str), read_pos_(0), read_only_(false) { | 962 : str_(*str), read_pos_(0), read_only_(false) { |
| 830 } | 963 } |
| 831 | 964 |
| 832 StringStream::StringStream(const std::string& str) | 965 StringStream::StringStream(const std::string& str) |
| 833 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) { | 966 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 } | 1026 } |
| 894 | 1027 |
| 895 bool StringStream::ReserveSize(size_t size) { | 1028 bool StringStream::ReserveSize(size_t size) { |
| 896 if (read_only_) | 1029 if (read_only_) |
| 897 return false; | 1030 return false; |
| 898 str_.reserve(size); | 1031 str_.reserve(size); |
| 899 return true; | 1032 return true; |
| 900 } | 1033 } |
| 901 | 1034 |
| 902 /////////////////////////////////////////////////////////////////////////////// | 1035 /////////////////////////////////////////////////////////////////////////////// |
| 1036 // StreamReference |
| 1037 /////////////////////////////////////////////////////////////////////////////// |
| 1038 |
| 1039 StreamReference::StreamReference(StreamInterface* stream) |
| 1040 : StreamAdapterInterface(stream, false) { |
| 1041 // owner set to false so the destructor does not free the stream. |
| 1042 stream_ref_count_ = new StreamRefCount(stream); |
| 1043 } |
| 1044 |
| 1045 StreamInterface* StreamReference::NewReference() { |
| 1046 stream_ref_count_->AddReference(); |
| 1047 return new StreamReference(stream_ref_count_, stream()); |
| 1048 } |
| 1049 |
| 1050 StreamReference::~StreamReference() { |
| 1051 stream_ref_count_->Release(); |
| 1052 } |
| 1053 |
| 1054 StreamReference::StreamReference(StreamRefCount* stream_ref_count, |
| 1055 StreamInterface* stream) |
| 1056 : StreamAdapterInterface(stream, false), |
| 1057 stream_ref_count_(stream_ref_count) { |
| 1058 } |
| 1059 |
| 1060 /////////////////////////////////////////////////////////////////////////////// |
| 903 | 1061 |
| 904 StreamResult Flow(StreamInterface* source, | 1062 StreamResult Flow(StreamInterface* source, |
| 905 char* buffer, | 1063 char* buffer, |
| 906 size_t buffer_len, | 1064 size_t buffer_len, |
| 907 StreamInterface* sink, | 1065 StreamInterface* sink, |
| 908 size_t* data_len /* = nullptr */) { | 1066 size_t* data_len /* = nullptr */) { |
| 909 RTC_DCHECK(buffer_len > 0); | 1067 RTC_DCHECK(buffer_len > 0); |
| 910 | 1068 |
| 911 StreamResult result; | 1069 StreamResult result; |
| 912 size_t count, read_pos, write_pos; | 1070 size_t count, read_pos, write_pos; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 | 1114 |
| 957 if (data_len) { | 1115 if (data_len) { |
| 958 *data_len = 0; | 1116 *data_len = 0; |
| 959 } | 1117 } |
| 960 return SR_SUCCESS; | 1118 return SR_SUCCESS; |
| 961 } | 1119 } |
| 962 | 1120 |
| 963 /////////////////////////////////////////////////////////////////////////////// | 1121 /////////////////////////////////////////////////////////////////////////////// |
| 964 | 1122 |
| 965 } // namespace rtc | 1123 } // namespace rtc |
| OLD | NEW |