blob: 55f5a06a43a9683323bbacde5720211866294756 [file] [log] [blame]
nisse8e7577c2016-10-06 01:37:37 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_TRANSFORMADAPTER_H_
12#define RTC_BASE_TRANSFORMADAPTER_H_
nisse8e7577c2016-10-06 01:37:37 -070013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/stream.h"
nisse8e7577c2016-10-06 01:37:37 -070015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016namespace rtc {
17///////////////////////////////////////////////////////////////////////////////
nisse8e7577c2016-10-06 01:37:37 -070018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019class TransformInterface {
Yves Gerey665174f2018-06-19 15:03:05 +020020 public:
21 virtual ~TransformInterface() {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
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.
Yves Gerey665174f2018-06-19 15:03:05 +020030 virtual StreamResult Transform(const void* input,
31 size_t* in_len,
32 void* output,
33 size_t* out_len,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 bool flush) = 0;
35};
36
37///////////////////////////////////////////////////////////////////////////////
38
39// TransformAdapter causes all data passed through to be transformed by the
40// supplied TransformInterface object, which may apply compression, encryption,
41// etc.
42
43class TransformAdapter : public StreamAdapterInterface {
Yves Gerey665174f2018-06-19 15:03:05 +020044 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045 // Note that the transformation is unidirectional, in the direction specified
46 // by the constructor. Operations in the opposite direction result in SR_EOS.
Yves Gerey665174f2018-06-19 15:03:05 +020047 TransformAdapter(StreamInterface* stream,
48 TransformInterface* transform,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 bool direction_read);
50 ~TransformAdapter() override;
51
52 StreamResult Read(void* buffer,
53 size_t buffer_len,
54 size_t* read,
55 int* error) override;
56 StreamResult Write(const void* data,
57 size_t data_len,
58 size_t* written,
59 int* error) override;
60 void Close() override;
61
62 // Apriori, we can't tell what the transformation does to the stream length.
63 bool GetAvailable(size_t* size) const override;
64 bool ReserveSize(size_t size) override;
65
66 // Transformations might not be restartable
67 virtual bool Rewind();
68
Yves Gerey665174f2018-06-19 15:03:05 +020069 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 enum State { ST_PROCESSING, ST_FLUSHING, ST_COMPLETE, ST_ERROR };
71 enum { BUFFER_SIZE = 1024 };
72
Yves Gerey665174f2018-06-19 15:03:05 +020073 TransformInterface* transform_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 bool direction_read_;
75 State state_;
76 int error_;
77
78 char buffer_[BUFFER_SIZE];
79 size_t len_;
80};
81
82///////////////////////////////////////////////////////////////////////////////
83
Yves Gerey665174f2018-06-19 15:03:05 +020084} // namespace rtc
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085
Yves Gerey665174f2018-06-19 15:03:05 +020086#endif // RTC_BASE_TRANSFORMADAPTER_H_