19
|
1 |
/*
|
|
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:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <pthread.h>
|
|
19 |
#include <gst.h>
|
|
20 |
#include "xamediarecorderadaptctx.h"
|
|
21 |
#include "xarecorditfadaptation.h"
|
|
22 |
#include "xaadaptationgst.h"
|
|
23 |
#include "xacameraadaptctx.h"
|
|
24 |
|
|
25 |
/*forward declaration of position updater callback*/
|
|
26 |
gboolean XARecordItfAdapt_PositionUpdate(gpointer ctx);
|
|
27 |
|
|
28 |
extern XAboolean cameraRealized;
|
|
29 |
extern XACameraAdaptationCtx_* cameraCtx;
|
|
30 |
|
|
31 |
/*
|
|
32 |
* XAresult XARecordItfAdapt_SetRecordState(XAAdaptationGstCtx *ctx, XAuint32 state)
|
|
33 |
* Sets record state to GStreamer.
|
|
34 |
* @param XAAdaptationGstCtx *ctx - Adaptation context
|
|
35 |
* XAuint32 state - Record state to be set
|
|
36 |
* @return XAresult ret - Success value
|
|
37 |
*/
|
|
38 |
XAresult XARecordItfAdapt_SetRecordState(XAAdaptationGstCtx *bCtx, XAuint32 state)
|
|
39 |
{
|
|
40 |
XAresult ret = XA_RESULT_SUCCESS;
|
|
41 |
XAboolean closeSink = XA_BOOLEAN_FALSE;
|
|
42 |
XAboolean requestStateChange = XA_BOOLEAN_FALSE;
|
|
43 |
GstStateChangeReturn gstRet = GST_STATE_CHANGE_SUCCESS;
|
|
44 |
XAMediaRecorderAdaptationCtx* mCtx = NULL;
|
|
45 |
XAboolean recording = XA_BOOLEAN_FALSE;
|
|
46 |
DEBUG_API_A1("->XARecordItfAdapt_SetRecordState %s",RECORDSTATENAME(state));
|
|
47 |
if(!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
48 |
{
|
|
49 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
50 |
DEBUG_API("<-XARecordItfAdapt_SetRecordState");
|
|
51 |
return XA_RESULT_PARAMETER_INVALID;
|
|
52 |
}
|
|
53 |
|
|
54 |
mCtx = (XAMediaRecorderAdaptationCtx*) bCtx;
|
|
55 |
|
|
56 |
mCtx->isRecord = XA_BOOLEAN_TRUE;
|
|
57 |
|
|
58 |
switch ( state )
|
|
59 |
{
|
|
60 |
case XA_RECORDSTATE_STOPPED:
|
|
61 |
{
|
|
62 |
// Audio sourse should be stopped
|
|
63 |
if ( mCtx->audiosource )
|
|
64 |
{
|
|
65 |
if ( gst_element_send_event (mCtx->audiosource, gst_event_new_eos ()) == TRUE )
|
|
66 |
{
|
|
67 |
DEBUG_INFO ("posted eos");
|
|
68 |
}
|
|
69 |
else
|
|
70 |
{
|
|
71 |
DEBUG_ERR("FAIL : post eos");
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
if ( cameraCtx && cameraRealized && mCtx->isobjvsrc && mCtx->videosource )
|
|
76 |
{
|
|
77 |
cameraCtx->recording = XA_BOOLEAN_FALSE;
|
|
78 |
|
|
79 |
if(!cameraCtx->playing && !cameraCtx->snapshotting)
|
|
80 |
{
|
|
81 |
/* Neither view finder or recorder is running -> pause camera */
|
|
82 |
if ( GST_STATE( GST_ELEMENT(mCtx->videosource)) == GST_STATE_PLAYING )
|
|
83 |
{
|
|
84 |
GstStateChangeReturn gret;
|
|
85 |
DEBUG_INFO("Stop camera source");
|
|
86 |
gret = gst_element_set_state( GST_ELEMENT(mCtx->videosource), GST_STATE_PAUSED );
|
|
87 |
if(gret == GST_STATE_CHANGE_SUCCESS)
|
|
88 |
gret = gst_element_get_state( GST_ELEMENT(mCtx->videosource), NULL,NULL, XA_ADAPT_ASYNC_TIMEOUT_SHORT_NSEC );
|
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
bCtx->binWantedState = GST_STATE_PAUSED;
|
|
94 |
closeSink = XA_BOOLEAN_TRUE;
|
|
95 |
if(mCtx->runpositiontimer > 0)
|
|
96 |
{
|
|
97 |
g_source_remove(mCtx->runpositiontimer);
|
|
98 |
mCtx->runpositiontimer=0;
|
|
99 |
}
|
|
100 |
|
|
101 |
if ( mCtx->recThrCtx.bufInsufficientSem )
|
|
102 |
{
|
|
103 |
DEBUG_INFO("No buffer-insufficient received, posting record thr semaphore.");
|
|
104 |
if ( XAImpl_PostSemaphore( mCtx->recThrCtx.bufInsufficientSem ) != XA_RESULT_SUCCESS)
|
|
105 |
{
|
|
106 |
DEBUG_ERR("Posting buffer-insufficient semaphore FAILED!");
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
break;
|
|
111 |
}
|
|
112 |
case XA_RECORDSTATE_PAUSED:
|
|
113 |
{
|
|
114 |
if ( cameraCtx && cameraRealized && mCtx->isobjvsrc && mCtx->videosource )
|
|
115 |
{
|
|
116 |
cameraCtx->recording = XA_BOOLEAN_FALSE;
|
|
117 |
if(!cameraCtx->playing && !cameraCtx->snapshotting)
|
|
118 |
{
|
|
119 |
/* Neither view finder or recorder is running -> pause camera */
|
|
120 |
if ( GST_STATE( GST_ELEMENT(mCtx->videosource)) == GST_STATE_PLAYING )
|
|
121 |
{
|
|
122 |
GstStateChangeReturn gret;
|
|
123 |
DEBUG_INFO("Stop camera source");
|
|
124 |
gret = gst_element_set_state( GST_ELEMENT(mCtx->videosource), GST_STATE_PAUSED );
|
|
125 |
if(gret == GST_STATE_CHANGE_SUCCESS)
|
|
126 |
gret = gst_element_get_state( GST_ELEMENT(mCtx->videosource), NULL,NULL, XA_ADAPT_ASYNC_TIMEOUT_SHORT_NSEC );
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
if(mCtx->xaRecordState==XA_RECORDSTATE_STOPPED && mCtx->encodingchanged)
|
|
132 |
{
|
|
133 |
XAMediaRecorderAdapt_ChangeEncoders( mCtx );
|
|
134 |
mCtx->encodingchanged = XA_BOOLEAN_FALSE;
|
|
135 |
}
|
|
136 |
bCtx->binWantedState = GST_STATE_PAUSED;
|
|
137 |
if(mCtx->runpositiontimer > 0)
|
|
138 |
{
|
|
139 |
g_source_remove(mCtx->runpositiontimer);
|
|
140 |
mCtx->runpositiontimer=0;
|
|
141 |
}
|
|
142 |
break;
|
|
143 |
}
|
|
144 |
case XA_RECORDSTATE_RECORDING:
|
|
145 |
{
|
|
146 |
if ( cameraCtx && mCtx->isobjvsrc )
|
|
147 |
{
|
|
148 |
cameraCtx->recording = XA_BOOLEAN_TRUE;
|
|
149 |
}
|
|
150 |
|
|
151 |
if(mCtx->xaRecordState==XA_RECORDSTATE_STOPPED && (mCtx->encodingchanged))
|
|
152 |
{
|
|
153 |
XAMediaRecorderAdapt_ChangeEncoders( mCtx );
|
|
154 |
mCtx->encodingchanged = XA_BOOLEAN_FALSE;
|
|
155 |
}
|
|
156 |
|
|
157 |
if ( mCtx->recThrCtx.bufInsufficientSem )
|
|
158 |
{
|
|
159 |
/* Recording to address and recording thread semaphora is created */
|
|
160 |
if(! XAImpl_StartThread(&(mCtx->recordingEventThr),NULL, &XAMediaRecorderAdapt_RecordEventThr, (void*)mCtx ))
|
|
161 |
{
|
|
162 |
DEBUG_ERR("Start thread Failed");
|
|
163 |
return XA_RESULT_INTERNAL_ERROR;
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
bCtx->binWantedState = GST_STATE_PLAYING;
|
|
168 |
recording = XA_BOOLEAN_TRUE;
|
|
169 |
break;
|
|
170 |
}
|
|
171 |
default:
|
|
172 |
DEBUG_ERR("Unhandled state");
|
|
173 |
ret = XA_RESULT_PARAMETER_INVALID;
|
|
174 |
break;
|
|
175 |
}
|
|
176 |
|
|
177 |
if( ret == XA_RESULT_SUCCESS )
|
|
178 |
{
|
|
179 |
mCtx->xaRecordState = state;
|
|
180 |
}
|
|
181 |
|
|
182 |
/* launch Gstreamer state change only if necessary */
|
|
183 |
if( GST_STATE_TARGET(bCtx->bin) == bCtx->binWantedState )
|
|
184 |
{
|
|
185 |
DEBUG_INFO("Gst already transitioning to wanted state!!");
|
|
186 |
requestStateChange = XA_BOOLEAN_FALSE;
|
|
187 |
}
|
|
188 |
else
|
|
189 |
{
|
|
190 |
if( (GST_STATE(bCtx->bin) == bCtx->binWantedState) &&
|
|
191 |
(GST_STATE_PENDING(bCtx->bin) == GST_STATE_VOID_PENDING) )
|
|
192 |
{
|
|
193 |
DEBUG_ERR_A3("WARNING : gststate %d == wanted %d != gsttarget %d and no statechange pending",
|
|
194 |
GST_STATE(bCtx->bin), bCtx->binWantedState, GST_STATE_TARGET(bCtx->bin));
|
|
195 |
}
|
|
196 |
requestStateChange = XA_BOOLEAN_TRUE;
|
|
197 |
}
|
|
198 |
|
|
199 |
if( requestStateChange )
|
|
200 |
{
|
|
201 |
XAAdaptationGst_PrepareAsyncWait(bCtx);
|
|
202 |
DEBUG_INFO_A1("Sending change state request to state %d", bCtx->binWantedState);
|
|
203 |
gstRet = gst_element_set_state( GST_ELEMENT(bCtx->bin), bCtx->binWantedState);
|
|
204 |
switch ( gstRet )
|
|
205 |
{
|
|
206 |
case GST_STATE_CHANGE_FAILURE:
|
|
207 |
DEBUG_ERR_A1("FAILED to change state (target %d)",bCtx->binWantedState);
|
|
208 |
bCtx->binWantedState = GST_STATE(bCtx->bin);
|
|
209 |
ret = XA_RESULT_INTERNAL_ERROR;
|
|
210 |
break;
|
|
211 |
case GST_STATE_CHANGE_ASYNC:
|
|
212 |
DEBUG_INFO_A1("Change state will happen asyncronously (target %d)",bCtx->binWantedState);
|
|
213 |
XAAdaptationGst_StartAsyncWait(bCtx);
|
|
214 |
ret = XA_RESULT_SUCCESS;
|
|
215 |
break;
|
|
216 |
case GST_STATE_CHANGE_NO_PREROLL:
|
|
217 |
DEBUG_INFO("GST_STATE_CHANGE_NO_PREROLL");
|
|
218 |
/* deliberate fall-through */
|
|
219 |
case GST_STATE_CHANGE_SUCCESS:
|
|
220 |
DEBUG_INFO_A1("Successfully changed state (target %d)",bCtx->binWantedState);
|
|
221 |
ret = XA_RESULT_SUCCESS;
|
|
222 |
break;
|
|
223 |
default:
|
|
224 |
DEBUG_ERR_A1("Unhandled error (%d)",gstRet);
|
|
225 |
ret = XA_RESULT_UNKNOWN_ERROR;
|
|
226 |
break;
|
|
227 |
}
|
|
228 |
bCtx->waitingasyncop = XA_BOOLEAN_FALSE;
|
|
229 |
}
|
|
230 |
if( (GST_STATE(bCtx->bin) > GST_STATE_READY) && closeSink )
|
|
231 |
{ /* close the sink*/
|
|
232 |
gst_element_send_event(bCtx->bin,gst_event_new_flush_start());
|
|
233 |
gst_element_send_event(bCtx->bin,gst_event_new_flush_stop());
|
|
234 |
}
|
|
235 |
|
|
236 |
if ( recording && mCtx->isobjvsrc && mCtx->videosource )
|
|
237 |
{
|
|
238 |
GstPad *pad = gst_element_get_static_pad( GST_ELEMENT(mCtx->videosource), "MRObjSrc");
|
|
239 |
if( pad && gst_pad_is_linked(pad) )
|
|
240 |
{
|
|
241 |
DEBUG_INFO_A2("unblock element:%s pad:%s",
|
|
242 |
gst_element_get_name(mCtx->videosource),
|
|
243 |
gst_pad_get_name(pad));
|
|
244 |
gst_pad_set_blocked_async(pad, FALSE, XAAdaptationGst_PadBlockCb, NULL);
|
|
245 |
}
|
|
246 |
|
|
247 |
if ( GST_STATE( GST_ELEMENT(mCtx->videosource)) != GST_STATE_PLAYING )
|
|
248 |
{
|
|
249 |
GstStateChangeReturn gret = GST_STATE_CHANGE_SUCCESS;
|
|
250 |
DEBUG_INFO("Start camera source");
|
|
251 |
gret = gst_element_set_state( GST_ELEMENT(mCtx->videosource), GST_STATE_PLAYING );
|
|
252 |
if(gret == GST_STATE_CHANGE_SUCCESS)
|
|
253 |
gret = gst_element_get_state( GST_ELEMENT(mCtx->videosource), NULL,NULL, XA_ADAPT_ASYNC_TIMEOUT_SHORT_NSEC );
|
|
254 |
}
|
|
255 |
}
|
|
256 |
DEBUG_API("<-XARecordItfAdapt_SetRecordState");
|
|
257 |
return ret;
|
|
258 |
}
|
|
259 |
|
|
260 |
/*
|
|
261 |
* XAresult XARecordItfAdapt_GetRecordState(XAAdaptationGstCtx *bCtx, XAuint32 *state)
|
|
262 |
* Description: Return record state
|
|
263 |
*/
|
|
264 |
XAresult XARecordItfAdapt_GetRecordState(XAAdaptationGstCtx *bCtx, XAuint32 *state)
|
|
265 |
{
|
|
266 |
XAMediaRecorderAdaptationCtx* mCtx = NULL;
|
|
267 |
DEBUG_API("->XARecordItfAdapt_GetRecordState");
|
|
268 |
|
|
269 |
if(!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
270 |
{
|
|
271 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
272 |
DEBUG_API("<-XARecordItfAdapt_GetRecordState");
|
|
273 |
return XA_RESULT_PARAMETER_INVALID;
|
|
274 |
}
|
|
275 |
|
|
276 |
mCtx = (XAMediaRecorderAdaptationCtx*) bCtx;
|
|
277 |
|
|
278 |
*state = mCtx->xaRecordState;
|
|
279 |
|
|
280 |
DEBUG_API("<-XARecordItfAdapt_GetRecordState");
|
|
281 |
return XA_RESULT_SUCCESS;
|
|
282 |
}
|
|
283 |
|
|
284 |
/*
|
|
285 |
* XAresult XARecordItfAdapt_GetPosition(XAAdaptationGstCtx *ctx, AdaptationContextIDS ctxIDs, XAmillisecond *pMsec)
|
|
286 |
* @param XAAdaptationGstCtx *ctx - Adaptation context, this will be casted to correct type regarding to contextID value given as 2nd parameter
|
|
287 |
* XAmillisecond *pMsec - Pointer where to store current position in stream.
|
|
288 |
* @return XAresult ret - Success value
|
|
289 |
*/
|
|
290 |
XAresult XARecordItfAdapt_GetPosition(XAAdaptationGstCtx *bCtx, XAmillisecond *pMsec)
|
|
291 |
{
|
|
292 |
XAresult ret = XA_RESULT_SUCCESS;
|
|
293 |
|
|
294 |
gint64 position=0;
|
|
295 |
GstFormat format = GST_FORMAT_TIME;
|
|
296 |
DEBUG_API("->XARecordItfAdapt_GetPosition");
|
|
297 |
|
|
298 |
if(!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
299 |
{
|
|
300 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
301 |
DEBUG_API("<-XARecordItfAdapt_GetPosition");
|
|
302 |
/* invalid parameter */
|
|
303 |
return XA_RESULT_PARAMETER_INVALID;
|
|
304 |
}
|
|
305 |
if ( gst_element_query_position( GST_ELEMENT(bCtx->bin), &format, &position ) )
|
|
306 |
{
|
|
307 |
ret = XA_RESULT_SUCCESS;
|
|
308 |
*pMsec = GST_TIME_AS_MSECONDS(position); /*Warning ok due to used API specification*/
|
|
309 |
DEBUG_INFO_A1("Gst: Position in microseconds : %u", *pMsec );
|
|
310 |
}
|
|
311 |
else
|
|
312 |
{
|
|
313 |
DEBUG_ERR("WARNING: Gst: could not get position");
|
|
314 |
/* probably not fully prerolled - safe assumption for position = 0 */
|
|
315 |
*pMsec = 0;
|
|
316 |
ret = XA_RESULT_SUCCESS;
|
|
317 |
}
|
|
318 |
|
|
319 |
DEBUG_API("<-XARecordItfAdapt_GetPosition");
|
|
320 |
return ret;
|
|
321 |
}
|
|
322 |
|
|
323 |
|
|
324 |
/*
|
|
325 |
* gboolean XARecordItfAdapt_PositionUpdate(gpointer ctx)
|
|
326 |
* callback.
|
|
327 |
* If position tracking enabled, periodic timer calls this method every XA_ADAPT_PU_INTERVAL msecs
|
|
328 |
* @return false to stop periodic calls
|
|
329 |
*/
|
|
330 |
gboolean XARecordItfAdapt_PositionUpdate(gpointer ctx)
|
|
331 |
{
|
|
332 |
XAAdaptationGstCtx *bCtx = (XAAdaptationGstCtx*) ctx;
|
|
333 |
XAMediaRecorderAdaptationCtx* mCtx = (XAMediaRecorderAdaptationCtx*) ctx;
|
|
334 |
DEBUG_API("->XARecordItfAdapt_PositionUpdate");
|
|
335 |
|
|
336 |
if( mCtx && mCtx->trackpositionenabled )
|
|
337 |
{
|
|
338 |
GstFormat format = GST_FORMAT_TIME;
|
|
339 |
gint64 position = 0;
|
|
340 |
if ( gst_element_query_position( GST_ELEMENT(bCtx->bin), &format, &position ) )
|
|
341 |
{
|
|
342 |
XAuint32 posMsec = GST_TIME_AS_MSECONDS(position);/*Warning ok due to used API specification*/
|
|
343 |
XAAdaptEvent event = {XA_RECORDITFEVENTS, XA_ADAPT_POSITION_UPDATE_EVT, 1, NULL};
|
|
344 |
event.data = &posMsec;
|
|
345 |
DEBUG_API_A1("XARecordItfAdapt_PositionUpdate: pos %lu ms", posMsec);
|
|
346 |
/* send needed events */
|
|
347 |
XAAdaptationBase_SendAdaptEvents(&bCtx->baseObj, &event );
|
|
348 |
}
|
|
349 |
else
|
|
350 |
{
|
|
351 |
DEBUG_ERR("Gst: Failed to get position");
|
|
352 |
}
|
|
353 |
DEBUG_API_A1("<-XARecordItfAdapt_PositionUpdate: %d", mCtx->runpositiontimer);
|
|
354 |
/* return false to stop timer */
|
|
355 |
return( mCtx->runpositiontimer );
|
|
356 |
}
|
|
357 |
return FALSE;
|
|
358 |
}
|
|
359 |
|
|
360 |
/*
|
|
361 |
* XAresult XARecordItfAdapt_EnablePositionTracking
|
|
362 |
* Enable/disable periodic position tracking callbacks
|
|
363 |
*/
|
|
364 |
XAresult XARecordItfAdapt_EnablePositionTracking(XAAdaptationGstCtx *bCtx, XAboolean enable)
|
|
365 |
{
|
|
366 |
XAMediaRecorderAdaptationCtx* mCtx = (XAMediaRecorderAdaptationCtx*) bCtx;
|
|
367 |
DEBUG_API_A1("->XARecordItfAdapt_EnablePositionTracking (enable: %lu)", enable);
|
|
368 |
|
|
369 |
if(!bCtx || bCtx->baseObj.ctxId != XAMediaRecorderAdaptation)
|
|
370 |
{
|
|
371 |
DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
|
|
372 |
DEBUG_API("<-XARecordItfAdapt_EnablePositionTracking");
|
|
373 |
/* invalid parameter */
|
|
374 |
return XA_RESULT_PARAMETER_INVALID;
|
|
375 |
}
|
|
376 |
|
|
377 |
|
|
378 |
/* create a timer to track position of playback */
|
|
379 |
if(enable && !(mCtx->trackpositionenabled))
|
|
380 |
{
|
|
381 |
mCtx->trackpositionenabled = XA_BOOLEAN_TRUE;
|
|
382 |
mCtx->positionCb = &XARecordItfAdapt_PositionUpdate;
|
|
383 |
/* if recording is already on, create a timer to track position of recording */
|
|
384 |
if( GST_STATE(bCtx->bin) == GST_STATE_PLAYING )
|
|
385 |
{
|
|
386 |
mCtx->runpositiontimer = g_timeout_add(XA_ADAPT_PU_INTERVAL, mCtx->positionCb, mCtx);
|
|
387 |
}
|
|
388 |
}
|
|
389 |
else if (!enable && (mCtx->trackpositionenabled))
|
|
390 |
{
|
|
391 |
mCtx->trackpositionenabled = XA_BOOLEAN_FALSE;
|
|
392 |
if(mCtx->runpositiontimer > 0)
|
|
393 |
{
|
|
394 |
g_source_remove(mCtx->runpositiontimer);
|
|
395 |
mCtx->runpositiontimer=0;
|
|
396 |
}
|
|
397 |
}
|
|
398 |
|
|
399 |
DEBUG_API("<-XARecordItfAdapt_EnablePositionTracking");
|
|
400 |
return XA_RESULT_SUCCESS;
|
|
401 |
}
|
|
402 |
|