blob: 290d5605ffb8b794c1b30fd0575817b5d456e04c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000048 ~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;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60 // Apriori, we can't tell what the transformation does to the stream length.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000061 bool GetAvailable(size_t* size) const override;
62 bool ReserveSize(size_t size) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
64 // Transformations might not be restartable
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065 virtual bool Rewind();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
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__