aboutsummaryrefslogtreecommitdiffstats
path: root/distrib/sdl-1.2.15/src/audio/symbian/streamplayer.cpp
blob: dd733a1d17480f69e711ec0c7cb5fc643ee45f0f (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
#include "streamplayer.h"
#include<mda/common/audio.h>



const TInt KMaxVolume(256);

LOCAL_C TInt GetSampleRate(TInt aRate)
    {
    switch(aRate)
        {
        case 8000: return TMdaAudioDataSettings::ESampleRate8000Hz;
        case 11025: return TMdaAudioDataSettings::ESampleRate11025Hz;
        case 12000: return TMdaAudioDataSettings::ESampleRate12000Hz;
        case 16000: return TMdaAudioDataSettings::ESampleRate16000Hz;
        case 22050: return TMdaAudioDataSettings::ESampleRate22050Hz;
        case 24000: return TMdaAudioDataSettings::ESampleRate24000Hz;
        case 32000: return TMdaAudioDataSettings::ESampleRate32000Hz;
        case 44100: return TMdaAudioDataSettings::ESampleRate44100Hz;
        case 48000: return TMdaAudioDataSettings::ESampleRate48000Hz;
        case 96000: return TMdaAudioDataSettings::ESampleRate96000Hz;
        case 64000: return TMdaAudioDataSettings::ESampleRate64000Hz;
        }
    return KErrNotFound;
    }

LOCAL_C TInt GetChannels(TInt aChannels)
    {
    switch(aChannels)
        {
        case 1: return TMdaAudioDataSettings::EChannelsMono;
        case 2: return TMdaAudioDataSettings::EChannelsStereo;
        }
    return KErrNotFound;
    }
    
TInt CStreamPlayer::ClosestSupportedRate(TInt aRate)
	{
	if(aRate > 96000)
		return 96000;
	TInt rate = aRate;
	while(GetSampleRate(rate) == KErrNotFound)
		{
		++rate;
		}
	return rate;
	}

CStreamPlayer::CStreamPlayer(MStreamProvider& aProvider,  MStreamObs& aObs) :
 iProvider(aProvider), iObs(aObs), iVolume(KMaxVolume)
	{	
	}
		
CStreamPlayer::~CStreamPlayer()
	{
	iState |= EDied;
	if(iState & EInited)
		Close();
	User::After(100000); //wait buffer to be flushed
	ASSERT(iPtr.Length() == 0);
	delete iStream;
	}
		
		
void CStreamPlayer::ConstructL()
	{
	iStream = CMdaAudioOutputStream::NewL(*this, EMdaPriorityMax);
	iSilence.SetMax();
	iSilence.FillZ();
	}
		

TInt CStreamPlayer::OpenStream(TInt aRate, TInt aChannels, TUint32 aType)
	{
	Close();
	
	iType = aType;
	
	iRate = GetSampleRate(aRate);
	if(iRate == KErrNotFound)
		return KErrNotSupported;
	
	iChannels = GetChannels(aChannels);
	if(iChannels == KErrNotFound)
		return KErrNotSupported;
	
	Open();
    
    return KErrNone;
	}


TInt CStreamPlayer::MaxVolume() const
	{
	return KMaxVolume;
	}

void CStreamPlayer::SetVolume(TInt aNew)
    {
    
    const TInt maxi = MaxVolume();
    if(aNew > maxi)
		return;
    if(aNew < 0)
    	return;
    
    iVolume = aNew;
    
    iState |= EVolumeChange;
    }
    
 TInt CStreamPlayer::Volume() const
    {
	return iVolume;
    }

void CStreamPlayer::Open()
	{
	TMdaAudioDataSettings audioSettings;
	audioSettings.Query();
    audioSettings.iCaps = TMdaAudioDataSettings::ERealTime |
     	TMdaAudioDataSettings::ESampleRateFixed; 
    audioSettings.iSampleRate = iRate; 
    audioSettings.iChannels = iChannels;
	audioSettings.iFlags = TMdaAudioDataSettings::ENoNetworkRouting;
	audioSettings.iVolume = 0;
	
	iState &= ~EStopped;
    iStream->Open(&audioSettings);    
	}
	
void CStreamPlayer::Stop()
	{
	if(iState & (EStarted | EInited))
		{
		Close();
		iState |= EStopped;
		}
	}
	
void CStreamPlayer::Start()
	{
	if(iPtr.Length() == 0)
		{
		iState |= EStarted;
		if(iState & EInited)
			{
			Request();
			}
		else if(iState & EStopped)
			{
			Open();
			}
		}
	}

void CStreamPlayer::Close()
	{
	iState &= ~EInited;
	iStream->Stop();
	iState &= ~EStarted;
	}
	
void CStreamPlayer::Request()
	{
	if(iState & EInited)
		{
		iPtr.Set(KNullDesC8);
		
		if(iState & EVolumeChange)
			{
       		const TReal newVol = iVolume;
       		const TReal newMax = MaxVolume();
       		const TInt maxVol = iStream->MaxVolume();
       		const TReal max = static_cast<TReal>(maxVol);
       		const TReal newvolume = (newVol * max) / newMax;
       		const TInt vol = static_cast<TReal>(newvolume);
       		iStream->SetVolume(vol);
			iState &= ~EVolumeChange;
			}
			
		if(iState & EStarted)
			{
	 		iPtr.Set(iProvider.Data());
			}
		if(iPtr.Length() == 0)
			{
			iPtr.Set(iSilence);
			}
		TRAPD(err, iStream->WriteL(iPtr));
		if(err != KErrNone)
			{
			iObs.Complete(MStreamObs::EWrite, err);	
			}
	/*	else
			{
			iProvider.Written(iPtr.Length());
			}*/
		}
	}
	

void CStreamPlayer::SetCapsL()
	{
	iStream->SetDataTypeL(iType);
	iStream->SetAudioPropertiesL(iRate, iChannels);
	}

void CStreamPlayer::MaoscOpenComplete(TInt aError) 
	{
	if(aError == KErrNone)
		{
		TRAPD(err, SetCapsL());
    	if(err == KErrNone)
    		{
    		iStream->SetPriority(EPriorityNormal, EMdaPriorityPreferenceTime);
    		iState |= EInited;
    		
    		
        	SetVolume(Volume());	
    		
    		if(iState & EStarted)
    			{
    			Request();
    			}
    			
			}
		aError = err;
		}
	if(!(iState & EDied))
		iObs.Complete(MStreamObs::EInit, aError);
	}
	
void CStreamPlayer::MaoscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
	{
	iPtr.Set(KNullDesC8);
	if(aError == KErrNone)
		{
		if(iState & EInited)
			Request();
		else
			iStream->Stop();
		}
	else if(!(iState & EDied))
		iObs.Complete(MStreamObs::EPlay, aError);
	}
	
void CStreamPlayer::MaoscPlayComplete(TInt aError)
	{	
	iPtr.Set(KNullDesC8);
	iState &= ~EStarted;
	if(!(iState & EDied))
		iObs.Complete(MStreamObs::EClose, aError);
	}
	
TBool CStreamPlayer::Playing() const
	{
	return (iState & EInited) && (iState & EStarted);
	}

TBool CStreamPlayer::Closed() const
	{
	return !(iState & EInited) && !(iState & EDied);
	}
	
	/*
void CStreamPlayer::Request()
	{
	SetActive();
	TRequestStatus* s = &iStatus;
	User::RequestComplete(s, KErrNone);
	}
	//		iTimer.After(0);
	*/