forked from fishpond-haiku/Haiku-Radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamPlayer.cpp
248 lines (218 loc) · 6.64 KB
/
StreamPlayer.cpp
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
/*
* File: StreamPlayer.cpp
* Author: Kai Niessen
*
* Created on January 25, 2016, 6:49 PM
*/
#include <MediaDecoder.h>
#include <MediaFile.h>
#include <MediaTrack.h>
#include "StreamPlayer.h"
#include "Debug.h"
#include "StreamIO.h"
#define milliseconds * 1000
#define seconds * 1000 milliseconds
StreamPlayer::StreamPlayer(Station* station, BLooper* Notify)
: BLocker(),
fStation(station),
fNotify(Notify),
fNoNotifyCount(0),
fMediaFile(NULL),
fPlayer(NULL),
fState(Stopped)
{
TRACE("Trying to set player for stream %s\n", station->StreamUrl().UrlString().String());
fStream = new (std::nothrow) StreamIO(station, Notify);
fInitStatus = fStream->Open();
if (fInitStatus != B_OK) {
MSG("Error retrieving stream from %s - %s\n",
station->StreamUrl().UrlString().String(),
strerror(fInitStatus));
return;
}
}
StreamPlayer::~StreamPlayer() {
setState(Stopped);
if (fInitStatus == B_OK && fPlayer)
fPlayer->Stop(true, false);
delete fPlayer;
delete fStream;
delete fMediaFile;
}
void
StreamPlayer::GetDecodedChunk(void* cookie, void* buffer, size_t size, const media_raw_audio_format& format) {
StreamPlayer* player = (StreamPlayer*)(cookie);
BMediaFile* fMediaFile = player->fMediaFile;
int64 reqFrames = size / format.channel_count / (format.format & media_raw_audio_format::B_AUDIO_SIZE_MASK);
fMediaFile->TrackAt(0)->ReadFrames(buffer, &reqFrames, &player->fHeader, &player->fInfo);
}
status_t
StreamPlayer::Play() {
switch(fState) {
case Playing:
case Buffering:
return B_OK;
break;
case Stopped:
thread_id playThreadId = spawn_thread((thread_func)StreamPlayer::StartPlayThreadFunc, fStation->Name()->String(), B_NORMAL_PRIORITY, this);
resume_thread(playThreadId);
break;
}
}
#ifdef DEBUG
void dumpMessage(BMessage* msg) {
int n = msg->CountNames(B_ANY_TYPE);
type_code typeCode;
int32 arraySize;
char* name;
for (long int i=0; i<n; i++) {
msg->GetInfo((long unsigned int)B_ANY_TYPE, i, &name, &typeCode, &arraySize);
MSG("Meta data %s = %s\n", name, name);
}
}
#else
void inline dumpMessage(BMessage* msg) { }
#endif
status_t
StreamPlayer::StartPlayThreadFunc(StreamPlayer* _this) {
_this->Lock();
_this->setState(Buffering);
_this->fStopRequested = false;
_this->fStream->SetLimiter(0x40000);
_this->fMediaFile = new (std::nothrow) BMediaFile(_this->fStream);
_this->fInitStatus = _this->fMediaFile->InitCheck();
if (_this->fInitStatus != B_OK)
MSG("Error creating media extractor for %s - %s\n", _this->fStation->StreamUrl().UrlString().String(), strerror(_this->fInitStatus));
if (_this->fInitStatus != B_OK || _this->fStopRequested) {
delete _this->fStream;
_this->fStream = NULL;
delete _this->fMediaFile;
_this->fMediaFile = NULL;
_this->Unlock();
_this->setState(Stopped);
return _this->fInitStatus;
}
_this->fStream->SetLimiter(0);
if (_this->fMediaFile->CountTracks() == 0)
MSG("No track found inside stream %s - %s\n", _this->fStation->StreamUrl().UrlString().String(), strerror(_this->fInitStatus));
if (_this->fInitStatus != B_OK || _this->fStopRequested) {
delete _this->fMediaFile;
_this->fMediaFile = NULL;
_this->Unlock();
_this->setState(Stopped);
return _this->fInitStatus;
}
_this->fInitStatus = _this->fMediaFile->TrackAt(0)->GetCodecInfo(&_this->fCodecInfo);
if (_this->fInitStatus != B_OK)
MSG("Could not create decoder for %s - %s\n", _this->fStation->StreamUrl().UrlString().String(), strerror(_this->fInitStatus));
if (_this->fInitStatus != B_OK || _this->fStopRequested) {
delete _this->fMediaFile;
_this->fMediaFile = NULL;
_this->Unlock();
_this->setState(Stopped);
return _this->fInitStatus;
}
_this->fInitStatus = _this->fMediaFile->TrackAt(0)->DecodedFormat(&_this->fDecodedFormat);
if (_this->fInitStatus != B_OK)
MSG("Could not negotiate output format - %s\n", strerror(_this->fInitStatus));
if (_this->fInitStatus != B_OK || _this->fStopRequested) {
delete _this->fMediaFile;
_this->fMediaFile = NULL;
_this->Unlock();
_this->setState(Stopped);
return _this->fInitStatus;
}
TRACE("Found stream with %ld channels with %f Hz\n",
_this->fDecodedFormat.u.raw_audio.channel_count,
_this->fDecodedFormat.u.raw_audio.frame_rate);
_this->fPlayer = new BSoundPlayer(&_this->fDecodedFormat.u.raw_audio,
_this->fStation->Name()->String(),
&StreamPlayer::GetDecodedChunk,
NULL, _this);
_this->fStatus = _this->fPlayer->InitCheck();
if (_this->fStatus != B_OK)
MSG("Sound Player failed to initialize (%s)\r\n", strerror(_this->fStatus));
if (_this->fInitStatus != B_OK || _this->fStopRequested) {
_this->fPlayer->Stop(true, true);
delete _this->fPlayer;
_this->fPlayer = NULL;
_this->Unlock();
_this->setState(Stopped);
return _this->fStatus;
}
_this->fPlayer->Preroll();
_this->fInitStatus = _this->fPlayer->Start();
if (_this->fInitStatus != B_OK)
MSG("Sound Player failed to start (%s)\r\n", strerror(_this->fStatus));
if (_this->fInitStatus != B_OK || _this->fStopRequested) {
delete _this->fPlayer;
_this->fPlayer = NULL;
_this->Unlock();
_this->setState(Stopped);
return _this->fInitStatus;
}
_this->setState(Playing);
_this->Unlock();
return _this->fStatus;
}
status_t
StreamPlayer::Stop() {
switch (fState) {
case Buffering:
fStopRequested = true;
break;
case Playing:
Lock();
if (fPlayer) {
fPlayer->Stop(false, true);
delete fPlayer;
fPlayer = NULL;
}
if (fMediaFile) {
fMediaFile->CloseFile();
delete fMediaFile;
fMediaFile = NULL;
}
Unlock();
setState(StreamPlayer::Stopped);
break;
case Stopped:
case StreamPlayer::InActive:
return B_OK;
break;
}
}
void
StreamPlayer::setState(StreamPlayer::PlayState state) {
if (fState != state) {
fState = state;
if (fNotify) {
BMessage* notification = new BMessage(MSG_PLAYER_STATE_CHANGED);
BMessenger messenger(fNotify);
notification->AddPointer("player", this);
notification->AddInt32("state", fState);
messenger.SendMessage(notification, fNotify);
}
}
}
float
StreamPlayer::Volume() {
if (fPlayer) {
media_node node;
int32 paramID;
float minDB, maxDB;
fPlayer->GetVolumeInfo(&node, ¶mID, &minDB, &maxDB);
return (fPlayer->VolumeDB(false) - minDB) / (maxDB - minDB);
} else
return 0;
}
void
StreamPlayer::SetVolume(float volume) {
if (fPlayer) {
media_node node;
int32 paramID;
float minDB, maxDB;
fPlayer->GetVolumeInfo(&node, ¶mID, &minDB, &maxDB);
fPlayer->SetVolumeDB(minDB + volume * (maxDB - minDB));
}
}