Debian Codegraph @

components/output/oss/oss.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2017 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #ifdef __OpenBSD__
14 # include <soundcard.h>
15 #else
16 # include <sys/soundcard.h>
17 #endif
18
19 #include <unistd.h>
20 #include <fcntl.h>
21
22 #include "oss.h"
23
24 const String &BoCA::OutputOSS::GetComponentSpecs()
25 {
26 static String componentSpecs;
27
28 int device_fd = NIL;
29
30 #if defined __OpenBSD__ || defined __NetBSD__
31 if ((device_fd = open("/dev/sound", O_WRONLY, 0)) >= 0)
32 #else
33 if ((device_fd = open("/dev/dsp", O_WRONLY, 0)) >= 0)
34 #endif
35 {
36 close(device_fd);
37
38 componentSpecs = " \
39 \
40 <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
41 <component> \
42 <name>OSS Output Plugin</name> \
43 <version>1.0</version> \
44 <id>oss-out</id> \
45 <type>output</type> \
46 <input bits=\"8\" signed=\"false\"/> \
47 <input bits=\"16\"/> \
48 \
49 ";
50
51 #ifdef AFMT_S32_LE
52 componentSpecs.Append(" \
53 \
54 <input bits=\"32\"/> \
55 \
56 ");
57 #endif
58
59 componentSpecs.Append(" \
60 \
61 </component> \
62 \
63 ");
64 }
65
66 return componentSpecs;
67 }
68
69 BoCA::OutputOSS::OutputOSS()
70 {
71 device_fd = -1;
72
73 paused = False;
74 }
75
76 BoCA::OutputOSS::~OutputOSS()
77 {
78 }
79
80 Bool BoCA::OutputOSS::Activate()
81 {
82 static Endianness endianness = CPU().GetEndianness();
83
84 const Format &format = track.GetFormat();
85
86 #if defined __OpenBSD__ || defined __NetBSD__
87 if ((device_fd = open("/dev/sound", O_WRONLY, 0)) < 0) return False;
88 #else
89 if ((device_fd = open("/dev/dsp", O_WRONLY, 0)) < 0) return False;
90 #endif
91
92 int samples = 0;
93
94 if (format.bits == 8) samples = AFMT_U8;
95 else if (format.bits == 16) samples = (endianness == EndianBig ? AFMT_S16_BE : AFMT_S16_LE);
96 #ifdef AFMT_S32_LE
97 else if (format.bits == 32) samples = (endianness == EndianBig ? AFMT_S32_BE : AFMT_S32_LE);
98 #endif
99
100 int channels = format.channels;
101 int rate = format.rate;
102
103 if (ioctl(device_fd, SNDCTL_DSP_SETFMT, &samples) == -1) return False;
104 if (ioctl(device_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) return False;
105 if (ioctl(device_fd, SNDCTL_DSP_SPEED, &rate) == -1) return False;
106
107 return True;
108 }
109
110 Bool BoCA::OutputOSS::Deactivate()
111 {
112 close(device_fd);
113
114 device_fd = -1;
115
116 return True;
117 }
118
119 Int BoCA::OutputOSS::WriteData(Buffer<UnsignedByte> &data)
120 {
121 Int bytes = write(device_fd, data, data.Size());
122
123 if (bytes < 0) return 0;
124
125 return bytes;
126 }
127
128 Int BoCA::OutputOSS::CanWrite()
129 {
130 if (device_fd == -1) return 0;
131
132 if (paused) return 0;
133
134 const Format &format = track.GetFormat();
135
136 return format.rate * format.channels * (format.bits / 8) / 4;
137 }
138
139 Int BoCA::OutputOSS::SetPause(Bool pause)
140 {
141 if (device_fd == -1) return Error();
142
143 #ifdef SNDCTL_DSP_SKIP
144 if (pause) ioctl(device_fd, SNDCTL_DSP_SKIP, NIL);
145 #endif
146
147 paused = pause;
148
149 return Success();
150 }
151
152 Bool BoCA::OutputOSS::IsPlaying()
153 {
154 if (device_fd == -1) return False;
155
156 #ifdef SNDCTL_DSP_CURRENT_OPTR
157 oss_count_t ptr;
158
159 if (ioctl(device_fd, SNDCTL_DSP_CURRENT_OPTR, &ptr) == 0)
160 {
161 if (ptr.fifo_samples > 0) return True;
162 }
163 #else
164 count_info ci;
165
166 if (ioctl(device_fd, SNDCTL_DSP_GETOPTR, &ci) == 0)
167 {
168 if (ci.ptr > 0) return True;
169 }
170 #endif
171
172 return False;
173 }