CLAM-Development  1.4.0
BaseAudioApplication.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "BaseAudioApplication.hxx"
23 #include "AudioManager.hxx"
24 #include <pthread.h>
25 #include <cstdio>
26 #include <iostream>
27 #ifdef WIN32
28 #include "CLAM_windows.h"
29 #undef GetClassName
30 #else
31 #include <unistd.h>
32 #endif
33 #include "AudioManager.hxx"
34 
35 namespace CLAM {
36 
37  typedef void *(*startfn) (void *);
38  typedef void (*cleanfn) (void *);
39 
41  :Application()
42  {
43  cancel = false;
44  }
45 
47  {
48  cancel = false;
49  pthread_create(&thread,NULL,(startfn)SAudioThread,this);
50  }
51 
53  {
54  pthread_cleanup_push((cleanfn)SAudioThreadCleanup,this);
55 
56  cancel = true;
57  pthread_join(thread,NULL);
58 
59  pthread_cleanup_pop(1);
60  cancel = false;
61  }
62 
63  void BaseAudioApplication::Run(int argc,char** argv)
64  {
65  Start();
66  UserMain();
67  Stop();
68  }
69 
70  void BaseAudioApplication::UserMain(void)
71  {
72  printf("Press enter to terminate\n");
73  getchar();
74  }
75 
76  void* BaseAudioApplication::SAudioThread(BaseAudioApplication *pThis)
77  {
78  #ifdef WIN32
79  BOOL res;
80  DWORD err;
81 
82  // Maximum priority for the process: this has the upper hand over
83  // everything else, including most essential OS services
84  //res = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
85  // Toned down priority: this enables cache flush (at least)
86  //res = SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS );
87 
88  // With the new AudioIO approach (POLL instead of NOTIFY), it seems that
89  // we should NORMAL priority. If not, the system gets very slow.
90  //
91  // the other solution (now used) is to have a Sleep in the polling loop
92  res = SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS );
93 
94  err = GetLastError();
95 
96  // Maximum priority for the thread under Win32
97  //res = SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
98  //Toned down thread priority
99  //res = SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST );
100  // With the new AudioIO approach (POLL instead of NOTIFY), it seems that
101  // we should should NORMAL priority. If not, the system gets very slow
102  res = SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST );
103  err = GetLastError();
104  #else
105  struct sched_param sched_param;
106  int policy;
107 
108  if (pthread_getschedparam(pthread_self(), &policy, &sched_param) < 0) {
109  printf("Scheduler getparam failed...\n");
110  }
111  sched_param.sched_priority = sched_get_priority_max(SCHED_RR)-1;
112  if (!pthread_setschedparam(pthread_self(), SCHED_RR, &sched_param)) {
113  printf("Scheduler set to Round Robin with priority %i...\n", sched_param.sched_priority);
114  }
115 
116  #endif
117 
118  pThis->AudioMain();
119 
120  return NULL;
121  }
122 
123  void BaseAudioApplication::SAudioThreadCleanup(BaseAudioApplication *pThis)
124  {
125  }
126 
127 } // namespace CLAM
128