update the copyright date.
[gnash.git] / libmedia / MediaHandler.cpp
blob596ed8b33690f4a5a68f2fbdc6c3fc8e76f8911e
1 // MediaHandler.cpp: Default MediaHandler implementation, for Gnash.
2 //
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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.
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
21 #ifdef HAVE_CONFIG_H
22 #include "gnashconfig.h"
23 #endif
25 #include "MediaHandler.h"
26 #include "FLVParser.h"
27 #include "IOChannel.h"
28 #include "AudioDecoderSimple.h"
29 #include "log.h"
31 #ifdef DECODING_SPEEX
32 # include "AudioDecoderSpeex.h"
33 #endif
35 namespace gnash {
36 namespace media {
38 bool
39 MediaHandler::isFLV(IOChannel& stream)
41 char head[4] = {0, 0, 0, 0};
42 stream.seek(0);
43 size_t actuallyRead = stream.read(head, 3);
44 stream.seek(0);
46 if (actuallyRead < 3)
48 throw IOException(_("MediaHandler::isFLV: Could not read 3 bytes "
49 "from input stream"));
52 if (!std::equal(head, head + 3, "FLV")) return false;
53 return true;
56 std::auto_ptr<MediaParser>
57 MediaHandler::createMediaParser(std::auto_ptr<IOChannel> stream)
59 std::auto_ptr<MediaParser> parser;
61 try {
62 if (!isFLV(*stream))
64 log_error(_("MediaHandler::createMediaParser: only FLV input is "
65 "supported by this MediaHandler"));
66 return parser;
69 catch (IOException& m) {
70 log_error(_("Exception while reading from stream: %s"), m.what());
71 return parser;
74 parser.reset( new FLVParser(stream) );
75 assert(!stream.get()); // TODO: when ownership will be transferred...
77 return parser;
80 std::auto_ptr<AudioDecoder>
81 MediaHandler::createFlashAudioDecoder(const AudioInfo& info)
83 assert (info.type == CODEC_TYPE_FLASH );
85 audioCodecType codec = static_cast<audioCodecType>(info.codec);
86 switch (codec)
88 case media::AUDIO_CODEC_ADPCM:
89 case media::AUDIO_CODEC_RAW:
90 case media::AUDIO_CODEC_UNCOMPRESSED:
92 std::auto_ptr<AudioDecoder> ret(new AudioDecoderSimple(info));
93 return ret;
96 #ifdef DECODING_SPEEX
97 case AUDIO_CODEC_SPEEX:
99 std::auto_ptr<AudioDecoder> ret(new AudioDecoderSpeex);
100 return ret;
102 #endif
104 default:
106 boost::format err = boost::format(
107 _("MediaHandler::createFlashAudioDecoder:"
108 " no available flash decoders for codec %d (%s)")) %
109 (int)codec % codec;
110 throw MediaException(err.str());
115 } // namespace media
116 } // namespace gnash
118 /// Here follows handler registration code.
120 #ifdef ENABLE_FFMPEG_MEDIA
121 #include "ffmpeg/MediaHandlerFfmpeg.h"
122 #endif
123 #ifdef ENABLE_GST_MEDIA
124 #include "gst/MediaHandlerGst.h"
125 #endif
126 #ifdef ENABLE_HAIKU_MEDIA
127 #include "haiku/MediaHandlerHaiku.h"
128 #endif
130 namespace gnash {
131 namespace media {
133 RegisterAllHandlers::RegisterAllHandlers()
135 #ifdef ENABLE_FFMPEG_MEDIA
136 static const MediaFactory::RegisterHandler<ffmpeg::MediaHandlerFfmpeg>
137 ffmpeg("ffmpeg");
138 #endif
139 #ifdef ENABLE_GST_MEDIA
140 static const MediaFactory::RegisterHandler<gst::MediaHandlerGst> gst("gst");
141 #endif
142 #ifdef ENABLE_HAIKU_MEDIA
143 static const MediaFactory::RegisterHandler<haiku::MediaHandlerHaiku>
144 haiku("haiku");
145 #endif
148 } // namespace media
149 } // namespace gnash