1f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/* 2f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * 4f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * Use of this source code is governed by a BSD-style license 5f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * that can be found in the LICENSE file in the root of the source 6f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * tree. An additional intellectual property rights grant can be found 7f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * in the file PATENTS. All contributing project authors may 8f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org */ 10f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 11f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#ifndef WEBRTC_BASE_STREAM_H_ 12f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#define WEBRTC_BASE_STREAM_H_ 13f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 14f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include <stdio.h> 15f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 16f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/basictypes.h" 17f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/buffer.h" 18f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/criticalsection.h" 19f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/logging.h" 20f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/messagehandler.h" 21f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/messagequeue.h" 22f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/scoped_ptr.h" 23f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/sigslot.h" 24f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 25f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgnamespace rtc { 26f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 27f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 28f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// StreamInterface is a generic asynchronous stream interface, supporting read, 29f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// write, and close operations, and asynchronous signalling of state changes. 30f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// The interface is designed with file, memory, and socket implementations in 31f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// mind. Some implementations offer extended operations, such as seeking. 32f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 33f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 34f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// The following enumerations are declared outside of the StreamInterface 35f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// class for brevity in use. 36f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 37f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// The SS_OPENING state indicates that the stream will signal open or closed 38f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// in the future. 39f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgenum StreamState { SS_CLOSED, SS_OPENING, SS_OPEN }; 40f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 41f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// Stream read/write methods return this value to indicate various success 42f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// and failure conditions described below. 43f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgenum StreamResult { SR_ERROR, SR_SUCCESS, SR_BLOCK, SR_EOS }; 44f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 45f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// StreamEvents are used to asynchronously signal state transitionss. The flags 46f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// may be combined. 47f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// SE_OPEN: The stream has transitioned to the SS_OPEN state 48f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// SE_CLOSE: The stream has transitioned to the SS_CLOSED state 49f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// SE_READ: Data is available, so Read is likely to not return SR_BLOCK 50f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// SE_WRITE: Data can be written, so Write is likely to not return SR_BLOCK 51f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgenum StreamEvent { SE_OPEN = 1, SE_READ = 2, SE_WRITE = 4, SE_CLOSE = 8 }; 52f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 53f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass Thread; 54f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 55f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgstruct StreamEventData : public MessageData { 56f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int events, error; 57f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamEventData(int ev, int er) : events(ev), error(er) { } 58f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 59f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 60f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass StreamInterface : public MessageHandler { 61f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 62f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org enum { 63f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org MSG_POST_EVENT = 0xF1F1, MSG_MAX = MSG_POST_EVENT 64f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org }; 65f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 6667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~StreamInterface() override; 67f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 68f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual StreamState GetState() const = 0; 69f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 70f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Read attempts to fill buffer of size buffer_len. Write attempts to send 71f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // data_len bytes stored in data. The variables read and write are set only 72f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // on SR_SUCCESS (see below). Likewise, error is only set on SR_ERROR. 73f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Read and Write return a value indicating: 74f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // SR_ERROR: an error occurred, which is returned in a non-null error 75f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // argument. Interpretation of the error requires knowledge of the 76f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // stream's concrete type, which limits its usefulness. 77f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // SR_SUCCESS: some number of bytes were successfully written, which is 78f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // returned in a non-null read/write argument. 79f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // SR_BLOCK: the stream is in non-blocking mode, and the operation would 80f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // block, or the stream is in SS_OPENING state. 81f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // SR_EOS: the end-of-stream has been reached, or the stream is in the 82f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // SS_CLOSED state. 83f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual StreamResult Read(void* buffer, size_t buffer_len, 84f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* read, int* error) = 0; 85f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual StreamResult Write(const void* data, size_t data_len, 86f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* written, int* error) = 0; 87f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Attempt to transition to the SS_CLOSED state. SE_CLOSE will not be 88f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // signalled as a result of this call. 89f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void Close() = 0; 90f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 91f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Streams may signal one or more StreamEvents to indicate state changes. 92f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The first argument identifies the stream on which the state change occured. 93f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The second argument is a bit-wise combination of StreamEvents. 94f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // If SE_CLOSE is signalled, then the third argument is the associated error 95f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // code. Otherwise, the value is undefined. 96f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Note: Not all streams will support asynchronous event signalling. However, 97f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // SS_OPENING and SR_BLOCK returned from stream member functions imply that 98f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // certain events will be raised in the future. 99f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org sigslot::signal3<StreamInterface*, int, int> SignalEvent; 100f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 101f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Like calling SignalEvent, but posts a message to the specified thread, 102f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // which will call SignalEvent. This helps unroll the stack and prevent 103f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // re-entrancy. 104f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void PostEvent(Thread* t, int events, int err); 105f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Like the aforementioned method, but posts to the current thread. 106f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void PostEvent(int events, int err); 107f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 108f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 109f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // OPTIONAL OPERATIONS 110f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 111f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Not all implementations will support the following operations. In general, 112f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // a stream will only support an operation if it reasonably efficient to do 113f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // so. For example, while a socket could buffer incoming data to support 114f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // seeking, it will not do so. Instead, a buffering stream adapter should 115f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // be used. 116f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 117f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Even though several of these operations are related, you should 118f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // always use whichever operation is most relevant. For example, you may 119f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // be tempted to use GetSize() and GetPosition() to deduce the result of 120f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // GetAvailable(). However, a stream which is read-once may support the 121f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // latter operation but not the former. 122f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 123f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 124f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The following four methods are used to avoid copying data multiple times. 125f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 126f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // GetReadData returns a pointer to a buffer which is owned by the stream. 127f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The buffer contains data_len bytes. NULL is returned if no data is 128f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // available, or if the method fails. If the caller processes the data, it 129f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // must call ConsumeReadData with the number of processed bytes. GetReadData 130f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // does not require a matching call to ConsumeReadData if the data is not 131f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // processed. Read and ConsumeReadData invalidate the buffer returned by 132f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // GetReadData. 13367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual const void* GetReadData(size_t* data_len); 134f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void ConsumeReadData(size_t used) {} 135f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 136f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // GetWriteBuffer returns a pointer to a buffer which is owned by the stream. 137f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The buffer has a capacity of buf_len bytes. NULL is returned if there is 138f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // no buffer available, or if the method fails. The call may write data to 139f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // the buffer, and then call ConsumeWriteBuffer with the number of bytes 140f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // written. GetWriteBuffer does not require a matching call to 141f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // ConsumeWriteData if no data is written. Write, ForceWrite, and 142f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // ConsumeWriteData invalidate the buffer returned by GetWriteBuffer. 143f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // TODO: Allow the caller to specify a minimum buffer size. If the specified 144f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // amount of buffer is not yet available, return NULL and Signal SE_WRITE 145f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // when it is available. If the requested amount is too large, return an 146f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // error. 14767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual void* GetWriteBuffer(size_t* buf_len); 148f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void ConsumeWriteBuffer(size_t used) {} 149f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 150f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Write data_len bytes found in data, circumventing any throttling which 151f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // would could cause SR_BLOCK to be returned. Returns true if all the data 152f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // was written. Otherwise, the method is unsupported, or an unrecoverable 153f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // error occurred, and the error value is set. This method should be used 154f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // sparingly to write critical data which should not be throttled. A stream 155f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // which cannot circumvent its blocking constraints should not implement this 156f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // method. 157f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // NOTE: This interface is being considered experimentally at the moment. It 158f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // would be used by JUDP and BandwidthStream as a way to circumvent certain 159f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // soft limits in writing. 160f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org //virtual bool ForceWrite(const void* data, size_t data_len, int* error) { 161f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // if (error) *error = -1; 162f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // return false; 163f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org //} 164f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 165f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Seek to a byte offset from the beginning of the stream. Returns false if 166f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // the stream does not support seeking, or cannot seek to the specified 167f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // position. 16867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool SetPosition(size_t position); 169f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 170f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Get the byte offset of the current position from the start of the stream. 171f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns false if the position is not known. 17267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool GetPosition(size_t* position) const; 173f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 174f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Get the byte length of the entire stream. Returns false if the length 175f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // is not known. 17667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool GetSize(size_t* size) const; 177f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 178f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Return the number of Read()-able bytes remaining before end-of-stream. 179f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns false if not known. 18067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool GetAvailable(size_t* size) const; 181f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 182f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Return the number of Write()-able bytes remaining before end-of-stream. 183f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns false if not known. 18467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool GetWriteRemaining(size_t* size) const; 185f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 186f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Return true if flush is successful. 18767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool Flush(); 188f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 189f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Communicates the amount of data which will be written to the stream. The 190f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // stream may choose to preallocate memory to accomodate this data. The 191f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // stream may return false to indicate that there is not enough room (ie, 192f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Write will return SR_EOS/SR_ERROR at some point). Note that calling this 193f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // function should not affect the existing state of data in the stream. 19467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual bool ReserveSize(size_t size); 195f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 196f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 197f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // CONVENIENCE METHODS 198f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 199f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // These methods are implemented in terms of other methods, for convenience. 200f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // 201f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 202f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Seek to the start of the stream. 203f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org inline bool Rewind() { return SetPosition(0); } 204f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 205f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // WriteAll is a helper function which repeatedly calls Write until all the 206f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // data is written, or something other than SR_SUCCESS is returned. Note that 207f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // unlike Write, the argument 'written' is always set, and may be non-zero 208f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // on results other than SR_SUCCESS. The remaining arguments have the 209f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // same semantics as Write. 210f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult WriteAll(const void* data, size_t data_len, 211f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* written, int* error); 212f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 213f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Similar to ReadAll. Calls Read until buffer_len bytes have been read, or 214f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // until a non-SR_SUCCESS result is returned. 'read' is always set. 215f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult ReadAll(void* buffer, size_t buffer_len, 216f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* read, int* error); 217f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 218f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // ReadLine is a helper function which repeatedly calls Read until it hits 219f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // the end-of-line character, or something other than SR_SUCCESS. 220f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // TODO: this is too inefficient to keep here. Break this out into a buffered 221f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // readline object or adapter 222f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult ReadLine(std::string* line); 223f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 224f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org protected: 225f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface(); 226f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 227f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // MessageHandler Interface 22867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void OnMessage(Message* msg) override; 229f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 230f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 2313c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterface); 232f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 233f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 234f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 235f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// StreamAdapterInterface is a convenient base-class for adapting a stream. 236f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// By default, all operations are pass-through. Override the methods that you 237f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// require adaptation. Streams should really be upgraded to reference-counted. 238f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// In the meantime, use the owned flag to indicate whether the adapter should 239f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// own the adapted stream. 240f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 241f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 242f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass StreamAdapterInterface : public StreamInterface, 243f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public sigslot::has_slots<> { 244f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 245f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit StreamAdapterInterface(StreamInterface* stream, bool owned = true); 246f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 247f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Core Stream Interface 24867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamState GetState() const override; 24967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 25067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t buffer_len, 25167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* read, 25267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 25367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* data, 25467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t data_len, 25567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* written, 25667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 25767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 258f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 259f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Optional Stream Interface 260f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org /* Note: Many stream adapters were implemented prior to this Read/Write 261f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org interface. Therefore, a simple pass through of data in those cases may 262f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org be broken. At a later time, we should do a once-over pass of all 263f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org adapters, and make them compliant with these interfaces, after which this 264f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org code can be uncommented. 265f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual const void* GetReadData(size_t* data_len) { 266f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org return stream_->GetReadData(data_len); 267f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 268f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void ConsumeReadData(size_t used) { 269f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org stream_->ConsumeReadData(used); 270f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 271f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 272f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void* GetWriteBuffer(size_t* buf_len) { 273f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org return stream_->GetWriteBuffer(buf_len); 274f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 275f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void ConsumeWriteBuffer(size_t used) { 276f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org stream_->ConsumeWriteBuffer(used); 277f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 278f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org */ 279f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 280f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org /* Note: This interface is currently undergoing evaluation. 281f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual bool ForceWrite(const void* data, size_t data_len, int* error) { 282f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org return stream_->ForceWrite(data, data_len, error); 283f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 284f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org */ 285f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 28667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool SetPosition(size_t position) override; 28767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetPosition(size_t* position) const override; 28867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetSize(size_t* size) const override; 28967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetAvailable(size_t* size) const override; 29067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetWriteRemaining(size_t* size) const override; 29167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool ReserveSize(size_t size) override; 29267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool Flush() override; 293f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 294f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void Attach(StreamInterface* stream, bool owned = true); 295f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* Detach(); 296f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 297f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org protected: 29867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~StreamAdapterInterface() override; 299f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 300f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Note that the adapter presents itself as the origin of the stream events, 301f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // since users of the adapter may not recognize the adapted object. 30267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org virtual void OnEvent(StreamInterface* stream, int events, int err); 303f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* stream() { return stream_; } 304f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 305f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 306f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* stream_; 307f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool owned_; 3083c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface); 309f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 310f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 311f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 312f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// StreamTap is a non-modifying, pass-through adapter, which copies all data 313f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// in either direction to the tap. Note that errors or blocking on writing to 314f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// the tap will prevent further tap writes from occurring. 315f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 316f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 317f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass StreamTap : public StreamAdapterInterface { 318f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 319f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit StreamTap(StreamInterface* stream, StreamInterface* tap); 32067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~StreamTap() override; 321f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 322f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void AttachTap(StreamInterface* tap); 323f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* DetachTap(); 324f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult GetTapResult(int* error); 325f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 326f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // StreamAdapterInterface Interface 32767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 32867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t buffer_len, 32967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* read, 33067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 33167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* data, 33267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t data_len, 33367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* written, 33467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 335f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 336f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 337f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org scoped_ptr<StreamInterface> tap_; 338f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult tap_result_; 339f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int tap_error_; 3403c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(StreamTap); 341f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 342f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 343f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 344f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// NullStream gives errors on read, and silently discards all written data. 345f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 346f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 347f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass NullStream : public StreamInterface { 348f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 349f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org NullStream(); 35067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~NullStream() override; 351f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 352f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // StreamInterface Interface 35367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamState GetState() const override; 35467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 35567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t buffer_len, 35667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* read, 35767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 35867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* data, 35967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t data_len, 36067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* written, 36167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 36267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 363f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 364f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 365f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 366f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// FileStream is a simple implementation of a StreamInterface, which does not 367f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// support asynchronous notification. 368f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 369f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 370f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass FileStream : public StreamInterface { 371f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 372f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org FileStream(); 37367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~FileStream() override; 374f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 375f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The semantics of filename and mode are the same as stdio's fopen 376f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual bool Open(const std::string& filename, const char* mode, int* error); 377f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual bool OpenShare(const std::string& filename, const char* mode, 378f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int shflag, int* error); 379f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 380f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // By default, reads and writes are buffered for efficiency. Disabling 381f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // buffering causes writes to block until the bytes on disk are updated. 382f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual bool DisableBuffering(); 383f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 38467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamState GetState() const override; 38567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 38667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t buffer_len, 38767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* read, 38867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 38967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* data, 39067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t data_len, 39167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* written, 39267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 39367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 39467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool SetPosition(size_t position) override; 39567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetPosition(size_t* position) const override; 39667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetSize(size_t* size) const override; 39767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetAvailable(size_t* size) const override; 39867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool ReserveSize(size_t size) override; 39967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org 40067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool Flush() override; 401f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 402f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#if defined(WEBRTC_POSIX) && !defined(__native_client__) 403f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Tries to aquire an exclusive lock on the file. 404f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Use OpenShare(...) on win32 to get similar functionality. 405f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool TryLock(); 406f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool Unlock(); 407f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#endif 408f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 409f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Note: Deprecated in favor of Filesystem::GetFileSize(). 410f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org static bool GetSize(const std::string& filename, size_t* size); 411f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 412f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org protected: 413f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual void DoClose(); 414f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 415f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org FILE* file_; 416f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 417f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 4183c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(FileStream); 419f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 420f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 421f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 422f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// MemoryStream is a simple implementation of a StreamInterface over in-memory 423f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// data. Data is read and written at the current seek position. Reads return 424f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// end-of-stream when they reach the end of data. Writes actually extend the 425f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// end of data mark. 426f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 427f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 428f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass MemoryStreamBase : public StreamInterface { 429f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 43067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamState GetState() const override; 43167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 43267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t bytes, 43367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* bytes_read, 43467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 43567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* buffer, 43667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t bytes, 43767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* bytes_written, 43867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 43967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 44067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool SetPosition(size_t position) override; 44167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetPosition(size_t* position) const override; 44267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetSize(size_t* size) const override; 44367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetAvailable(size_t* size) const override; 44467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool ReserveSize(size_t size) override; 445f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 446f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org char* GetBuffer() { return buffer_; } 447f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org const char* GetBuffer() const { return buffer_; } 448f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 449f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org protected: 450f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org MemoryStreamBase(); 451f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 452f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org virtual StreamResult DoReserve(size_t size, int* error); 453f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 454f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Invariant: 0 <= seek_position <= data_length_ <= buffer_length_ 455f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org char* buffer_; 456f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t buffer_length_; 457f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t data_length_; 458f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t seek_position_; 459f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 460f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 4613c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(MemoryStreamBase); 462f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 463f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 464f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// MemoryStream dynamically resizes to accomodate written data. 465f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 466f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass MemoryStream : public MemoryStreamBase { 467f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 468f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org MemoryStream(); 469f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit MemoryStream(const char* data); // Calls SetData(data, strlen(data)) 470f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org MemoryStream(const void* data, size_t length); // Calls SetData(data, length) 47167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~MemoryStream() override; 472f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 473f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetData(const void* data, size_t length); 474f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 475f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org protected: 47667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult DoReserve(size_t size, int* error) override; 477f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Memory Streams are aligned for efficiency. 478f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org static const int kAlignment = 16; 479f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org char* buffer_alloc_; 480f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 481f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 482f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// ExternalMemoryStream adapts an external memory buffer, so writes which would 483f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// extend past the end of the buffer will return end-of-stream. 484f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 485f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass ExternalMemoryStream : public MemoryStreamBase { 486f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 487f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org ExternalMemoryStream(); 488f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org ExternalMemoryStream(void* data, size_t length); 48967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~ExternalMemoryStream() override; 490f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 491f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetData(void* data, size_t length); 492f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 493f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 494f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// FifoBuffer allows for efficient, thread-safe buffering of data between 495f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// writer and reader. As the data can wrap around the end of the buffer, 496f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// MemoryStreamBase can't help us here. 497f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 498f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass FifoBuffer : public StreamInterface { 499f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 500f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates a FIFO buffer with the specified capacity. 501f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit FifoBuffer(size_t length); 502f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates a FIFO buffer with the specified capacity and owner 503f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org FifoBuffer(size_t length, Thread* owner); 50467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~FifoBuffer() override; 505f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Gets the amount of data currently readable from the buffer. 506f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool GetBuffered(size_t* data_len) const; 507f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Resizes the buffer to the specified capacity. Fails if data_length_ > size 508f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool SetCapacity(size_t length); 509f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 510f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Read into |buffer| with an offset from the current read position, offset 511f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // is specified in number of bytes. 512f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // This method doesn't adjust read position nor the number of available 513f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // bytes, user has to call ConsumeReadData() to do this. 514f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult ReadOffset(void* buffer, size_t bytes, size_t offset, 515f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* bytes_read); 516f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 517f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Write |buffer| with an offset from the current write position, offset is 518f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // specified in number of bytes. 519f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // This method doesn't adjust the number of buffered bytes, user has to call 520f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // ConsumeWriteBuffer() to do this. 521f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult WriteOffset(const void* buffer, size_t bytes, size_t offset, 522f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* bytes_written); 523f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 524f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // StreamInterface methods 52567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamState GetState() const override; 52667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 52767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t bytes, 52867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* bytes_read, 52967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 53067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* buffer, 53167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t bytes, 53267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* bytes_written, 53367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 53467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 53567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org const void* GetReadData(size_t* data_len) override; 53667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void ConsumeReadData(size_t used) override; 53767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void* GetWriteBuffer(size_t* buf_len) override; 53867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void ConsumeWriteBuffer(size_t used) override; 53967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetWriteRemaining(size_t* size) const override; 540f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 541f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 542f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Helper method that implements ReadOffset. Caller must acquire a lock 543f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // when calling this method. 544f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult ReadOffsetLocked(void* buffer, size_t bytes, size_t offset, 545f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t* bytes_read); 546f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 547f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Helper method that implements WriteOffset. Caller must acquire a lock 548f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // when calling this method. 549f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamResult WriteOffsetLocked(const void* buffer, size_t bytes, 550f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t offset, size_t* bytes_written); 551f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 552f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamState state_; // keeps the opened/closed state of the stream 553f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org scoped_ptr<char[]> buffer_; // the allocated buffer 554f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t buffer_length_; // size of the allocated buffer 555f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t data_length_; // amount of readable data in the buffer 556f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t read_position_; // offset to the readable data 557f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org Thread* owner_; // stream callbacks are dispatched on this thread 558f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org mutable CriticalSection crit_; // object lock 5593c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer); 560f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 561f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 562f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 563f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 564f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass LoggingAdapter : public StreamAdapterInterface { 565f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 566f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org LoggingAdapter(StreamInterface* stream, LoggingSeverity level, 567f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org const std::string& label, bool hex_mode = false); 568f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 569f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void set_label(const std::string& label); 570f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 57167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 57267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t buffer_len, 57367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* read, 57467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 57567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* data, 57667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t data_len, 57767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* written, 57867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 57967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 580f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 581f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org protected: 58267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void OnEvent(StreamInterface* stream, int events, int err) override; 583f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 584f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 585f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org LoggingSeverity level_; 586f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string label_; 587f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool hex_mode_; 588f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org LogMultilineState lms_; 589f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 5903c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(LoggingAdapter); 591f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 592f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 593f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 594f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// StringStream - Reads/Writes to an external std::string 595f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 596f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 597f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass StringStream : public StreamInterface { 598f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 59900aac5aacfa1778cafa1a67036743df990e21153Tommi explicit StringStream(std::string* str); 600f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit StringStream(const std::string& str); 601f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 60267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamState GetState() const override; 60367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Read(void* buffer, 60467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t buffer_len, 60567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* read, 60667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 60767186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org StreamResult Write(const void* data, 60867186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t data_len, 60967186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org size_t* written, 61067186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org int* error) override; 61167186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org void Close() override; 61267186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool SetPosition(size_t position) override; 61367186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetPosition(size_t* position) const override; 61467186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetSize(size_t* size) const override; 61567186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool GetAvailable(size_t* size) const override; 61667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org bool ReserveSize(size_t size) override; 617f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 618f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 619f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string& str_; 620f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t read_pos_; 621f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool read_only_; 622f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 623f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 624f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 625f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// StreamReference - A reference counting stream adapter 626f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 627f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 628f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// Keep in mind that the streams and adapters defined in this file are 629f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// not thread-safe, so this has limited uses. 630f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 631f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// A StreamRefCount holds the reference count and a pointer to the 632f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// wrapped stream. It deletes the wrapped stream when there are no 633f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// more references. We can then have multiple StreamReference 634f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// instances pointing to one StreamRefCount, all wrapping the same 635f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// stream. 636f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 637f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass StreamReference : public StreamAdapterInterface { 638f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org class StreamRefCount; 639f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 640f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Constructor for the first reference to a stream 641f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Note: get more references through NewReference(). Use this 642f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // constructor only once on a given stream. 643f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit StreamReference(StreamInterface* stream); 644f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* GetStream() { return stream(); } 645f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* NewReference(); 64667186fe00cc68cbe03aa66d17fb4962458ca96d2kwiberg@webrtc.org ~StreamReference() override; 647f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 648f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 649f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org class StreamRefCount { 650f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 651f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit StreamRefCount(StreamInterface* stream) 652f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org : stream_(stream), ref_count_(1) { 653f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 654f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void AddReference() { 655f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org CritScope lock(&cs_); 656f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org ++ref_count_; 657f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 658f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void Release() { 659f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int ref_count; 660f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org { // Atomic ops would have been a better fit here. 661f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org CritScope lock(&cs_); 662f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org ref_count = --ref_count_; 663f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 664f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org if (ref_count == 0) { 665f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org delete stream_; 666f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org delete this; 667f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 668f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 669f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 670f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* stream_; 671f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int ref_count_; 672f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org CriticalSection cs_; 6733c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(StreamRefCount); 674f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org }; 675f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 676f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Constructor for adding references 677f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org explicit StreamReference(StreamRefCount* stream_ref_count, 678f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* stream); 679f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 680f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamRefCount* stream_ref_count_; 6813c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(StreamReference); 682f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 683f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 684f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 685f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 686f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// Flow attempts to move bytes from source to sink via buffer of size 687f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// buffer_len. The function returns SR_SUCCESS when source reaches 688f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// end-of-stream (returns SR_EOS), and all the data has been written successful 689f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// to sink. Alternately, if source returns SR_BLOCK or SR_ERROR, or if sink 690f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// returns SR_BLOCK, SR_ERROR, or SR_EOS, then the function immediately returns 691f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// with the unexpected StreamResult value. 692f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// data_len is the length of the valid data in buffer. in case of error 693f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// this is the data that read from source but can't move to destination. 694f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// as a pass in parameter, it indicates data in buffer that should move to sink 695f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgStreamResult Flow(StreamInterface* source, 696f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org char* buffer, size_t buffer_len, 697f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org StreamInterface* sink, size_t* data_len = NULL); 698f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 699f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////// 700f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 701f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org} // namespace rtc 702f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 703f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#endif // WEBRTC_BASE_STREAM_H_ 704