Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 2cc5c835 in mainline


Ignore:
Timestamp:
2012-07-15T15:14:10Z (10 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial
Children:
57e8b3b
Parents:
b497018
Message:

Cleanup audio_pcm interface.

Location:
uspace
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/dplay/dplay.c

    rb497018 r2cc5c835  
    6363        fibril_mutex_t mutex;
    6464        fibril_condvar_t cv;
    65         async_exch_t *device;
     65        audio_pcm_sess_t *device;
    6666} playback_t;
    6767
    68 static void playback_initialize(playback_t *pb, async_exch_t *exch)
    69 {
    70         assert(exch);
     68static void playback_initialize(playback_t *pb, audio_pcm_sess_t *sess)
     69{
     70        assert(sess);
    7171        assert(pb);
    7272        pb->buffer.base = NULL;
     
    7575        pb->playing = false;
    7676        pb->source = NULL;
    77         pb->device = exch;
     77        pb->device = sess;
    7878        fibril_mutex_initialize(&pb->mutex);
    7979        fibril_condvar_initialize(&pb->cv);
     
    168168        }
    169169
    170         devman_handle_t pcm_handle;
    171         int ret = devman_fun_get_handle(device, &pcm_handle, 0);
    172         if (ret != EOK) {
    173                 printf("Failed to get device(%s) handle: %s.\n",
    174                     device, str_error(ret));
    175                 return 1;
    176         }
    177 
    178         async_sess_t *session = devman_device_connect(
    179             EXCHANGE_SERIALIZE, pcm_handle, IPC_FLAG_BLOCKING);
     170        audio_pcm_sess_t *session = audio_pcm_open(device);
    180171        if (!session) {
    181172                printf("Failed to connect to device.\n");
     
    183174        }
    184175
    185         async_exch_t *exch = async_exchange_begin(session);
    186         if (!exch) {
    187                 ret = EPARTY;
    188                 printf("Failed to start session exchange.\n");
    189                 goto close_session;
    190         }
    191176        const char* info = NULL;
    192         ret = audio_pcm_get_info_str(exch, &info);
     177        int ret = audio_pcm_get_info_str(session, &info);
    193178        if (ret != EOK) {
    194179                printf("Failed to get PCM info.\n");
     
    199184
    200185        playback_t pb;
    201         playback_initialize(&pb, exch);
     186        playback_initialize(&pb, session);
    202187
    203188        ret = audio_pcm_get_buffer(pb.device, &pb.buffer.base,
     
    236221cleanup:
    237222        munmap(pb.buffer.base, pb.buffer.size);
    238         audio_pcm_release_buffer(exch);
     223        audio_pcm_release_buffer(pb.device);
    239224close_session:
    240         async_exchange_end(exch);
    241         async_hangup(session);
     225        audio_pcm_close(session);
    242226        return ret == EOK ? 0 : 1;
    243227}
  • uspace/app/drec/drec.c

    rb497018 r2cc5c835  
    6565        } buffer;
    6666        FILE* file;
    67         async_exch_t *device;
     67        audio_pcm_sess_t *device;
    6868} record_t;
    6969
    70 static void record_initialize(record_t *rec, async_exch_t *exch)
    71 {
    72         assert(exch);
     70static void record_initialize(record_t *rec, audio_pcm_sess_t *sess)
     71{
     72        assert(sess);
    7373        assert(rec);
    7474        rec->buffer.base = NULL;
     
    7676        rec->buffer.position = NULL;
    7777        rec->file = NULL;
    78         rec->device = exch;
     78        rec->device = sess;
    7979}
    8080
     
    151151        }
    152152
    153         devman_handle_t pcm_handle;
    154         int ret = devman_fun_get_handle(device, &pcm_handle, 0);
    155         if (ret != EOK) {
    156                 printf("Failed to get device(%s) handle: %s.\n",
    157                     device, str_error(ret));
    158                 return 1;
    159         }
    160 
    161         async_sess_t *session = devman_device_connect(
    162             EXCHANGE_SERIALIZE, pcm_handle, IPC_FLAG_BLOCKING);
     153
     154        audio_pcm_sess_t *session = audio_pcm_open(device);
    163155        if (!session) {
    164156                printf("Failed to connect to device.\n");
     
    166158        }
    167159
    168         async_exch_t *exch = async_exchange_begin(session);
    169         if (!exch) {
    170                 ret = EPARTY;
    171                 printf("Failed to start session exchange.\n");
    172                 goto close_session;
    173         }
    174160        const char* info = NULL;
    175         ret = audio_pcm_get_info_str(exch, &info);
     161        int ret = audio_pcm_get_info_str(session, &info);
    176162        if (ret != EOK) {
    177163                printf("Failed to get PCM info.\n");
     
    182168
    183169        record_t rec;
    184         record_initialize(&rec, exch);
     170        record_initialize(&rec, session);
    185171
    186172        ret = audio_pcm_get_buffer(rec.device, &rec.buffer.base,
     
    220206cleanup:
    221207        munmap(rec.buffer.base, rec.buffer.size);
    222         audio_pcm_release_buffer(exch);
     208        audio_pcm_release_buffer(rec.device);
    223209close_session:
    224         async_exchange_end(exch);
    225210        async_hangup(session);
    226211        return ret == EOK ? 0 : 1;
  • uspace/lib/drv/generic/remote_audio_pcm.c

    rb497018 r2cc5c835  
    3333
    3434#include <async.h>
     35#include <devman.h>
    3536#include <ddf/log.h>
    3637#include <errno.h>
     
    5556 * CLIENT SIDE
    5657 */
    57 int audio_pcm_get_info_str(async_exch_t *exch, const char **name)
    58 {
    59         if (!exch)
    60                 return EINVAL;
     58audio_pcm_sess_t *audio_pcm_open(const char *name)
     59{
     60        devman_handle_t device_handle = 0;
     61        const int ret = devman_fun_get_handle(name, &device_handle, 0);
     62        if (ret != EOK)
     63                return NULL;
     64        return devman_device_connect(EXCHANGE_SERIALIZE, device_handle,
     65            IPC_FLAG_BLOCKING);
     66}
     67
     68audio_pcm_sess_t *audio_pcm_open_service(service_id_t id)
     69{
     70        return loc_service_connect(EXCHANGE_SERIALIZE, id, IPC_FLAG_BLOCKING);
     71}
     72
     73void audio_pcm_close(audio_pcm_sess_t *sess)
     74{
     75        if (sess)
     76                async_hangup(sess);
     77}
     78
     79int audio_pcm_get_info_str(audio_pcm_sess_t *sess, const char **name)
     80{
     81        async_exch_t *exch = async_exchange_begin(sess);
    6182        sysarg_t name_size;
    6283        const int ret = async_req_1_1(exch,
     
    6990                         * as it waits for read request */
    7091                        async_data_read_start(exch, (void*)-1, 0);
     92                        async_exchange_end(exch);
    7193                        return ENOMEM;
    7294                }
     
    7597                if (ret != EOK) {
    7698                        free(name_place);
     99                        async_exchange_end(exch);
    77100                        return ret;
    78101                }
    79102                *name = name_place;
    80103        }
    81         return ret;
    82 }
    83 
    84 int audio_pcm_get_buffer(async_exch_t *exch, void **buffer, size_t *size,
     104        async_exchange_end(exch);
     105        return ret;
     106}
     107
     108int audio_pcm_get_buffer(audio_pcm_sess_t *sess, void **buffer, size_t *size,
    85109    async_client_conn_t event_rec, void* arg)
    86110{
    87         if (!exch || !buffer || !size)
     111        if (!buffer || !size)
    88112                return EINVAL;
     113
     114        async_exch_t *exch = async_exchange_begin(sess);
    89115
    90116        sysarg_t buffer_size = *size;
     
    96122                int ret = async_share_in_start_0_0(exch, buffer_size, &dst);
    97123                if (ret != EOK) {
     124                        async_exchange_end(exch);
    98125                        return ret;
    99126                }
    100127                ret = async_connect_to_me(exch, 0, 0, 0, event_rec, arg);
    101128                if (ret != EOK) {
     129                        async_exchange_end(exch);
    102130                        return ret;
    103131                }
     
    106134                *size = buffer_size;
    107135        }
    108         return ret;
    109 }
    110 
    111 int audio_pcm_release_buffer(async_exch_t *exch)
    112 {
    113         if (!exch)
    114                 return EINVAL;
    115         return async_req_1_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
     136        async_exchange_end(exch);
     137        return ret;
     138}
     139
     140int audio_pcm_release_buffer(audio_pcm_sess_t *sess)
     141{
     142        async_exch_t *exch = async_exchange_begin(sess);
     143        const int ret = async_req_1_0(exch,
     144            DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    116145            IPC_M_AUDIO_PCM_RELEASE_BUFFER);
    117 }
    118 
    119 int audio_pcm_start_playback(async_exch_t *exch, unsigned parts,
     146        async_exchange_end(exch);
     147        return ret;
     148}
     149
     150int audio_pcm_start_playback(audio_pcm_sess_t *sess, unsigned parts,
    120151    unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
    121152{
    122         if (!exch)
    123                 return EINVAL;
    124153        if (parts > UINT8_MAX || channels > UINT8_MAX)
    125154                return EINVAL;
     
    127156        const sysarg_t packed =
    128157            (parts << 24) | (channels << 16) | (format & UINT16_MAX);
    129         return async_req_3_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    130             IPC_M_AUDIO_PCM_START_PLAYBACK, sample_rate, packed);
    131 }
    132 
    133 int audio_pcm_stop_playback(async_exch_t *exch)
    134 {
    135         if (!exch)
    136                 return EINVAL;
    137         return async_req_1_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
     158        async_exch_t *exch = async_exchange_begin(sess);
     159        const int ret = async_req_3_0(exch,
     160            DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
     161            IPC_M_AUDIO_PCM_START_PLAYBACK,
     162            sample_rate, packed);
     163        async_exchange_end(exch);
     164        return ret;
     165}
     166
     167int audio_pcm_stop_playback(audio_pcm_sess_t *sess)
     168{
     169        async_exch_t *exch = async_exchange_begin(sess);
     170        const int ret = async_req_1_0(exch,
     171            DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    138172            IPC_M_AUDIO_PCM_STOP_PLAYBACK);
    139 }
    140 
    141 int audio_pcm_start_record(async_exch_t *exch, unsigned parts,
     173        async_exchange_end(exch);
     174        return ret;
     175}
     176
     177int audio_pcm_start_record(audio_pcm_sess_t *sess, unsigned parts,
    142178    unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
    143179{
    144         if (!exch)
    145                 return EINVAL;
    146180        if (parts > UINT8_MAX || channels > UINT8_MAX)
    147181                return EINVAL;
     
    149183        const sysarg_t packed =
    150184            (parts << 24) | (channels << 16) | (format & UINT16_MAX);
    151         return async_req_3_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    152             IPC_M_AUDIO_PCM_START_RECORD, sample_rate, packed);
    153 }
    154 
    155 int audio_pcm_stop_record(async_exch_t *exch)
    156 {
    157         if (!exch)
    158                 return EINVAL;
    159         return async_req_1_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    160             IPC_M_AUDIO_PCM_STOP_RECORD);
     185        async_exch_t *exch = async_exchange_begin(sess);
     186        const int ret = async_req_3_0(exch,
     187            DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_START_RECORD,
     188            sample_rate, packed);
     189        async_exchange_end(exch);
     190        return ret;
     191}
     192
     193int audio_pcm_stop_record(audio_pcm_sess_t *sess)
     194{
     195        async_exch_t *exch = async_exchange_begin(sess);
     196        const int ret = async_req_1_0(exch,
     197            DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_STOP_RECORD);
     198        async_exchange_end(exch);
     199        return ret;
    161200}
    162201
  • uspace/lib/drv/include/audio_pcm_iface.h

    rb497018 r2cc5c835  
    3939#include <async.h>
    4040#include <bool.h>
     41#include <loc.h>
    4142#include <pcm_sample_format.h>
    4243
     
    5051};
    5152
     53typedef async_sess_t audio_pcm_sess_t;
    5254
    53 int audio_pcm_get_info_str(async_exch_t *, const char **);
    54 int audio_pcm_get_buffer(async_exch_t *, void **, size_t *,
     55audio_pcm_sess_t *audio_pcm_open(const char *);
     56audio_pcm_sess_t *audio_pcm_open_service(service_id_t service);
     57void audio_pcm_close(audio_pcm_sess_t *);
     58
     59int audio_pcm_get_info_str(audio_pcm_sess_t *, const char **);
     60int audio_pcm_get_buffer(audio_pcm_sess_t *, void **, size_t *,
    5561    async_client_conn_t, void *);
    56 int audio_pcm_release_buffer(async_exch_t *);
     62int audio_pcm_release_buffer(audio_pcm_sess_t *);
    5763
    58 int audio_pcm_start_playback(async_exch_t *, unsigned,
     64int audio_pcm_start_playback(audio_pcm_sess_t *, unsigned,
    5965    unsigned, unsigned, pcm_sample_format_t);
    60 int audio_pcm_stop_playback(async_exch_t *);
     66int audio_pcm_stop_playback(audio_pcm_sess_t *);
    6167
    62 int audio_pcm_start_record(async_exch_t *, unsigned,
     68int audio_pcm_start_record(audio_pcm_sess_t *, unsigned,
    6369    unsigned, unsigned, pcm_sample_format_t);
    64 int audio_pcm_stop_record(async_exch_t *);
     70int audio_pcm_stop_record(audio_pcm_sess_t *);
    6571
    6672/** Audio pcm communication interface. */
  • uspace/srv/audio/hound/audio_device.c

    rb497018 r2cc5c835  
    4242#include <str_error.h>
    4343
    44 #include <audio_pcm_iface.h>
    4544
    4645#include "audio_device.h"
     
    5453static int get_buffer(audio_device_t *dev);
    5554static int release_buffer(audio_device_t *dev);
    56 static int start_playback(audio_device_t *dev);
    57 static int stop_playback(audio_device_t *dev);
    58 static int start_recording(audio_device_t *dev);
    59 static int stop_recording(audio_device_t *dev);
    6055
    6156
     
    6661        dev->id = id;
    6762        dev->name = str_dup(name);
    68         dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, id, 0);
     63        dev->sess = audio_pcm_open_service(id);
    6964        if (!dev->sess) {
    7065                log_debug("Failed to connect to device \"%s\"", name);
     
    108103                }
    109104
    110                 ret = start_playback(dev);
     105                /* Fill the buffer first */
     106                audio_sink_mix_inputs(&dev->sink,
     107                    dev->buffer.base, dev->buffer.size);
     108
     109                ret = audio_pcm_start_playback(dev->sess, BUFFER_BLOCKS,
     110                    dev->sink.format.channels, dev->sink.format.sampling_rate,
     111                    dev->sink.format.sample_format);
    111112                if (ret != EOK) {
    112113                        log_error("Failed to start playback: %s",
     
    119120                assert(!new);
    120121                log_verbose("No connections on device sink '%s'", sink->name);
    121                 int ret = stop_playback(dev);
     122                int ret = audio_pcm_stop_playback(dev->sess);
    122123                if (ret != EOK) {
    123124                        log_error("Failed to start playback: %s",
     
    147148                        return ret;
    148149                }
    149                 ret = start_recording(dev);
     150                ret = audio_pcm_start_record(dev->sess, BUFFER_BLOCKS,
     151                    dev->sink.format.channels, dev->sink.format.sampling_rate,
     152                    dev->sink.format.sample_format);
    150153                if (ret != EOK) {
    151154                        log_error("Failed to start recording: %s",
     
    155158                }
    156159        } else { /* Disconnected */
    157                 int ret = stop_recording(dev);
     160                int ret = audio_pcm_stop_record(dev->sess);
    158161                if (ret != EOK) {
    159162                        log_error("Failed to start recording: %s",
     
    229232        dev->buffer.size = 0;
    230233
    231         async_exch_t *exch = async_exchange_begin(dev->sess);
    232         const int ret = audio_pcm_get_buffer(exch, &dev->buffer.base,
     234        return audio_pcm_get_buffer(dev->sess, &dev->buffer.base,
    233235            &dev->buffer.size, device_event_callback, dev);
    234         async_exchange_end(exch);
    235         return ret;
    236 }
    237 
    238 #define CHECK_BUFFER_AND_CONNECTION() \
    239 do { \
    240         assert(dev); \
    241         if (!dev->sess) { \
    242                 log_debug("No connection to device"); \
    243                 return EIO; \
    244         } \
    245         if (!dev->buffer.base) { \
    246                 log_debug("We don't have a buffer"); \
    247                 return ENOENT; \
    248         } \
    249 } while (0)
    250 
     236}
    251237
    252238static int release_buffer(audio_device_t *dev)
    253239{
    254         CHECK_BUFFER_AND_CONNECTION();
    255 
    256         async_exch_t *exch = async_exchange_begin(dev->sess);
    257         const int ret = audio_pcm_release_buffer(exch);
    258         async_exchange_end(exch);
     240        assert(dev);
     241        assert(dev->buffer.base);
     242
     243        const int ret = audio_pcm_release_buffer(dev->sess);
    259244        if (ret == EOK) {
    260245                dev->buffer.base = NULL;
    261246                dev->buffer.size = 0;
    262247                dev->buffer.position = NULL;
     248        } else {
     249                log_debug("Failed to release buffer: %s", str_error(ret));
    263250        }
    264251        return ret;
    265252}
    266 
    267 static int start_playback(audio_device_t *dev)
    268 {
    269         CHECK_BUFFER_AND_CONNECTION();
    270 
    271         /* Fill the buffer first */
    272         audio_sink_mix_inputs(&dev->sink, dev->buffer.base, dev->buffer.size);
    273 
    274         async_exch_t *exch = async_exchange_begin(dev->sess);
    275         const int ret = audio_pcm_start_playback(exch,
    276             BUFFER_BLOCKS, dev->sink.format.channels,
    277             dev->sink.format.sampling_rate, dev->sink.format.sample_format);
    278         async_exchange_end(exch);
    279         return ret;
    280 }
    281 
    282 static int stop_playback(audio_device_t *dev)
    283 {
    284         CHECK_BUFFER_AND_CONNECTION();
    285 
    286         async_exch_t *exch = async_exchange_begin(dev->sess);
    287         const int ret = audio_pcm_stop_playback(exch);
    288         async_exchange_end(exch);
    289         return ret;
    290 }
    291 
    292 static int start_recording(audio_device_t *dev)
    293 {
    294         CHECK_BUFFER_AND_CONNECTION();
    295 
    296         async_exch_t *exch = async_exchange_begin(dev->sess);
    297         const int ret = audio_pcm_start_record(exch,
    298             BUFFER_BLOCKS, dev->sink.format.channels,
    299             dev->sink.format.sampling_rate, dev->sink.format.sample_format);
    300         async_exchange_end(exch);
    301         return ret;
    302 }
    303 
    304 static int stop_recording(audio_device_t *dev)
    305 {
    306         CHECK_BUFFER_AND_CONNECTION();
    307 
    308         async_exch_t *exch = async_exchange_begin(dev->sess);
    309         const int ret = audio_pcm_stop_record(exch);
    310         async_exchange_end(exch);
    311         return ret;
    312 }
    313 
    314253/**
    315254 * @}
  • uspace/srv/audio/hound/audio_device.h

    rb497018 r2cc5c835  
    4343#include <errno.h>
    4444#include <ipc/loc.h>
     45#include <audio_pcm_iface.h>
    4546
    4647#include "audio_source.h"
     
    5051        link_t link;
    5152        service_id_t id;
    52         async_sess_t *sess;
     53        audio_pcm_sess_t *sess;
    5354        char *name;
    5455        struct {
Note: See TracChangeset for help on using the changeset viewer.