summaryrefslogtreecommitdiffstats
path: root/akode/plugins/pulse_sink/pulse_sink.cpp
blob: 395bfb31056e82654e447b139d583c64529bd241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*  aKode: Pulse-Sink

    Copyright (C) 2004 Allan Sandfeld Jensen <kde@carewolf.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"

#ifdef HAVE_STDINT_H
#include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#endif
#include <pulse/simple.h>

#include "audioframe.h"
#include "audiobuffer.h"
#include "pulse_sink.h"

#include <iostream>

namespace aKode {

extern "C" { PulseSinkPlugin pulse_sink; };

struct PulseSink::private_data
{
    private_data() : server(0), error(false) {};

    pa_simple *server;
    pa_sample_spec sample_spec;

    bool error;

    AudioConfiguration config;
};

PulseSink::PulseSink()
{
    m_data = new private_data;
    m_data->sample_spec.rate = 44100;
    m_data->sample_spec.channels = 2;
    m_data->sample_spec.format = PA_SAMPLE_S16NE;
}

bool PulseSink::open() {
    int error = 0;
    m_data->server = pa_simple_new(0, "akode-client", PA_STREAM_PLAYBACK, 0, "", &m_data->sample_spec, 0, 0, &error );
    if (!m_data->server || error != 0) {
        m_data->error = true;
        close();
        std::cout << "Cannot open client\n";
        return false;
    }


    return true;
}

void PulseSink::close() {
    if (m_data->server) {
        pa_simple_free(m_data->server);
        m_data->server = 0;
    }
}

PulseSink::~PulseSink()
{
    close();
    delete m_data;
}

int PulseSink::setAudioConfiguration(const AudioConfiguration* config)
{
    if (m_data->error) return -1;

    int res = 0;
    if (*config == m_data->config) return 0;
    m_data->config = *config;

    if (config->channel_config != MonoStereo ) return -1;
    m_data->sample_spec.channels = config->channels;

    if (config->sample_width != 16) {
        res = 1;
        m_data->config.sample_width = 16;
    }

    m_data->sample_spec.rate = config->sample_rate;

    // create new connection
    close();
    open();

    return res;
}

const AudioConfiguration* PulseSink::audioConfiguration() const
{
    return &m_data->config;
}

bool PulseSink::writeFrame(AudioFrame* frame)
{
    if ( m_data->error ) return false;

    if ( frame->channels != m_data->config.channels || frame->sample_rate != m_data->config.sample_rate)
    {
        if (setAudioConfiguration(frame)!=0)
            return false;
    }

    int channels = m_data->config.channels;
    int length = frame->length;

    int16_t *buffer = new int16_t[length*channels];
    int16_t** data = (int16_t**)frame->data;
    for(int i = 0; i<length; i++)
        for(int j=0; j<channels; j++)
            buffer[i*channels+j] = data[j][i];

    int error = 0;
    pa_simple_write(m_data->server, buffer, channels*length*2, &error);
    delete[] buffer;

    return (error == 0);
}

} // namespace