summaryrefslogtreecommitdiff
path: root/modules/mda/player.cpp
blob: 4cfa18c644f4ad45d6d7d65269d64e214b132c81 (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
/**
 * @file player.cpp  Symbian MDA audio driver -- player
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <e32def.h>
#include <e32std.h>
#include <mdaaudiooutputstream.h>
#include <mda/common/audio.h>

extern "C" {
#include <re.h>
#include <baresip.h>
#include "mda.h"

#define DEBUG_MODULE "player"
#define DEBUG_LEVEL 5
#include <re_dbg.h>
}


enum {VOLUME = 100};

class mda_player;
struct auplay_st {
	struct auplay *ap;      /* inheritance */
	mda_player *mda;
	auplay_write_h *wh;
	void *arg;
};


class mda_player : public MMdaAudioOutputStreamCallback, public CBase
{
public:
	mda_player(struct auplay_st *st, struct auplay_prm *prm);
	~mda_player();
	void play();

	/* from MMdaAudioOutputStreamCallback */
	virtual void MaoscOpenComplete(TInt aError);
	virtual void MaoscBufferCopied(TInt aError, const TDesC8& aBuffer);
	virtual void MaoscPlayComplete(TInt aError);

private:
	CMdaAudioOutputStream *iOutput;
	TMdaAudioDataSettings iSettings;
	TBool iIsReady;
	TBuf8<320> iBuf;
	struct auplay_st *state;
};


mda_player::mda_player(struct auplay_st *st, struct auplay_prm *prm)
	:iIsReady(EFalse)
{
	state = st;

	iBuf.FillZ(320);

	iSettings.iSampleRate = convert_srate(prm->srate);
	iSettings.iChannels = convert_channels(prm->ch);
	iSettings.iVolume = VOLUME;

	iOutput = CMdaAudioOutputStream::NewL(*this);
	iOutput->Open(&iSettings);
}


mda_player::~mda_player()
{
	if (iOutput) {
		iOutput->Stop();
		delete iOutput;
	}
}


void mda_player::play()
{
	/* call write handler here */
	state->wh((uint8_t *)&iBuf[0], iBuf.Length(), state->arg);

	TRAPD(ret, iOutput->WriteL(iBuf));
	if (KErrNone != ret) {
		DEBUG_WARNING("WriteL left with %d\n", ret);
	}
}


void mda_player::MaoscOpenComplete(TInt aError)
{
	if (KErrNone != aError) {
		iIsReady = EFalse;
		DEBUG_WARNING("mda player error: %d\n", aError);
		return;
	}

	iOutput->SetAudioPropertiesL(iSettings.iSampleRate,
				     iSettings.iChannels);
	iOutput->SetPriority(EMdaPriorityNormal,
			     EMdaPriorityPreferenceTime);

	iIsReady = ETrue;

	play();
}


/*
 * Note: In reality, this function is called approx. 1 millisecond after the
 * last block was played, hence we have to generate buffer N+1 while buffer N
 * is playing.
 */
void mda_player::MaoscBufferCopied(TInt aError, const TDesC8& aBuffer)
{
	(void)aBuffer;

	if (KErrNone != aError && KErrCancel != aError) {
		DEBUG_WARNING("MaoscBufferCopied [aError=%d]\n", aError);
	}
	if (aError == KErrAbort) {
		DEBUG_NOTICE("player aborted\n");
		return;
	}

	play();
}


void mda_player::MaoscPlayComplete(TInt aError)
{
	if (KErrNone != aError) {
		DEBUG_WARNING("MaoscPlayComplete [aError=%d]\n", aError);
	}
}


static void auplay_destructor(void *arg)
{
	struct auplay_st *st = (struct auplay_st *)arg;

	delete st->mda;

	mem_deref(st->ap);
}


int mda_player_alloc(struct auplay_st **stp, struct auplay *ap,
		     struct auplay_prm *prm, const char *device,
		     auplay_write_h *wh, void *arg)
{
	struct auplay_st *st;
	int err = 0;

	(void)device;

	st = (struct auplay_st *)mem_zalloc(sizeof(*st), auplay_destructor);
	if (!st)
		return ENOMEM;

	st->ap  = (struct auplay *)mem_ref(ap);
	st->wh  = wh;
	st->arg = arg;

	st->mda = new mda_player(st, prm);
	if (!st->mda)
		err = ENOMEM;

	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}