SeekableChannel.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __SEEKABLE_CHANNEL_H__
28 #define __SEEKABLE_CHANNEL_H__
29 
30 /*******************************************************************************
31  *******************************************************************************
32  *
33  * Seekable channel.
34  *
35  *******************************************************************************
36  ******************************************************************************/
37 
43 /*******************************************************************************
44  *
45  * Seekable channel configuration.
46  *
47  ******************************************************************************/
48 
49 /*
50  * Seekable channel XPCOM component definitions.
51  */
52 
53 #define SONGBIRD_SEEKABLECHANNEL_CONTRACTID \
54  "@songbirdnest.com/Songbird/SeekableChannel;1"
55 #define SONGBIRD_SEEKABLECHANNEL_CLASSNAME \
56  "Songbird Seekable Channel Component"
57 #define SONGBIRD_SEEKABLECHANNEL_CID \
58 { \
59  0x2030739B, \
60  0x5E60, \
61  0x492B, \
62  { 0xA1, 0xA7, 0x74, 0x70, 0x67, 0x47, 0x5B, 0xFD } \
63 }
64 
65 
66 /*******************************************************************************
67  *
68  * Seekable channel imported services.
69  *
70  ******************************************************************************/
71 
72 /* Local imports. */
73 #include <sbISeekableChannel.h>
74 
75 /* Mozilla imports. */
76 #include <nsCOMPtr.h>
77 #include <nsIChannel.h>
78 #include <nsIChannelEventSink.h>
79 #include <nsIInterfaceRequestor.h>
80 #include <nsIStreamListener.h>
81 
82 /* C++ std imports. */
83 #include <set>
84 
85 
86 /*******************************************************************************
87  *
88  * Seekable channel constants.
89  *
90  ******************************************************************************/
91 
92 #define NS_ERROR_SONGBIRD_SEEKABLE_CHANNEL_RESTART \
93  NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_GENERAL, 1)
94 
95 
96 /*******************************************************************************
97  *
98  * Seekable channel classes.
99  *
100  ******************************************************************************/
101 
102 /*
103  * sbSeekableChannel class
104  */
105 
107  public nsIStreamListener,
108  public nsIChannelEventSink,
109  public nsIInterfaceRequestor
110 {
111  /***************************************************************************
112  *
113  * Public interface.
114  *
115  **************************************************************************/
116 
117  public:
118 
119  /*
120  * Inherited interfaces.
121  */
122 
124  NS_DECL_SBISEEKABLECHANNEL
125  NS_DECL_NSISTREAMLISTENER
126  NS_DECL_NSIREQUESTOBSERVER
127  NS_DECL_NSICHANNELEVENTSINK
128  NS_DECL_NSIINTERFACEREQUESTOR
129 
130 
131  /*
132  * Public seekable channel services.
133  */
134 
136 
137  virtual ~sbSeekableChannel();
138 
139 
140  /***************************************************************************
141  *
142  * Private interface.
143  *
144  **************************************************************************/
145 
146  private:
147 
148  /*
149  * Segment
150  *
151  * This class is used to hold channel data segments.
152  */
153 
154  class Segment
155  {
156  public:
157 
158  /*
159  * offset Offset of data within the channel.
160  * length Length of data.
161  * buffer Buffer containing data.
162  */
163 
164  PRUint64 offset;
165  PRUint64 length;
166  char *buffer;
167 
168 
169  /*
170  * Public data segment services.
171  */
172 
173  Segment();
174 
175  virtual ~Segment();
176 
177  bool operator()(
178  const Segment *pSegment1,
179  const Segment *pSegment2) const;
180  };
181 
182 
183  /*
184  * DataSet Channel data set type.
185  */
186 
187  typedef std::set<Segment *, Segment> DataSet;
188 
189 
190  /*
191  * mpChannel Base channel.
192  * mpListener Channel listener.
193  * mChannelData Set of data read from channel.
194  * mContentLength Channel content length;
195  * mPos Current position within channel.
196  * mBasePos Current position within base channel.
197  * mRestarting True if restarting channel.
198  * mCompleted True if channel reading is complete.
199  * mDataReceivedSinceStart True if data has been received since the
200  * channel request was started.
201  */
202 
203  nsCOMPtr<nsIChannel> mpChannel;
204  nsCOMPtr<sbISeekableChannelListener>
205  mpListener;
206  DataSet mChannelData;
207  PRUint64 mContentLength;
208  PRUint64 mPos;
209  PRUint64 mBasePos;
210  PRBool mRestarting;
211  PRBool mCompleted;
212  PRBool mDataReceivedSinceStart;
213 
214 
215  /*
216  * Private seekable channel services.
217  */
218 
219  nsresult ReadSegment(
220  nsIInputStream *pStream,
221  PRUint32 numBytes);
222 
223  nsresult InsertSegment(
224  Segment *pSegment);
225 
226  nsresult MergeSegments(
227  Segment *pSegment1,
228  Segment *pSegment2,
229  Segment **ppMergedSegment);
230 
231  PRBool AllDataRead();
232 
233  nsresult Restart(
234  PRUint64 pos);
235 
236  void DumpChannelData();
237 };
238 
239 
240 #endif /* __SEEKABLE_CHANNEL_H__ */
virtual ~sbSeekableChannel()
A seekable wrapper for an nsIChannel.
NS_DECL_ISUPPORTS NS_DECL_SBISEEKABLECHANNEL NS_DECL_NSISTREAMLISTENER NS_DECL_NSIREQUESTOBSERVER NS_DECL_NSICHANNELEVENTSINK NS_DECL_NSIINTERFACEREQUESTOR sbSeekableChannel()
attribute PRUInt64 pos
The current read position (for the read methods)
PRUint32 & offset
const nsIInterfaceRequestor