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