]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/board/mmbman.cpp
61f5f29b85987deea6d603b0758b31a48b406ce4
[wxWidgets.git] / utils / wxMMedia2 / board / mmbman.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mmbman.cpp
3 // Purpose: Multimedia Board manager
4 // Author: Guilhem Lavaux, <guilhem.lavaux@libertysurf.fr>
5 // Modified by:
6 // Created: 13/02/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000, Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "mmbman.cpp"
14 #endif
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // Personal headers
34
35 #include "wx/stream.h"
36 #include "wx/wfstream.h"
37
38 #include "sndbase.h"
39 #include "sndfile.h"
40 #include "sndwav.h"
41 #include "sndaiff.h"
42 #include "sndpcm.h"
43 #include "sndulaw.h"
44
45 #ifdef __UNIX__
46 #include "sndoss.h"
47 #include "sndesd.h"
48 #endif
49
50 #ifdef __WIN32__
51 #include "sndwin.h"
52 #endif
53
54 #include "vidbase.h"
55 #ifdef __UNIX__
56 #include "vidxanm.h"
57 #endif
58
59 #ifdef __WIN32__
60 #include "vidwin.h"
61 #endif
62
63 #include "mmboard.h"
64 #include "mmbman.h"
65
66 // ----------------------------------------------------------------------------
67 // Private class definitions
68 // ----------------------------------------------------------------------------
69
70 class MMBoardSoundFile: public MMBoardFile {
71 public:
72 MMBoardSoundFile(const wxString& filename);
73 ~MMBoardSoundFile();
74
75 bool NeedWindow();
76
77 void SetWindow(wxWindow *window);
78
79 void Play();
80 void Pause();
81 void Resume();
82 void Stop();
83
84 MMBoardTime GetPosition();
85 MMBoardTime GetLength();
86
87 bool IsStopped();
88 bool IsPaused();
89
90 wxString GetStringType();
91 wxString GetStringInformation();
92
93 protected:
94 wxSoundFileStream *GetDecoder();
95
96 wxSoundStream *m_output_stream;
97 wxInputStream *m_input_stream;
98 wxSoundFileStream *m_file_stream;
99
100 MMBoardTime m_length;
101 wxUint8 m_file_type;
102 };
103
104 class MMBoardVideoFile: public MMBoardFile {
105 public:
106 MMBoardVideoFile(const wxString& filename);
107 ~MMBoardVideoFile();
108
109 bool NeedWindow();
110
111 void SetWindow(wxWindow *window);
112
113 void Play();
114 void Pause();
115 void Resume();
116 void Stop();
117
118 MMBoardTime GetPosition();
119 MMBoardTime GetLength();
120
121 bool IsStopped();
122 bool IsPaused();
123
124 wxString GetStringType();
125 wxString GetStringInformation();
126
127 protected:
128 wxWindow *m_output_window;
129 wxVideoBaseDriver *m_video_driver;
130 };
131
132 // ----------------------------------------------------------------------------
133 // Implementation
134 // ----------------------------------------------------------------------------
135
136 #define MMBoard_UNKNOWNTYPE 0
137 #define MMBoard_WAVE 1
138 #define MMBoard_AIFF 2
139
140 // ----------------------------------------------------------------------------
141 // MMBoardSoundFile
142
143 MMBoardSoundFile::MMBoardSoundFile(const wxString& filename)
144 : MMBoardFile()
145 {
146 m_input_stream = new wxFileInputStream(filename);
147 m_output_stream = MMBoardManager::OpenSoundStream();
148
149 m_file_stream = GetDecoder();
150
151 if (!m_file_stream) {
152 SetError(MMBoard_UnknownFile);
153 return;
154 }
155
156 // Compute length
157 wxUint32 length, seconds;
158
159 length = m_file_stream->GetLength();
160 seconds = m_file_stream->GetSoundFormat().GetTimeFromBytes(length);
161 m_length.seconds = seconds % 60;
162 m_length.minutes = (seconds / 60) % 60;
163 m_length.hours = seconds / 3600;
164 }
165
166 MMBoardSoundFile::~MMBoardSoundFile()
167 {
168 if (m_file_stream)
169 delete m_file_stream;
170 MMBoardManager::UnrefSoundStream(m_output_stream);
171 delete m_input_stream;
172 }
173
174 wxSoundFileStream *MMBoardSoundFile::GetDecoder()
175 {
176 wxSoundFileStream *f_stream;
177
178 // First, we try a Wave decoder
179 f_stream = new wxSoundWave(*m_input_stream, *m_output_stream);
180 m_file_type = MMBoard_WAVE;
181 if (f_stream->CanRead())
182 return f_stream;
183 delete f_stream;
184
185 // Then, a AIFF decoder
186 f_stream = new wxSoundAiff(*m_input_stream, *m_output_stream);
187 m_file_type = MMBoard_AIFF;
188 if (f_stream->CanRead())
189 return f_stream;
190 delete f_stream;
191
192 m_file_type = MMBoard_UNKNOWNTYPE;
193
194 // TODO: automate
195
196 return NULL;
197 }
198
199 MMBoardTime MMBoardSoundFile::GetLength()
200 {
201 return m_length;
202 }
203
204 bool MMBoardSoundFile::IsStopped()
205 {
206 return m_file_stream->IsStopped();
207 }
208
209 bool MMBoardSoundFile::IsPaused()
210 {
211 return m_file_stream->IsPaused();
212 }
213
214 MMBoardTime MMBoardSoundFile::GetPosition()
215 {
216 wxUint32 length, seconds;
217 MMBoardTime file_time;
218
219 file_time.seconds = file_time.minutes = file_time.hours = 0;
220 if (m_file_stream->IsStopped())
221 return file_time;
222
223 length = m_file_stream->GetPosition();
224 seconds = m_file_stream->GetSoundFormat().GetTimeFromBytes(length);
225 file_time.seconds = seconds % 60;
226 file_time.minutes = (seconds / 60) % 60;
227 file_time.hours = seconds / 3600;
228
229 return file_time;
230 }
231
232 bool MMBoardSoundFile::NeedWindow()
233 {
234 return FALSE;
235 }
236
237 void MMBoardSoundFile::SetWindow(wxWindow *window)
238 {
239 }
240
241 void MMBoardSoundFile::Play()
242 {
243 m_file_stream->Play();
244 }
245
246 void MMBoardSoundFile::Pause()
247 {
248 m_file_stream->Pause();
249 }
250
251 void MMBoardSoundFile::Resume()
252 {
253 m_file_stream->Resume();
254 }
255
256 void MMBoardSoundFile::Stop()
257 {
258 m_file_stream->Stop();
259 }
260
261 wxString MMBoardSoundFile::GetStringType()
262 {
263 switch (m_file_type) {
264 case MMBoard_WAVE:
265 return wxString(wxT("WAVE file"));
266 break;
267 case MMBoard_AIFF:
268 return wxString(wxT("AIFF file"));
269 break;
270 default:
271 return wxString(wxT("Unknown file"));
272 break;
273 }
274 }
275
276 wxString MMBoardSoundFile::GetStringInformation()
277 {
278 wxString info;
279 wxSoundFormatBase *format;
280
281 format = &(m_file_stream->GetSoundFormat());
282
283 info = wxT("Data encoding: ");
284 switch (format->GetType()) {
285 case wxSOUND_PCM: {
286 wxSoundFormatPcm *pcm_format = (wxSoundFormatPcm *)format;
287
288 info += wxT("PCM\n");
289 info += wxString::Format(wxT("Sampling rate: %d\n")
290 wxT("Bits per sample: %d\n")
291 wxT("Number of channels: %d\n"),
292 pcm_format->GetSampleRate(),
293 pcm_format->GetBPS(),
294 pcm_format->GetChannels());
295
296 break;
297 }
298 case wxSOUND_ULAW: {
299 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
300 info += wxT("ULAW\n");
301 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
302 break;
303 }
304 default:
305 info += wxT("Unknown");
306 break;
307 }
308 return info;
309 }
310
311 // ----------------------------------------------------------------------------
312
313
314 // ----------------------------------------------------------------------------
315 // MMBoardVideoFile
316
317 MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
318 {
319 m_output_window = NULL;
320
321 #if defined(__UNIX__)
322 m_video_driver = new wxVideoXANIM(filename);
323 #elif defined(__WIN32__)
324 m_video_driver = new wxVideoWindows(filename);
325 #else
326 m_video_driver = NULL;
327 SetError(MMBoard_UnknownFile);
328 #endif
329 }
330
331 MMBoardVideoFile::~MMBoardVideoFile()
332 {
333 if (m_video_driver)
334 delete m_video_driver;
335 }
336
337 bool MMBoardVideoFile::NeedWindow()
338 {
339 return TRUE;
340 }
341
342 void MMBoardVideoFile::SetWindow(wxWindow *window)
343 {
344 m_output_window = window;
345 m_video_driver->AttachOutput(*window);
346
347 wxSize size;
348 m_video_driver->GetSize(size);
349 window->SetSize(size);
350 // BAD BAD
351 window->GetParent()->GetSizer()->Fit(window->GetParent());
352 }
353
354 void MMBoardVideoFile::Play()
355 {
356 m_video_driver->Play();
357 }
358
359 void MMBoardVideoFile::Pause()
360 {
361 m_video_driver->Pause();
362 }
363
364 void MMBoardVideoFile::Resume()
365 {
366 m_video_driver->Resume();
367 }
368
369 void MMBoardVideoFile::Stop()
370 {
371 m_video_driver->Stop();
372 }
373
374 MMBoardTime MMBoardVideoFile::GetPosition()
375 {
376 MMBoardTime btime;
377
378 btime.seconds = btime.minutes = btime.hours = 0;
379 return btime;
380 }
381
382 MMBoardTime MMBoardVideoFile::GetLength()
383 {
384 MMBoardTime btime;
385 int frameTime;
386
387 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
388
389 btime.seconds = frameTime % 60;
390 btime.minutes = (frameTime / 60) % 60;
391 btime.hours = frameTime / 3600;
392 return btime;
393 }
394
395 bool MMBoardVideoFile::IsStopped()
396 {
397 return m_video_driver->IsStopped();
398 }
399
400 bool MMBoardVideoFile::IsPaused()
401 {
402 return m_video_driver->IsPaused();
403 }
404
405 wxString MMBoardVideoFile::GetStringType()
406 {
407 return wxString(wxT("Video XANIM"));
408 }
409
410 wxString MMBoardVideoFile::GetStringInformation()
411 {
412 wxString info;
413
414 info = wxT("Video codec: ");
415 info += m_video_driver->GetMovieCodec() + "\n";
416 info += wxT("Audio codec: ");
417 info += m_video_driver->GetAudioCodec();
418 info += wxString::Format(" Sample rate: %d Channels: %d\n", m_video_driver->GetSampleRate(),
419 m_video_driver->GetBPS());
420 info += wxString::Format(" Frame rate: %.01f", m_video_driver->GetFrameRate());
421 return info;
422 }
423
424 // ----------------------------------------------------------------------------
425
426 // ----------------------------------------------------------------------------
427 // MMBoardFile
428
429 MMBoardFile::MMBoardFile()
430 {
431 m_error = 0;
432 }
433
434 MMBoardFile::~MMBoardFile()
435 {
436 }
437
438 //
439 // ----------------------------------------------------------------------------
440
441 // ----------------------------------------------------------------------------
442 // MMBoardManager
443
444 MMBoardFile *MMBoardManager::Open(const wxString& filename)
445 {
446 MMBoardFile *file;
447
448 // Test the audio codec
449 file = new MMBoardSoundFile(filename);
450 if (!file->GetError())
451 return file;
452 delete file;
453
454 // Test the video codec
455 file = new MMBoardVideoFile(filename);
456 if (!file->GetError())
457 return file;
458 delete file;
459
460 // Arrrgh, we just could not see what is that file ...
461 return NULL;
462 }
463
464 DECLARE_APP(MMBoardApp)
465
466 wxSoundStream *MMBoardManager::OpenSoundStream()
467 {
468 #ifdef __UNIX__
469 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
470 return new wxSoundStreamESD();
471
472 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
473 return new wxSoundStreamOSS();
474 #endif
475
476 #ifdef __WIN32__
477 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
478 return new wxSoundStreamWin();
479 #endif
480
481 wxMessageBox("You are trying to open a multimedia but you have not devices", "Error", wxOK | wxICON_ERROR, NULL);
482
483 return NULL;
484 }
485
486 void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
487 {
488 delete stream;
489 }
490
491 // ----------------------------------------------------------------------------
492