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 /////////////////////////////////////////////////////////////////////////////// |
313 // NullStream gives errors on read, and silently discards all written data. | 345 // NullStream gives errors on read, and silently discards all written data. |
314 /////////////////////////////////////////////////////////////////////////////// | 346 /////////////////////////////////////////////////////////////////////////////// |
315 | 347 |
316 class NullStream : public StreamInterface { | 348 class NullStream : public StreamInterface { |
317 public: | 349 public: |
318 NullStream(); | 350 NullStream(); |
319 ~NullStream() override; | 351 ~NullStream() override; |
320 | 352 |
321 // StreamInterface Interface | 353 // StreamInterface Interface |
322 StreamState GetState() const override; | 354 StreamState GetState() const override; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 | 473 |
442 void SetData(const void* data, size_t length); | 474 void SetData(const void* data, size_t length); |
443 | 475 |
444 protected: | 476 protected: |
445 StreamResult DoReserve(size_t size, int* error) override; | 477 StreamResult DoReserve(size_t size, int* error) override; |
446 // Memory Streams are aligned for efficiency. | 478 // Memory Streams are aligned for efficiency. |
447 static const int kAlignment = 16; | 479 static const int kAlignment = 16; |
448 char* buffer_alloc_; | 480 char* buffer_alloc_; |
449 }; | 481 }; |
450 | 482 |
| 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 |
451 // FifoBuffer allows for efficient, thread-safe buffering of data between | 495 // FifoBuffer allows for efficient, thread-safe buffering of data between |
452 // writer and reader. As the data can wrap around the end of the buffer, | 496 // writer and reader. As the data can wrap around the end of the buffer, |
453 // MemoryStreamBase can't help us here. | 497 // MemoryStreamBase can't help us here. |
454 | 498 |
455 class FifoBuffer : public StreamInterface { | 499 class FifoBuffer : public StreamInterface { |
456 public: | 500 public: |
457 // Creates a FIFO buffer with the specified capacity. | 501 // Creates a FIFO buffer with the specified capacity. |
458 explicit FifoBuffer(size_t length); | 502 explicit FifoBuffer(size_t length); |
459 // Creates a FIFO buffer with the specified capacity and owner | 503 // Creates a FIFO buffer with the specified capacity and owner |
460 FifoBuffer(size_t length, Thread* owner); | 504 FifoBuffer(size_t length, Thread* owner); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 // offset to the readable data | 563 // offset to the readable data |
520 size_t read_position_ GUARDED_BY(crit_); | 564 size_t read_position_ GUARDED_BY(crit_); |
521 // stream callbacks are dispatched on this thread | 565 // stream callbacks are dispatched on this thread |
522 Thread* owner_; | 566 Thread* owner_; |
523 // object lock | 567 // object lock |
524 CriticalSection crit_; | 568 CriticalSection crit_; |
525 RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer); | 569 RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer); |
526 }; | 570 }; |
527 | 571 |
528 /////////////////////////////////////////////////////////////////////////////// | 572 /////////////////////////////////////////////////////////////////////////////// |
| 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 /////////////////////////////////////////////////////////////////////////////// |
529 // StringStream - Reads/Writes to an external std::string | 604 // StringStream - Reads/Writes to an external std::string |
530 /////////////////////////////////////////////////////////////////////////////// | 605 /////////////////////////////////////////////////////////////////////////////// |
531 | 606 |
532 class StringStream : public StreamInterface { | 607 class StringStream : public StreamInterface { |
533 public: | 608 public: |
534 explicit StringStream(std::string* str); | 609 explicit StringStream(std::string* str); |
535 explicit StringStream(const std::string& str); | 610 explicit StringStream(const std::string& str); |
536 | 611 |
537 StreamState GetState() const override; | 612 StreamState GetState() const override; |
538 StreamResult Read(void* buffer, | 613 StreamResult Read(void* buffer, |
(...skipping 11 matching lines...) Expand all Loading... |
550 bool GetAvailable(size_t* size) const override; | 625 bool GetAvailable(size_t* size) const override; |
551 bool ReserveSize(size_t size) override; | 626 bool ReserveSize(size_t size) override; |
552 | 627 |
553 private: | 628 private: |
554 std::string& str_; | 629 std::string& str_; |
555 size_t read_pos_; | 630 size_t read_pos_; |
556 bool read_only_; | 631 bool read_only_; |
557 }; | 632 }; |
558 | 633 |
559 /////////////////////////////////////////////////////////////////////////////// | 634 /////////////////////////////////////////////////////////////////////////////// |
| 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 /////////////////////////////////////////////////////////////////////////////// |
560 | 695 |
561 // Flow attempts to move bytes from source to sink via buffer of size | 696 // Flow attempts to move bytes from source to sink via buffer of size |
562 // buffer_len. The function returns SR_SUCCESS when source reaches | 697 // buffer_len. The function returns SR_SUCCESS when source reaches |
563 // end-of-stream (returns SR_EOS), and all the data has been written successful | 698 // end-of-stream (returns SR_EOS), and all the data has been written successful |
564 // to sink. Alternately, if source returns SR_BLOCK or SR_ERROR, or if sink | 699 // to sink. Alternately, if source returns SR_BLOCK or SR_ERROR, or if sink |
565 // returns SR_BLOCK, SR_ERROR, or SR_EOS, then the function immediately returns | 700 // returns SR_BLOCK, SR_ERROR, or SR_EOS, then the function immediately returns |
566 // with the unexpected StreamResult value. | 701 // with the unexpected StreamResult value. |
567 // data_len is the length of the valid data in buffer. in case of error | 702 // data_len is the length of the valid data in buffer. in case of error |
568 // this is the data that read from source but can't move to destination. | 703 // this is the data that read from source but can't move to destination. |
569 // as a pass in parameter, it indicates data in buffer that should move to sink | 704 // as a pass in parameter, it indicates data in buffer that should move to sink |
570 StreamResult Flow(StreamInterface* source, | 705 StreamResult Flow(StreamInterface* source, |
571 char* buffer, | 706 char* buffer, |
572 size_t buffer_len, | 707 size_t buffer_len, |
573 StreamInterface* sink, | 708 StreamInterface* sink, |
574 size_t* data_len = nullptr); | 709 size_t* data_len = nullptr); |
575 | 710 |
576 /////////////////////////////////////////////////////////////////////////////// | 711 /////////////////////////////////////////////////////////////////////////////// |
577 | 712 |
578 } // namespace rtc | 713 } // namespace rtc |
579 | 714 |
580 #endif // WEBRTC_BASE_STREAM_H_ | 715 #endif // WEBRTC_BASE_STREAM_H_ |
OLD | NEW |