2
// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 3 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
#ifndef __DISKSTREAM_H__
20
#define __DISKSTREAM_H__
23
#include "gnashconfig.h"
33
#include "statistics.h"
34
#include "getclocktime.hpp"
36
#include <boost/scoped_ptr.hpp>
39
/// This is the main namespace for Gnash and it's libraries.
43
/// This class handles the loading of files into memory. Instead
44
/// of using read() from the standard library, this uses mmap() to
45
/// map the file into memory in chunks of the memory pagesize,
46
/// which is much faster and less resource intensive.
47
class DSOEXPORT DiskStream {
49
/// \enum DiskStream::state_e
50
/// This represents the state of the current stream.
89
DSOEXPORT DiskStream();
90
DSOEXPORT DiskStream(const std::string &filespec);
91
DSOEXPORT DiskStream(const std::string &filespec, cygnal::Buffer &buf);
92
DSOEXPORT DiskStream(const std::string &filespec, boost::uint8_t *data, size_t size);
93
DSOEXPORT DiskStream(const std::string &filespec, int netfd);
94
DSOEXPORT ~DiskStream();
96
/// \brief Close the open disk file and it's associated stream.
97
DSOEXPORT void close();
99
/// \brief Open a file to be streamed.
101
/// @param filespec The full path and file name for the data to be
102
/// read. The file must already exist.
104
/// @param netfd An optional file descriptor to read data from
106
/// @param statistics The optional data structure to use for
107
/// collecting statistics on this stream.
109
/// @return True if the file was opened sucessfully, false if not.
110
DSOEXPORT bool open(const std::string &filespec);
111
DSOEXPORT bool open(const std::string &filespec, int netfd);
112
DSOEXPORT bool open(const std::string &filespec, int netfd, gnash::Statistics &statistics);
114
/// \brief Stream the file that has been loaded,
116
/// @param netfd An optional file descriptor to read data from
118
/// @param flag True to play the entire file, false to play part.
120
/// @return True if the data was streamed sucessfully, false if not.
122
bool play(bool flag);
123
bool play(int netfd, bool flag);
125
/// \brief Stream a preview of the file.
126
/// A preview is a series of video frames from
127
/// the video file. Each video frame is taken by sampling
128
/// the file at a set interval.
130
/// @param filespec The full path and file name for the data to be
133
/// @param quantity The number of frames to stream..
135
/// @return True if the thumbnails were streamed sucessfully, false if not.
136
bool preview(const std::string &filespec, int frames);
138
/// \brief Stream a series of thumbnails.
139
/// A thumbnail is a series of jpg images of frames from
140
/// the video file instead of video frames. Each thumbnail
141
/// is taken by sampling the file at a set interval.
143
/// @param filespec The full path and file name for the data to be
146
/// @param quantity The number of thumbnails to stream..
148
/// @return True if the thumbnails were streamed sucessfully, false if not.
149
bool thumbnail(const std::string &filespec, int quantity);
151
/// \brief Pause the stream currently being played.
153
/// @return True if the stream was paused sucessfully, false if not.
156
/// \brief Seek within the stream.
158
/// @param the offset in bytes to the location within the file to
161
/// @return A real pointer to the location of the data seeked to.
162
boost::uint8_t * seek(off_t offset);
164
/// \brief Upload a file into a sandbox.
165
/// The sandbox is an area where uploaded files can get
166
/// written to safely. For SWF content, the file name also
167
/// includes a few optional paths used to seperate
168
/// applications from each other.
170
/// @param filespec The file name for the data to be written.
172
/// @return True if the file was uploaded sucessfully, false if not.
173
bool upload(const std::string &filespec);
175
// Stream a single "real-time" source.
176
bool multicast(const std::string &filespec);
178
/// \brief Load a chunk of the file into memory
179
/// This offset must be a multipe of the pagesize.
181
/// @param size The amount of bytes to read, often the filesize
182
/// for smaller files below CACHE_LIMIT.
184
/// @param offset The location in bytes in the file of the desired data.
186
/// @return A real pointer to the location of the data at the
187
/// location pointed to by the offset.
188
DSOEXPORT boost::uint8_t *loadToMem(size_t filesize, off_t offset);
189
DSOEXPORT boost::uint8_t *loadToMem(off_t offset);
190
DSOEXPORT boost::uint8_t *loadToMem() { return loadToMem(_offset); };
192
/// \brief Write the data in memory to disk
194
/// @param filespec The relative path to the file to write, which goes in
195
/// a safebox for storage.
197
/// @param data The data to be written
199
/// @param size The amount of data in bytes to be written
201
/// @return true if the operation suceeded, false if it failed.
202
DSOEXPORT bool writeToDisk(const std::string &filespec, boost::uint8_t *data, size_t size);
203
DSOEXPORT bool writeToDisk(const std::string &filespec, cygnal::Buffer &data);
204
DSOEXPORT bool writeToDisk(const std::string &filespec);
205
DSOEXPORT bool writeToDisk();
207
/// \brief Write the existing data to the Network.
209
/// @return true is the write suceeded, false if it failed.
210
bool writeToNet(int start, int bytes);
212
/// \brief Get the memory page size
213
/// This is a cached value of the system configuration
214
/// value for the default size in bytes of a memory page.
216
/// @return the currently cached memory page size.
217
size_t getPagesize() { return _pagesize; };
219
/// \brief Set the memory page size
220
/// This is a cached value of the system configuration
221
/// value for the default size in bytes of a memory page.
223
/// @param size The size of a page of memory to cache.
226
void setPagesize(size_t size) { _pagesize = size; };
228
/// \brief copy another DiskStream into ourselves, so they share data
230
DiskStream &operator=(DiskStream *stream);
232
/// \brief Dump the internal data of this class in a human readable form.
233
/// @remarks This should only be used for debugging purposes.
235
// friend std::ostream& operator<< (std::ostream &os, const DiskStream &ds);
237
/// \brief Get the base address for the memory page.
239
/// @return A real pointer to the base address data in the file, but after
240
/// the header bytes.
241
boost::uint8_t *get() { return _dataptr; };
242
bool fullyPopulated();
243
// bool fullyPopulated() { return ((_seekptr - _filesize) == _dataptr); };
245
/// \brief Get the size of the file.
247
/// @return A value that is the size of the file in bytes.
248
size_t getFileSize() { return _filesize; };
250
DiskStream::filetype_e getFileType() { return _filetype; };
252
std::string &getFilespec() { return _filespec; }
253
void setFilespec(std::string filespec) { _filespec = filespec; }
255
/// \brief Get the time of the last access.
257
/// @return A real pointer to the struct timespec of the last access.
258
struct timespec *getLastAccessTime() { return &_last_access; };
260
state_e getState() { return _state; };
261
void setState(state_e state) { _state = state; };
263
int getFileFd() { return _filefd; };
264
int getNetFd() { return _netfd; };
266
#ifdef USE_STATS_CACHE
267
/// \brief Get the time of the first access.
268
/// This function should only be used for debugging and
269
/// performance testing.
271
/// @return A real pointer to the struct timespec of the last access.
272
struct timespec *getFirstAccessTime() { return &_first_access; };
273
size_t getAccessCount() { return _accesses; };
276
/// \brief Dump the internal data of this class in a human readable form.
277
/// @remarks This should only be used for debugging purposes.
278
void dump() const { dump(std::cerr); }
279
/// \overload dump(std::ostream& os) const
280
void dump(std::ostream& os) const;
283
/// \var DiskStream::_state
284
/// The current status of the stream while streaming.
287
/// \var DiskStream::_filefd
288
/// The file descriptor of the disk file.
291
/// \var DiskStream::_netfd
292
/// The file descriptor of the network connection.
295
/// \var DiskStream::_filespec
296
/// The path and file name of the disk file to stream.
297
std::string _filespec;
299
/// \var DiskStream::_statistics
300
/// The data structure for collecting statistical
302
gnash::Statistics _statistics;
304
/// \var DiskStream::_dataptr
305
/// The base address of the memory page.
306
boost::uint8_t *_dataptr;
308
/// \var DiskStream::_max_memload
309
/// The maximum amount of data to load into memory
312
/// \var DiskStream::_seekptr
313
/// The current location within the current memory page where
314
/// the data ends, which is in increments of the empry page size.
315
boost::uint8_t *_seekptr;
317
/// \var DiskStream::_filesize
318
/// The size of the disk file to stream.
321
/// \var DiskStream::_pagesize
322
/// The memory page size.
325
/// \var DiskStream::_offset
326
/// The offset within the file of the current memory
330
/// \brief An internal routine used to extract the type of file.
332
/// @param filespec An optional filename to extract the type from.
334
/// @return an AMF filetype
335
filetype_e determineFileType();
336
filetype_e determineFileType( boost::uint8_t *data);
337
filetype_e determineFileType(const std::string &filespec);
339
// Get the file stats, so we know how to set the
340
// Content-Length in the header.
341
bool getFileStats(const std::string &filespec);
342
DiskStream::filetype_e _filetype;
344
struct timespec _last_access;
346
#ifdef USE_STATS_CACHE
347
struct timespec _first_access; // used for timing how long data stays in the queue.
351
#ifdef USE_STATS_FILE
355
// The header, tag, and onMetaData from the FLV file.
356
boost::shared_ptr<cygnal::Flv> _flv;
359
/// \brief Dump to the specified output stream.
360
inline std::ostream& operator << (std::ostream& os, const DiskStream& ds)
366
} // end of cygnal namespace
368
#endif // __DISKSTREAM_H__
372
// indent-tabs-mode: t