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

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

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


enum {VOLUME = 100};

class mda_recorder;
struct ausrc_st {
	struct ausrc *as;      /* inheritance */
	mda_recorder *mda;
	ausrc_read_h *rh;
	void *arg;
};


class mda_recorder : public MMdaAudioInputStreamCallback, public CBase
{
public:
	mda_recorder(struct ausrc_st *st, struct ausrc_prm *prm);
	~mda_recorder();

	/* from MMdaAudioInputStreamCallback */
	virtual void MaiscOpenComplete(TInt aError);
	virtual void MaiscBufferCopied(TInt aError, const TDesC8& aBuffer);
	virtual void MaiscRecordComplete(TInt aError);

private:
	CMdaAudioInputStream *iInput;
	TMdaAudioDataSettings iSettings;
	TBool iIsReady;
	TBuf8<320> iBuf;
	struct ausrc_st *state;
};


mda_recorder::mda_recorder(struct ausrc_st *st, struct ausrc_prm *prm)
	:iIsReady(EFalse)
{
	state = st;

	iInput = CMdaAudioInputStream::NewL(*this);

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

	iInput->Open(&iSettings);
}


mda_recorder::~mda_recorder()
{
	if (iInput) {
		iInput->Stop();
		delete iInput;
	}
}


void mda_recorder::MaiscOpenComplete(TInt aError)
{
	if (KErrNone != aError) {
		DEBUG_WARNING("MaiscOpenComplete %d\n", aError);
		return;
	}

	iInput->SetGain(iInput->MaxGain());
	iInput->SetAudioPropertiesL(iSettings.iSampleRate,
				    iSettings.iChannels);
	iInput->SetPriority(EMdaPriorityNormal,
			    EMdaPriorityPreferenceTime);

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


void mda_recorder::MaiscBufferCopied(TInt aError, const TDesC8& aBuffer)
{
	if (KErrNone != aError) {
		DEBUG_WARNING("MaiscBufferCopied: error=%d %d bytes\n",
			      aError, aBuffer.Length());
		return;
	}

	state->rh(aBuffer.Ptr(), aBuffer.Length(), state->arg);

	iBuf.Zero();
	TRAPD(ret, iInput->ReadL(iBuf));
	if (KErrNone != ret) {
		DEBUG_WARNING("ReadL left with %d\n", ret);
	}
}


void mda_recorder::MaiscRecordComplete(TInt aError)
{
	DEBUG_NOTICE("MaiscRecordComplete: error=%d\n", aError);

#if 0
	if (KErrOverflow == aError) {

		/* re-open input stream */
		iInput->Stop();
		iInput->Open(&iSettings);
	}
#endif
}


static void ausrc_destructor(void *arg)
{
	struct ausrc_st *st = (struct ausrc_st *)arg;

	delete st->mda;

	mem_deref(st->as);
}


int mda_recorder_alloc(struct ausrc_st **stp, struct ausrc *as,
		       struct media_ctx **ctx,
		       struct ausrc_prm *prm, const char *device,
		       ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
	struct ausrc_st *st;
	int err = 0;

	(void)ctx;
	(void)device;
	(void)errh;

	st = (struct ausrc_st *)mem_zalloc(sizeof(*st), ausrc_destructor);
	if (!st)
		return ENOMEM;

	st->as  = (struct ausrc *)mem_ref(as);
	st->rh  = rh;
	st->arg = arg;

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

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

	return err;
}