File: AudioCollector.cpp

package info (click to toggle)
camstream 0.27%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,320 kB
  • ctags: 5,393
  • sloc: cpp: 17,031; sh: 8,154; asm: 455; ansic: 440; makefile: 343
file content (130 lines) | stat: -rw-r--r-- 3,650 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*  audiodevs: Abstraction layer for audio hardware & samples
    Copyright (C) 2003-2004 Nemosoft Unv.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    For questions, remarks, patches, etc. for this program, the author can be
    reached at camstream@smcc.demon.nl.
*/

/**
  \class CAudioCollector
  \brief Class that detects the present audio devices in the system


  Use a singleton pattern, i.e. there can only be one instance of this class present;
  get a pointer to this instance through the \ref Instance() member function.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include "AudioCollector.h"

#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
  #ifdef HAVE_ALSA
    #define ALSA_PCM_NEW_HW_PARAMS_API
    #define ALSA_PCM_NEW_SW_PARAMS_API
    #include <alsa/asoundlib.h>
  #endif
  #include "../linux/AudioDeviceLinux.h"
#endif

CAudioCollector *CAudioCollector::s_pAudioCollector = 0;

CAudioCollector::CAudioCollector()
{
   qDebug("CAudioCollector::CAudioCollector()");
}

// private

void CAudioCollector::Scan()
{
#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
#ifdef HAVE_ALSA
   int snd_card_number = -1;
   CAudioDeviceLinux *a;

   while (true) {
      snd_card_next(&snd_card_number);
      if (snd_card_number < 0)
        break;
      a = new CAudioDeviceLinux(snd_card_number);
      if (a->IsValid())
        m_Devices.append(a);
   };
#else
#error Only ALSA is supported at the moment!
#endif
#endif
}

// public

CAudioCollector *CAudioCollector::Instance()
{
   if (s_pAudioCollector == 0) {
     s_pAudioCollector = new CAudioCollector();
     s_pAudioCollector->Scan();
   }
   return s_pAudioCollector;
}

uint CAudioCollector::NumberOfAudioDevices() const
{
   return m_Devices.count();
}

CAudioDevice *CAudioCollector::GetAudioDevice(uint n)
{
   if (n >= m_Devices.count())
     return 0;
   return m_Devices.at(n);
}

/* \brief Find an AudioDevice, based on name and node
   \param device_name The name, as returned by \ref CAudioDevice::GetName()
   \param node_name The node, as return by \ref CAudioDevice::GetNode()
   \return A pointer to a \ref CAudioDevice, or 0 if no match was found

This function searchs the currently available audio devices and tries to find a
match based on device name and node name. Since it is a-priory not known which
number an audio devices gets, we cannot use hardcoded device numbers. The function
first matches on device_name, and in case of multiple candidates, uses the node_name
as well to find a best match. If no device with a matching name is found, 0 is returned.

*/
CAudioDevice *CAudioCollector::Find(const QString &device_name, const QString &node_name)
{
   uint u;
   CAudioDevice *best = 0, *dev;

   for (u = 0; u < m_Devices.count(); u++)
   {
      dev = m_Devices.at(u);
      if (dev == 0)
        continue;
      if (dev->GetName() == device_name)
      {
        if (dev->GetNodeName() == node_name || best == 0)
          best = dev;
      }
   }
   return best;
}