4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyuntae Kim <ht1211.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #ifndef __MM_STREAMRECORDER_INTERNAL_H__
23 #define __MM_STREAMRECORDER_INTERNAL_H__
25 /*=======================================================================================
27 ========================================================================================*/
32 #include <semaphore.h>
36 #include <mm_attrs_private.h>
37 #include <mm_message.h>
39 #include "mm_streamrecorder.h"
42 /* streamrecorder sub module */
43 #include "mm_streamrecorder_util.h"
44 #include "mm_streamrecorder_video.h"
45 #include "mm_streamrecorder_audio.h"
46 #include "mm_streamrecorder_fileinfo.h"
47 #include "mm_streamrecorder_gstcommon.h"
48 #include "mm_streamrecorder_ini.h"
49 #include "mm_streamrecorder_buffer_manager.h"
55 /*=======================================================================================
57 ========================================================================================*/
61 * Macro for getting array size
63 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
66 #define _MMSTREAMRECORDER_STATE_SET_COUNT 3 /* checking interval */
67 #define _MMSTREAMRECORDER_STATE_CHECK_INTERVAL 1000 //5000 /* checking interval */
70 * Functions related with LOCK and WAIT
72 #define _MMSTREAMRECORDER_CAST_MTSAFE(handle) (((mmf_streamrecorder_t*)handle)->mtsafe)
73 #define _MMSTREAMRECORDER_LOCK_FUNC(mutex) pthread_mutex_lock(&mutex)
74 #define _MMSTREAMRECORDER_TRYLOCK_FUNC(mutex) (!pthread_mutex_trylock(&mutex))
75 #define _MMSTREAMRECORDER_UNLOCK_FUNC(mutex) pthread_mutex_unlock(&mutex)
77 #define _MMSTREAMRECORDER_GET_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).lock)
78 #define _MMSTREAMRECORDER_LOCK(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_LOCK(handle))
79 #define _MMSTREAMRECORDER_TRYLOCK(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_LOCK(handle))
80 #define _MMSTREAMRECORDER_UNLOCK(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_LOCK(handle))
82 #define _MMSTREAMRECORDER_GET_COND(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).cond)
83 #define _MMSTREAMRECORDER_WAIT(handle) pthread_cond_wait(&_MMSTREAMRECORDER_GET_COND(handle), _MMSTREAMRECORDER_GET_LOCK(handle))
84 #define _MMSTREAMRECORDER_TIMED_WAIT(handle, timeout) pthread_cond_timedwait(&_MMSTREAMRECORDER_GET_COND(handle), _MMSTREAMRECORDER_GET_LOCK(handle),&timeout)
87 #define _MMSTREAMRECORDER_GET_CMD_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).cmd_lock)
88 #define _MMSTREAMRECORDER_LOCK_CMD(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_CMD_LOCK(handle))
89 #define _MMSTREAMRECORDER_TRYLOCK_CMD(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_CMD_LOCK(handle))
90 #define _MMSTREAMRECORDER_UNLOCK_CMD(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_CMD_LOCK(handle))
92 /* for state change */
93 #define _MMSTREAMRECORDER_GET_STATE_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).state_lock)
94 #define _MMSTREAMRECORDER_LOCK_STATE(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_STATE_LOCK(handle))
95 #define _MMSTREAMRECORDER_TRYLOCK_STATE(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_STATE_LOCK(handle))
96 #define _MMSTREAMRECORDER_UNLOCK_STATE(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_STATE_LOCK(handle))
98 /* for gstreamer state change */
99 #define _MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).gst_state_lock)
100 #define _MMSTREAMRECORDER_LOCK_GST_STATE(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle))
101 #define _MMSTREAMRECORDER_TRYLOCK_GST_STATE(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle))
102 #define _MMSTREAMRECORDER_UNLOCK_GST_STATE(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle))
104 /* for setting/calling callback */
105 #define _MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).message_cb_lock)
106 #define _MMSTREAMRECORDER_LOCK_MESSAGE_CALLBACK(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle))
107 #define _MMSTREAMRECORDER_TRYLOCK_MESSAGE_CALLBACK(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle))
108 #define _MMSTREAMRECORDER_UNLOCK_MESSAGE_CALLBACK(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle))
111 * Caster of main handle (streamrecorder)
113 #define MMF_STREAMRECORDER(h) (mmf_streamrecorder_t *)(h)
116 * Caster of subcontext
118 #define MMF_STREAMRECORDER_SUBCONTEXT(h) (((mmf_streamrecorder_t *)(h))->sub_context)
120 /* LOCAL CONSTANT DEFINITIONS */
122 /*=======================================================================================
124 ========================================================================================*/
127 * streamrecorder Pipeline's Element name.
128 * @note index of element.
131 _MMSTREAMRECORDER_ENCODE_NONE = (-1),
133 /* Main Pipeline Element */
134 _MMSTREAMRECORDER_ENCODE_MAIN_PIPE = 0x00,
136 /* Pipeline element of Audio input */
137 _MMSTREAMRECORDER_AUDIOSRC_BIN,
138 _MMSTREAMRECORDER_AUDIOSRC_SRC, /* appsrc */
139 _MMSTREAMRECORDER_AUDIOSRC_FILT,
141 /* Pipeline element of Encodebin */
142 _MMSTREAMRECORDER_ENCSINK_BIN,
143 _MMSTREAMRECORDER_ENCSINK_SRC, /* video appsrc */
144 _MMSTREAMRECORDER_ENCSINK_FILT,
145 _MMSTREAMRECORDER_ENCSINK_ENCBIN,
146 _MMSTREAMRECORDER_ENCSINK_AQUE,
147 _MMSTREAMRECORDER_ENCSINK_CONV,
148 _MMSTREAMRECORDER_ENCSINK_AENC,
149 _MMSTREAMRECORDER_ENCSINK_AENC_QUE,
150 _MMSTREAMRECORDER_ENCSINK_VQUE,
151 _MMSTREAMRECORDER_ENCSINK_VCONV,
152 _MMSTREAMRECORDER_ENCSINK_VENC,
153 _MMSTREAMRECORDER_ENCSINK_VENC_QUE,
154 _MMSTREAMRECORDER_ENCSINK_PARSER,
155 _MMSTREAMRECORDER_ENCSINK_ITOG,
156 _MMSTREAMRECORDER_ENCSINK_ICROP,
157 _MMSTREAMRECORDER_ENCSINK_ISCALE,
158 _MMSTREAMRECORDER_ENCSINK_IFILT,
159 _MMSTREAMRECORDER_ENCSINK_IQUE,
160 _MMSTREAMRECORDER_ENCSINK_IENC,
161 _MMSTREAMRECORDER_ENCSINK_MUX,
162 _MMSTREAMRECORDER_ENCSINK_SINK,
164 _MMSTREAMRECORDER_ENCODE_PIPELINE_ELEMENT_NUM,
165 } _MMSTREAMRECORDER_ENCODE_PIPELINE_ELELMENT;
168 * Command type for streamrecorder.
172 _MM_STREAMRECORDER_CMD_CREATE,
173 _MM_STREAMRECORDER_CMD_DESTROY,
174 _MM_STREAMRECORDER_CMD_REALIZE,
175 _MM_STREAMRECORDER_CMD_UNREALIZE,
176 _MM_STREAMRECORDER_CMD_START,
177 _MM_STREAMRECORDER_CMD_STOP,
178 _MM_STREAMRECORDER_CMD_RECORD,
179 _MM_STREAMRECORDER_CMD_PAUSE,
180 _MM_STREAMRECORDER_CMD_COMMIT,
181 _MM_STREAMRECORDER_CMD_CANCEL,
182 _MM_STREAMRECORDER_CMD_QUIT,
183 } _MMstreamrecorderCommandType;
186 * System state change cause
189 _MMSTREAMRECORDER_STATE_CHANGE_NORMAL = 0,
190 } _MMstreamrecorderStateChange;
193 /*=======================================================================================
194 | STRUCTURE DEFINITIONS |
195 ========================================================================================*/
198 * MMstreamrecorder information for Multi-Thread Safe
201 pthread_mutex_t lock; /**< Mutex (for general use) */
202 pthread_cond_t cond; /**< Condition (for general use) */
203 pthread_mutex_t cmd_lock; /**< Mutex (for command) */
204 pthread_mutex_t state_lock; /**< Mutex (for state change) */
205 pthread_mutex_t gst_state_lock; /**< Mutex (for state change) */
206 pthread_mutex_t message_cb_lock;/**< Mutex (for message callback) */
207 } _MMStreamRecorderMTSafe;
210 * MMstreamrecorder information for command loop
214 * MMstreamrecorder Sub Context
217 bool isMaxsizePausing; /**< Because of size limit, pipeline is paused. */
218 bool isMaxtimePausing; /**< Because of time limit, pipeline is paused. */
219 int encode_element_num; /**< count of encode element */
220 GstClockTime pipeline_time; /**< current time of Gstreamer Pipeline */
221 GstClockTime pause_time; /**< amount of time while pipeline is in PAUSE state.*/
222 gboolean error_occurs; /**< flag for error */
223 int error_code; /**< error code for internal gstreamer error */
224 gboolean ferror_send; /**< file write/seek error **/
225 guint ferror_count; /**< file write/seek error count **/
226 gboolean bget_eos; /**< Whether getting EOS */
227 gboolean video_enable; /**< whether video is disabled or not when record */
228 gboolean audio_enable; /**< whether audio is disabled or not when record */
230 /* INI information */
231 unsigned int fourcc; /**< Get fourcc value */
232 _MMStreamRecorderVideoInfo *info_video; /**< extra information for video recording */
233 _MMStreamRecorderAudioInfo *info_audio; /**< extra information for audio recording */
234 _MMStreamRecorderFileInfo *info_file; /**< extra information for audio recording */
235 _MMStreamRecorderGstElement *encode_element;
236 /**< array of encode element */
238 // type_element *VideosinkElement; /**< configure data of videosink element */
239 } _MMStreamRecorderSubContext;
242 * _MMstreamrecorderContext
244 typedef struct mmf_streamrecorder {
246 // int type; /**< mmstreamrecorder_mode_type */
247 int state; /**< state of streamrecorder */
248 // int target_state; /**< Target state that want to set. This is a flag that
249 // * stands for async state changing. If this value differ from state,
250 // * it means state is changing now asychronously. */
253 MMHandleType attributes; /**< Attribute handle */
254 _MMStreamRecorderSubContext *sub_context; /**< sub context */
255 GList *buffer_probes; /**< a list of buffer probe handle */
256 GList *event_probes; /**< a list of event probe handle */
257 GList *data_probes; /**< a list of data probe handle */
258 GList *signals; /**< a list of signal handle */
259 GList *msg_data; /**< a list of msg data */
260 guint pipeline_cb_event_id; /**< Event source ID of pipeline message callback */
261 guint encode_pipeline_cb_event_id; /**< Event source ID of encode pipeline message callback */
263 /* callback handlers */
264 MMMessageCallback msg_cb; /**< message callback */
265 void *msg_cb_param; /**< message callback parameter */
266 int (*command) (MMHandleType, int); /**< streamrecorder's command */
269 _MMStreamRecorderMTSafe mtsafe; /**< Thread safe */
270 mm_streamrecorder_ini_t ini;
272 int reserved[4]; /**< reserved */
273 } mmf_streamrecorder_t;
275 /*=======================================================================================
276 | EXTERN GLOBAL VARIABLE |
277 ========================================================================================*/
279 /*=======================================================================================
280 | GLOBAL FUNCTION PROTOTYPES |
281 ========================================================================================*/
283 * This function creates streamrecorder for capturing still image and recording.
285 * @param[out] handle Specifies the streamrecorder handle
286 * @param[in] info Preset information of streamrecorder
287 * @return This function returns zero on success, or negative value with error code.
288 * @remarks When this function calls successfully, streamrecorder handle will be filled with a @n
289 * valid value and the state of the streamrecorder will become MM_streamrecorder_STATE_NULL.@n
290 * Note that it's not ready to working streamrecorder. @n
291 * You should call mmstreamrecorder_realize before starting streamrecorder.
292 * @see _mmstreamrecorder_create
294 int _mmstreamrecorder_create(MMHandleType *handle);
297 * This function gets the current state of streamreccorder.
298 * mm_streamrecorderr is working on the base of its state. An user should check the state of mm_streamrecorder before calling its functions.
299 * If the handle is avaiable, user can retrieve the value.
301 * @param[in] streamrecorder A handle of streamrecorder.
302 * @param[out] state On return, it contains current state of streamrecorder.
303 * @return This function returns zero(MM_ERROR_NONE) on success, or negative value with error code.
304 * Please refer 'mm_error.h' to know the exact meaning of the error.
305 * @see MMStreamRecorderStateType
311 MMStreamRecorderStateType _mmstreamrecorder_get_state(MMHandleType handle);
314 * This function destroys instance of streamrecorder.
316 * @param[in] hstreamrecorder Specifies the streamrecorder handle
317 * @return This function returns zero on success, or negative value with error code.
318 * @see _mmstreamrecorder_create
320 int _mmstreamrecorder_destroy(MMHandleType handle);
323 * This function allocates memory for streamrecorder.
325 * @param[in] hstreamrecorder Specifies the streamrecorder handle
326 * @return This function returns zero on success, or negative value with error code.
327 * @remarks This function can be called successfully when current state is MM_streamrecorder_STATE_NULL @n
328 * and the state of the streamrecorder will become MM_streamrecorder_STATE_READY. @n
329 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
330 * @see _mmstreamrecorder_unrealize
331 * @pre MM_streamrecorder_STATE_NULL
332 * @post MM_streamrecorder_STATE_READY
334 int _mmstreamrecorder_realize(MMHandleType hstreamrecorder);
337 * This function free allocated memory for streamrecorder.
339 * @param[in] hstreamrecorder Specifies the streamrecorder handle
340 * @return This function returns zero on success, or negative value with error code.
341 * @remarks This function release all resources which are allocated for the streamrecorder engine.@n
342 * This function can be called successfully when current state is MM_streamrecorder_STATE_READY and @n
343 * the state of the streamrecorder will become MM_streamrecorder_STATE_NULL. @n
344 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
345 * @see _mmstreamrecorder_realize
346 * @pre MM_streamrecorder_STATE_READY
347 * @post MM_streamrecorder_STATE_NULL
349 int _mmstreamrecorder_unrealize(MMHandleType hstreamrecorder);
352 * This function is to start previewing.
354 * @param[in] hstreamrecorder Specifies the streamrecorder handle
355 * @return This function returns zero on success, or negative value with error code.
356 * @remarks This function can be called successfully when current state is MM_streamrecorder_STATE_READY and @n
357 * the state of the streamrecorder will become MM_streamrecorder_STATE_PREPARE. @n
358 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
359 * @see _mmstreamrecorder_stop
361 int _mmstreamrecorder_record(MMHandleType hstreamrecorder);
363 int _mmstreamrecorder_push_stream_buffer(MMHandleType handle, MMStreamRecorderStreamType streamtype, unsigned long timestamp, void *buffer, int size);
366 * This function is to pause video and audio recording
368 * @param[in] hstreamrecorder Specifies the streamrecorder handle
369 * @return This function returns zero on success, or negative value with error code.
370 * @remarks This function can be called successfully when current state is MM_streamrecorder_STATE_RECORDING and @n
371 * the state of the streamrecorder will become MM_streamrecorder_STATE_PAUSED.@n
372 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.@n
373 * @see _mmstreamrecorder_record
375 int _mmstreamrecorder_pause(MMHandleType hstreamrecorder);
378 * This function is to stop video and audio recording and save results.
380 * @param[in] hstreamrecorder Specifies the streamrecorder handle
381 * @return This function returns zero on success, or negative value with error code.
382 * @remarks This function can be called successfully when current state is @n
383 * MM_streamrecorder_STATE_PAUSED or MM_streamrecorder_STATE_RECORDING and @n
384 * the state of the streamrecorder will become MM_streamrecorder_STATE_PREPARE. @n
385 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION
386 * @see _mmstreamrecorder_cancel
388 int _mmstreamrecorder_commit(MMHandleType hstreamrecorder);
391 * This function is to stop video and audio recording and do not save results.
393 * @param[in] hstreamrecorder Specifies the streamrecorder handle
394 * @return This function returns zero on success, or negative value with error code.
395 * @remarks This function can be called successfully when current state is @n
396 * MM_streamrecorder_STATE_PAUSED or MM_streamrecorder_STATE_RECORDING and @n
397 * the state of the streamrecorder will become MM_streamrecorder_STATE_PREPARE. @n
398 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
399 * @see _mmstreamrecorder_commit
401 int _mmstreamrecorder_cancel(MMHandleType hstreamrecorder);
404 * This function is to set callback for receiving messages from streamrecorder.
406 * @param[in] hstreamrecorder Specifies the streamrecorder handle
407 * @param[in] callback Specifies the function pointer of callback function
408 * @param[in] user_data Specifies the user poiner for passing to callback function
410 * @return This function returns zero on success, or negative value with error code.
411 * @remarks typedef bool (*mm_message_callback) (int msg, mm_messageType *param, void *user_param);@n
426 * } mm_message_type; @n
428 * If a message value for mm_message_callback is MM_MESSAGE_STATE_CHANGED, @n
429 * state value in mm_message_type will be a mmstreamrecorder_state_type enum value;@n
431 * If a message value for mm_message_callback is MM_MESSAGE_ERROR, @n
432 * the code value in mm_message_type will be a mmplayer_error_type enum value;
434 * @see mm_message_type, mmstreamrecorder_state_type, mmstreamrecorder_error_type
436 int _mmstreamrecorder_set_message_callback(MMHandleType hstreamrecorder, MMMessageCallback callback, void *user_data);
439 * This function allocates structure of subsidiary attributes.
441 * @param[in] type Allocation type of streamrecorder context.
442 * @return This function returns structure pointer on success, NULL value on failure.
444 * @see _mmstreamrecorder_dealloc_subcontext()
447 int _mmstreamrecorder_alloc_subcontext(MMHandleType handle);
449 int _mmstreamrecorder_alloc_subcontext_videoinfo(MMHandleType handle);
451 int _mmstreamrecorder_alloc_subcontext_audioinfo(MMHandleType handle);
453 int _mmstreamrecorder_alloc_subcontext_fileinfo(MMHandleType handle);
456 * This function releases structure of subsidiary attributes.
458 * @param[in] sc Handle of streamrecorder subcontext.
461 * @see _mmstreamrecorder_alloc_subcontext()
464 void _mmstreamrecorder_dealloc_subcontext(MMHandleType handle);
467 * This function sets command function according to the type.
469 * @param[in] handle Handle of streamrecorder context.
470 * @param[in] type Allocation type of streamrecorder context.
471 * @return This function returns MM_ERROR_NONE on success, or other values with error code.
473 * @see __mmstreamrecorder_video_command(), __mmstreamrecorder_audio_command(), __mmstreamrecorder_image_command()
476 void _mmstreamrecorder_set_functions(MMHandleType handle);
478 void _mmstreamrecorder_unset_functions(MMHandleType handle);
483 #endif /* __MM_streamrecorder_INTERNAL_H__ */