blob: ad24438ea850f4c4a35b36c75b57aad6de9027ee [file] [log] [blame]
henrike@webrtc.orgf7795df2014-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);
48 virtual ~TransformAdapter();
49
50 virtual StreamResult Read(void * buffer, size_t buffer_len,
51 size_t * read, int * error);
52 virtual StreamResult Write(const void * data, size_t data_len,
53 size_t * written, int * error);
54 virtual void Close();
55
56 // Apriori, we can't tell what the transformation does to the stream length.
57 virtual bool GetAvailable(size_t* size) const { return false; }
58 virtual bool ReserveSize(size_t size) { return true; }
59
60 // Transformations might not be restartable
61 virtual bool Rewind() { return false; }
62
63private:
64 enum State { ST_PROCESSING, ST_FLUSHING, ST_COMPLETE, ST_ERROR };
65 enum { BUFFER_SIZE = 1024 };
66
67 TransformInterface * transform_;
68 bool direction_read_;
69 State state_;
70 int error_;
71
72 char buffer_[BUFFER_SIZE];
73 size_t len_;
74};
75
76///////////////////////////////////////////////////////////////////////////////
77
78} // namespace rtc
79
80#endif // WEBRTC_BASE_TRANSFORMADAPTER_H__