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 /////////////////////////////////////////////////////////////////////////////// | |
296 // NullStream | 241 // NullStream |
297 /////////////////////////////////////////////////////////////////////////////// | 242 /////////////////////////////////////////////////////////////////////////////// |
298 | 243 |
299 NullStream::NullStream() { | 244 NullStream::NullStream() { |
300 } | 245 } |
301 | 246 |
302 NullStream::~NullStream() { | 247 NullStream::~NullStream() { |
303 } | 248 } |
304 | 249 |
305 StreamState NullStream::GetState() const { | 250 StreamState NullStream::GetState() const { |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 return SR_SUCCESS; | 600 return SR_SUCCESS; |
656 } | 601 } |
657 | 602 |
658 if (error) { | 603 if (error) { |
659 *error = ENOMEM; | 604 *error = ENOMEM; |
660 } | 605 } |
661 return SR_ERROR; | 606 return SR_ERROR; |
662 } | 607 } |
663 | 608 |
664 /////////////////////////////////////////////////////////////////////////////// | 609 /////////////////////////////////////////////////////////////////////////////// |
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 /////////////////////////////////////////////////////////////////////////////// | |
683 // FifoBuffer | 610 // FifoBuffer |
684 /////////////////////////////////////////////////////////////////////////////// | 611 /////////////////////////////////////////////////////////////////////////////// |
685 | 612 |
686 FifoBuffer::FifoBuffer(size_t size) | 613 FifoBuffer::FifoBuffer(size_t size) |
687 : state_(SS_OPEN), buffer_(new char[size]), buffer_length_(size), | 614 : state_(SS_OPEN), buffer_(new char[size]), buffer_length_(size), |
688 data_length_(0), read_position_(0), owner_(Thread::Current()) { | 615 data_length_(0), read_position_(0), owner_(Thread::Current()) { |
689 // all events are done on the owner_ thread | 616 // all events are done on the owner_ thread |
690 } | 617 } |
691 | 618 |
692 FifoBuffer::FifoBuffer(size_t size, Thread* owner) | 619 FifoBuffer::FifoBuffer(size_t size, Thread* owner) |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 const char* const p = static_cast<const char*>(buffer); | 814 const char* const p = static_cast<const char*>(buffer); |
888 memcpy(&buffer_[write_position], p, tail_copy); | 815 memcpy(&buffer_[write_position], p, tail_copy); |
889 memcpy(&buffer_[0], p + tail_copy, copy - tail_copy); | 816 memcpy(&buffer_[0], p + tail_copy, copy - tail_copy); |
890 | 817 |
891 if (bytes_written) { | 818 if (bytes_written) { |
892 *bytes_written = copy; | 819 *bytes_written = copy; |
893 } | 820 } |
894 return SR_SUCCESS; | 821 return SR_SUCCESS; |
895 } | 822 } |
896 | 823 |
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 | |
957 /////////////////////////////////////////////////////////////////////////////// | 824 /////////////////////////////////////////////////////////////////////////////// |
958 // StringStream - Reads/Writes to an external std::string | 825 // StringStream - Reads/Writes to an external std::string |
959 /////////////////////////////////////////////////////////////////////////////// | 826 /////////////////////////////////////////////////////////////////////////////// |
960 | 827 |
961 StringStream::StringStream(std::string* str) | 828 StringStream::StringStream(std::string* str) |
962 : str_(*str), read_pos_(0), read_only_(false) { | 829 : str_(*str), read_pos_(0), read_only_(false) { |
963 } | 830 } |
964 | 831 |
965 StringStream::StringStream(const std::string& str) | 832 StringStream::StringStream(const std::string& str) |
966 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) { | 833 : 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... |
1026 } | 893 } |
1027 | 894 |
1028 bool StringStream::ReserveSize(size_t size) { | 895 bool StringStream::ReserveSize(size_t size) { |
1029 if (read_only_) | 896 if (read_only_) |
1030 return false; | 897 return false; |
1031 str_.reserve(size); | 898 str_.reserve(size); |
1032 return true; | 899 return true; |
1033 } | 900 } |
1034 | 901 |
1035 /////////////////////////////////////////////////////////////////////////////// | 902 /////////////////////////////////////////////////////////////////////////////// |
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 /////////////////////////////////////////////////////////////////////////////// | |
1061 | 903 |
1062 StreamResult Flow(StreamInterface* source, | 904 StreamResult Flow(StreamInterface* source, |
1063 char* buffer, | 905 char* buffer, |
1064 size_t buffer_len, | 906 size_t buffer_len, |
1065 StreamInterface* sink, | 907 StreamInterface* sink, |
1066 size_t* data_len /* = nullptr */) { | 908 size_t* data_len /* = nullptr */) { |
1067 RTC_DCHECK(buffer_len > 0); | 909 RTC_DCHECK(buffer_len > 0); |
1068 | 910 |
1069 StreamResult result; | 911 StreamResult result; |
1070 size_t count, read_pos, write_pos; | 912 size_t count, read_pos, write_pos; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1114 | 956 |
1115 if (data_len) { | 957 if (data_len) { |
1116 *data_len = 0; | 958 *data_len = 0; |
1117 } | 959 } |
1118 return SR_SUCCESS; | 960 return SR_SUCCESS; |
1119 } | 961 } |
1120 | 962 |
1121 /////////////////////////////////////////////////////////////////////////////// | 963 /////////////////////////////////////////////////////////////////////////////// |
1122 | 964 |
1123 } // namespace rtc | 965 } // namespace rtc |
OLD | NEW |