1 /* PipeWire
2  *
3  * Copyright © 2020 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <spa/param/props.h>
26 #include <spa/param/audio/raw.h>
27 #include <spa/pod/iter.h>
28 #include <spa/utils/defs.h>
29 #include <pipewire/log.h>
30 
31 #include "log.h"
32 #include "volume.h"
33 
volume_compare(struct volume * vol,struct volume * other)34 int volume_compare(struct volume *vol, struct volume *other)
35 {
36 	uint8_t i;
37 	if (vol->channels != other->channels) {
38 		pw_log_info("channels %d<>%d", vol->channels, other->channels);
39 		return -1;
40 	}
41 	for (i = 0; i < vol->channels; i++) {
42 		if (vol->values[i] != other->values[i]) {
43 			pw_log_info("%d: val %f<>%f", i, vol->values[i], other->values[i]);
44 			return -1;
45 		}
46 	}
47 	return 0;
48 }
49 
volume_parse_param(const struct spa_pod * param,struct volume_info * info,bool monitor)50 int volume_parse_param(const struct spa_pod *param, struct volume_info *info, bool monitor)
51 {
52 	struct spa_pod_object *obj = (struct spa_pod_object *) param;
53 	struct spa_pod_prop *prop;
54 
55 	SPA_POD_OBJECT_FOREACH(obj, prop) {
56 		switch (prop->key) {
57 		case SPA_PROP_volume:
58 			if (spa_pod_get_float(&prop->value, &info->level) < 0)
59 				continue;
60 			SPA_FLAG_UPDATE(info->flags, VOLUME_HW_VOLUME,
61 					prop->flags & SPA_POD_PROP_FLAG_HARDWARE);
62 
63 			break;
64 		case SPA_PROP_mute:
65 			if (monitor)
66 				continue;
67 			if (spa_pod_get_bool(&prop->value, &info->mute) < 0)
68 				continue;
69 			SPA_FLAG_UPDATE(info->flags, VOLUME_HW_MUTE,
70 					prop->flags & SPA_POD_PROP_FLAG_HARDWARE);
71 			break;
72 		case SPA_PROP_channelVolumes:
73 			if (monitor)
74 				continue;
75 			info->volume.channels = spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
76 					info->volume.values, SPA_AUDIO_MAX_CHANNELS);
77 			SPA_FLAG_UPDATE(info->flags, VOLUME_HW_VOLUME,
78 					prop->flags & SPA_POD_PROP_FLAG_HARDWARE);
79 			break;
80 		case SPA_PROP_monitorMute:
81 			if (!monitor)
82 				continue;
83 			if (spa_pod_get_bool(&prop->value, &info->mute) < 0)
84 				continue;
85 			SPA_FLAG_CLEAR(info->flags, VOLUME_HW_MUTE);
86 			break;
87 		case SPA_PROP_monitorVolumes:
88 			if (!monitor)
89 				continue;
90 			info->volume.channels = spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
91 					info->volume.values, SPA_AUDIO_MAX_CHANNELS);
92 			SPA_FLAG_CLEAR(info->flags, VOLUME_HW_VOLUME);
93 			break;
94 		case SPA_PROP_volumeBase:
95 			if (spa_pod_get_float(&prop->value, &info->base) < 0)
96 				continue;
97 			break;
98 		case SPA_PROP_volumeStep:
99 		{
100 			float step;
101 			if (spa_pod_get_float(&prop->value, &step) >= 0)
102 				info->steps = 0x10000u * step;
103 			break;
104 		}
105 		case SPA_PROP_channelMap:
106 			info->map.channels = spa_pod_copy_array(&prop->value, SPA_TYPE_Id,
107 					info->map.map, SPA_AUDIO_MAX_CHANNELS);
108 			break;
109 		default:
110 			break;
111 		}
112 	}
113 	return 0;
114 }
115