]> git.saurik.com Git - wxWidgets.git/blame - utils/wxMMedia2/board/mmbman.cpp
Moved wxMMedia to contrib/src/mmedia
[wxWidgets.git] / utils / wxMMedia2 / board / mmbman.cpp
CommitLineData
55196c54
GL
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
d73dd2b2 33// Personal headers
55196c54
GL
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
d73dd2b2
GL
54#include "vidbase.h"
55#ifdef __UNIX__
56#include "vidxanm.h"
57#endif
58
59#ifdef __WIN32__
60#include "vidwin.h"
61#endif
62
55196c54
GL
63#include "mmboard.h"
64#include "mmbman.h"
65
66// ----------------------------------------------------------------------------
67// Private class definitions
68// ----------------------------------------------------------------------------
69
70class MMBoardSoundFile: public MMBoardFile {
71public:
794bcc2d
GL
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
55196c54 94protected:
794bcc2d
GL
95 wxSoundFileStream *GetDecoder();
96
97 wxSoundStream *m_output_stream;
98 wxInputStream *m_input_stream;
99 wxSoundFileStream *m_file_stream;
55196c54 100
794bcc2d
GL
101 MMBoardTime m_length;
102 wxUint8 m_file_type;
d73dd2b2
GL
103};
104
105class MMBoardVideoFile: public MMBoardFile {
106public:
794bcc2d
GL
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
d73dd2b2 129protected:
794bcc2d
GL
130 wxWindow *m_output_window;
131 wxVideoBaseDriver *m_video_driver;
55196c54
GL
132};
133
134// ----------------------------------------------------------------------------
135// Implementation
136// ----------------------------------------------------------------------------
137
d73dd2b2
GL
138#define MMBoard_UNKNOWNTYPE 0
139#define MMBoard_WAVE 1
140#define MMBoard_AIFF 2
55196c54
GL
141
142// ----------------------------------------------------------------------------
143// MMBoardSoundFile
144
145MMBoardSoundFile::MMBoardSoundFile(const wxString& filename)
146 : MMBoardFile()
147{
794bcc2d
GL
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 }
d73dd2b2 157
794bcc2d
GL
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;
55196c54
GL
166}
167
168MMBoardSoundFile::~MMBoardSoundFile()
169{
794bcc2d
GL
170 if (m_file_stream)
171 delete m_file_stream;
172 MMBoardManager::UnrefSoundStream(m_output_stream);
173 delete m_input_stream;
55196c54
GL
174}
175
176wxSoundFileStream *MMBoardSoundFile::GetDecoder()
177{
794bcc2d
GL
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;
55196c54
GL
199}
200
201MMBoardTime MMBoardSoundFile::GetLength()
202{
794bcc2d 203 return m_length;
55196c54
GL
204}
205
206bool MMBoardSoundFile::IsStopped()
207{
794bcc2d 208 return m_file_stream->IsStopped();
55196c54
GL
209}
210
5bee458a
GL
211bool MMBoardSoundFile::IsPaused()
212{
794bcc2d 213 return m_file_stream->IsPaused();
5bee458a
GL
214}
215
55196c54
GL
216MMBoardTime MMBoardSoundFile::GetPosition()
217{
794bcc2d
GL
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
55196c54 231 return file_time;
794bcc2d
GL
232}
233
234void MMBoardSoundFile::SetPosition(MMBoardTime btime)
235{
236 wxUint32 itime;
55196c54 237
794bcc2d 238 itime = btime.seconds + btime.minutes * 60 + btime.hours;
55196c54 239
794bcc2d
GL
240 m_file_stream->SetPosition(
241 m_file_stream->GetSoundFormat().GetBytesFromTime(itime)
242 );
55196c54
GL
243}
244
245bool MMBoardSoundFile::NeedWindow()
246{
794bcc2d 247 return FALSE;
55196c54
GL
248}
249
250void MMBoardSoundFile::SetWindow(wxWindow *window)
251{
252}
253
254void MMBoardSoundFile::Play()
255{
794bcc2d 256 m_file_stream->Play();
55196c54
GL
257}
258
259void MMBoardSoundFile::Pause()
260{
794bcc2d 261 m_file_stream->Pause();
55196c54
GL
262}
263
264void MMBoardSoundFile::Resume()
265{
794bcc2d 266 m_file_stream->Resume();
55196c54
GL
267}
268
269void MMBoardSoundFile::Stop()
270{
794bcc2d 271 m_file_stream->Stop();
55196c54
GL
272}
273
274wxString MMBoardSoundFile::GetStringType()
275{
794bcc2d
GL
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 }
55196c54
GL
287}
288
289wxString MMBoardSoundFile::GetStringInformation()
290{
ebaad2cc
GL
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
8b33ae2d
GL
301 info += wxString::Format(wxT("PCM %s %s\n"),
302 pcm_format->Signed() ? wxT("signed") : wxT("unsigned"),
303 pcm_format->GetOrder() == wxLITTLE_ENDIAN ? wxT("little endian") : wxT("big endian"));
ebaad2cc
GL
304 info += wxString::Format(wxT("Sampling rate: %d\n")
305 wxT("Bits per sample: %d\n")
306 wxT("Number of channels: %d\n"),
307 pcm_format->GetSampleRate(),
308 pcm_format->GetBPS(),
309 pcm_format->GetChannels());
310
311 break;
312 }
313 case wxSOUND_ULAW: {
314 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
315 info += wxT("ULAW\n");
316 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
317 break;
318 }
319 default:
320 info += wxT("Unknown");
321 break;
322 }
323 return info;
55196c54
GL
324}
325
326// ----------------------------------------------------------------------------
327
328
d73dd2b2
GL
329// ----------------------------------------------------------------------------
330// MMBoardVideoFile
331
332MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
333{
ebaad2cc 334 m_output_window = NULL;
d73dd2b2
GL
335
336#if defined(__UNIX__)
ebaad2cc 337 m_video_driver = new wxVideoXANIM(filename);
d73dd2b2 338#elif defined(__WIN32__)
ebaad2cc 339 m_video_driver = new wxVideoWindows(filename);
d73dd2b2 340#else
ebaad2cc
GL
341 m_video_driver = NULL;
342 SetError(MMBoard_UnknownFile);
d73dd2b2
GL
343#endif
344}
345
346MMBoardVideoFile::~MMBoardVideoFile()
347{
ebaad2cc
GL
348 if (m_video_driver)
349 delete m_video_driver;
d73dd2b2
GL
350}
351
352bool MMBoardVideoFile::NeedWindow()
353{
ebaad2cc 354 return TRUE;
d73dd2b2
GL
355}
356
357void MMBoardVideoFile::SetWindow(wxWindow *window)
358{
ebaad2cc
GL
359 m_output_window = window;
360 m_video_driver->AttachOutput(*window);
2018e574
GL
361
362 wxSize size;
363 m_video_driver->GetSize(size);
364 window->SetSize(size);
365 // BAD BAD
c12a24e2
GL
366 // and we remove
367 // window->GetParent()->GetSizer()->Fit(window->GetParent());
d73dd2b2
GL
368}
369
370void MMBoardVideoFile::Play()
371{
ebaad2cc 372 m_video_driver->Play();
d73dd2b2
GL
373}
374
375void MMBoardVideoFile::Pause()
376{
ebaad2cc 377 m_video_driver->Pause();
d73dd2b2
GL
378}
379
380void MMBoardVideoFile::Resume()
381{
ebaad2cc 382 m_video_driver->Resume();
d73dd2b2
GL
383}
384
385void MMBoardVideoFile::Stop()
386{
ebaad2cc 387 m_video_driver->Stop();
d73dd2b2
GL
388}
389
390MMBoardTime MMBoardVideoFile::GetPosition()
391{
ebaad2cc 392 MMBoardTime btime;
d73dd2b2 393
ebaad2cc
GL
394 btime.seconds = btime.minutes = btime.hours = 0;
395 return btime;
d73dd2b2
GL
396}
397
398MMBoardTime MMBoardVideoFile::GetLength()
399{
ebaad2cc 400 MMBoardTime btime;
2018e574 401 int frameTime;
d73dd2b2 402
2018e574
GL
403 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
404
405 btime.seconds = frameTime % 60;
406 btime.minutes = (frameTime / 60) % 60;
407 btime.hours = frameTime / 3600;
ebaad2cc 408 return btime;
d73dd2b2
GL
409}
410
794bcc2d
GL
411void MMBoardVideoFile::SetPosition(MMBoardTime btime)
412{
413}
414
d73dd2b2
GL
415bool MMBoardVideoFile::IsStopped()
416{
ebaad2cc 417 return m_video_driver->IsStopped();
d73dd2b2
GL
418}
419
420bool MMBoardVideoFile::IsPaused()
421{
ebaad2cc 422 return m_video_driver->IsPaused();
d73dd2b2
GL
423}
424
425wxString MMBoardVideoFile::GetStringType()
426{
ebaad2cc 427 return wxString(wxT("Video XANIM"));
d73dd2b2
GL
428}
429
430wxString MMBoardVideoFile::GetStringInformation()
431{
2018e574
GL
432 wxString info;
433
434 info = wxT("Video codec: ");
435 info += m_video_driver->GetMovieCodec() + "\n";
436 info += wxT("Audio codec: ");
437 info += m_video_driver->GetAudioCodec();
438 info += wxString::Format(" Sample rate: %d Channels: %d\n", m_video_driver->GetSampleRate(),
439 m_video_driver->GetBPS());
440 info += wxString::Format(" Frame rate: %.01f", m_video_driver->GetFrameRate());
441 return info;
d73dd2b2
GL
442}
443
444// ----------------------------------------------------------------------------
445
55196c54
GL
446// ----------------------------------------------------------------------------
447// MMBoardFile
448
449MMBoardFile::MMBoardFile()
450{
451 m_error = 0;
452}
453
454MMBoardFile::~MMBoardFile()
455{
456}
457
458//
459// ----------------------------------------------------------------------------
460
461// ----------------------------------------------------------------------------
462// MMBoardManager
463
464MMBoardFile *MMBoardManager::Open(const wxString& filename)
465{
466 MMBoardFile *file;
467
d73dd2b2 468 // Test the audio codec
55196c54 469 file = new MMBoardSoundFile(filename);
d73dd2b2
GL
470 if (!file->GetError())
471 return file;
472 delete file;
473
474 // Test the video codec
475 file = new MMBoardVideoFile(filename);
476 if (!file->GetError())
477 return file;
478 delete file;
479
480 // Arrrgh, we just could not see what is that file ...
481 return NULL;
55196c54
GL
482}
483
484DECLARE_APP(MMBoardApp)
485
486wxSoundStream *MMBoardManager::OpenSoundStream()
487{
488#ifdef __UNIX__
489 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
490 return new wxSoundStreamESD();
491
492 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
493 return new wxSoundStreamOSS();
494#endif
495
496#ifdef __WIN32__
497 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
498 return new wxSoundStreamWin();
499#endif
500
501 wxMessageBox("You are trying to open a multimedia but you have not devices", "Error", wxOK | wxICON_ERROR, NULL);
502
503 return NULL;
504}
505
506void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
507{
508 delete stream;
509}
510
511// ----------------------------------------------------------------------------
512