| 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 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 virtual void OnEvent(StreamInterface* stream, int events, int err); | 303 virtual void OnEvent(StreamInterface* stream, int events, int err); |
| 304 StreamInterface* stream() { return stream_; } | 304 StreamInterface* stream() { return stream_; } |
| 305 | 305 |
| 306 private: | 306 private: |
| 307 StreamInterface* stream_; | 307 StreamInterface* stream_; |
| 308 bool owned_; | 308 bool owned_; |
| 309 RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface); | 309 RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface); |
| 310 }; | 310 }; |
| 311 | 311 |
| 312 /////////////////////////////////////////////////////////////////////////////// | 312 /////////////////////////////////////////////////////////////////////////////// |
| 313 // StreamTap is a non-modifying, pass-through adapter, which copies all data | |
| 314 // in either direction to the tap. Note that errors or blocking on writing to | |
| 315 // the tap will prevent further tap writes from occurring. | |
| 316 /////////////////////////////////////////////////////////////////////////////// | |
| 317 | |
| 318 class StreamTap : public StreamAdapterInterface { | |
| 319 public: | |
| 320 explicit StreamTap(StreamInterface* stream, StreamInterface* tap); | |
| 321 ~StreamTap() override; | |
| 322 | |
| 323 void AttachTap(StreamInterface* tap); | |
| 324 StreamInterface* DetachTap(); | |
| 325 StreamResult GetTapResult(int* error); | |
| 326 | |
| 327 // StreamAdapterInterface Interface | |
| 328 StreamResult Read(void* buffer, | |
| 329 size_t buffer_len, | |
| 330 size_t* read, | |
| 331 int* error) override; | |
| 332 StreamResult Write(const void* data, | |
| 333 size_t data_len, | |
| 334 size_t* written, | |
| 335 int* error) override; | |
| 336 | |
| 337 private: | |
| 338 std::unique_ptr<StreamInterface> tap_; | |
| 339 StreamResult tap_result_; | |
| 340 int tap_error_; | |
| 341 RTC_DISALLOW_COPY_AND_ASSIGN(StreamTap); | |
| 342 }; | |
| 343 | |
| 344 /////////////////////////////////////////////////////////////////////////////// | |
| 345 // NullStream gives errors on read, and silently discards all written data. | 313 // NullStream gives errors on read, and silently discards all written data. |
| 346 /////////////////////////////////////////////////////////////////////////////// | 314 /////////////////////////////////////////////////////////////////////////////// |
| 347 | 315 |
| 348 class NullStream : public StreamInterface { | 316 class NullStream : public StreamInterface { |
| 349 public: | 317 public: |
| 350 NullStream(); | 318 NullStream(); |
| 351 ~NullStream() override; | 319 ~NullStream() override; |
| 352 | 320 |
| 353 // StreamInterface Interface | 321 // StreamInterface Interface |
| 354 StreamState GetState() const override; | 322 StreamState GetState() const override; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 | 441 |
| 474 void SetData(const void* data, size_t length); | 442 void SetData(const void* data, size_t length); |
| 475 | 443 |
| 476 protected: | 444 protected: |
| 477 StreamResult DoReserve(size_t size, int* error) override; | 445 StreamResult DoReserve(size_t size, int* error) override; |
| 478 // Memory Streams are aligned for efficiency. | 446 // Memory Streams are aligned for efficiency. |
| 479 static const int kAlignment = 16; | 447 static const int kAlignment = 16; |
| 480 char* buffer_alloc_; | 448 char* buffer_alloc_; |
| 481 }; | 449 }; |
| 482 | 450 |
| 483 // ExternalMemoryStream adapts an external memory buffer, so writes which would | |
| 484 // extend past the end of the buffer will return end-of-stream. | |
| 485 | |
| 486 class ExternalMemoryStream : public MemoryStreamBase { | |
| 487 public: | |
| 488 ExternalMemoryStream(); | |
| 489 ExternalMemoryStream(void* data, size_t length); | |
| 490 ~ExternalMemoryStream() override; | |
| 491 | |
| 492 void SetData(void* data, size_t length); | |
| 493 }; | |
| 494 | |
| 495 // FifoBuffer allows for efficient, thread-safe buffering of data between | 451 // FifoBuffer allows for efficient, thread-safe buffering of data between |
| 496 // writer and reader. As the data can wrap around the end of the buffer, | 452 // writer and reader. As the data can wrap around the end of the buffer, |
| 497 // MemoryStreamBase can't help us here. | 453 // MemoryStreamBase can't help us here. |
| 498 | 454 |
| 499 class FifoBuffer : public StreamInterface { | 455 class FifoBuffer : public StreamInterface { |
| 500 public: | 456 public: |
| 501 // Creates a FIFO buffer with the specified capacity. | 457 // Creates a FIFO buffer with the specified capacity. |
| 502 explicit FifoBuffer(size_t length); | 458 explicit FifoBuffer(size_t length); |
| 503 // Creates a FIFO buffer with the specified capacity and owner | 459 // Creates a FIFO buffer with the specified capacity and owner |
| 504 FifoBuffer(size_t length, Thread* owner); | 460 FifoBuffer(size_t length, Thread* owner); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 // offset to the readable data | 519 // offset to the readable data |
| 564 size_t read_position_ GUARDED_BY(crit_); | 520 size_t read_position_ GUARDED_BY(crit_); |
| 565 // stream callbacks are dispatched on this thread | 521 // stream callbacks are dispatched on this thread |
| 566 Thread* owner_; | 522 Thread* owner_; |
| 567 // object lock | 523 // object lock |
| 568 CriticalSection crit_; | 524 CriticalSection crit_; |
| 569 RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer); | 525 RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer); |
| 570 }; | 526 }; |
| 571 | 527 |
| 572 /////////////////////////////////////////////////////////////////////////////// | 528 /////////////////////////////////////////////////////////////////////////////// |
| 573 | |
| 574 class LoggingAdapter : public StreamAdapterInterface { | |
| 575 public: | |
| 576 LoggingAdapter(StreamInterface* stream, LoggingSeverity level, | |
| 577 const std::string& label, bool hex_mode = false); | |
| 578 | |
| 579 void set_label(const std::string& label); | |
| 580 | |
| 581 StreamResult Read(void* buffer, | |
| 582 size_t buffer_len, | |
| 583 size_t* read, | |
| 584 int* error) override; | |
| 585 StreamResult Write(const void* data, | |
| 586 size_t data_len, | |
| 587 size_t* written, | |
| 588 int* error) override; | |
| 589 void Close() override; | |
| 590 | |
| 591 protected: | |
| 592 void OnEvent(StreamInterface* stream, int events, int err) override; | |
| 593 | |
| 594 private: | |
| 595 LoggingSeverity level_; | |
| 596 std::string label_; | |
| 597 bool hex_mode_; | |
| 598 LogMultilineState lms_; | |
| 599 | |
| 600 RTC_DISALLOW_COPY_AND_ASSIGN(LoggingAdapter); | |
| 601 }; | |
| 602 | |
| 603 /////////////////////////////////////////////////////////////////////////////// | |
| 604 // StringStream - Reads/Writes to an external std::string | 529 // StringStream - Reads/Writes to an external std::string |
| 605 /////////////////////////////////////////////////////////////////////////////// | 530 /////////////////////////////////////////////////////////////////////////////// |
| 606 | 531 |
| 607 class StringStream : public StreamInterface { | 532 class StringStream : public StreamInterface { |
| 608 public: | 533 public: |
| 609 explicit StringStream(std::string* str); | 534 explicit StringStream(std::string* str); |
| 610 explicit StringStream(const std::string& str); | 535 explicit StringStream(const std::string& str); |
| 611 | 536 |
| 612 StreamState GetState() const override; | 537 StreamState GetState() const override; |
| 613 StreamResult Read(void* buffer, | 538 StreamResult Read(void* buffer, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 625 bool GetAvailable(size_t* size) const override; | 550 bool GetAvailable(size_t* size) const override; |
| 626 bool ReserveSize(size_t size) override; | 551 bool ReserveSize(size_t size) override; |
| 627 | 552 |
| 628 private: | 553 private: |
| 629 std::string& str_; | 554 std::string& str_; |
| 630 size_t read_pos_; | 555 size_t read_pos_; |
| 631 bool read_only_; | 556 bool read_only_; |
| 632 }; | 557 }; |
| 633 | 558 |
| 634 /////////////////////////////////////////////////////////////////////////////// | 559 /////////////////////////////////////////////////////////////////////////////// |
| 635 // StreamReference - A reference counting stream adapter | |
| 636 /////////////////////////////////////////////////////////////////////////////// | |
| 637 | |
| 638 // Keep in mind that the streams and adapters defined in this file are | |
| 639 // not thread-safe, so this has limited uses. | |
| 640 | |
| 641 // A StreamRefCount holds the reference count and a pointer to the | |
| 642 // wrapped stream. It deletes the wrapped stream when there are no | |
| 643 // more references. We can then have multiple StreamReference | |
| 644 // instances pointing to one StreamRefCount, all wrapping the same | |
| 645 // stream. | |
| 646 | |
| 647 class StreamReference : public StreamAdapterInterface { | |
| 648 class StreamRefCount; | |
| 649 public: | |
| 650 // Constructor for the first reference to a stream | |
| 651 // Note: get more references through NewReference(). Use this | |
| 652 // constructor only once on a given stream. | |
| 653 explicit StreamReference(StreamInterface* stream); | |
| 654 StreamInterface* GetStream() { return stream(); } | |
| 655 StreamInterface* NewReference(); | |
| 656 ~StreamReference() override; | |
| 657 | |
| 658 private: | |
| 659 class StreamRefCount { | |
| 660 public: | |
| 661 explicit StreamRefCount(StreamInterface* stream) | |
| 662 : stream_(stream), ref_count_(1) { | |
| 663 } | |
| 664 void AddReference() { | |
| 665 CritScope lock(&cs_); | |
| 666 ++ref_count_; | |
| 667 } | |
| 668 void Release() { | |
| 669 int ref_count; | |
| 670 { // Atomic ops would have been a better fit here. | |
| 671 CritScope lock(&cs_); | |
| 672 ref_count = --ref_count_; | |
| 673 } | |
| 674 if (ref_count == 0) { | |
| 675 delete stream_; | |
| 676 delete this; | |
| 677 } | |
| 678 } | |
| 679 private: | |
| 680 StreamInterface* stream_; | |
| 681 int ref_count_; | |
| 682 CriticalSection cs_; | |
| 683 RTC_DISALLOW_COPY_AND_ASSIGN(StreamRefCount); | |
| 684 }; | |
| 685 | |
| 686 // Constructor for adding references | |
| 687 explicit StreamReference(StreamRefCount* stream_ref_count, | |
| 688 StreamInterface* stream); | |
| 689 | |
| 690 StreamRefCount* stream_ref_count_; | |
| 691 RTC_DISALLOW_COPY_AND_ASSIGN(StreamReference); | |
| 692 }; | |
| 693 | |
| 694 /////////////////////////////////////////////////////////////////////////////// | |
| 695 | 560 |
| 696 // Flow attempts to move bytes from source to sink via buffer of size | 561 // Flow attempts to move bytes from source to sink via buffer of size |
| 697 // buffer_len. The function returns SR_SUCCESS when source reaches | 562 // buffer_len. The function returns SR_SUCCESS when source reaches |
| 698 // end-of-stream (returns SR_EOS), and all the data has been written successful | 563 // end-of-stream (returns SR_EOS), and all the data has been written successful |
| 699 // to sink. Alternately, if source returns SR_BLOCK or SR_ERROR, or if sink | 564 // to sink. Alternately, if source returns SR_BLOCK or SR_ERROR, or if sink |
| 700 // returns SR_BLOCK, SR_ERROR, or SR_EOS, then the function immediately returns | 565 // returns SR_BLOCK, SR_ERROR, or SR_EOS, then the function immediately returns |
| 701 // with the unexpected StreamResult value. | 566 // with the unexpected StreamResult value. |
| 702 // data_len is the length of the valid data in buffer. in case of error | 567 // data_len is the length of the valid data in buffer. in case of error |
| 703 // this is the data that read from source but can't move to destination. | 568 // this is the data that read from source but can't move to destination. |
| 704 // as a pass in parameter, it indicates data in buffer that should move to sink | 569 // as a pass in parameter, it indicates data in buffer that should move to sink |
| 705 StreamResult Flow(StreamInterface* source, | 570 StreamResult Flow(StreamInterface* source, |
| 706 char* buffer, | 571 char* buffer, |
| 707 size_t buffer_len, | 572 size_t buffer_len, |
| 708 StreamInterface* sink, | 573 StreamInterface* sink, |
| 709 size_t* data_len = nullptr); | 574 size_t* data_len = nullptr); |
| 710 | 575 |
| 711 /////////////////////////////////////////////////////////////////////////////// | 576 /////////////////////////////////////////////////////////////////////////////// |
| 712 | 577 |
| 713 } // namespace rtc | 578 } // namespace rtc |
| 714 | 579 |
| 715 #endif // WEBRTC_BASE_STREAM_H_ | 580 #endif // WEBRTC_BASE_STREAM_H_ |
| OLD | NEW |