19
|
1 |
/*
|
25
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Record Itf GST adaptation
|
|
15 |
*
|
|
16 |
*/
|
19
|
17 |
|
|
18 |
#include <pthread.h>
|
20
|
19 |
#include <gst/gst.h>
|
19
|
20 |
#include "xamediarecorderadaptctx.h"
|
|
21 |
#include "xarecorditfadaptation.h"
|
|
22 |
#include "xaadaptationgst.h"
|
33
|
23 |
|
19
|
24 |
|
|
25 |
/*forward declaration of position updater callback*/
|
|
26 |
gboolean XARecordItfAdapt_PositionUpdate(gpointer ctx);
|
|
27 |
|
|
28 |
/*
|
|
29 |
* XAresult XARecordItfAdapt_SetRecordState(XAAdaptationGstCtx *ctx, XAuint32 state)
|
|
30 |
* Sets record state to GStreamer.
|
|
31 |
* @param XAAdaptationGstCtx *ctx - Adaptation context
|
|
32 |
* XAuint32 state - Record state to be set
|
|
33 |
* @return XAresult ret - Success value
|
|
34 |
*/
|
25
|
35 |
XAresult XARecordItfAdapt_SetRecordState(XAAdaptationGstCtx *bCtx,
|
|
36 |
XAuint32 state)
|
|
37 |
{
|
19
|
38 |
XAresult ret = XA_RESULT_SUCCESS;
|
|
39 |
XAboolean closeSink = XA_BOOLEAN_FALSE;
|
|
40 |
XAboolean requestStateChange = XA_BOOLEAN_FALSE;
|
|
41 |
GstStateChangeReturn gstRet = GST_STATE_CHANGE_SUCCESS;
|
|
42 |
XAMediaRecorderAdaptationCtx* mCtx = NULL;
|
53
|
43 |
DEBUG_API_A1_STR("->XARecordItfAdapt_SetRecordState %s",RECORDSTATENAME(state));
|
25
|
44 |
if (!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
45 |
{
|
19
|
46 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
47 |
DEBUG_API("<-XARecordItfAdapt_SetRecordState");
|
|
48 |
return XA_RESULT_PARAMETER_INVALID;
|
25
|
49 |
}
|
19
|
50 |
|
|
51 |
mCtx = (XAMediaRecorderAdaptationCtx*) bCtx;
|
|
52 |
|
25
|
53 |
switch (state)
|
|
54 |
{
|
19
|
55 |
case XA_RECORDSTATE_STOPPED:
|
25
|
56 |
{
|
|
57 |
// Audio sourse should be stopped
|
53
|
58 |
if (!bCtx->busError && mCtx->audiosource && mCtx->isRecord == XA_BOOLEAN_TRUE)
|
19
|
59 |
{
|
53
|
60 |
XAAdaptationGst_PrepareAsyncWait(bCtx);
|
25
|
61 |
if (gst_element_send_event(mCtx->audiosource,
|
|
62 |
gst_event_new_eos()) == TRUE)
|
|
63 |
{
|
|
64 |
DEBUG_INFO ("posted eos");
|
53
|
65 |
XAAdaptationGst_StartAsyncWait(bCtx);
|
25
|
66 |
}
|
|
67 |
else
|
|
68 |
{
|
|
69 |
DEBUG_ERR("FAIL : post eos");
|
|
70 |
}
|
19
|
71 |
}
|
25
|
72 |
|
53
|
73 |
bCtx->binWantedState = GST_STATE_READY;
|
19
|
74 |
closeSink = XA_BOOLEAN_TRUE;
|
25
|
75 |
if (mCtx->runpositiontimer > 0)
|
|
76 |
{
|
19
|
77 |
g_source_remove(mCtx->runpositiontimer);
|
25
|
78 |
mCtx->runpositiontimer = 0;
|
|
79 |
}
|
19
|
80 |
|
|
81 |
break;
|
25
|
82 |
}
|
19
|
83 |
case XA_RECORDSTATE_PAUSED:
|
|
84 |
{
|
53
|
85 |
mCtx->isRecord = XA_BOOLEAN_TRUE;
|
25
|
86 |
if (mCtx->xaRecordState == XA_RECORDSTATE_STOPPED
|
|
87 |
&& mCtx->encodingchanged)
|
|
88 |
{
|
28
|
89 |
ret = XAMediaRecorderAdapt_ChangeEncoders(mCtx);
|
25
|
90 |
mCtx->encodingchanged = XA_BOOLEAN_FALSE;
|
28
|
91 |
if(ret != XA_RESULT_SUCCESS)
|
|
92 |
{
|
|
93 |
return ret;
|
|
94 |
}
|
25
|
95 |
}
|
|
96 |
bCtx->binWantedState = GST_STATE_PAUSED;
|
|
97 |
if (mCtx->runpositiontimer > 0)
|
|
98 |
{
|
19
|
99 |
g_source_remove(mCtx->runpositiontimer);
|
25
|
100 |
mCtx->runpositiontimer = 0;
|
|
101 |
}
|
|
102 |
break;
|
19
|
103 |
}
|
|
104 |
case XA_RECORDSTATE_RECORDING:
|
|
105 |
{
|
53
|
106 |
mCtx->isRecord = XA_BOOLEAN_TRUE;
|
25
|
107 |
if (mCtx->xaRecordState == XA_RECORDSTATE_STOPPED
|
|
108 |
&& (mCtx->encodingchanged))
|
|
109 |
{
|
28
|
110 |
ret = XAMediaRecorderAdapt_ChangeEncoders(mCtx);
|
19
|
111 |
mCtx->encodingchanged = XA_BOOLEAN_FALSE;
|
28
|
112 |
if(ret != XA_RESULT_SUCCESS)
|
|
113 |
{
|
|
114 |
return ret;
|
|
115 |
}
|
25
|
116 |
}
|
19
|
117 |
|
|
118 |
bCtx->binWantedState = GST_STATE_PLAYING;
|
|
119 |
break;
|
25
|
120 |
}
|
19
|
121 |
default:
|
53
|
122 |
DEBUG_ERR("Unhandled state");
|
19
|
123 |
ret = XA_RESULT_PARAMETER_INVALID;
|
|
124 |
break;
|
25
|
125 |
}
|
19
|
126 |
|
25
|
127 |
if (ret == XA_RESULT_SUCCESS)
|
|
128 |
{
|
19
|
129 |
mCtx->xaRecordState = state;
|
25
|
130 |
}
|
19
|
131 |
|
|
132 |
/* launch Gstreamer state change only if necessary */
|
25
|
133 |
if (GST_STATE_TARGET(bCtx->bin) == bCtx->binWantedState)
|
|
134 |
{
|
19
|
135 |
DEBUG_INFO("Gst already transitioning to wanted state!!");
|
|
136 |
requestStateChange = XA_BOOLEAN_FALSE;
|
25
|
137 |
}
|
19
|
138 |
else
|
|
139 |
{
|
25
|
140 |
if ((GST_STATE(bCtx->bin) == bCtx->binWantedState)
|
|
141 |
&& (GST_STATE_PENDING(bCtx->bin) == GST_STATE_VOID_PENDING))
|
|
142 |
{
|
19
|
143 |
DEBUG_ERR_A3("WARNING : gststate %d == wanted %d != gsttarget %d and no statechange pending",
|
25
|
144 |
GST_STATE(bCtx->bin), bCtx->binWantedState, GST_STATE_TARGET(bCtx->bin));
|
|
145 |
}
|
19
|
146 |
requestStateChange = XA_BOOLEAN_TRUE;
|
25
|
147 |
}
|
19
|
148 |
|
25
|
149 |
if (requestStateChange)
|
|
150 |
{
|
19
|
151 |
XAAdaptationGst_PrepareAsyncWait(bCtx);
|
|
152 |
DEBUG_INFO_A1("Sending change state request to state %d", bCtx->binWantedState);
|
25
|
153 |
gstRet = gst_element_set_state(GST_ELEMENT(bCtx->bin),
|
|
154 |
bCtx->binWantedState);
|
|
155 |
switch (gstRet)
|
|
156 |
{
|
19
|
157 |
case GST_STATE_CHANGE_FAILURE:
|
53
|
158 |
DEBUG_ERR_A1("FAILED to change state (target %d)",bCtx->binWantedState);
|
19
|
159 |
bCtx->binWantedState = GST_STATE(bCtx->bin);
|
|
160 |
ret = XA_RESULT_INTERNAL_ERROR;
|
|
161 |
break;
|
|
162 |
case GST_STATE_CHANGE_ASYNC:
|
|
163 |
DEBUG_INFO_A1("Change state will happen asyncronously (target %d)",bCtx->binWantedState);
|
|
164 |
XAAdaptationGst_StartAsyncWait(bCtx);
|
|
165 |
ret = XA_RESULT_SUCCESS;
|
|
166 |
break;
|
|
167 |
case GST_STATE_CHANGE_NO_PREROLL:
|
53
|
168 |
DEBUG_INFO("GST_STATE_CHANGE_NO_PREROLL");
|
19
|
169 |
/* deliberate fall-through */
|
|
170 |
case GST_STATE_CHANGE_SUCCESS:
|
53
|
171 |
DEBUG_INFO_A1("Successfully changed state (target %d)",bCtx->binWantedState);
|
19
|
172 |
ret = XA_RESULT_SUCCESS;
|
|
173 |
break;
|
|
174 |
default:
|
53
|
175 |
DEBUG_ERR_A1("Unhandled error (%d)",gstRet);
|
19
|
176 |
ret = XA_RESULT_UNKNOWN_ERROR;
|
|
177 |
break;
|
25
|
178 |
}
|
19
|
179 |
bCtx->waitingasyncop = XA_BOOLEAN_FALSE;
|
25
|
180 |
}
|
|
181 |
if ((GST_STATE(bCtx->bin) > GST_STATE_READY) && closeSink)
|
|
182 |
{ /* close the sink*/
|
|
183 |
gst_element_send_event(bCtx->bin, gst_event_new_flush_start());
|
|
184 |
gst_element_send_event(bCtx->bin, gst_event_new_flush_stop());
|
|
185 |
}
|
19
|
186 |
|
33
|
187 |
DEBUG_API("<-XARecordItfAdapt_SetRecordState");
|
25
|
188 |
return ret;
|
19
|
189 |
}
|
|
190 |
|
|
191 |
/*
|
|
192 |
* XAresult XARecordItfAdapt_GetRecordState(XAAdaptationGstCtx *bCtx, XAuint32 *state)
|
|
193 |
* Description: Return record state
|
|
194 |
*/
|
25
|
195 |
XAresult XARecordItfAdapt_GetRecordState(XAAdaptationGstCtx *bCtx,
|
|
196 |
XAuint32 *state)
|
|
197 |
{
|
19
|
198 |
XAMediaRecorderAdaptationCtx* mCtx = NULL;
|
|
199 |
DEBUG_API("->XARecordItfAdapt_GetRecordState");
|
|
200 |
|
25
|
201 |
if (!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
202 |
{
|
19
|
203 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
204 |
DEBUG_API("<-XARecordItfAdapt_GetRecordState");
|
|
205 |
return XA_RESULT_PARAMETER_INVALID;
|
25
|
206 |
}
|
19
|
207 |
|
|
208 |
mCtx = (XAMediaRecorderAdaptationCtx*) bCtx;
|
|
209 |
|
|
210 |
*state = mCtx->xaRecordState;
|
|
211 |
|
|
212 |
DEBUG_API("<-XARecordItfAdapt_GetRecordState");
|
|
213 |
return XA_RESULT_SUCCESS;
|
25
|
214 |
}
|
19
|
215 |
|
|
216 |
/*
|
|
217 |
* XAresult XARecordItfAdapt_GetPosition(XAAdaptationGstCtx *ctx, AdaptationContextIDS ctxIDs, XAmillisecond *pMsec)
|
|
218 |
* @param XAAdaptationGstCtx *ctx - Adaptation context, this will be casted to correct type regarding to contextID value given as 2nd parameter
|
|
219 |
* XAmillisecond *pMsec - Pointer where to store current position in stream.
|
|
220 |
* @return XAresult ret - Success value
|
|
221 |
*/
|
25
|
222 |
XAresult XARecordItfAdapt_GetPosition(XAAdaptationGstCtx *bCtx,
|
|
223 |
XAmillisecond *pMsec)
|
|
224 |
{
|
19
|
225 |
XAresult ret = XA_RESULT_SUCCESS;
|
25
|
226 |
|
|
227 |
gint64 position = 0;
|
19
|
228 |
GstFormat format = GST_FORMAT_TIME;
|
|
229 |
DEBUG_API("->XARecordItfAdapt_GetPosition");
|
|
230 |
|
25
|
231 |
if (!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
232 |
{
|
19
|
233 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
234 |
DEBUG_API("<-XARecordItfAdapt_GetPosition");
|
|
235 |
/* invalid parameter */
|
|
236 |
return XA_RESULT_PARAMETER_INVALID;
|
25
|
237 |
}
|
|
238 |
if (gst_element_query_position(GST_ELEMENT(bCtx->bin), &format, &position))
|
|
239 |
{
|
19
|
240 |
ret = XA_RESULT_SUCCESS;
|
|
241 |
*pMsec = GST_TIME_AS_MSECONDS(position); /*Warning ok due to used API specification*/
|
|
242 |
DEBUG_INFO_A1("Gst: Position in microseconds : %u", *pMsec );
|
25
|
243 |
}
|
19
|
244 |
else
|
25
|
245 |
{
|
19
|
246 |
DEBUG_ERR("WARNING: Gst: could not get position");
|
|
247 |
/* probably not fully prerolled - safe assumption for position = 0 */
|
|
248 |
*pMsec = 0;
|
|
249 |
ret = XA_RESULT_SUCCESS;
|
25
|
250 |
}
|
19
|
251 |
|
|
252 |
DEBUG_API("<-XARecordItfAdapt_GetPosition");
|
|
253 |
return ret;
|
25
|
254 |
}
|
19
|
255 |
|
|
256 |
/*
|
|
257 |
* gboolean XARecordItfAdapt_PositionUpdate(gpointer ctx)
|
|
258 |
* callback.
|
|
259 |
* If position tracking enabled, periodic timer calls this method every XA_ADAPT_PU_INTERVAL msecs
|
|
260 |
* @return false to stop periodic calls
|
|
261 |
*/
|
|
262 |
gboolean XARecordItfAdapt_PositionUpdate(gpointer ctx)
|
25
|
263 |
{
|
19
|
264 |
XAAdaptationGstCtx *bCtx = (XAAdaptationGstCtx*) ctx;
|
|
265 |
XAMediaRecorderAdaptationCtx* mCtx = (XAMediaRecorderAdaptationCtx*) ctx;
|
|
266 |
DEBUG_API("->XARecordItfAdapt_PositionUpdate");
|
|
267 |
|
25
|
268 |
if (mCtx && mCtx->trackpositionenabled)
|
|
269 |
{
|
19
|
270 |
GstFormat format = GST_FORMAT_TIME;
|
|
271 |
gint64 position = 0;
|
25
|
272 |
if (gst_element_query_position(GST_ELEMENT(bCtx->bin), &format,
|
|
273 |
&position))
|
|
274 |
{
|
|
275 |
XAuint32 posMsec = GST_TIME_AS_MSECONDS(position);
|
|
276 |
/*Warning ok due to used API specification*/
|
|
277 |
XAAdaptEvent event =
|
|
278 |
{
|
|
279 |
XA_RECORDITFEVENTS, XA_ADAPT_POSITION_UPDATE_EVT, 1, NULL
|
|
280 |
};
|
19
|
281 |
event.data = &posMsec;
|
|
282 |
DEBUG_API_A1("XARecordItfAdapt_PositionUpdate: pos %lu ms", posMsec);
|
|
283 |
/* send needed events */
|
25
|
284 |
XAAdaptationBase_SendAdaptEvents(&bCtx->baseObj, &event);
|
|
285 |
}
|
19
|
286 |
else
|
25
|
287 |
{
|
19
|
288 |
DEBUG_ERR("Gst: Failed to get position");
|
25
|
289 |
}
|
|
290 |
DEBUG_API_A1("<-XARecordItfAdapt_PositionUpdate: %d", mCtx->runpositiontimer);
|
|
291 |
/* return false to stop timer */
|
|
292 |
return (mCtx->runpositiontimer);
|
19
|
293 |
}
|
25
|
294 |
return FALSE;
|
19
|
295 |
}
|
|
296 |
|
|
297 |
/*
|
|
298 |
* XAresult XARecordItfAdapt_EnablePositionTracking
|
|
299 |
* Enable/disable periodic position tracking callbacks
|
|
300 |
*/
|
25
|
301 |
XAresult XARecordItfAdapt_EnablePositionTracking(XAAdaptationGstCtx *bCtx,
|
|
302 |
XAboolean enable)
|
|
303 |
{
|
19
|
304 |
XAMediaRecorderAdaptationCtx* mCtx = (XAMediaRecorderAdaptationCtx*) bCtx;
|
|
305 |
DEBUG_API_A1("->XARecordItfAdapt_EnablePositionTracking (enable: %lu)", enable);
|
|
306 |
|
25
|
307 |
if (!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
308 |
{
|
19
|
309 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
310 |
DEBUG_API("<-XARecordItfAdapt_EnablePositionTracking");
|
|
311 |
/* invalid parameter */
|
|
312 |
return XA_RESULT_PARAMETER_INVALID;
|
25
|
313 |
}
|
19
|
314 |
|
|
315 |
/* create a timer to track position of playback */
|
25
|
316 |
if (enable && !(mCtx->trackpositionenabled))
|
|
317 |
{
|
19
|
318 |
mCtx->trackpositionenabled = XA_BOOLEAN_TRUE;
|
|
319 |
mCtx->positionCb = &XARecordItfAdapt_PositionUpdate;
|
|
320 |
/* if recording is already on, create a timer to track position of recording */
|
25
|
321 |
if (GST_STATE(bCtx->bin) == GST_STATE_PLAYING)
|
|
322 |
{
|
|
323 |
mCtx->runpositiontimer = g_timeout_add(XA_ADAPT_PU_INTERVAL,
|
|
324 |
mCtx->positionCb, mCtx);
|
|
325 |
}
|
19
|
326 |
}
|
|
327 |
else if (!enable && (mCtx->trackpositionenabled))
|
25
|
328 |
{
|
19
|
329 |
mCtx->trackpositionenabled = XA_BOOLEAN_FALSE;
|
25
|
330 |
if (mCtx->runpositiontimer > 0)
|
|
331 |
{
|
19
|
332 |
g_source_remove(mCtx->runpositiontimer);
|
25
|
333 |
mCtx->runpositiontimer = 0;
|
|
334 |
}
|
19
|
335 |
}
|
|
336 |
|
|
337 |
DEBUG_API("<-XARecordItfAdapt_EnablePositionTracking");
|
|
338 |
return XA_RESULT_SUCCESS;
|
25
|
339 |
}
|
19
|
340 |
|