1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <errno.h>
6 #include <ulog/ulog.h>
7 #include <string.h>
8 #include "pcm_dev.h"
9
10
11 #define LOG_TAG "[pcm_dev]"
12
audio_pcm_device_new(int dirType,const char * name,pcm_device_ops_t * ops,void * private_data)13 pcm_device_t * audio_pcm_device_new(int dirType, const char *name, pcm_device_ops_t *ops, void *private_data)
14 {
15 pcm_device_t *dev = NULL;
16
17 if((dirType < AUDIO_DEVICE_TYPE_PCM_CAPTURE) || (dirType > AUDIO_DEVICE_TYPE_PCM_PLAYBACK)) {
18 LOGE(LOG_TAG, "%s:%d, invalid dirType %d", __func__, __LINE__, dirType);
19 return NULL;
20 }
21 if(!name) {
22 LOGE(LOG_TAG, "%s:%d, pcm name is null", __func__, __LINE__);
23 return NULL;
24 }
25
26 dev = malloc(sizeof(pcm_device_t));
27 if(!dev) {
28 LOGE(LOG_TAG, "%s:%d, dev is null", __func__, __LINE__);
29 return NULL;
30 }
31 dev->dirType = dirType;
32 dev->name = strdup(name);
33 if(!dev->name) {
34 LOGE(LOG_TAG, "%s:%d, strdup name failed", __func__, __LINE__);
35 free(dev);
36 return NULL;
37 }
38 dev->ops = ops;
39 dev->private_data = private_data;
40 dlist_init(&dev->list);
41
42 return dev;
43 }
44
audio_pcm_device_free(pcm_device_t * dev)45 int audio_pcm_device_free(pcm_device_t *dev)
46 {
47 if(!dev) {
48 LOGE(LOG_TAG, "%s:%d, dev is null", __func__, __LINE__);
49 return -EINVAL;
50 }
51 dlist_del(&dev->list);
52 free(dev->name);
53 free(dev);
54 dev = NULL;
55 return 0;
56 }
57