1 | //driver_sysclock.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #ifdef ROAR_HAVE_DRIVER_SYSCLOCK |
---|
29 | int driver_sysclock_open_vio(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) { |
---|
30 | struct driver_sysclock * self = roar_mm_malloc(sizeof(struct driver_sysclock)); |
---|
31 | |
---|
32 | if ( self == NULL ) |
---|
33 | return -1; |
---|
34 | |
---|
35 | if ( device != NULL ) { |
---|
36 | return -1; |
---|
37 | } |
---|
38 | |
---|
39 | memset(self, 0, sizeof(struct driver_sysclock)); |
---|
40 | memset(inst, 0, sizeof(struct roar_vio_calls)); |
---|
41 | inst->flags = ROAR_VIO_FLAGS_NONE; |
---|
42 | inst->refc = 1; |
---|
43 | |
---|
44 | inst->inst = self; |
---|
45 | inst->close = driver_sysclock_close; |
---|
46 | inst->write = driver_sysclock_write; |
---|
47 | inst->ctl = driver_dummy_ctl; |
---|
48 | |
---|
49 | self->bps = roar_info2bitspersec(info)/8; |
---|
50 | |
---|
51 | if (!self->bps) { |
---|
52 | roar_mm_free(self); |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | switch (info->codec) { |
---|
57 | case ROAR_CODEC_PCM_S_LE: |
---|
58 | case ROAR_CODEC_PCM_S_BE: |
---|
59 | case ROAR_CODEC_PCM_S_PDP: |
---|
60 | case ROAR_CODEC_PCM_U_LE: |
---|
61 | case ROAR_CODEC_PCM_U_BE: |
---|
62 | case ROAR_CODEC_PCM_U_PDP: |
---|
63 | case ROAR_CODEC_ALAW: |
---|
64 | case ROAR_CODEC_MULAW: |
---|
65 | break; |
---|
66 | default: |
---|
67 | roar_mm_free(self); |
---|
68 | return -1; |
---|
69 | break; |
---|
70 | } |
---|
71 | |
---|
72 | gettimeofday(&(self->lasttime), NULL); |
---|
73 | |
---|
74 | self->last_wanted = 0; |
---|
75 | |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | int driver_sysclock_close (struct roar_vio_calls * vio) { |
---|
80 | struct driver_sysclock * self = vio->inst; |
---|
81 | |
---|
82 | if ( self == NULL ) |
---|
83 | return -1; |
---|
84 | |
---|
85 | roar_mm_free(self); |
---|
86 | |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | ssize_t driver_sysclock_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
91 | struct driver_sysclock * self = vio->inst; |
---|
92 | struct timeval now; |
---|
93 | long long diff = (1000000LL * count / (long long)self->bps); |
---|
94 | long long ago; |
---|
95 | |
---|
96 | gettimeofday(&now, NULL); |
---|
97 | |
---|
98 | ago = now.tv_usec - self->lasttime.tv_usec; |
---|
99 | ago += 1000000*(now.tv_sec - self->lasttime.tv_sec); |
---|
100 | |
---|
101 | ago -= self->last_wanted; |
---|
102 | |
---|
103 | memcpy(&(self->lasttime), &now, sizeof(now)); |
---|
104 | |
---|
105 | ROAR_DBG("driver_sysclock_write(*): count=%u, bps=%u, diff=%llu, ago=%llu", count, self->bps, diff, ago); |
---|
106 | |
---|
107 | diff -= ago; |
---|
108 | |
---|
109 | self->last_wanted = diff; |
---|
110 | |
---|
111 | ROAR_DBG("driver_sysclock_write(*): diff=%lli", diff); |
---|
112 | |
---|
113 | if ( diff > 0 ) |
---|
114 | roar_usleep(diff); |
---|
115 | |
---|
116 | return count; |
---|
117 | } |
---|
118 | |
---|
119 | #endif |
---|
120 | |
---|
121 | //ll |
---|