nisse | 8e7577c | 2016-10-06 01:37:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_TRANSFORMADAPTER_H_ |
| 12 | #define RTC_BASE_TRANSFORMADAPTER_H_ |
nisse | 8e7577c | 2016-10-06 01:37:37 -0700 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/stream.h" |
nisse | 8e7577c | 2016-10-06 01:37:37 -0700 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | namespace rtc { |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
nisse | 8e7577c | 2016-10-06 01:37:37 -0700 | [diff] [blame] | 18 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | class TransformInterface { |
| 20 | public: |
| 21 | virtual ~TransformInterface() { } |
| 22 | |
| 23 | // Transform should convert the in_len bytes of input into the out_len-sized |
| 24 | // output buffer. If flush is true, there will be no more data following |
| 25 | // input. |
| 26 | // After the transformation, in_len contains the number of bytes consumed, and |
| 27 | // out_len contains the number of bytes ready in output. |
| 28 | // Note: Transform should not return SR_BLOCK, as there is no asynchronous |
| 29 | // notification available. |
| 30 | virtual StreamResult Transform(const void * input, size_t * in_len, |
| 31 | void * output, size_t * out_len, |
| 32 | bool flush) = 0; |
| 33 | }; |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | // TransformAdapter causes all data passed through to be transformed by the |
| 38 | // supplied TransformInterface object, which may apply compression, encryption, |
| 39 | // etc. |
| 40 | |
| 41 | class TransformAdapter : public StreamAdapterInterface { |
| 42 | public: |
| 43 | // Note that the transformation is unidirectional, in the direction specified |
| 44 | // by the constructor. Operations in the opposite direction result in SR_EOS. |
| 45 | TransformAdapter(StreamInterface * stream, |
| 46 | TransformInterface * transform, |
| 47 | bool direction_read); |
| 48 | ~TransformAdapter() override; |
| 49 | |
| 50 | StreamResult Read(void* buffer, |
| 51 | size_t buffer_len, |
| 52 | size_t* read, |
| 53 | int* error) override; |
| 54 | StreamResult Write(const void* data, |
| 55 | size_t data_len, |
| 56 | size_t* written, |
| 57 | int* error) override; |
| 58 | void Close() override; |
| 59 | |
| 60 | // Apriori, we can't tell what the transformation does to the stream length. |
| 61 | bool GetAvailable(size_t* size) const override; |
| 62 | bool ReserveSize(size_t size) override; |
| 63 | |
| 64 | // Transformations might not be restartable |
| 65 | virtual bool Rewind(); |
| 66 | |
| 67 | private: |
| 68 | enum State { ST_PROCESSING, ST_FLUSHING, ST_COMPLETE, ST_ERROR }; |
| 69 | enum { BUFFER_SIZE = 1024 }; |
| 70 | |
| 71 | TransformInterface * transform_; |
| 72 | bool direction_read_; |
| 73 | State state_; |
| 74 | int error_; |
| 75 | |
| 76 | char buffer_[BUFFER_SIZE]; |
| 77 | size_t len_; |
| 78 | }; |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | } // namespace rtc |
| 83 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 84 | #endif // RTC_BASE_TRANSFORMADAPTER_H_ |