source: mainline/uspace/app/wavplay/drec.c@ c7afdf7a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c7afdf7a was 4a8d0dd1, checked in by Jakub Jermar <jakub@…>, 7 years ago

Do not async_accept_0() callback connections

Callback connections do not have the initial call which must be
accepted. The async framework asserts on attempts to answer or accept
calls with invalid capability handler.

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2013 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup wavplay
30 * @{
31 */
32/**
33 * @file PCM playback audio devices
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <str_error.h>
39#include <audio_pcm_iface.h>
40#include <pcm/format.h>
41#include <stdio.h>
42#include <as.h>
43#include <inttypes.h>
44#include <str.h>
45
46#include "wave.h"
47#include "drec.h"
48
49#define BUFFER_PARTS 16
50
51/** Recording format */
52static const pcm_format_t format = {
53 .sampling_rate = 44100,
54 .channels = 2,
55 .sample_format = PCM_SAMPLE_SINT16_LE,
56};
57
58/** Recording helper structure */
59typedef struct {
60 struct {
61 void *base;
62 size_t size;
63 unsigned id;
64 void *position;
65 } buffer;
66 FILE *file;
67 audio_pcm_sess_t *device;
68} record_t;
69
70/**
71 * Initialize recording helper structure.
72 * @param rec Recording structure.
73 * @param sess Session to IPC device.
74 */
75static void record_initialize(record_t *rec, audio_pcm_sess_t *sess)
76{
77 assert(sess);
78 assert(rec);
79 rec->buffer.base = NULL;
80 rec->buffer.size = 0;
81 rec->buffer.position = NULL;
82 rec->file = NULL;
83 rec->device = sess;
84}
85
86/** Recording callback.
87 *
88 * Writes recorded data.
89 *
90 * @param icall Poitner to IPC call structure.
91 * @param arg Argument. Poitner to recording helper structure.
92 *
93 */
94static void device_event_callback(ipc_call_t *icall, void *arg)
95{
96 record_t *rec = arg;
97 const size_t buffer_part = rec->buffer.size / BUFFER_PARTS;
98 bool record = true;
99
100 while (record) {
101 ipc_call_t call;
102 async_get_call(&call);
103
104 switch (IPC_GET_IMETHOD(call)) {
105 case PCM_EVENT_CAPTURE_TERMINATED:
106 printf("Recording terminated\n");
107 record = false;
108 break;
109 case PCM_EVENT_FRAMES_CAPTURED:
110 printf("%" PRIun " frames\n", IPC_GET_ARG1(call));
111 break;
112 default:
113 printf("Unknown event %" PRIun ".\n", IPC_GET_IMETHOD(call));
114 async_answer_0(&call, ENOTSUP);
115 continue;
116 }
117
118 if (!record) {
119 async_answer_0(&call, EOK);
120 break;
121 }
122
123 /* Write directly from device buffer to file */
124 const size_t bytes = fwrite(rec->buffer.position,
125 sizeof(uint8_t), buffer_part, rec->file);
126 printf("%zu ", bytes);
127 rec->buffer.position += buffer_part;
128
129 if (rec->buffer.position >= (rec->buffer.base + rec->buffer.size))
130 rec->buffer.position = rec->buffer.base;
131 async_answer_0(&call, EOK);
132 }
133}
134
135/**
136 * Start fragment based recording.
137 * @param rec Recording helper structure.
138 * @param f PCM format
139 */
140static void record_fragment(record_t *rec, pcm_format_t f)
141{
142 assert(rec);
143 assert(rec->device);
144 errno_t ret = audio_pcm_register_event_callback(rec->device,
145 device_event_callback, rec);
146 if (ret != EOK) {
147 printf("Failed to register for events: %s.\n", str_error(ret));
148 return;
149 }
150 rec->buffer.position = rec->buffer.base;
151 printf("Recording: %dHz, %s, %d channel(s).\n", f.sampling_rate,
152 pcm_sample_format_str(f.sample_format), f.channels);
153 const unsigned frames =
154 pcm_format_size_to_frames(rec->buffer.size / BUFFER_PARTS, &f);
155 ret = audio_pcm_start_capture_fragment(rec->device,
156 frames, f.channels, f.sampling_rate, f.sample_format);
157 if (ret != EOK) {
158 printf("Failed to start recording: %s.\n", str_error(ret));
159 return;
160 }
161
162 getchar();
163 printf("\n");
164 audio_pcm_stop_capture(rec->device);
165 /* XXX Control returns even before we can be sure callbacks finished */
166 printf("Delay before playback termination\n");
167 fibril_usleep(1000000);
168 printf("Terminate playback\n");
169}
170
171/**
172 * Record directly from a device to a file.
173 * @param device The device.
174 * @param file The file.
175 * @return 0 on succes, non-zero on failure.
176 */
177int drecord(const char *device, const char *file)
178{
179 errno_t ret = EOK;
180 audio_pcm_sess_t *session = NULL;
181 sysarg_t val;
182 if (str_cmp(device, "default") == 0) {
183 session = audio_pcm_open_default();
184 } else {
185 session = audio_pcm_open(device);
186 }
187 if (!session) {
188 printf("Failed to connect to device %s.\n", device);
189 return 1;
190 }
191 printf("Recording on device: %s.\n", device);
192 ret = audio_pcm_query_cap(session, AUDIO_CAP_CAPTURE, &val);
193 if (ret != EOK || !val) {
194 printf("Device %s does not support recording\n", device);
195 ret = ENOTSUP;
196 goto close_session;
197 }
198
199 char *info = NULL;
200 ret = audio_pcm_get_info_str(session, &info);
201 if (ret != EOK) {
202 printf("Failed to get PCM info.\n");
203 goto close_session;
204 }
205 printf("Capturing on %s.\n", info);
206 free(info);
207
208 record_t rec;
209 record_initialize(&rec, session);
210
211 ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
212 &rec.buffer.size);
213 if (ret != EOK) {
214 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
215 goto close_session;
216 }
217 printf("Buffer: %p %zu.\n", rec.buffer.base, rec.buffer.size);
218
219 rec.file = fopen(file, "w");
220 if (rec.file == NULL) {
221 ret = ENOENT;
222 printf("Failed to open file: %s.\n", file);
223 goto cleanup;
224 }
225
226 wave_header_t header;
227 fseek(rec.file, sizeof(header), SEEK_SET);
228 if (ret != EOK) {
229 printf("Error parsing wav header\n");
230 goto cleanup;
231 }
232 ret = audio_pcm_query_cap(rec.device, AUDIO_CAP_INTERRUPT, &val);
233 if (ret == EOK && val)
234 record_fragment(&rec, format);
235 else
236 printf("Recording method is not supported");
237 //TODO consider buffer position interface
238
239 wav_init_header(&header, format, ftell(rec.file) - sizeof(header));
240 fseek(rec.file, 0, SEEK_SET);
241 fwrite(&header, sizeof(header), 1, rec.file);
242
243cleanup:
244 fclose(rec.file);
245 as_area_destroy(rec.buffer.base);
246 audio_pcm_release_buffer(rec.device);
247close_session:
248 audio_pcm_close(session);
249 return ret == EOK ? 0 : 1;
250}
251/**
252 * @}
253 */
Note: See TracBrowser for help on using the repository browser.