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 
33 #include "MediaParser.h"
34 #include "PlayHead.h" // for composition
35 #include "VideoDecoder.h" // for visibility of dtor
36 #include "AudioDecoder.h" // for visibility of dtor
37 #include "VirtualClock.h"
38 #include "Relay.h" // for ActiveRelay inheritance
39 
40 // Forward declarations
41 namespace gnash {
42  class CharacterProxy;
43  class IOChannel;
44  class NetConnection_as;
45  class as_function;
46  class DisplayObject;
47  struct ObjectURI;
48  namespace media {
49  class MediaHandler;
50  }
51  namespace sound {
52  class sound_handler;
53  class InputStream;
54  }
55 }
56 
57 namespace gnash {
58 
60 //
70 public:
71 
76 
78  //
83  {
84  public:
86  :
87  m_size(0),
88  m_data(NULL),
89  m_ptr(NULL)
90  {}
91 
93  {
94  delete [] m_data;
95  }
96 
98  boost::uint32_t m_size;
99 
101  //
104  boost::uint8_t* m_data;
105 
107  boost::uint8_t* m_ptr;
108  };
109 
110  typedef boost::ptr_deque<CursoredBuffer> AudioQueue;
111 
112  // Delete all samples in the audio queue.
113  void cleanAudioQueue();
114 
116 
120 
123 
126  boost::mutex _audioQueueMutex;
127 
128  // Id of an attached audio streamer, 0 if none
130 
132  //
136  void attachAuxStreamer();
137 
139  //
143  void detachAuxStreamer();
144 
146  unsigned int fetch(boost::int16_t* samples, unsigned int nSamples,
147  bool& eof);
148 
150  static unsigned int fetchWrapper(void* owner, boost::int16_t* samples,
151  unsigned int nSamples, bool& eof);
152 
154  //
160  void push(CursoredBuffer* audio);
161 
162 };
163 
164 // -----------------------------------------------------------------
165 
167 //
171 class NetStream_as : public ActiveRelay
172 {
173 
174 public:
175 
176  enum PauseMode {
180  };
181 
183 
184  ~NetStream_as();
185 
187  return _playHead.getState();
188  }
189 
191  //
195  int videoHeight() const;
196 
198  //
202  int videoWidth() const;
203 
206  void close();
207 
209  void setAudioController(DisplayObject* controller);
210 
212  //
215  void pause(PauseMode mode);
216 
218  //
222  void play(const std::string& source);
223 
225  //
230  void seek(boost::uint32_t pos);
231 
233  //
236  boost::int32_t time();
237 
242  void update();
243 
245  double getCurrentFPS() { return 0; }
246 
248  //
253  _netCon = nc;
254  }
255 
257  bool isConnected() const { return (_netCon); }
258 
261  //
265  void setBufferTime(boost::uint32_t time);
266 
269  //
272  boost::uint32_t bufferTime() { return _bufferTime; }
273 
275  long bytesLoaded();
276 
278  //
281  long bytesTotal();
282 
285  //
289  long bufferLength();
290 
292  //
296  std::auto_ptr<image::GnashImage> get_video();
297 
300  {
301  _invalidatedVideoCharacter = ch;
302  }
303 
304  virtual void markReachableResources() const;
305 
307  //
313  static unsigned int audio_streamer(void *udata, boost::int16_t* samples,
314  unsigned int nSamples, bool& eof);
315 
316 private:
317 
319  enum StatusCode {
320 
321  // Internal status, not a valid ActionScript value
322  invalidStatus,
323 
325  bufferEmpty,
326 
328  bufferFull,
329 
331  bufferFlush,
332 
334  playStart,
335 
337  playStop,
338 
340  seekNotify,
341 
343  streamNotFound,
344 
346  invalidTime
347  };
348 
349  enum DecodingState {
350  DEC_NONE,
351  DEC_STOPPED,
352  DEC_DECODING,
353  DEC_BUFFERING
354  };
355 
356  typedef std::pair<std::string, std::string> NetStreamStatus;
357 
359  //
363  void getStatusCodeInfo(StatusCode code, NetStreamStatus& info);
364 
366  as_object* getStatusObject(StatusCode code);
367 
369  //
372  void initVideoDecoder(const media::VideoInfo& info);
373 
375  //
378  void initAudioDecoder(const media::AudioInfo& parser);
379 
380  // Setups the playback
381  bool startPlayback();
382 
383  // Pauses the playhead
384  //
385  // Users:
386  // - ::decodeFLVFrame()
387  // - ::pause()
388  // - ::play()
389  //
390  void pausePlayback();
391 
392  // Resumes the playback
393  //
394  // Users:
395  // - ::av_streamer()
396  // - ::play()
397  // - ::startPlayback()
398  // - ::advance()
399  //
400  void unpausePlayback();
401 
403  //
416  void refreshVideoFrame(bool alsoIfPaused = false);
417 
420  void refreshAudioBuffer();
421 
423  //
426  std::auto_ptr<image::GnashImage> decodeNextVideoFrame();
427 
429  //
432  BufferedAudioStreamer::CursoredBuffer* decodeNextAudioFrame();
433 
437  void pushDecodedAudioFrames(boost::uint32_t ts);
438 
440  //
449  std::auto_ptr<image::GnashImage> getDecodedVideoFrame(boost::uint32_t ts);
450 
451  DecodingState decodingStatus(DecodingState newstate = DEC_NONE);
452 
456  void parseNextChunk();
457 
459  //
476  void setStatus(StatusCode code);
477 
481  //
488  void processStatusNotifications();
489 
491  void stopAdvanceTimer();
492 
494  void startAdvanceTimer();
495 
496  NetConnection_as* _netCon;
497 
498  boost::scoped_ptr<CharacterProxy> _audioController;
499 
500  // The size of the buffer in milliseconds
501  boost::uint32_t _bufferTime;
502 
503  // Mutex to insure we don't corrupt the image
504  boost::mutex image_mutex;
505 
506  // The image/videoframe which is given to the renderer
507  std::auto_ptr<image::GnashImage> _imageframe;
508 
509  // The video URL
510  std::string url;
511 
512  // The input media parser
513  std::auto_ptr<media::MediaParser> _parser;
514 
515  // The position in the inputfile, only used when not playing a FLV
516  long _inputPos;
517 
519  DisplayObject* _invalidatedVideoCharacter;
520 
521  DecodingState _decoding_state;
522 
523  // Mutex protecting _playback_state and _decoding_state
524  // (not sure a single one is appropriate)
525  boost::mutex _state_mutex;
526 
528  std::auto_ptr<media::VideoDecoder> _videoDecoder;
529 
531  bool _videoInfoKnown;
532 
534  std::auto_ptr<media::AudioDecoder> _audioDecoder;
535 
537  bool _audioInfoKnown;
538 
540  boost::scoped_ptr<InterruptableVirtualClock> _playbackClock;
541 
543  PlayHead _playHead;
544 
545  // Current sound handler
546  sound::sound_handler* _soundHandler;
547 
548  // Current media handler
549  media::MediaHandler* _mediaHandler;
550 
552  //
556  std::auto_ptr<IOChannel> _inputStream;
557 
559  BufferedAudioStreamer _audioStreamer;
560 
562  StatusCode _statusCode;
563 
565  boost::mutex _statusMutex;
566 
567 };
568 
569 void netstream_class_init(as_object& global, const ObjectURI& uri);
570 
571 void registerNetStreamNative(as_object& global);
572 
573 } // gnash namespace
574 
575 #endif
576