Android media: VideoFrame should not store so many sync points.
[chromium-blink-merge.git] / content / browser / streams / stream.h
blobe7e9586ca963c6a4391c43e8ec66433bdfb0a673
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_STREAMS_STREAM_H_
6 #define CONTENT_BROWSER_STREAMS_STREAM_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/byte_stream.h"
12 #include "content/common/content_export.h"
13 #include "url/gurl.h"
15 namespace net {
16 class HttpResponseHeaders;
17 class IOBuffer;
20 namespace content {
22 class StreamHandle;
23 class StreamHandleImpl;
24 class StreamReadObserver;
25 class StreamRegistry;
26 class StreamWriteObserver;
28 // A stream that sends data from an arbitrary source to an internal URL
29 // that can be read by an internal consumer. It will continue to pull from the
30 // original URL as long as there is data available. It can be read from
31 // multiple clients, but only one can be reading at a time. This allows a
32 // reader to consume part of the stream, then pass it along to another client
33 // to continue processing the stream.
34 class CONTENT_EXPORT Stream : public base::RefCountedThreadSafe<Stream> {
35 public:
36 enum StreamState {
37 STREAM_HAS_DATA,
38 STREAM_COMPLETE,
39 STREAM_EMPTY,
40 STREAM_ABORTED,
43 // Creates a stream.
45 // Security origin of Streams is checked in Blink (See BlobRegistry,
46 // BlobURL and SecurityOrigin to understand how it works). There's no security
47 // origin check in Chromium side for now.
48 Stream(StreamRegistry* registry,
49 StreamWriteObserver* write_observer,
50 const GURL& url);
52 // Sets the reader of this stream. Returns true on success, or false if there
53 // is already a reader.
54 bool SetReadObserver(StreamReadObserver* observer);
56 // Removes the read observer. |observer| must be the current observer.
57 void RemoveReadObserver(StreamReadObserver* observer);
59 // Removes the write observer. |observer| must be the current observer.
60 void RemoveWriteObserver(StreamWriteObserver* observer);
62 // Stops accepting new data, clears all buffer, unregisters this stream from
63 // |registry_| and make coming ReadRawData() calls return STREAM_ABORTED.
64 void Abort();
66 // Adds the data in |buffer| to the stream. Takes ownership of |buffer|.
67 void AddData(scoped_refptr<net::IOBuffer> buffer, size_t size);
68 // Adds data of |size| at |data| to the stream. This method creates a copy
69 // of the data, and then passes it to |writer_|.
70 void AddData(const char* data, size_t size);
72 // Notifies this stream that it will not be receiving any more data.
73 void Finalize();
75 // Reads a maximum of |buf_size| from the stream into |buf|. Sets
76 // |*bytes_read| to the number of bytes actually read.
77 // Returns STREAM_HAS_DATA if data was read, STREAM_EMPTY if no data was read,
78 // and STREAM_COMPLETE if the stream is finalized and all data has been read.
79 StreamState ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read);
81 scoped_ptr<StreamHandle> CreateHandle(
82 const GURL& original_url,
83 const std::string& mime_type,
84 scoped_refptr<net::HttpResponseHeaders> response_headers);
85 void CloseHandle();
87 // Indicates whether there is space in the buffer to add more data.
88 bool can_add_data() const { return can_add_data_; }
90 const GURL& url() const { return url_; }
92 // For StreamRegistry to remember the last memory usage reported to it.
93 size_t last_total_buffered_bytes() const {
94 return last_total_buffered_bytes_;
97 private:
98 friend class base::RefCountedThreadSafe<Stream>;
100 virtual ~Stream();
102 void OnSpaceAvailable();
103 void OnDataAvailable();
105 // Clears |data_| and related variables.
106 void ClearBuffer();
108 bool can_add_data_;
110 GURL url_;
112 // Buffer for storing data read from |reader_| but not yet read out from this
113 // Stream by ReadRawData() method.
114 scoped_refptr<net::IOBuffer> data_;
115 // Number of bytes read from |reader_| into |data_| including bytes already
116 // read out.
117 size_t data_length_;
118 // Number of bytes in |data_| that are already read out.
119 size_t data_bytes_read_;
121 // Last value returned by writer_->TotalBufferedBytes() in AddData(). Stored
122 // in order to check memory usage.
123 size_t last_total_buffered_bytes_;
125 scoped_ptr<ByteStreamWriter> writer_;
126 scoped_ptr<ByteStreamReader> reader_;
128 StreamRegistry* registry_;
129 StreamReadObserver* read_observer_;
130 StreamWriteObserver* write_observer_;
132 StreamHandleImpl* stream_handle_;
134 base::WeakPtrFactory<Stream> weak_ptr_factory_;
135 DISALLOW_COPY_AND_ASSIGN(Stream);
138 } // namespace content
140 #endif // CONTENT_BROWSER_STREAMS_STREAM_H_