]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/board/mmbman.cpp
7f875c2b8f5bc97a3d97b77e5a2cc180e7af88bd
[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 void SetPosition(MMBoardTime btime);
87
88 bool IsStopped();
89 bool IsPaused();
90
91 wxString GetStringType();
92 wxString GetStringInformation();
93
94 protected:
95 wxSoundFileStream *GetDecoder();
96
97 wxSoundStream *m_output_stream;
98 wxInputStream *m_input_stream;
99 wxSoundFileStream *m_file_stream;
100
101 MMBoardTime m_length;
102 wxUint8 m_file_type;
103 };
104
105 class MMBoardVideoFile: public MMBoardFile {
106 public:
107 MMBoardVideoFile(const wxString& filename);
108 ~MMBoardVideoFile();
109
110 bool NeedWindow();
111
112 void SetWindow(wxWindow *window);
113
114 void Play();
115 void Pause();
116 void Resume();
117 void Stop();
118
119 MMBoardTime GetPosition();
120 MMBoardTime GetLength();
121 void SetPosition(MMBoardTime btime);
122
123 bool IsStopped();
124 bool IsPaused();
125
126 wxString GetStringType();
127 wxString GetStringInformation();
128
129 protected:
130 wxWindow *m_output_window;
131 wxVideoBaseDriver *m_video_driver;
132 };
133
134 // ----------------------------------------------------------------------------
135 // Implementation
136 // ----------------------------------------------------------------------------
137
138 #define MMBoard_UNKNOWNTYPE 0
139 #define MMBoard_WAVE 1
140 #define MMBoard_AIFF 2
141
142 // ----------------------------------------------------------------------------
143 // MMBoardSoundFile
144
145 MMBoardSoundFile::MMBoardSoundFile(const wxString& filename)
146 : MMBoardFile()
147 {
148 m_input_stream = new wxFileInputStream(filename);
149 m_output_stream = MMBoardManager::OpenSoundStream();
150
151 m_file_stream = GetDecoder();
152
153 if (!m_file_stream) {
154 SetError(MMBoard_UnknownFile);
155 return;
156 }
157
158 // Compute length
159 wxUint32 length, seconds;
160
161 length = m_file_stream->GetLength();
162 seconds = m_file_stream->GetSoundFormat().GetTimeFromBytes(length);
163 m_length.seconds = seconds % 60;
164 m_length.minutes = (seconds / 60) % 60;
165 m_length.hours = seconds / 3600;
166 }
167
168 MMBoardSoundFile::~MMBoardSoundFile()
169 {
170 if (m_file_stream)
171 delete m_file_stream;
172 MMBoardManager::UnrefSoundStream(m_output_stream);
173 delete m_input_stream;
174 }
175
176 wxSoundFileStream *MMBoardSoundFile::GetDecoder()
177 {
178 wxSoundFileStream *f_stream;
179
180 // First, we try a Wave decoder
181 f_stream = new wxSoundWave(*m_input_stream, *m_output_stream);
182 m_file_type = MMBoard_WAVE;
183 if (f_stream->CanRead())
184 return f_stream;
185 delete f_stream;
186
187 // Then, a AIFF decoder
188 f_stream = new wxSoundAiff(*m_input_stream, *m_output_stream);
189 m_file_type = MMBoard_AIFF;
190 if (f_stream->CanRead())
191 return f_stream;
192 delete f_stream;
193
194 m_file_type = MMBoard_UNKNOWNTYPE;
195
196 // TODO: automate
197
198 return NULL;
199 }
200
201 MMBoardTime MMBoardSoundFile::GetLength()
202 {
203 return m_length;
204 }
205
206 bool MMBoardSoundFile::IsStopped()
207 {
208 return m_file_stream->IsStopped();
209 }
210
211 bool MMBoardSoundFile::IsPaused()
212 {
213 return m_file_stream->IsPaused();
214 }
215
216 MMBoardTime MMBoardSoundFile::GetPosition()
217 {
218 wxUint32 length, seconds;
219 MMBoardTime file_time;
220
221 file_time.seconds = file_time.minutes = file_time.hours = 0;
222 if (m_file_stream->IsStopped())
223 return file_time;
224
225 length = m_file_stream->GetPosition();
226 seconds = m_file_stream->GetSoundFormat().GetTimeFromBytes(length);
227 file_time.seconds = seconds % 60;
228 file_time.minutes = (seconds / 60) % 60;
229 file_time.hours = seconds / 3600;
230
231 return file_time;
232 }
233
234 void MMBoardSoundFile::SetPosition(MMBoardTime btime)
235 {
236 wxUint32 itime;
237
238 itime = btime.seconds + btime.minutes * 60 + btime.hours;
239
240 m_file_stream->SetPosition(
241 m_file_stream->GetSoundFormat().GetBytesFromTime(itime)
242 );
243 }
244
245 bool MMBoardSoundFile::NeedWindow()
246 {
247 return FALSE;
248 }
249
250 void MMBoardSoundFile::SetWindow(wxWindow *window)
251 {
252 }
253
254 void MMBoardSoundFile::Play()
255 {
256 m_file_stream->Play();
257 }
258
259 void MMBoardSoundFile::Pause()
260 {
261 m_file_stream->Pause();
262 }
263
264 void MMBoardSoundFile::Resume()
265 {
266 m_file_stream->Resume();
267 }
268
269 void MMBoardSoundFile::Stop()
270 {
271 m_file_stream->Stop();
272 }
273
274 wxString MMBoardSoundFile::GetStringType()
275 {
276 switch (m_file_type) {
277 case MMBoard_WAVE:
278 return wxString(wxT("WAVE file"));
279 break;
280 case MMBoard_AIFF:
281 return wxString(wxT("AIFF file"));
282 break;
283 default:
284 return wxString(wxT("Unknown file"));
285 break;
286 }
287 }
288
289 wxString MMBoardSoundFile::GetStringInformation()
290 {
291 wxString info;
292 wxSoundFormatBase *format;
293
294 format = &(m_file_stream->GetSoundFormat());
295
296 info = wxT("Data encoding: ");
297 switch (format->GetType()) {
298 case wxSOUND_PCM: {
299 wxSoundFormatPcm *pcm_format = (wxSoundFormatPcm *)format;
300
301 info += wxT("PCM\n");
302 info += wxString::Format(wxT("Sampling rate: %d\n")
303 wxT("Bits per sample: %d\n")
304 wxT("Number of channels: %d\n"),
305 pcm_format->GetSampleRate(),
306 pcm_format->GetBPS(),
307 pcm_format->GetChannels());
308
309 break;
310 }
311 case wxSOUND_ULAW: {
312 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
313 info += wxT("ULAW\n");
314 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
315 break;
316 }
317 default:
318 info += wxT("Unknown");
319 break;
320 }
321 return info;
322 }
323
324 // ----------------------------------------------------------------------------
325
326
327 // ----------------------------------------------------------------------------
328 // MMBoardVideoFile
329
330 MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
331 {
332 m_output_window = NULL;
333
334 #if defined(__UNIX__)
335 m_video_driver = new wxVideoXANIM(filename);
336 #elif defined(__WIN32__)
337 m_video_driver = new wxVideoWindows(filename);
338 #else
339 m_video_driver = NULL;
340 SetError(MMBoard_UnknownFile);
341 #endif
342 }
343
344 MMBoardVideoFile::~MMBoardVideoFile()
345 {
346 if (m_video_driver)
347 delete m_video_driver;
348 }
349
350 bool MMBoardVideoFile::NeedWindow()
351 {
352 return TRUE;
353 }
354
355 void MMBoardVideoFile::SetWindow(wxWindow *window)
356 {
357 m_output_window = window;
358 m_video_driver->AttachOutput(*window);
359
360 wxSize size;
361 m_video_driver->GetSize(size);
362 window->SetSize(size);
363 // BAD BAD
364 // and we remove
365 // window->GetParent()->GetSizer()->Fit(window->GetParent());
366 }
367
368 void MMBoardVideoFile::Play()
369 {
370 m_video_driver->Play();
371 }
372
373 void MMBoardVideoFile::Pause()
374 {
375 m_video_driver->Pause();
376 }
377
378 void MMBoardVideoFile::Resume()
379 {
380 m_video_driver->Resume();
381 }
382
383 void MMBoardVideoFile::Stop()
384 {
385 m_video_driver->Stop();
386 }
387
388 MMBoardTime MMBoardVideoFile::GetPosition()
389 {
390 MMBoardTime btime;
391
392 btime.seconds = btime.minutes = btime.hours = 0;
393 return btime;
394 }
395
396 MMBoardTime MMBoardVideoFile::GetLength()
397 {
398 MMBoardTime btime;
399 int frameTime;
400
401 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
402
403 btime.seconds = frameTime % 60;
404 btime.minutes = (frameTime / 60) % 60;
405 btime.hours = frameTime / 3600;
406 return btime;
407 }
408
409 void MMBoardVideoFile::SetPosition(MMBoardTime btime)
410 {
411 }
412
413 bool MMBoardVideoFile::IsStopped()
414 {
415 return m_video_driver->IsStopped();
416 }
417
418 bool MMBoardVideoFile::IsPaused()
419 {
420 return m_video_driver->IsPaused();
421 }
422
423 wxString MMBoardVideoFile::GetStringType()
424 {
425 return wxString(wxT("Video XANIM"));
426 }
427
428 wxString MMBoardVideoFile::GetStringInformation()
429 {
430 wxString info;
431
432 info = wxT("Video codec: ");
433 info += m_video_driver->GetMovieCodec() + "\n";
434 info += wxT("Audio codec: ");
435 info += m_video_driver->GetAudioCodec();
436 info += wxString::Format(" Sample rate: %d Channels: %d\n", m_video_driver->GetSampleRate(),
437 m_video_driver->GetBPS());
438 info += wxString::Format(" Frame rate: %.01f", m_video_driver->GetFrameRate());
439 return info;
440 }
441
442 // ----------------------------------------------------------------------------
443
444 // ----------------------------------------------------------------------------
445 // MMBoardFile
446
447 MMBoardFile::MMBoardFile()
448 {
449 m_error = 0;
450 }
451
452 MMBoardFile::~MMBoardFile()
453 {
454 }
455
456 //
457 // ----------------------------------------------------------------------------
458
459 // ----------------------------------------------------------------------------
460 // MMBoardManager
461
462 MMBoardFile *MMBoardManager::Open(const wxString& filename)
463 {
464 MMBoardFile *file;
465
466 // Test the audio codec
467 file = new MMBoardSoundFile(filename);
468 if (!file->GetError())
469 return file;
470 delete file;
471
472 // Test the video codec
473 file = new MMBoardVideoFile(filename);
474 if (!file->GetError())
475 return file;
476 delete file;
477
478 // Arrrgh, we just could not see what is that file ...
479 return NULL;
480 }
481
482 DECLARE_APP(MMBoardApp)
483
484 wxSoundStream *MMBoardManager::OpenSoundStream()
485 {
486 #ifdef __UNIX__
487 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
488 return new wxSoundStreamESD();
489
490 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
491 return new wxSoundStreamOSS();
492 #endif
493
494 #ifdef __WIN32__
495 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
496 return new wxSoundStreamWin();
497 #endif
498
499 wxMessageBox("You are trying to open a multimedia but you have not devices", "Error", wxOK | wxICON_ERROR, NULL);
500
501 return NULL;
502 }
503
504 void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
505 {
506 delete stream;
507 }
508
509 // ----------------------------------------------------------------------------
510