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 
32 #include <atomic>
33 #include <vector>
34 #include <map>
35 #include <set>
36 #include <string>
37 #include <memory>
38 #include <mutex>
39 #include <thread>
40 #include <condition_variable>
41 
42 #include "movie_definition.h" // for inheritance
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  namespace SWF {
62  class DefinitionTag;
63  }
64 }
65 
66 namespace gnash {
67 
70 {
71 public:
72 
74 
75  ~SWFMovieLoader();
76 
78  //
83  bool start();
84 
86  bool started() const;
87 
89  bool isSelfThread() const;
90 
91 private:
92 
93  SWFMovieDefinition& _movie_def;
94 
95  mutable std::mutex _mutex;
96  std::thread _thread;
97 };
98 
100 //
105 {
106 public:
107 
109  //
112  typedef std::map<int, boost::intrusive_ptr<SWF::DefinitionTag> >
114 
115  typedef CharacterContainer::iterator CharacterIterator;
116 
117  typedef CharacterContainer::const_iterator CharacterConstIterator;
118 
120  //
123  boost::intrusive_ptr<SWF::DefinitionTag> getDisplayObject(int id) const;
124 
126  //
129  DSOTEXPORT void addDisplayObject(int id, boost::intrusive_ptr<SWF::DefinitionTag> c);
130 
132  CharacterIterator begin() { return _map.begin(); }
133 
135  CharacterConstIterator begin() const { return _map.begin(); }
136 
138  CharacterIterator end() { return _map.end(); }
139 
141  CharacterConstIterator end() const { return _map.end(); }
142 
143  friend std::ostream& operator<<(std::ostream& o,
144  const CharacterDictionary& cd);
145 
146 private:
147 
148  CharacterContainer _map;
149 
150 };
151 
152 
154 //
159 {
160 public:
161 
163  //
166  SWFMovieDefinition(const RunResources& runResources);
167 
169 
171  size_t get_frame_count() const {
172  return m_frame_count;
173  }
174 
176  float get_frame_rate() const {
177  return m_frame_rate;
178  }
179 
181  const SWFRect& get_frame_size() const {
182  return m_frame_size;
183  }
184 
185  size_t get_width_pixels() const {
186  return std::ceil(twipsToPixels(m_frame_size.width()));
187  }
188 
189  size_t get_height_pixels() const {
190  return std::ceil(twipsToPixels(m_frame_size.height()));
191  }
192 
194  void setAS3() {
195  _as3 = true;
196  }
197 
199  bool isAS3() const {
200  return _as3.load();
201  }
202 
204  //
207  virtual int get_version() const { return m_version; }
208 
210  //
216  virtual size_t get_loading_frame() const;
217 
219  size_t get_bytes_loaded() const {
220  return _bytes_loaded.load();
221  }
222 
224  size_t get_bytes_total() const {
225  return m_file_length;
226  }
227 
228  DSOTEXPORT virtual void importResources(boost::intrusive_ptr<movie_definition> source,
229  const Imports& imports);
230 
231  virtual void addDisplayObject(std::uint16_t id, SWF::DefinitionTag* c);
232 
234  DSOTEXPORT SWF::DefinitionTag* getDefinitionTag(std::uint16_t id) const;
235 
236  // See dox in movie_definition
237  //
238  // locks _namedFramesMutex
239  DSOTEXPORT bool get_labeled_frame(const std::string& label, size_t& frame_number)
240  const;
241 
242  DSOTEXPORT void add_font(int font_id, boost::intrusive_ptr<Font> f);
243 
244  DSOTEXPORT Font* get_font(int font_id) const;
245 
246  Font* get_font(const std::string& name, bool bold, bool italic) const;
247 
248  // See dox in movie_definition.h
249  DSOTEXPORT CachedBitmap* getBitmap(int DisplayObject_id) const;
250 
251  // See dox in movie_definition.h
252  void addBitmap(int DisplayObject_id, boost::intrusive_ptr<CachedBitmap> im);
253 
254  // See dox in movie_definition.h
255  sound_sample* get_sound_sample(int DisplayObject_id) const;
256 
257  // See dox in movie_definition.h
258  virtual void add_sound_sample(int DisplayObject_id, sound_sample* sam);
259 
260  // See dox in movie_definition.h
261  virtual void set_loading_sound_stream_id(int id) {
262  m_loading_sound_stream = id;
263  }
264 
265  // See dox in movie_definition.h
267  return m_loading_sound_stream;
268  }
269 
270  // See dox in movie_definition.h
271  void addControlTag(boost::intrusive_ptr<SWF::ControlTag> tag) {
272  assert(tag);
273  size_t frames_loaded = get_loading_frame();
274  m_playlist[frames_loaded].push_back(tag);
275  }
276 
277  // See dox in movie_definition.h
278  //
279  // locks _namedFramesMutex
280  //
281  DSOTEXPORT void add_frame_name(const std::string& name);
282 
285  DSOTEXPORT void set_jpeg_loader(std::unique_ptr<image::JpegInput> j_in);
286 
287  // See dox in movie_definition.h
289  return m_jpeg_in.get();
290  }
291 
292  virtual const PlayList* getPlaylist(size_t frame_number) const {
293 
294 #ifndef NDEBUG
295  assert(frame_number <= _frames_loaded.load());
296 #endif
297 
298  PlayListMap::const_iterator it = m_playlist.find(frame_number);
299  if ( it == m_playlist.end() ) return nullptr;
300  else return &(it->second);
301  }
302 
304  //
312  bool readHeader(std::unique_ptr<IOChannel> in, const std::string& url);
313 
315  //
321  bool completeLoad();
322 
326  bool ensure_frame_loaded(size_t framenum) const;
327 
329  //
334  void read_all_swf();
335 
337  //
345  Movie* createMovie(Global_as& gl, DisplayObject* parent = nullptr);
346 
348  const {
349  return nullptr;
350  }
351 
352  virtual const std::string& get_url() const { return _url; }
353 
355  //
357  //
362  std::uint16_t exportID(const std::string& symbol) const;
363 
365  //
367  //
371  void registerExport(const std::string& symbol, std::uint16_t id);
372 
373 
374 #ifdef USE_SWFTREE
375 
376  // These methods attach the contents of the METADATA tag
377  // to a movie_definition.
378  virtual void storeDescriptiveMetadata(const std::string& data) {
379  _metadata = data;
380  }
381 
382  virtual const std::string& getDescriptiveMetadata() const {
383  return _metadata;
384  }
385 
386 #endif
387 
388 private:
389 
390 #ifdef USE_SWFTREE
391  // For storing descriptive metadata (information only)
392  std::string _metadata;
393 #endif
394 
396  CharacterDictionary _dictionary;
397 
399  mutable std::mutex _dictionaryMutex;
400 
401  typedef std::map<int, boost::intrusive_ptr<Font> > FontMap;
402  FontMap m_fonts;
403 
404  typedef std::map<int, boost::intrusive_ptr<CachedBitmap> > Bitmaps;
405  Bitmaps _bitmaps;
406 
407  typedef std::map<int, boost::intrusive_ptr<sound_sample> > SoundSampleMap;
408  SoundSampleMap m_sound_samples;
409 
410  typedef std::map<size_t, PlayList> PlayListMap;
411 
413  PlayListMap m_playlist;
414 
416  typedef std::map<std::string, size_t, StringNoCaseLessThan> NamedFrameMap;
417  NamedFrameMap _namedFrames;
418 
419  // Mutex protecting access to _namedFrames
420  mutable std::mutex _namedFramesMutex;
421 
423  typedef std::map<std::string, std::uint16_t,
424  StringNoCaseLessThan> Exports;
425 
427  Exports _exportTable;
428 
429  // Mutex protecting access to the export map.
430  mutable std::mutex _exportedResourcesMutex;
431 
434  typedef std::vector<boost::intrusive_ptr<movie_definition> > ImportVect;
435  ImportVect m_import_source_movies;
436 
437  SWFRect m_frame_size;
438  float m_frame_rate;
439  size_t m_frame_count;
440  int m_version;
441 
443  std::atomic<size_t> _frames_loaded;
444 
446  mutable std::condition_variable _frame_reached_condition;
447 
449  mutable std::atomic<size_t> _waiting_for_frame;
450 
452  std::atomic<unsigned long> _bytes_loaded;
453 
454  int m_loading_sound_stream;
455 
456  std::uint32_t m_file_length;
457 
458  std::unique_ptr<image::JpegInput> m_jpeg_in;
459 
460  std::string _url;
461 
463  std::unique_ptr<SWFStream> _str;
464 
465  std::unique_ptr<IOChannel> _in;
466 
468  // This is set by readHeader, and used in the parsing thread, which starts
469  // after readHeader() runs.
470  size_t _swf_end_pos;
471 
473  SWFMovieLoader _loader;
474 
478  DSOTEXPORT virtual void incrementLoadedFrames();
479 
481  void setBytesLoaded(unsigned long bytes)
482  {
483  _bytes_loaded=bytes;
484  }
485 
487  mutable std::mutex _loadingCanceledMutex;
488  bool _loadingCanceled;
489 
491  std::set< boost::intrusive_ptr<movie_definition> > _importSources;
492 
493 private:
494 
497  //
501  const RunResources& _runResources;
502 
503  std::atomic<bool> _as3;
504 
505 };
506 
507 } // namespace gnash
508 
509 #endif
void setAS3()
Call this to inform callers that tags should be executed using AVM2.
Definition: SWFMovieDefinition.h:194
std::map< int, boost::intrusive_ptr< SWF::DefinitionTag > > CharacterContainer
The container used by this dictionary.
Definition: SWFMovieDefinition.h:113
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
Definition: klash_part.cpp:329
std::vector< ImportSpec > Imports
Definition: movie_definition.h:166
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:168
A Font resource.
Definition: Font.h:89
virtual const std::string & get_url() const
Return the URL of the SWF stream this definition has been read from.
Definition: SWFMovieDefinition.h:352
CharacterConstIterator begin() const
Return a const_iterator to the first dictionary element.
Definition: SWFMovieDefinition.h:135
Definition: klash_part.cpp:330
virtual DisplayObject * createDisplayObject(Global_as &, DisplayObject *) const
Create a DisplayObject with the given parent.
Definition: SWFMovieDefinition.h:347
size_t get_width_pixels() const
Frame width in pixels.
Definition: SWFMovieDefinition.h:185
Font * get_font(const std::string &name, bool bold, bool italic)
Definition: fontlib.cpp:50
double twipsToPixels(int i)
Definition: GnashNumeric.h:97
const SWFRect & get_frame_size() const
Return dimensions of the SWFMovie.
Definition: SWFMovieDefinition.h:181
A top-level, standalone Movie that can be loaded and played.
Definition: Movie.h:46
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Immutable data representing the definition of a movie display element.
Definition: DefinitionTag.h:47
size_t get_bytes_total() const
Get total number of bytes as parsed from the SWF header.
Definition: SWFMovieDefinition.h:224
Helper class handling start and execution of a loading thread.
Definition: SWFMovieDefinition.h:69
virtual const PlayList * getPlaylist(size_t frame_number) const
Return the list of execute tags for given frame number.
Definition: SWFMovieDefinition.h:292
Definition: GnashKey.h:149
Definition: GnashKey.h:152
Definition: GnashKey.h:161
Immutable definition of a SWF movie&#39;s contents.
Definition: SWFMovieDefinition.h:158
The Characters dictionary associated with each SWF file.
Definition: SWFMovieDefinition.h:104
CharacterContainer::const_iterator CharacterConstIterator
Definition: SWFMovieDefinition.h:117
CharacterIterator end()
Return an iterator to one-past last dictionary element.
Definition: SWFMovieDefinition.h:138
std::ostream & operator<<(std::ostream &o, const URL &u)
Definition: URL.cpp:447
size_t get_height_pixels() const
Frame height in pixels.
Definition: SWFMovieDefinition.h:189
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:53
std::vector< boost::intrusive_ptr< SWF::ControlTag > > PlayList
Definition: movie_definition.h:98
CharacterContainer::iterator CharacterIterator
Definition: SWFMovieDefinition.h:115
float get_frame_rate() const
Return frame rate advertised for the SWFMovie.
Definition: SWFMovieDefinition.h:176
virtual void set_loading_sound_stream_id(int id)
Set the currently being loaded sound stream.
Definition: SWFMovieDefinition.h:261
void add_font(Font *f)
Definition: fontlib.cpp:67
void addControlTag(boost::intrusive_ptr< SWF::ControlTag > tag)
Add an ControlTag to this movie_definition&#39;s playlist.
Definition: SWFMovieDefinition.h:271
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:49
int get_loading_sound_stream_id() const
Get the currently being loaded sound stream, if any.
Definition: SWFMovieDefinition.h:266
image::JpegInput * get_jpeg_loader() const
Get the jpeg input loader, to load a DefineBits image (one without table info).
Definition: SWFMovieDefinition.h:288
CharacterIterator begin()
Return an iterator to the first dictionary element.
Definition: SWFMovieDefinition.h:132
Class for reading JPEG image data.
Definition: GnashImageJpeg.h:49
A case-insensitive string comparator.
Definition: StringPredicates.h:31
Rectangle class, see swf defined rectangle record.
Definition: SWFRect.h:44
std::string url
Definition: gnash.cpp:59
virtual int get_version() const
Return the advertised version for the SWFMovie.
Definition: SWFMovieDefinition.h:207
#define DSOTEXPORT
Definition: dsodefs.h:63
bool isAS3() const
Check whether tags should be executed using AVM2.
Definition: SWFMovieDefinition.h:199
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
size_t get_frame_count() const
Return total number of frames advertised for the SWFMovie.
Definition: SWFMovieDefinition.h:171
std::string name
Definition: LocalConnection_as.cpp:149
Definition: GnashKey.h:331
CharacterConstIterator end() const
Return a const_iterator to one-past last dictionary element.
Definition: SWFMovieDefinition.h:141
size_t get_bytes_loaded() const
Get number of bytes loaded from input stream.
Definition: SWFMovieDefinition.h:219