summaryrefslogtreecommitdiffstats
path: root/kradio3/plugins/streaming/streaming-job.cpp
blob: 326661868f681f63995172b8f9ae8b0c39b56189 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***************************************************************************
                          streaming-job.cpp  -  description
                             -------------------
    begin                : Sun Sept 3 2006
    copyright            : (C) 2006 by Martin Witte
    email                : witte@kawo1.rwth-aachen.de
 ***************************************************************************/

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

#include "streaming-job.h"

#include "../../src/include/utils.h"
#include <kurl.h>

#include <kio/job.h>


StreamingJob::StreamingJob()
  : TQObject(),
    m_URL(TQString()),
    m_SoundFormat(),
    m_BufferSize(65536),
    m_Buffer(m_BufferSize),
    m_OpenCounter(0),
    m_StreamPos(0),
    m_StartTime(0),
    m_SkipCount(0),
    m_KIO_Job(NULL),
    m_capturing(false)
{
}

StreamingJob::StreamingJob(const TQString &_URL, const SoundFormat &_SoundFormat, size_t _bufferSize)
  : TQObject(),
    m_URL(_URL),
    m_SoundFormat(_SoundFormat),
    m_BufferSize(_bufferSize),
    m_Buffer(m_BufferSize),
    m_OpenCounter(0),
    m_StreamPos(0),
    m_StartTime(0),
    m_SkipCount(0),
    m_KIO_Job(NULL),
    m_capturing(false)
{
}

StreamingJob::StreamingJob(const StreamingJob &c)
  : TQObject(),
    m_URL(c.m_URL),
    m_SoundFormat(c.m_SoundFormat),
    m_BufferSize(c.m_BufferSize),
    m_Buffer(m_BufferSize),
    m_OpenCounter(0),
    m_StreamPos(0),
    m_StartTime(0),
    m_SkipCount(0),
    m_KIO_Job(NULL),
    m_capturing(c.m_capturing)
{
}

StreamingJob::~StreamingJob()
{
}


void StreamingJob::setURL(const TQString &url)
{
    if (m_URL != url) {
        m_URL = url;
        delete m_KIO_Job;
        m_KIO_Job = NULL;
        if (!m_capturing) {
            startPutJob();
        } else {    
            startGetJob();
        }
    }
}


void StreamingJob::setSoundFormat(const SoundFormat &sf)
{
    m_SoundFormat = sf;
}


void StreamingJob::setBufferSize(size_t buffer_size)
{
    if (m_BufferSize != buffer_size) {
        m_Buffer.clear();
        m_Buffer.resize(m_BufferSize = buffer_size);
    }
}


bool StreamingJob::startPutJob()
{
    m_KIO_Job = KIO::put(m_URL, -1, true, false, false);
    if (!m_KIO_Job)
        return false;
    m_KIO_Job->setAsyncDataEnabled(true);
    connect (m_KIO_Job, TQT_SIGNAL(dataReq(KIO::Job *job, TQByteArray &data)),
        this, TQT_SLOT(slotWriteData  (KIO::Job *job, TQByteArray &data)));
    connect (m_KIO_Job, TQT_SIGNAL(result(KIO::Job *)),
        this, TQT_SLOT(slotIOJobResult(KIO::Job *)));
    return true;
}


bool StreamingJob::startPlayback()
{
    if (!m_OpenCounter) {
        m_Buffer.clear();
        m_OpenCounter = 1;
        if (!startPutJob())
            return false;
        m_StartTime = time(NULL);
        m_StreamPos = 0;
        if (m_KIO_Job->error()) {
            emit logStreamError(m_URL, m_KIO_Job->errorString());
        }
        return m_KIO_Job->error() == 0;
    }
    else {
        return true;
    }
}

bool StreamingJob::stopPlayback()
{
    if (m_OpenCounter) {
        if (!--m_OpenCounter) {
            delete m_KIO_Job;
            m_KIO_Job = NULL;
        }
    }
    return true;
}


bool StreamingJob::startGetJob()
{
    m_KIO_Job = KIO::get(m_URL, false, false);
    if (!m_KIO_Job)
        return false;
    m_KIO_Job->setAsyncDataEnabled(true);
    connect (m_KIO_Job, TQT_SIGNAL(data(KIO::Job *, const TQByteArray &)),
                this, TQT_SLOT(slotReadData(KIO::Job *, const TQByteArray &)));
    connect (m_KIO_Job, TQT_SIGNAL(result(KIO::Job *)),
                this, TQT_SLOT(slotIOJobResult(KIO::Job *)));
    return true;
}


bool StreamingJob::startCapture(const SoundFormat &/*proposed_format*/,
                                SoundFormat       &real_format,
                                bool               /*force_format*/)
{
    if (!m_OpenCounter) {
        m_capturing = true;
        m_Buffer.clear();
        if (!startGetJob())
            return false;
        m_StartTime = time(NULL);
        m_StreamPos = 0;
        if (m_KIO_Job->error()) {
            emit logStreamError(m_URL, m_KIO_Job->errorString());
        }
        return m_KIO_Job->error() == 0;
    }
    ++m_OpenCounter;
    real_format = m_SoundFormat;
    return true;
}


bool StreamingJob::stopCapture()
{
    if (m_OpenCounter) {
        if (!--m_OpenCounter) {
            delete m_KIO_Job;
            m_KIO_Job = NULL;
        }
    }
    return true;
}


void   StreamingJob::slotReadData  (KIO::Job */*job*/, const TQByteArray &data)
{
    size_t free = m_Buffer.getFreeSize();
    if (free < data.size()) {
        m_SkipCount += data.size() - free;
        emit logStreamWarning(m_URL, i18n("skipped %1 bytes").arg(data.size() - free)); 
    }
    else {
        free = data.size();
    }

    m_Buffer.addData(data.data(), free);
    m_StreamPos += free;

    if (m_Buffer.getFreeSize() < data.size()) {
        m_KIO_Job->suspend();
    }
}


void   StreamingJob::slotWriteData (KIO::Job */*job*/, TQByteArray &)
{
    size_t size = m_Buffer.getFillSize();
    if (size) {
        char *buf = new char [size];
        size = m_Buffer.takeData(buf, size);
        TQByteArray data;
        data.assign(buf, size);
        m_KIO_Job->sendAsyncData(data);
        m_StreamPos += size;
    }
    else {
        // does a warning really make sense here?
        //emit logStreamWarning(m_URL, i18n("buffer underrun")); 
        m_SkipCount++; 
    }
}


void StreamingJob::playData(const char *data, size_t size, size_t &consumed_size)
{
    size_t free = m_Buffer.getFreeSize();
    consumed_size = (consumed_size == SIZE_T_DONT_CARE) ? free : min(consumed_size, free);
    if (free > size) {
        free = size;
    }
    m_Buffer.addData(data, free);
}


bool   StreamingJob::hasRecordedData() const
{
    return m_Buffer.getFillSize() > m_Buffer.getSize() / 3;
}


void StreamingJob::lockData(const char *&data, size_t &size, SoundMetaData &meta_data)
{
    data = m_Buffer.getData(size);
    time_t cur_time = time(NULL);
    meta_data = SoundMetaData(m_StreamPos, cur_time - m_StartTime, cur_time, m_URL);
}


void StreamingJob::removeData(size_t size)
{
    m_Buffer.removeData(size);
    if (m_Buffer.getFreeSize() > m_Buffer.getSize() / 2) {
        m_KIO_Job->resume();
    }
}

void   StreamingJob::slotIOJobResult (KIO::Job *job)
{
    if (job && job->error()) {
        emit logStreamError(m_URL, job->errorString());
    }
}

#include "streaming-job.moc"