Child: [084e49] (diff)

Download this file

songcastsender.cpp    271 lines (241 with data), 9.0 kB

  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
/* Copyright (C) 2015 J.F.Dockes
* 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.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "songcastsender.h"
extern bool g_quitrequest;
SongcastSender::SongcastSender(Environment& aEnv, OhmSender* aSender,
OhmSenderDriver* aDriver, const Brx& aUri,
AudioReader* audio, const string& codec,
bool paced)
: iEnv(aEnv)
, iSender(aSender)
, iDriver(aDriver)
, iUri(aUri)
, m_audio(audio)
, iTimer(aEnv, MakeFunctor(*this, &SongcastSender::TimerExpired), "SongcastSender")
, iMutex("SCSP")
, iPaused(true)
, iSpeed(kSpeedNormal)
, iIndex(0)
, iLastTimeUs(0)
, iTimeOffsetUs(0)
, iVerbose(false)
, iPaced(paced)
{
CalculatePacketBytes();
m_encoder = AudioEncoderFactory::CreateAudioEncoder(codec, audio, aDriver,
iPacketBytes);
LOGDEB("bytes per packet: " << iPacketBytes << endl);
LOGDEB("frames per packet: " << iPacketFrames << endl);
LOGDEB("usec per packet: "<< iPacketTime << endl);
}
// We return true if the main thread should pause.
bool SongcastSender::Start(TBool aEnabled)
{
iSender->SetEnabled(true);
iSender->SetTrack(iUri, Brx::Empty(), m_audio->sampleCount(), 0);
iSender->SetMetatext(Brn("SongcastSender repeated play"));
// It seems that both hijacking the main thread and using the
// timer with a short timeout (see TimerExpired()) work.
// Don't know what's best. The timer approach is closer to the original
// code and leaves the main thread free for control ops if needed.
// Otoh, if no data appears on the fifo, the timer thread is stuck in read
// and the upnp side stops working (no sender advertised).
// Maybe the best approach would be to start a separate thread and
// use busyreading. Using the main thread for now.
static const bool optionbusy = true;
if (iPaced || !optionbusy) {
LOGDEB("SongcastSender::Start: using timers. Enabled? " << aEnabled << endl);
if (aEnabled)
Play();
return true;
} else {
LOGDEB("SongcastSender::Start: block on reading only\n");
busyRdWr();
return false;
}
}
void SongcastSender::Play()
{
iMutex.Wait();
if (iPaused) {
m_encoder->start();
if (m_audio)
m_audio->open();
iPaused = false;
iLastTimeUs = 0;
iTimeOffsetUs = 0;
iTimer.FireIn(kPeriodMs);
}
iMutex.Signal();
}
void SongcastSender::Stop()
{
iMutex.Wait();
if (!iPaused) {
m_encoder->finish();
if (m_audio)
m_audio->close();
iPaused = true;
}
iMutex.Signal();
}
void SongcastSender::PlayPause()
{
if (iPaused)
Play();
else
Stop();
}
void SongcastSender::Restart()
{
iMutex.Wait();
iIndex = 0;
iMutex.Signal();
}
TBool SongcastSender::Paused()
{
return (iPaused);
}
const string SongcastSender::CodecName()
{
return m_encoder->name();
}
void SongcastSender::CalculatePacketBytes()
{
// in order to let wavsender change the playback rate,
// we keep constant it's idea of how much audio time is in each packet,
// but vary the amount of data that is actually sent
// calculate the amount of time in each packet
TUint norm_bytes = (m_audio->sampleRate() * m_audio->bytesPerFrame() *
kPeriodMs) / 1000;
if (norm_bytes > kMaxPacketBytes) {
norm_bytes = kMaxPacketBytes;
}
TUint norm_packet_samples = norm_bytes / m_audio->bytesPerFrame();
iPacketTime = (norm_packet_samples*1000000/(m_audio->sampleRate()/10) + 5)/10;
// calculate the adjusted speed packet size
TUint bytes = (norm_bytes * iSpeed) / 100;
if (bytes > kMaxPacketBytes) {
bytes = kMaxPacketBytes;
}
iPacketFrames = bytes / m_audio->bytesPerFrame();
iPacketBytes = iPacketFrames * m_audio->bytesPerFrame();
}
void SongcastSender::busyRdWr()
{
LOGDEB("SongcastSender:busyRdWr: packetbytes " << iPacketBytes << endl);
while (true) {
if (g_quitrequest) {
return;
}
ssize_t nread = 0;
const unsigned char *cp = m_audio->data((size_t)iPacketBytes, nread);
if (cp == 0) {
return;
}
if (g_quitrequest) {
return;
}
m_encoder->encode(cp, nread, iPaused || g_quitrequest);
}
}
void SongcastSender::TimerExpired()
{
iMutex.Wait();
bool pause = iPaused || g_quitrequest;
ssize_t nread = 0;
const unsigned char *cp = m_audio->data((size_t)iPacketBytes, nread);
if (nread > 0) {
if ((size_t)nread < iPacketBytes) {
LOGDEB("SongcastSender::TimerExpired: requested " << iPacketBytes
<< " bytes, read " << nread << " bytes" << endl);
}
m_encoder->encode(cp, nread, pause);
} else if (pause) {
// The audio stream was paused and no data could be read from the
// audio source anymore. To notify the receivers, send an empty audio
// message with the halt flag set.
LOGDEB("SongcastSender::Send empty audio message with pause flag set\n");
m_encoder->encode(cp, 0, true);
}
if (!pause) {
TUint64 now = OsTimeInUs(iEnv.OsCtx());
if (!iPaced) {
// Means we're doing blocking reads on the source, and
// it's setting the pace. I'd like to actually use 0 here
// (ala qt processEvents()), but this appears to busyloop
// and not let the sender do its thing. Anyway, as long
// as we can read from the fifo in much less than (period-2),
// which should always be true, we should be ok.
// I can see not much difference between doing this or
// hijacking the main thread for busy read/write
iTimer.FireIn(2);
} else {
// skip the first packet, and any time the clock value wraps
if (iLastTimeUs && iLastTimeUs < now) {
// will contain the new time out in ms
TUint new_timer_ms = kPeriodMs;
// the difference in usec from where we should be
TInt32 diff = (TInt32)(now - iLastTimeUs) - iPacketTime;
// increment running offset
iTimeOffsetUs -= diff;
// determine new timer value based upon current offset from ideal
if (iTimeOffsetUs < -1000) {
// we are late
TInt32 time_offset_ms = iTimeOffsetUs/1000;
if (time_offset_ms < 1-(TInt32)kPeriodMs) {
// in case callback is severely late, we can only
// catch up so much
new_timer_ms = 1;
} else {
new_timer_ms = kPeriodMs + time_offset_ms;
}
} else if (iTimeOffsetUs > 1000) {
// we are early
new_timer_ms = kPeriodMs+1;
} else {
// we are about on time
new_timer_ms = kPeriodMs;
}
// set timer
iTimer.FireIn(new_timer_ms);
// logging
if (iVerbose) {
if (iTimeOffsetUs >= 1000)
printf ("tnow:%d tlast:%d actual:%4d diff:%4d offset:%5d timer:%d\n", (TUint)now, (TUint)iLastTimeUs, (TUint)(now-iLastTimeUs), diff, iTimeOffsetUs, new_timer_ms);
else
printf ("tnow:%d tlast:%d actual:%4d diff:%4d offset:%4d timer:%d\n", (TUint)now, (TUint)iLastTimeUs, (TUint)(now-iLastTimeUs), diff, iTimeOffsetUs, new_timer_ms);
}
} else {
iTimer.FireIn(kPeriodMs);
}
iLastTimeUs = now;
}
} else {
LOGDEB("SongcastSender::TimerExpired: Sender is paused. Stop firing\n");
m_encoder->finish();
if (m_audio)
m_audio->close();
}
iMutex.Signal();
}
SongcastSender::~SongcastSender()
{
iTimer.Cancel();
delete (iSender);
delete (iDriver);
}