Gnash  0.8.11dev
SWFMovieDefinition.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 // The SWFMovieDefinition is the 'root' definition of a SWF movie, including
20 // movies loaded into another SWF file. Each self-contained SWF file has exactly
21 // one SWFMovieDefinition.
22 
23 #ifndef GNASH_SWF_MOVIE_DEFINITION_H
24 #define GNASH_SWF_MOVIE_DEFINITION_H
25 
26 #ifdef HAVE_CONFIG_H
27 #include "gnashconfig.h" // for USE_SWFTREE
28 #endif
29 
30 #include <boost/intrusive_ptr.hpp>
31 #include <vector>
32 #include <map>
33 #include <set>
34 #include <string>
35 #include <memory>
36 #include <boost/thread/thread.hpp>
37 #include <boost/thread/condition.hpp>
38 #include <boost/thread/barrier.hpp>
39 #include <boost/scoped_ptr.hpp>
40 
41 #include "movie_definition.h" // for inheritance
42 #include "DefinitionTag.h" // for boost::intrusive_ptr visibility of dtor
43 #include "StringPredicates.h"
44 #include "SWFRect.h"
45 #include "GnashNumeric.h"
46 #include "dsodefs.h" // for DSOTEXPORT
47 
48 // Forward declarations
49 namespace gnash {
50  namespace image {
51  class JpegInput;
52  }
53  class IOChannel;
54  class SWFMovieDefinition;
55  class SWFStream;
56  class movie_root;
57  class MovieClip;
58  class SWFMovie;
59  class RunResources;
60  class Font;
61 }
62 
63 namespace gnash {
64 
67 {
68 public:
69 
71 
73 
75  //
80  bool start();
81 
83  bool started() const;
84 
86  bool isSelfThread() const;
87 
88 private:
89 
90  SWFMovieDefinition& _movie_def;
91 
92  mutable boost::mutex _mutex;
93  std::auto_ptr<boost::thread> _thread;
94 
95  // Barrier to ensure that _thread
96  // is initialized before the loader thread
97  // continues execution
98  boost::barrier _barrier;
99 
101  static void execute(SWFMovieLoader& ml, SWFMovieDefinition* md);
102 
103 };
104 
106 //
111 {
112 public:
113 
115  //
118  typedef std::map<int, boost::intrusive_ptr<SWF::DefinitionTag> >
120 
121  typedef CharacterContainer::iterator CharacterIterator;
122 
123  typedef CharacterContainer::const_iterator CharacterConstIterator;
124 
126  //
129  boost::intrusive_ptr<SWF::DefinitionTag> getDisplayObject(int id) const;
130 
132  //
135  DSOTEXPORT void addDisplayObject(int id, boost::intrusive_ptr<SWF::DefinitionTag> c);
136 
138  CharacterIterator begin() { return _map.begin(); }
139 
141  CharacterConstIterator begin() const { return _map.begin(); }
142 
144  CharacterIterator end() { return _map.end(); }
145 
147  CharacterConstIterator end() const { return _map.end(); }
148 
149  friend std::ostream& operator<<(std::ostream& o,
150  const CharacterDictionary& cd);
151 
152 private:
153 
154  CharacterContainer _map;
155 
156 };
157 
158 
160 //
165 {
166 public:
167 
169  //
172  SWFMovieDefinition(const RunResources& runResources);
173 
175 
177  size_t get_frame_count() const {
178  return m_frame_count;
179  }
180 
182  float get_frame_rate() const {
183  return m_frame_rate;
184  }
185 
187  const SWFRect& get_frame_size() const {
188  return m_frame_size;
189  }
190 
191  size_t get_width_pixels() const {
192  return std::ceil(twipsToPixels(m_frame_size.width()));
193  }
194 
195  size_t get_height_pixels() const {
196  return std::ceil(twipsToPixels(m_frame_size.height()));
197  }
198 
200  void setAS3() {
201  _as3 = true;
202  }
203 
205  bool isAS3() const {
206  return _as3;
207  }
208 
210  //
213  virtual int get_version() const { return m_version; }
214 
216  //
224  virtual size_t get_loading_frame() const;
225 
227  //
230  size_t get_bytes_loaded() const {
231  boost::mutex::scoped_lock lock(_bytes_loaded_mutex);
232  return _bytes_loaded;
233  }
234 
236  size_t get_bytes_total() const {
237  return m_file_length;
238  }
239 
240  DSOTEXPORT virtual void importResources(boost::intrusive_ptr<movie_definition> source,
241  const Imports& imports);
242 
243  virtual void addDisplayObject(boost::uint16_t id, SWF::DefinitionTag* c);
244 
246  DSOTEXPORT SWF::DefinitionTag* getDefinitionTag(boost::uint16_t id) const;
247 
248  // See dox in movie_definition
249  //
250  // locks _namedFramesMutex
251  DSOTEXPORT bool get_labeled_frame(const std::string& label, size_t& frame_number)
252  const;
253 
254  DSOTEXPORT void add_font(int font_id, boost::intrusive_ptr<Font> f);
255 
256  DSOTEXPORT Font* get_font(int font_id) const;
257 
258  Font* get_font(const std::string& name, bool bold, bool italic) const;
259 
260  // See dox in movie_definition.h
261  DSOTEXPORT CachedBitmap* getBitmap(int DisplayObject_id) const;
262 
263  // See dox in movie_definition.h
264  void addBitmap(int DisplayObject_id, boost::intrusive_ptr<CachedBitmap> im);
265 
266  // See dox in movie_definition.h
267  sound_sample* get_sound_sample(int DisplayObject_id) const;
268 
269  // See dox in movie_definition.h
270  virtual void add_sound_sample(int DisplayObject_id, sound_sample* sam);
271 
272  // See dox in movie_definition.h
273  virtual void set_loading_sound_stream_id(int id) {
274  m_loading_sound_stream = id;
275  }
276 
277  // See dox in movie_definition.h
279  return m_loading_sound_stream;
280  }
281 
282  // See dox in movie_definition.h
283  void addControlTag(boost::intrusive_ptr<SWF::ControlTag> tag) {
284  assert(tag);
285  boost::mutex::scoped_lock lock(_frames_loaded_mutex);
286  m_playlist[_frames_loaded].push_back(tag);
287  }
288 
289  // See dox in movie_definition.h
290  //
291  // locks _namedFramesMutex and _frames_loaded_mutex
292  //
293  DSOTEXPORT void add_frame_name(const std::string& name);
294 
297  DSOTEXPORT void set_jpeg_loader(std::auto_ptr<image::JpegInput> j_in);
298 
299  // See dox in movie_definition.h
301  return m_jpeg_in.get();
302  }
303 
304  virtual const PlayList* getPlaylist(size_t frame_number) const {
305 
306 #ifndef NDEBUG
307  boost::mutex::scoped_lock lock(_frames_loaded_mutex);
308  assert(frame_number <= _frames_loaded);
309 #endif
310 
311  PlayListMap::const_iterator it = m_playlist.find(frame_number);
312  if ( it == m_playlist.end() ) return NULL;
313  else return &(it->second);
314  }
315 
317  //
325  bool readHeader(std::auto_ptr<IOChannel> in, const std::string& url);
326 
328  //
334  bool completeLoad();
335 
339  bool ensure_frame_loaded(size_t framenum) const;
340 
342  //
347  void read_all_swf();
348 
350  //
358  Movie* createMovie(Global_as& gl, DisplayObject* parent = 0);
359 
361  const {
362  return 0;
363  }
364 
365  virtual const std::string& get_url() const { return _url; }
366 
368  //
370  //
375  boost::uint16_t exportID(const std::string& symbol) const;
376 
378  //
380  //
384  void registerExport(const std::string& symbol, boost::uint16_t id);
385 
386 
387 #ifdef USE_SWFTREE
388 
389  // These methods attach the contents of the METADATA tag
390  // to a movie_definition.
391  virtual void storeDescriptiveMetadata(const std::string& data) {
392  _metadata = data;
393  }
394 
395  virtual const std::string& getDescriptiveMetadata() const {
396  return _metadata;
397  }
398 
399 #endif
400 
401 private:
402 
403 #ifdef USE_SWFTREE
404  // For storing descriptive metadata (information only)
405  std::string _metadata;
406 #endif
407 
409  CharacterDictionary _dictionary;
410 
412  mutable boost::mutex _dictionaryMutex;
413 
414  typedef std::map<int, boost::intrusive_ptr<Font> > FontMap;
415  FontMap m_fonts;
416 
417  typedef std::map<int, boost::intrusive_ptr<CachedBitmap> > Bitmaps;
418  Bitmaps _bitmaps;
419 
420  typedef std::map<int, boost::intrusive_ptr<sound_sample> > SoundSampleMap;
421  SoundSampleMap m_sound_samples;
422 
423  typedef std::map<size_t, PlayList> PlayListMap;
424 
426  PlayListMap m_playlist;
427 
429  typedef std::map<std::string, size_t, StringNoCaseLessThan> NamedFrameMap;
430  NamedFrameMap _namedFrames;
431 
432  // Mutex protecting access to _namedFrames
433  mutable boost::mutex _namedFramesMutex;
434 
436  typedef std::map<std::string, boost::uint16_t,
437  StringNoCaseLessThan> Exports;
438 
440  Exports _exportTable;
441 
442  // Mutex protecting access to the export map.
443  mutable boost::mutex _exportedResourcesMutex;
444 
447  typedef std::vector<boost::intrusive_ptr<movie_definition> > ImportVect;
448  ImportVect m_import_source_movies;
449 
450  SWFRect m_frame_size;
451  float m_frame_rate;
452  size_t m_frame_count;
453  int m_version;
454 
456  size_t _frames_loaded;
457 
459  //
463  mutable boost::mutex _frames_loaded_mutex;
464 
466  mutable boost::condition _frame_reached_condition;
467 
469  //
472  mutable size_t _waiting_for_frame;
473 
475  unsigned long _bytes_loaded;
476 
478  //
482  mutable boost::mutex _bytes_loaded_mutex;
483 
484  int m_loading_sound_stream;
485 
486  boost::uint32_t m_file_length;
487 
488  std::auto_ptr<image::JpegInput> m_jpeg_in;
489 
490  std::string _url;
491 
493  boost::scoped_ptr<SWFStream> _str;
494 
495  std::auto_ptr<IOChannel> _in;
496 
498  size_t _swf_end_pos;
499 
501  SWFMovieLoader _loader;
502 
510  DSOTEXPORT virtual void incrementLoadedFrames();
511 
513  //
515  void setBytesLoaded(unsigned long bytes)
516  {
517  boost::mutex::scoped_lock lock(_bytes_loaded_mutex);
518  _bytes_loaded=bytes;
519  }
520 
522  bool _loadingCanceled;
523 
525  std::set< boost::intrusive_ptr<movie_definition> > _importSources;
526 
527 private:
528 
531  //
535  const RunResources& _runResources;
536 
537  bool _as3;
538 
539 };
540 
541 } // namespace gnash
542 
543 #endif
bool isSelfThread() const
Return true if called from the loader thread.
Definition: SWFMovieDefinition.cpp:101
CharacterConstIterator end() const
Return a const_iterator to one-past last dictionary element.
Definition: SWFMovieDefinition.h:147
int get_loading_sound_stream_id() const
Get the currently being loaded sound stream, if any.
Definition: SWFMovieDefinition.h:278
size_t get_bytes_loaded() const
Get number of bytes loaded from input stream.
Definition: SWFMovieDefinition.h:230
void setAS3()
Call this to inform callers that tags should be executed using AVM2.
Definition: SWFMovieDefinition.h:200
bool started() const
Return true if the loader thread was started.
Definition: SWFMovieDefinition.cpp:93
size_t get_height_pixels() const
Frame height in pixels.
Definition: SWFMovieDefinition.h:195
std::map< int, boost::intrusive_ptr< SWF::DefinitionTag > > CharacterContainer
The container used by this dictionary.
Definition: SWFMovieDefinition.h:119
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
Definition: klash_part.cpp:329
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
A Font resource.
Definition: Font.h:90
SWFMovieLoader(SWFMovieDefinition &md)
Definition: SWFMovieDefinition.cpp:73
image::JpegInput * get_jpeg_loader() const
Get the jpeg input loader, to load a DefineBits image (one without table info).
Definition: SWFMovieDefinition.h:300
size_t get_frame_count() const
Return total number of frames advertised for the SWFMovie.
Definition: SWFMovieDefinition.h:177
virtual int get_version() const
Return the advertised version for the SWFMovie.
Definition: SWFMovieDefinition.h:213
Font * get_font(const std::string &name, bool bold, bool italic)
Definition: fontlib.cpp:50
CharacterConstIterator begin() const
Return a const_iterator to the first dictionary element.
Definition: SWFMovieDefinition.h:141
double twipsToPixels(int i)
Definition: GnashNumeric.h:96
A top-level, standalone Movie that can be loaded and played.
Definition: Movie.h:46
SimpleBuffer data
Definition: LocalConnection_as.cpp:153
Immutable data representing the definition of a movie display element.
Definition: DefinitionTag.h:48
Helper class handling start and execution of a loading thread.
Definition: SWFMovieDefinition.h:66
Definition: GnashKey.h:149
Definition: GnashKey.h:152
Definition: GnashKey.h:161
Immutable definition of a SWF movie&#39;s contents.
Definition: SWFMovieDefinition.h:164
The Characters dictionary associated with each SWF file.
Definition: SWFMovieDefinition.h:110
bool isAS3() const
Check whether tags should be executed using AVM2.
Definition: SWFMovieDefinition.h:205
CharacterContainer::const_iterator CharacterConstIterator
Definition: SWFMovieDefinition.h:123
const URL _url
Definition: NetConnection_as.cpp:258
CharacterIterator end()
Return an iterator to one-past last dictionary element.
Definition: SWFMovieDefinition.h:144
boost::intrusive_ptr< SWF::DefinitionTag > getDisplayObject(int id) const
Get the Character with the given id.
Definition: SWFMovieDefinition.cpp:425
~SWFMovieLoader()
Definition: SWFMovieDefinition.cpp:81
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:43
DSOTEXPORT void addDisplayObject(int id, boost::intrusive_ptr< SWF::DefinitionTag > c)
Add a Character assigning it the given id.
Definition: SWFMovieDefinition.cpp:440
virtual const std::string & get_url() const
Return the URL of the SWF stream this definition has been read from.
Definition: SWFMovieDefinition.h:365
std::vector< boost::intrusive_ptr< SWF::ControlTag > > PlayList
Definition: movie_definition.h:98
CharacterContainer::iterator CharacterIterator
Definition: SWFMovieDefinition.h:121
virtual void set_loading_sound_stream_id(int id)
Set the currently being loaded sound stream.
Definition: SWFMovieDefinition.h:273
bool start()
Start loading thread.
Definition: SWFMovieDefinition.cpp:126
void add_font(Font *f)
Definition: fontlib.cpp:68
void addControlTag(boost::intrusive_ptr< SWF::ControlTag > tag)
Add an ControlTag to this movie_definition&#39;s playlist.
Definition: SWFMovieDefinition.h:283
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:55
CharacterIterator begin()
Return an iterator to the first dictionary element.
Definition: SWFMovieDefinition.h:138
Class for reading JPEG image data.
Definition: GnashImageJpeg.h:49
Rectangle class, see swf defined rectangle record.
Definition: SWFRect.h:44
virtual const PlayList * getPlaylist(size_t frame_number) const
Return the list of execute tags for given frame number.
Definition: SWFMovieDefinition.h:304
std::string url
Definition: gnash.cpp:58
#define DSOTEXPORT
Definition: dsodefs.h:63
const SWFRect & get_frame_size() const
Return dimensions of the SWFMovie.
Definition: SWFMovieDefinition.h:187
size_t get_width_pixels() const
Frame width in pixels.
Definition: SWFMovieDefinition.h:191
size_t get_bytes_total() const
Get total number of bytes as parsed from the SWF header.
Definition: SWFMovieDefinition.h:236
virtual DisplayObject * createDisplayObject(Global_as &, DisplayObject *) const
Create a DisplayObject with the given parent.
Definition: SWFMovieDefinition.h:360
float get_frame_rate() const
Return frame rate advertised for the SWFMovie.
Definition: SWFMovieDefinition.h:182
An identifier for a sound sample managed by a sound_handler.
Definition: sound_definition.h:61
A CachedBitmap is created by the renderer in a format of its choosing.
Definition: CachedBitmap.h:37
std::string name
Definition: LocalConnection_as.cpp:151
friend std::ostream & operator<<(std::ostream &o, const CharacterDictionary &cd)
Definition: SWFMovieDefinition.cpp:410