Gnash  0.8.11dev
NetStream_as.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 
20 #ifndef GNASH_NETSTREAM_H
21 #define GNASH_NETSTREAM_H
22 
23 
24 #ifndef __STDC_CONSTANT_MACROS
25 #define __STDC_CONSTANT_MACROS
26 #endif
27 
28 #include <boost/intrusive_ptr.hpp>
29 #include <string>
30 #include <boost/ptr_container/ptr_deque.hpp>
31 #include <boost/scoped_ptr.hpp>
32 #include <boost/thread/mutex.hpp>
33 
34 #include "PlayHead.h" // for composition
35 #include "Relay.h" // for ActiveRelay inheritance
36 #include <memory>
37 
38 // Forward declarations
39 namespace gnash {
40  class InterruptableVirtualClock;
41  class as_object;
42  class CharacterProxy;
43  class IOChannel;
44  class NetConnection_as;
45  class as_function;
46  class DisplayObject;
47  struct ObjectURI;
48  namespace image {
49  class GnashImage;
50  }
51  namespace media {
52  class MediaHandler;
53  class AudioDecoder;
54  class AudioInfo;
55  class MediaParser;
56  class VideoDecoder;
57  class VideoInfo;
58  }
59  namespace sound {
60  class sound_handler;
61  class InputStream;
62  }
63 }
64 
65 namespace gnash {
66 
68 //
78 public:
79 
84 
86  //
91  {
92  public:
94  :
95  m_size(0),
96  m_data(NULL),
97  m_ptr(NULL)
98  {}
99 
101  {
102  delete [] m_data;
103  }
104 
106  boost::uint32_t m_size;
107 
109  //
112  boost::uint8_t* m_data;
113 
115  boost::uint8_t* m_ptr;
116  };
117 
118  typedef boost::ptr_deque<CursoredBuffer> AudioQueue;
119 
120  // Delete all samples in the audio queue.
121  void cleanAudioQueue();
122 
124 
127  AudioQueue _audioQueue;
128 
131 
134  boost::mutex _audioQueueMutex;
135 
136  // Id of an attached audio streamer, 0 if none
138 
140  //
144  void attachAuxStreamer();
145 
147  //
151  void detachAuxStreamer();
152 
154  unsigned int fetch(boost::int16_t* samples, unsigned int nSamples,
155  bool& eof);
156 
158  static unsigned int fetchWrapper(void* owner, boost::int16_t* samples,
159  unsigned int nSamples, bool& eof);
160 
162  //
168  void push(CursoredBuffer* audio);
169 
170 };
171 
172 // -----------------------------------------------------------------
173 
175 //
179 class NetStream_as : public ActiveRelay
180 {
181 
182 public:
183 
184  enum PauseMode {
188  };
189 
191 
192  ~NetStream_as();
193 
195  return _playHead.getState();
196  }
197 
199  //
203  int videoHeight() const;
204 
206  //
210  int videoWidth() const;
211 
214  void close();
215 
217  void setAudioController(DisplayObject* controller);
218 
220  //
223  void pause(PauseMode mode);
224 
226  //
230  void play(const std::string& source);
231 
233  //
238  void seek(boost::uint32_t pos);
239 
241  //
244  boost::int32_t time();
245 
250  void update();
251 
253  double getCurrentFPS() { return 0; }
254 
256  //
261  _netCon = nc;
262  }
263 
265  bool isConnected() const { return (_netCon); }
266 
269  //
273  void setBufferTime(boost::uint32_t time);
274 
277  //
280  boost::uint32_t bufferTime() { return _bufferTime; }
281 
283  long bytesLoaded();
284 
286  //
289  long bytesTotal();
290 
293  //
297  long bufferLength();
298 
300  //
304  std::auto_ptr<image::GnashImage> get_video();
305 
308  {
309  _invalidatedVideoCharacter = ch;
310  }
311 
312  virtual void markReachableResources() const;
313 
315  //
321  static unsigned int audio_streamer(void *udata, boost::int16_t* samples,
322  unsigned int nSamples, bool& eof);
323 
324 private:
325 
327  enum StatusCode {
328 
329  // Internal status, not a valid ActionScript value
330  invalidStatus,
331 
333  bufferEmpty,
334 
336  bufferFull,
337 
339  bufferFlush,
340 
342  playStart,
343 
345  playStop,
346 
348  seekNotify,
349 
351  streamNotFound,
352 
354  invalidTime
355  };
356 
357  enum DecodingState {
358  DEC_NONE,
359  DEC_STOPPED,
360  DEC_DECODING,
361  DEC_BUFFERING
362  };
363 
364  typedef std::pair<std::string, std::string> NetStreamStatus;
365 
367  //
371  void getStatusCodeInfo(StatusCode code, NetStreamStatus& info);
372 
374  as_object* getStatusObject(StatusCode code);
375 
377  //
380  void initVideoDecoder(const media::VideoInfo& info);
381 
383  //
386  void initAudioDecoder(const media::AudioInfo& parser);
387 
388  // Setups the playback
389  bool startPlayback();
390 
391  // Pauses the playhead
392  //
393  // Users:
394  // - ::decodeFLVFrame()
395  // - ::pause()
396  // - ::play()
397  //
398  void pausePlayback();
399 
400  // Resumes the playback
401  //
402  // Users:
403  // - ::av_streamer()
404  // - ::play()
405  // - ::startPlayback()
406  // - ::advance()
407  //
408  void unpausePlayback();
409 
411  //
424  void refreshVideoFrame(bool alsoIfPaused = false);
425 
428  void refreshAudioBuffer();
429 
431  //
434  std::auto_ptr<image::GnashImage> decodeNextVideoFrame();
435 
437  //
440  BufferedAudioStreamer::CursoredBuffer* decodeNextAudioFrame();
441 
445  void pushDecodedAudioFrames(boost::uint32_t ts);
446 
448  //
457  std::auto_ptr<image::GnashImage> getDecodedVideoFrame(boost::uint32_t ts);
458 
459  DecodingState decodingStatus(DecodingState newstate = DEC_NONE);
460 
464  void parseNextChunk();
465 
467  //
484  void setStatus(StatusCode code);
485 
489  //
496  void processStatusNotifications();
497 
499  void stopAdvanceTimer();
500 
502  void startAdvanceTimer();
503 
504  NetConnection_as* _netCon;
505 
506  boost::scoped_ptr<CharacterProxy> _audioController;
507 
508  // The size of the buffer in milliseconds
509  boost::uint32_t _bufferTime;
510 
511  // Mutex to insure we don't corrupt the image
512  boost::mutex image_mutex;
513 
514  // The image/videoframe which is given to the renderer
515  std::auto_ptr<image::GnashImage> _imageframe;
516 
517  // The video URL
518  std::string url;
519 
520  // The input media parser
521  std::auto_ptr<media::MediaParser> _parser;
522 
523  // The position in the inputfile, only used when not playing a FLV
524  long _inputPos;
525 
527  DisplayObject* _invalidatedVideoCharacter;
528 
529  DecodingState _decoding_state;
530 
531  // Mutex protecting _playback_state and _decoding_state
532  // (not sure a single one is appropriate)
533  boost::mutex _state_mutex;
534 
536  std::auto_ptr<media::VideoDecoder> _videoDecoder;
537 
539  bool _videoInfoKnown;
540 
542  std::auto_ptr<media::AudioDecoder> _audioDecoder;
543 
545  bool _audioInfoKnown;
546 
548  boost::scoped_ptr<InterruptableVirtualClock> _playbackClock;
549 
551  PlayHead _playHead;
552 
553  // Current sound handler
554  sound::sound_handler* _soundHandler;
555 
556  // Current media handler
557  media::MediaHandler* _mediaHandler;
558 
560  //
564  std::auto_ptr<IOChannel> _inputStream;
565 
567  BufferedAudioStreamer _audioStreamer;
568 
570  StatusCode _statusCode;
571 
573  boost::mutex _statusMutex;
574 
575 };
576 
577 void netstream_class_init(as_object& global, const ObjectURI& uri);
578 
579 void registerNetStreamNative(as_object& global);
580 
581 } // gnash namespace
582 
583 #endif
584 
A sound input stream.
Definition: InputStream.h:47
void detachAuxStreamer()
Detach the aux streamer.
Definition: NetStream_as.cpp:1470
sound::sound_handler * _soundHandler
Definition: NetStream_as.h:123
boost::uint32_t ts
Definition: LocalConnection_as.cpp:152
int videoHeight() const
Get the real height of the video in pixels if the decoder exists.
Definition: NetStream_as.cpp:1190
AudioQueue _audioQueue
Definition: NetStream_as.h:127
~NetStream_as()
Definition: NetStream_as.cpp:305
PlaybackStatus getState() const
Get current playback state.
Definition: PlayHead.h:80
Definition: NetStream_as.h:187
void registerNetStreamNative(as_object &global)
Definition: NetStream_as.cpp:139
void update()
Definition: NetStream_as.cpp:1205
Definition: klash_part.cpp:329
NetStream_as ActionScript class.
Definition: NetStream_as.h:179
boost::uint32_t bufferTime()
Definition: NetStream_as.h:280
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
long bufferLength()
Definition: NetStream_as.cpp:196
void attachAuxStreamer()
Attach the aux streamer.
Definition: NetStream_as.cpp:1448
void close()
Definition: NetStream_as.cpp:337
static unsigned int audio_streamer(void *udata, boost::int16_t *samples, unsigned int nSamples, bool &eof)
Callback used by sound_handler to get audio data.
long bytesLoaded()
Returns the number of bytes of the media file that have been buffered.
Definition: NetStream_as.cpp:1412
Sound mixer.
Definition: sound_handler.h:87
~CursoredBuffer()
Definition: NetStream_as.h:100
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
boost::ptr_deque< CursoredBuffer > AudioQueue
Definition: NetStream_as.h:118
bool isConnected() const
Return true if the NetStream has an associated NetConnection.
Definition: NetStream_as.h:265
boost::uint8_t * m_ptr
Cursor into the data.
Definition: NetStream_as.h:115
virtual void markReachableResources() const
Mark any reachable resources other than the owner.
Definition: NetStream_as.cpp:286
The base class for all ActionScript objects.
Definition: as_object.h:161
NetConnection ActionScript class.
Definition: NetConnection_as.h:46
void setNetCon(NetConnection_as *nc)
Sets the NetConnection needed to access external files.
Definition: NetStream_as.h:260
BufferedAudioStreamer(sound::sound_handler *handler)
Definition: NetStream_as.cpp:1493
void cleanAudioQueue()
Definition: NetStream_as.cpp:1571
code
Definition: GnashKey.h:43
void setBufferTime(boost::uint32_t time)
Definition: NetStream_as.cpp:188
static unsigned int fetchWrapper(void *owner, boost::int16_t *samples, unsigned int nSamples, bool &eof)
Fetch samples from the audio queue.
Definition: NetStream_as.cpp:1484
void pause(PauseMode mode)
Pauses/starts the playback of the media played by the current instance.
Definition: NetStream_as.cpp:313
boost::uint8_t * m_data
Actual data.
Definition: NetStream_as.h:112
double getCurrentFPS()
Returns the current framerate in frames per second.
Definition: NetStream_as.h:253
long bytesTotal()
Returns the total number of bytes (size) of the media file.
Definition: NetStream_as.cpp:1423
boost::uint32_t m_size
Number of samples left in buffer starting from cursor.
Definition: NetStream_as.h:106
boost::int32_t time()
Tells where the playhead currently is.
Definition: NetStream_as.cpp:1376
CursoredBuffer()
Definition: NetStream_as.h:93
void setAudioController(DisplayObject *controller)
Make audio controlled by given DisplayObject.
Definition: NetStream_as.cpp:280
as_object & owner() const
Return the as_object that this Relay is attached to.
Definition: Relay.h:108
boost::mutex _audioQueueMutex
Definition: NetStream_as.h:134
void seek(boost::uint32_t pos)
Seek in the media played by the current instance.
Definition: NetStream_as.cpp:724
PauseMode
Definition: NetStream_as.h:184
Definition: NetStream_as.h:186
Buffered AudioStreamer.
Definition: NetStream_as.h:77
int videoWidth() const
Get the real width of the video in pixels if the decoder exists.
Definition: NetStream_as.cpp:1197
void play(const std::string &source)
Starts the playback of the media.
Definition: NetStream_as.cpp:361
PlaybackStatus
Flags for playback state.
Definition: PlayHead.h:39
A buffer with a cursor state.
Definition: NetStream_as.h:90
Definition: NetStream_as.h:185
std::auto_ptr< image::GnashImage > get_video()
Returns the video frame closest to current cursor. See time().
Definition: NetStream_as.cpp:205
void setInvalidatedVideo(DisplayObject *ch)
Register the DisplayObject to invalidate on video updates.
Definition: NetStream_as.h:307
size_t _audioQueueSize
Number of bytes in the audio queue, protected by _audioQueueMutex.
Definition: NetStream_as.h:130
sound::InputStream * _auxStreamer
Definition: NetStream_as.h:137
NetStream_as(as_object *owner)
Contruct a NetStream object.
Definition: NetStream_as.cpp:100
A native type that requires periodic updates from the core (movie_root).
Definition: Relay.h:79
void push(CursoredBuffer *audio)
Push a buffer to the audio queue.
Definition: NetStream_as.cpp:1553
PlayHead::PlaybackStatus playbackState() const
Definition: NetStream_as.h:194
void netstream_class_init(as_object &where, const ObjectURI &uri)
Definition: NetStream_as.cpp:130
unsigned int fetch(boost::int16_t *samples, unsigned int nSamples, bool &eof)
Fetch samples from the audio queue.
Definition: NetStream_as.cpp:1503