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 <atomic>
29 #include <string>
30 #include <memory>
31 #include <mutex>
32 
33 #include <boost/intrusive_ptr.hpp>
34 #include <boost/ptr_container/ptr_deque.hpp>
35 
36 #include "PlayHead.h" // for composition
37 #include "Relay.h" // for ActiveRelay inheritance
38 #include <memory>
39 
40 // Forward declarations
41 namespace gnash {
42  class InterruptableVirtualClock;
43  class as_object;
44  class CharacterProxy;
45  class IOChannel;
46  class NetConnection_as;
47  class as_function;
48  class DisplayObject;
49  struct ObjectURI;
50  namespace image {
51  class GnashImage;
52  }
53  namespace media {
54  class MediaHandler;
55  class AudioDecoder;
56  class AudioInfo;
57  class MediaParser;
58  class VideoDecoder;
59  class VideoInfo;
60  }
61  namespace sound {
62  class sound_handler;
63  class InputStream;
64  }
65 }
66 
67 namespace gnash {
68 
70 //
80 public:
81 
86 
88  //
93  {
94  public:
96  :
97  m_size(0),
98  m_data(nullptr),
99  m_ptr(nullptr)
100  {}
101 
103  {
104  delete [] m_data;
105  }
106 
108  std::uint32_t m_size;
109 
111  //
114  std::uint8_t* m_data;
115 
117  std::uint8_t* m_ptr;
118  };
119 
120  typedef boost::ptr_deque<CursoredBuffer> AudioQueue;
121 
122  // Delete all samples in the audio queue.
123  void cleanAudioQueue();
124 
126 
129  AudioQueue _audioQueue;
130 
133 
136  std::mutex _audioQueueMutex;
137 
138  // Id of an attached audio streamer, 0 if none
140 
142  //
146  void attachAuxStreamer();
147 
149  //
153  void detachAuxStreamer();
154 
156  unsigned int fetch(std::int16_t* samples, unsigned int nSamples,
157  bool& eof);
158 
160  static unsigned int fetchWrapper(void* owner, std::int16_t* samples,
161  unsigned int nSamples, bool& eof);
162 
164  //
170  void push(CursoredBuffer* audio);
171 
172 };
173 
174 // -----------------------------------------------------------------
175 
177 //
181 class NetStream_as : public ActiveRelay
182 {
183 
184 public:
185 
186  enum PauseMode {
187  pauseModeToggle = -1,
188  pauseModePause = 0,
189  pauseModeUnPause = 1
190  };
191 
192  NetStream_as(as_object* owner);
193 
194  ~NetStream_as();
195 
197  return _playHead.getState();
198  }
199 
201  //
205  int videoHeight() const;
206 
208  //
212  int videoWidth() const;
213 
216  void close();
217 
219  void setAudioController(DisplayObject* controller);
220 
222  //
225  void pause(PauseMode mode);
226 
228  //
232  void play(const std::string& source);
233 
235  //
240  void seek(std::uint32_t pos);
241 
243  //
246  std::int32_t time();
247 
252  void update();
253 
255  double getCurrentFPS() { return 0; }
256 
258  //
263  _netCon = nc;
264  }
265 
267  bool isConnected() const { return (_netCon); }
268 
271  //
275  void setBufferTime(std::uint32_t time);
276 
279  //
282  std::uint32_t bufferTime() { return _bufferTime; }
283 
285  long bytesLoaded();
286 
288  //
291  long bytesTotal();
292 
295  //
299  long bufferLength();
300 
302  //
306  std::unique_ptr<image::GnashImage> get_video();
307 
310  {
311  _invalidatedVideoCharacter = ch;
312  }
313 
314  virtual void markReachableResources() const;
315 
317  //
323  static unsigned int audio_streamer(void *udata, std::int16_t* samples,
324  unsigned int nSamples, bool& eof);
325 
326 private:
327 
329  enum StatusCode {
330 
331  // Internal status, not a valid ActionScript value
332  invalidStatus,
333 
335  bufferEmpty,
336 
338  bufferFull,
339 
341  bufferFlush,
342 
344  playStart,
345 
347  playStop,
348 
350  seekNotify,
351 
353  streamNotFound,
354 
356  invalidTime
357  };
358 
359  enum DecodingState {
360  DEC_NONE,
361  DEC_STOPPED,
362  DEC_DECODING,
363  DEC_BUFFERING
364  };
365 
366  typedef std::pair<std::string, std::string> NetStreamStatus;
367 
369  //
373  void getStatusCodeInfo(StatusCode code, NetStreamStatus& info);
374 
376  as_object* getStatusObject(StatusCode code);
377 
379  //
382  void initVideoDecoder(const media::VideoInfo& info);
383 
385  //
388  void initAudioDecoder(const media::AudioInfo& parser);
389 
390  // Setups the playback
391  bool startPlayback();
392 
393  // Pauses the playhead
394  //
395  // Users:
396  // - ::decodeFLVFrame()
397  // - ::pause()
398  // - ::play()
399  //
400  void pausePlayback();
401 
402  // Resumes the playback
403  //
404  // Users:
405  // - ::av_streamer()
406  // - ::play()
407  // - ::startPlayback()
408  // - ::advance()
409  //
410  void unpausePlayback();
411 
413  //
426  void refreshVideoFrame(bool alsoIfPaused = false);
427 
430  void refreshAudioBuffer();
431 
433  //
436  std::unique_ptr<image::GnashImage> decodeNextVideoFrame();
437 
439  //
442  BufferedAudioStreamer::CursoredBuffer* decodeNextAudioFrame();
443 
447  void pushDecodedAudioFrames(std::uint32_t ts);
448 
450  //
459  std::unique_ptr<image::GnashImage> getDecodedVideoFrame(std::uint32_t ts);
460 
461  DecodingState decodingStatus(DecodingState newstate = DEC_NONE);
462 
466  void parseNextChunk();
467 
469  //
486  void setStatus(StatusCode code);
487 
491  //
498  void processStatusNotifications();
499 
501  void stopAdvanceTimer();
502 
504  void startAdvanceTimer();
505 
506  NetConnection_as* _netCon;
507 
508  std::unique_ptr<CharacterProxy> _audioController;
509 
510  // The size of the buffer in milliseconds
511  std::uint32_t _bufferTime;
512 
513  // Mutex to insure we don't corrupt the image
514  std::mutex image_mutex;
515 
516  // The image/videoframe which is given to the renderer
517  std::unique_ptr<image::GnashImage> _imageframe;
518 
519  // The video URL
520  std::string url;
521 
522  // The input media parser
523  std::unique_ptr<media::MediaParser> _parser;
524 
525  // The position in the inputfile, only used when not playing a FLV
526  long _inputPos;
527 
529  DisplayObject* _invalidatedVideoCharacter;
530 
532  std::atomic<int> _decoding_state;
533 
535  std::unique_ptr<media::VideoDecoder> _videoDecoder;
536 
538  bool _videoInfoKnown;
539 
541  std::unique_ptr<media::AudioDecoder> _audioDecoder;
542 
544  bool _audioInfoKnown;
545 
547  std::unique_ptr<InterruptableVirtualClock> _playbackClock;
548 
550  PlayHead _playHead;
551 
552  // Current sound handler
553  sound::sound_handler* _soundHandler;
554 
555  // Current media handler
556  media::MediaHandler* _mediaHandler;
557 
559  //
563  std::unique_ptr<IOChannel> _inputStream;
564 
566  BufferedAudioStreamer _audioStreamer;
567 
569  //
571  std::atomic<int> _statusCode;
572 };
573 
574 void netstream_class_init(as_object& global, const ObjectURI& uri);
575 
576 void registerNetStreamNative(as_object& global);
577 
578 } // gnash namespace
579 
580 #endif
581 
A sound input stream.
Definition: InputStream.h:47
sound::sound_handler * _soundHandler
Definition: NetStream_as.h:125
AudioQueue _audioQueue
Definition: NetStream_as.h:129
void registerNetStreamNative(as_object &global)
Definition: NetStream_as.cpp:139
Definition: klash_part.cpp:329
NetStream_as ActionScript class.
Definition: NetStream_as.h:181
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
std::mutex _audioQueueMutex
Definition: NetStream_as.h:136
std::uint32_t bufferTime()
Definition: NetStream_as.h:282
std::uint32_t ts
Definition: LocalConnection_as.cpp:150
bool isConnected() const
Return true if the NetStream has an associated NetConnection.
Definition: NetStream_as.h:267
Information about a video stream.
Definition: MediaParser.h:322
uri
Definition: test.py:12
Sound mixer.
Definition: sound_handler.h:87
~CursoredBuffer()
Definition: NetStream_as.h:102
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
boost::ptr_deque< CursoredBuffer > AudioQueue
Definition: NetStream_as.h:120
The base class for all ActionScript objects.
Definition: as_object.h:161
NetConnection ActionScript class.
Definition: NetConnection_as.h:45
void setNetCon(NetConnection_as *nc)
Sets the NetConnection needed to access external files.
Definition: NetStream_as.h:262
A URI for describing as_objects.
Definition: ObjectURI.h:44
Definition: klash_part.cpp:330
code
Definition: GnashKey.h:43
The MediaHandler class acts as a factory to provide parser and decoders.
Definition: MediaHandler.h:69
double getCurrentFPS()
Returns the current framerate in frames per second.
Definition: NetStream_as.h:255
The playback controller.
Definition: PlayHead.h:34
std::uint8_t * m_data
Actual data.
Definition: NetStream_as.h:114
CursoredBuffer()
Definition: NetStream_as.h:95
std::uint32_t m_size
Number of samples left in buffer starting from cursor.
Definition: NetStream_as.h:108
std::uint8_t * m_ptr
Cursor into the data.
Definition: NetStream_as.h:117
Information about an audio stream.
Definition: MediaParser.h:236
PauseMode
Definition: NetStream_as.h:186
Buffered AudioStreamer.
Definition: NetStream_as.h:79
PlayHead::PlaybackStatus playbackState() const
Definition: NetStream_as.h:196
PlaybackStatus
Flags for playback state.
Definition: PlayHead.h:39
A buffer with a cursor state.
Definition: NetStream_as.h:92
std::string url
Definition: gnash.cpp:59
void setInvalidatedVideo(DisplayObject *ch)
Register the DisplayObject to invalidate on video updates.
Definition: NetStream_as.h:309
size_t _audioQueueSize
Number of bytes in the audio queue, protected by _audioQueueMutex.
Definition: NetStream_as.h:132
sound::InputStream * _auxStreamer
Definition: NetStream_as.h:139
A native type that requires periodic updates from the core (movie_root).
Definition: Relay.h:79
void netstream_class_init(as_object &where, const ObjectURI &uri)
Definition: NetStream_as.cpp:130