summaryrefslogtreecommitdiff
path: root/tests/test5/old/test5.c
blob: ae9b15f019196b489a611d9e5c2d7da2d6bec797 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <gtk/gtk.h>

#include <pulse/pulseaudio.h>
#include <pulse/glib-mainloop.h>

#define APP_TITLE "Test 5 lpo"
#define BUFSIZE 1024

pa_context *context;

void create_stream();

static void stream_state_callback(pa_stream *s, void *unused) {
	switch (pa_stream_get_state(s)) {
		case PA_STREAM_UNCONNECTED:
		case PA_STREAM_CREATING:
			break;

		case PA_STREAM_READY:
			printf("PA_STREAM_READY\n");
/*
			g_assert(!mainWindow);
			mainWindow = new MainWindow(*pa_stream_get_channel_map(s), device_name, device_description);

			g_timeout_add(100, latency_func, NULL);
			pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_info_callback, NULL));
*/
			break;

		case PA_STREAM_FAILED:
			printf("PA_STREAM_FAILED\n");
			break;

		case PA_STREAM_TERMINATED:
			printf("PA_STREAM_TERMINATED\n");
			//Gtk::Main::quit();
	}
}

static void context_get_source_info_callback(pa_context *c, const pa_source_info *si, int is_last, void *unused) {
	printf("context_get_source_info_callback\n");

	if (is_last < 0) {
		printf("Failed to get source information");
		return;
	}

	if (!si)
		return;

	create_stream(si->name, si->description, si->sample_spec, si->channel_map);
}

static void context_get_sink_info_callback(pa_context *c, const pa_sink_info *si, int is_last, void *unused) {
	printf("context_get_sink_info_callback\n");
	if (is_last < 0) {
		printf("Failed to get sink information");
		return;
	}

	if (!si)
		return;

	create_stream(si->monitor_source_name, si->description, si->sample_spec, si->channel_map);
}


static void stream_read_callback(pa_stream *s, size_t l, void *unused) {
	const void *p;

	if (pa_stream_peek(s, &p, &l) < 0) {
		printf("pa_stream_peek() failed: %s", pa_strerror(pa_context_errno(context)));
		return;
	}

	printf("%f, %i\n", *((const float*)p), l/sizeof(float));

	pa_stream_drop(s);
}

static void context_state_callback(pa_context *c, void *unused) {
	switch (pa_context_get_state(c)) {
		case PA_CONTEXT_UNCONNECTED:
			printf("PA_CONTEXT_UNCONNECTED\n");
			break;
		case PA_CONTEXT_CONNECTING:
			printf("PA_CONTEXT_CONNECTING\n");
			break;
		case PA_CONTEXT_AUTHORIZING:
			printf("PA_CONTEXT_AUTHORIZING\n");
			break;
		case PA_CONTEXT_SETTING_NAME:
			printf("PA_CONTEXT_SETTING_NAME\n");
			break;

		case PA_CONTEXT_READY:
			printf("PA_CONTEXT_READY\n");
			g_assert(!stream);

			//if (device_name && mode == RECORD)
			//pa_operation_unref(pa_context_get_source_info_by_name(c, device_name, context_get_source_info_callback, NULL));
			/*else if (device_name && mode == PLAYBACK)
				pa_operation_unref(pa_context_get_sink_info_by_name(c, device_name, context_get_sink_info_callback, NULL));
			else
				pa_operation_unref(pa_context_get_server_info(c, context_get_server_info_callback, NULL));
			*/
			break;

		case PA_CONTEXT_FAILED:
			printf("PA_CONTEXT_FAILED\n");
			break;

		case PA_CONTEXT_TERMINATED:
			printf("PA_CONTEXT_TERMINATED\n");
			break;
	}
}

void create_stream(const char *name, const char *description, const pa_sample_spec *ss, const pa_channel_map *cmap) {
	/* The sample type to use */
	static const pa_sample_spec nss = {
		.format = PA_SAMPLE_S16LE,
		.rate = 44100,
		.channels = 1
	};

	pa_stream *stream=NULL;
	char t[256];
/*
	g_free(device_name);
	device_name = g_strdup(name);
	g_free(device_description);
	device_description = g_strdup(description);
*/

	printf("Using sample format: %s", pa_sample_spec_snprint(t, sizeof(t), &nss));
	printf("Using channel map: %s", pa_channel_map_snprint(t, sizeof(t), cmap));

	stream = pa_stream_new(context, APP_TITLE, &nss, cmap);
	pa_stream_set_state_callback(stream, stream_state_callback, NULL);
	pa_stream_set_read_callback(stream, stream_read_callback, NULL);
	pa_stream_connect_record(stream, name, NULL, (enum pa_stream_flags) 0);
}


int main(int argc, char*argv[]) {
	int res, retval;
/*
	int error;
	int i,j;
	float sum;
	g_thread_init(NULL);
	gdk_threads_init();
	gdk_threads_enter();
	gtk_init (&argc, &argv);
*/

/*	pa_glib_mainloop *m;
	m = pa_glib_mainloop_new(g_main_context_default()); g_assert(m);
	context = pa_context_new(pa_glib_mainloop_get_api(m), APP_TITLE); g_assert(context);
*/
	pa_mainloop *m;
	m=pa_mainloop_new(); g_assert(m);
        context = pa_context_new(pa_mainloop_get_api(m), APP_TITLE); g_assert(context);

	pa_context_set_state_callback(context, context_state_callback, NULL);
	pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);

	res=pa_mainloop_run(m,&retval);
	g_assert(res==0);
	pa_mainloop_free(m);

//	gtk_main ();
//	gdk_threads_leave();

	return 0;

}