1/***
2  This file is part of PulseAudio.
3
4  Copyright 2013 Intel Corporation
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include "stream-util.h"
25
26#include <pulse/def.h>
27
28#include <pulsecore/core-format.h>
29#include <pulsecore/macro.h>
30
31int pa_stream_get_volume_channel_map(const pa_cvolume *volume, const pa_channel_map *original_map, const pa_format_info *format,
32                                     pa_channel_map *volume_map) {
33    int r;
34    pa_channel_map volume_map_local;
35
36    pa_assert(volume);
37    pa_assert(format);
38    pa_assert(volume_map);
39
40    if (original_map) {
41        if (volume->channels == original_map->channels) {
42            *volume_map = *original_map;
43            return 0;
44        }
45
46        if (volume->channels == 1) {
47            pa_channel_map_init_mono(volume_map);
48            return 0;
49        }
50
51        pa_log_info("Invalid stream parameters: the volume is incompatible with the channel map.");
52        return -PA_ERR_INVALID;
53    }
54
55    r = pa_format_info_get_channel_map(format, &volume_map_local);
56    if (r == -PA_ERR_NOENTITY) {
57        if (volume->channels == 1) {
58            pa_channel_map_init_mono(volume_map);
59            return 0;
60        }
61
62        pa_log_info("Invalid stream parameters: multi-channel volume is set, but channel map is not.");
63        return -PA_ERR_INVALID;
64    }
65
66    if (r < 0) {
67        pa_log_info("Invalid channel map.");
68        return -PA_ERR_INVALID;
69    }
70
71    if (volume->channels == volume_map_local.channels) {
72        *volume_map = volume_map_local;
73        return 0;
74    }
75
76    if (volume->channels == 1) {
77        pa_channel_map_init_mono(volume_map);
78        return 0;
79    }
80
81    pa_log_info("Invalid stream parameters: the volume is incompatible with the channel map.");
82
83    return -PA_ERR_INVALID;
84}
85