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
11#ifndef WEBRTC_BASE_TRANSFORMADAPTER_H__
12#define WEBRTC_BASE_TRANSFORMADAPTER_H__
13
14#include "webrtc/base/stream.h"
15
16namespace rtc {
17///////////////////////////////////////////////////////////////////////////////
18
19class TransformInterface {
20public:
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
41class TransformAdapter : public StreamAdapterInterface {
42public:
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
67private:
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
84#endif // WEBRTC_BASE_TRANSFORMADAPTER_H__
85