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_message.h>
38 #include "mm_streamrecorder.h"
41 /* streamrecorder sub module */
42 #include "mm_streamrecorder_util.h"
43 #include "mm_streamrecorder_video.h"
44 #include "mm_streamrecorder_audio.h"
45 #include "mm_streamrecorder_fileinfo.h"
46 #include "mm_streamrecorder_gstcommon.h"
47 #include "mm_streamrecorder_ini.h"
48 #include "mm_streamrecorder_buffer_manager.h"
54 /*=======================================================================================
56 ========================================================================================*/
60 * Macro for getting array size
62 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
65 #define _MMSTREAMRECORDER_STATE_SET_COUNT 3 /* checking interval */
66 #define _MMSTREAMRECORDER_STATE_CHECK_INTERVAL 1000 //5000 /* checking interval */
69 * Functions related with LOCK and WAIT
71 #define _MMSTREAMRECORDER_CAST_MTSAFE(handle) (((mmf_streamrecorder_t*)handle)->mtsafe)
72 #define _MMSTREAMRECORDER_LOCK_FUNC(mutex) pthread_mutex_lock(&mutex)
73 #define _MMSTREAMRECORDER_TRYLOCK_FUNC(mutex) (!pthread_mutex_trylock(&mutex))
74 #define _MMSTREAMRECORDER_UNLOCK_FUNC(mutex) pthread_mutex_unlock(&mutex)
76 #define _MMSTREAMRECORDER_GET_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).lock)
77 #define _MMSTREAMRECORDER_LOCK(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_LOCK(handle))
78 #define _MMSTREAMRECORDER_TRYLOCK(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_LOCK(handle))
79 #define _MMSTREAMRECORDER_UNLOCK(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_LOCK(handle))
81 #define _MMSTREAMRECORDER_GET_COND(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).cond)
82 #define _MMSTREAMRECORDER_WAIT(handle) pthread_cond_wait(&_MMSTREAMRECORDER_GET_COND(handle), _MMSTREAMRECORDER_GET_LOCK(handle))
83 #define _MMSTREAMRECORDER_TIMED_WAIT(handle, timeout) pthread_cond_timedwait(&_MMSTREAMRECORDER_GET_COND(handle), _MMSTREAMRECORDER_GET_LOCK(handle), &timeout)
86 #define _MMSTREAMRECORDER_GET_CMD_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).cmd_lock)
87 #define _MMSTREAMRECORDER_LOCK_CMD(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_CMD_LOCK(handle))
88 #define _MMSTREAMRECORDER_TRYLOCK_CMD(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_CMD_LOCK(handle))
89 #define _MMSTREAMRECORDER_UNLOCK_CMD(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_CMD_LOCK(handle))
91 /* for state change */
92 #define _MMSTREAMRECORDER_GET_STATE_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).state_lock)
93 #define _MMSTREAMRECORDER_LOCK_STATE(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_STATE_LOCK(handle))
94 #define _MMSTREAMRECORDER_TRYLOCK_STATE(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_STATE_LOCK(handle))
95 #define _MMSTREAMRECORDER_UNLOCK_STATE(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_STATE_LOCK(handle))
97 /* for gstreamer state change */
98 #define _MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).gst_state_lock)
99 #define _MMSTREAMRECORDER_LOCK_GST_STATE(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle))
100 #define _MMSTREAMRECORDER_TRYLOCK_GST_STATE(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle))
101 #define _MMSTREAMRECORDER_UNLOCK_GST_STATE(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_GST_STATE_LOCK(handle))
103 /* for setting/calling callback */
104 #define _MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle) (_MMSTREAMRECORDER_CAST_MTSAFE(handle).message_cb_lock)
105 #define _MMSTREAMRECORDER_LOCK_MESSAGE_CALLBACK(handle) _MMSTREAMRECORDER_LOCK_FUNC(_MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle))
106 #define _MMSTREAMRECORDER_TRYLOCK_MESSAGE_CALLBACK(handle) _MMSTREAMRECORDER_TRYLOCK_FUNC(_MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle))
107 #define _MMSTREAMRECORDER_UNLOCK_MESSAGE_CALLBACK(handle) _MMSTREAMRECORDER_UNLOCK_FUNC(_MMSTREAMRECORDER_GET_MESSAGE_CALLBACK_LOCK(handle))
110 * Caster of main handle (streamrecorder)
112 #define MMF_STREAMRECORDER(h) (mmf_streamrecorder_t *)(h)
115 * Caster of subcontext
117 #define MMF_STREAMRECORDER_SUBCONTEXT(h) (((mmf_streamrecorder_t *)(h))->sub_context)
119 /* LOCAL CONSTANT DEFINITIONS */
121 /*=======================================================================================
123 ========================================================================================*/
126 * streamrecorder Pipeline's Element name.
127 * @note index of element.
130 _MMSTREAMRECORDER_ENCODE_NONE = (-1),
132 /* Main Pipeline Element */
133 _MMSTREAMRECORDER_ENCODE_MAIN_PIPE = 0x00,
135 /* Pipeline element of Audio input */
136 _MMSTREAMRECORDER_AUDIOSRC_BIN,
137 _MMSTREAMRECORDER_AUDIOSRC_SRC, /* appsrc */
138 _MMSTREAMRECORDER_AUDIOSRC_FILT,
139 _MMSTREAMRECORDER_AUDIOSRC_CONV,
140 _MMSTREAMRECORDER_AUDIOSRC_ENC,
141 _MMSTREAMRECORDER_AUDIOSRC_QUE,
143 /* Pipeline element of Encodebin */
144 _MMSTREAMRECORDER_ENCSINK_BIN,
145 _MMSTREAMRECORDER_ENCSINK_SRC, /* video appsrc */
146 _MMSTREAMRECORDER_ENCSINK_FILT,
147 _MMSTREAMRECORDER_ENCSINK_ENCBIN,
148 _MMSTREAMRECORDER_ENCSINK_AQUE,
149 _MMSTREAMRECORDER_ENCSINK_CONV,
150 _MMSTREAMRECORDER_ENCSINK_AENC,
151 _MMSTREAMRECORDER_ENCSINK_AENC_QUE,
152 _MMSTREAMRECORDER_ENCSINK_VQUE,
153 _MMSTREAMRECORDER_ENCSINK_VCONV,
154 _MMSTREAMRECORDER_ENCSINK_VENC,
155 _MMSTREAMRECORDER_ENCSINK_VENC_QUE,
156 _MMSTREAMRECORDER_ENCSINK_PARSER,
157 _MMSTREAMRECORDER_ENCSINK_ITOG,
158 _MMSTREAMRECORDER_ENCSINK_ICROP,
159 _MMSTREAMRECORDER_ENCSINK_ISCALE,
160 _MMSTREAMRECORDER_ENCSINK_IFILT,
161 _MMSTREAMRECORDER_ENCSINK_IQUE,
162 _MMSTREAMRECORDER_ENCSINK_IENC,
163 _MMSTREAMRECORDER_ENCSINK_MUX,
164 _MMSTREAMRECORDER_ENCSINK_SINK,
166 _MMSTREAMRECORDER_ENCODE_PIPELINE_ELEMENT_NUM,
167 } _MMSTREAMRECORDER_ENCODE_PIPELINE_ELELMENT;
170 /* Pipeline element of Audio input */
171 _MMSTREAMRECORDER_AUDIO_SRC = 0x01,
172 _MMSTREAMRECORDER_AUDIO_CONV,
173 _MMSTREAMRECORDER_AUDIO_ENC,
174 _MMSTREAMRECORDER_AUDIO_QUE,
176 /* Pipeline element of Video input */
177 _MMSTREAMRECORDER_VIDEO_SRC,
178 _MMSTREAMRECORDER_VIDEO_CAPS,
179 _MMSTREAMRECORDER_VIDEO_CONV,
180 _MMSTREAMRECORDER_VIDEO_ENC,
181 _MMSTREAMRECORDER_VIDEO_QUE,
183 /* Pipeline element of Common */
184 _MMSTREAMRECORDER_MUX,
185 _MMSTREAMRECORDER_SINK,
187 _MMSTREAMRECORDER_ELELMENT_NUM,
188 } _MMSTREAMRECORDER_ELELMENT;
191 * Command type for streamrecorder.
195 _MM_STREAMRECORDER_CMD_CREATE,
196 _MM_STREAMRECORDER_CMD_DESTROY,
197 _MM_STREAMRECORDER_CMD_REALIZE,
198 _MM_STREAMRECORDER_CMD_UNREALIZE,
199 _MM_STREAMRECORDER_CMD_START,
200 _MM_STREAMRECORDER_CMD_STOP,
201 _MM_STREAMRECORDER_CMD_RECORD,
202 _MM_STREAMRECORDER_CMD_PAUSE,
203 _MM_STREAMRECORDER_CMD_COMMIT,
204 _MM_STREAMRECORDER_CMD_CANCEL,
205 _MM_STREAMRECORDER_CMD_QUIT,
206 } _MMstreamrecorderCommandType;
209 * System state change cause
212 _MMSTREAMRECORDER_STATE_CHANGE_NORMAL = 0,
213 } _MMstreamrecorderStateChange;
216 /*=======================================================================================
217 | STRUCTURE DEFINITIONS |
218 ========================================================================================*/
221 * MMstreamrecorder information for Multi-Thread Safe
224 pthread_mutex_t lock; /**< Mutex (for general use) */
225 pthread_cond_t cond; /**< Condition (for general use) */
226 pthread_mutex_t cmd_lock; /**< Mutex (for command) */
227 pthread_mutex_t state_lock; /**< Mutex (for state change) */
228 pthread_mutex_t gst_state_lock; /**< Mutex (for state change) */
229 pthread_mutex_t message_cb_lock;/**< Mutex (for message callback) */
230 } _MMStreamRecorderMTSafe;
233 * MMstreamrecorder information for command loop
237 * MMstreamrecorder Sub Context
240 bool isMaxsizePausing; /**< Because of size limit, pipeline is paused. */
241 bool isMaxtimePausing; /**< Because of time limit, pipeline is paused. */
242 int encode_element_num; /**< count of encode element */
243 GstClockTime pipeline_time; /**< current time of Gstreamer Pipeline */
244 GstClockTime pause_time; /**< amount of time while pipeline is in PAUSE state.*/
245 gboolean error_occurs; /**< flag for error */
246 int error_code; /**< error code for internal gstreamer error */
247 gboolean ferror_send; /**< file write/seek error **/
248 guint ferror_count; /**< file write/seek error count **/
249 gboolean bget_eos; /**< Whether getting EOS */
250 gboolean video_enable; /**< whether video is disabled or not when record */
251 gboolean audio_enable; /**< whether audio is disabled or not when record */
253 /* INI information */
254 unsigned int fourcc; /**< Get fourcc value */
255 _MMStreamRecorderVideoInfo *info_video; /**< extra information for video recording */
256 _MMStreamRecorderAudioInfo *info_audio; /**< extra information for audio recording */
257 _MMStreamRecorderFileInfo *info_file; /**< extra information for audio recording */
258 _MMStreamRecorderGstElement *encode_element;
259 /**< array of encode element */
261 // type_element *VideosinkElement; /**< configure data of videosink element */
262 } _MMStreamRecorderSubContext;
265 * _MMstreamrecorderContext
267 typedef struct mmf_streamrecorder {
269 // int type; /**< mmstreamrecorder_mode_type */
270 int state; /**< state of streamrecorder */
271 // int target_state; /**< Target state that want to set. This is a flag that
272 // * stands for async state changing. If this value differ from state,
273 // * it means state is changing now asychronously. */
276 MMHandleType attributes; /**< Attribute handle */
277 _MMStreamRecorderSubContext *sub_context; /**< sub context */
278 GList *buffer_probes; /**< a list of buffer probe handle */
279 GList *event_probes; /**< a list of event probe handle */
280 GList *data_probes; /**< a list of data probe handle */
281 GList *signals; /**< a list of signal handle */
282 GList *msg_data; /**< a list of msg data */
283 guint pipeline_cb_event_id; /**< Event source ID of pipeline message callback */
284 guint encode_pipeline_cb_event_id; /**< Event source ID of encode pipeline message callback */
286 /* callback handlers */
287 MMMessageCallback msg_cb; /**< message callback */
288 void *msg_cb_param; /**< message callback parameter */
289 int (*command) (MMHandleType, int); /**< streamrecorder's command */
292 _MMStreamRecorderMTSafe mtsafe; /**< Thread safe */
293 mm_streamrecorder_ini_t ini;
295 int reserved[4]; /**< reserved */
296 } mmf_streamrecorder_t;
298 /*=======================================================================================
299 | EXTERN GLOBAL VARIABLE |
300 ========================================================================================*/
302 /*=======================================================================================
303 | GLOBAL FUNCTION PROTOTYPES |
304 ========================================================================================*/
306 * This function creates streamrecorder for capturing still image and recording.
308 * @param[out] handle Specifies the streamrecorder handle
309 * @param[in] info Preset information of streamrecorder
310 * @return This function returns zero on success, or negative value with error code.
311 * @remarks When this function calls successfully, streamrecorder handle will be filled with a @n
312 * valid value and the state of the streamrecorder will become MM_streamrecorder_STATE_NULL.@n
313 * Note that it's not ready to working streamrecorder. @n
314 * You should call mmstreamrecorder_realize before starting streamrecorder.
315 * @see _mmstreamrecorder_create
317 int _mmstreamrecorder_create(MMHandleType *handle);
320 * This function gets the current state of streamreccorder.
321 * mm_streamrecorderr is working on the base of its state. An user should check the state of mm_streamrecorder before calling its functions.
322 * If the handle is avaiable, user can retrieve the value.
324 * @param[in] streamrecorder A handle of streamrecorder.
325 * @param[out] state On return, it contains current state of streamrecorder.
326 * @return This function returns zero(MM_ERROR_NONE) on success, or negative value with error code.
327 * Please refer 'mm_error.h' to know the exact meaning of the error.
328 * @see MMStreamRecorderStateType
334 MMStreamRecorderStateType _mmstreamrecorder_get_state(MMHandleType handle);
337 * This function destroys instance of streamrecorder.
339 * @param[in] hstreamrecorder Specifies the streamrecorder handle
340 * @return This function returns zero on success, or negative value with error code.
341 * @see _mmstreamrecorder_create
343 int _mmstreamrecorder_destroy(MMHandleType handle);
346 * This function allocates memory for streamrecorder.
348 * @param[in] hstreamrecorder Specifies the streamrecorder handle
349 * @return This function returns zero on success, or negative value with error code.
350 * @remarks This function can be called successfully when current state is MM_streamrecorder_STATE_NULL @n
351 * and the state of the streamrecorder will become MM_streamrecorder_STATE_READY. @n
352 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
353 * @see _mmstreamrecorder_unrealize
354 * @pre MM_streamrecorder_STATE_NULL
355 * @post MM_streamrecorder_STATE_READY
357 int _mmstreamrecorder_realize(MMHandleType hstreamrecorder);
360 * This function free allocated memory for streamrecorder.
362 * @param[in] hstreamrecorder Specifies the streamrecorder handle
363 * @return This function returns zero on success, or negative value with error code.
364 * @remarks This function release all resources which are allocated for the streamrecorder engine.@n
365 * This function can be called successfully when current state is MM_streamrecorder_STATE_READY and @n
366 * the state of the streamrecorder will become MM_streamrecorder_STATE_NULL. @n
367 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
368 * @see _mmstreamrecorder_realize
369 * @pre MM_streamrecorder_STATE_READY
370 * @post MM_streamrecorder_STATE_NULL
372 int _mmstreamrecorder_unrealize(MMHandleType hstreamrecorder);
375 * This function is to start previewing.
377 * @param[in] hstreamrecorder Specifies the streamrecorder handle
378 * @return This function returns zero on success, or negative value with error code.
379 * @remarks This function can be called successfully when current state is MM_streamrecorder_STATE_READY and @n
380 * the state of the streamrecorder will become MM_streamrecorder_STATE_PREPARE. @n
381 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
382 * @see _mmstreamrecorder_stop
384 int _mmstreamrecorder_record(MMHandleType hstreamrecorder);
386 int _mmstreamrecorder_push_video_packet(MMHandleType handle, media_packet_h packet, unsigned long long timestamp, void *buffer);
388 int _mmstreamrecorder_push_stream_buffer(MMHandleType handle, MMStreamRecorderStreamType streamtype, unsigned long long timestamp, void *buffer, int size);
391 * This function is to pause video and audio recording
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 MM_streamrecorder_STATE_RECORDING and @n
396 * the state of the streamrecorder will become MM_streamrecorder_STATE_PAUSED.@n
397 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.@n
398 * @see _mmstreamrecorder_record
400 int _mmstreamrecorder_pause(MMHandleType hstreamrecorder);
403 * This function is to stop video and audio recording and save results.
405 * @param[in] hstreamrecorder Specifies the streamrecorder handle
406 * @return This function returns zero on success, or negative value with error code.
407 * @remarks This function can be called successfully when current state is @n
408 * MM_streamrecorder_STATE_PAUSED or MM_streamrecorder_STATE_RECORDING and @n
409 * the state of the streamrecorder will become MM_streamrecorder_STATE_PREPARE. @n
410 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION
411 * @see _mmstreamrecorder_cancel
413 int _mmstreamrecorder_commit(MMHandleType hstreamrecorder);
416 * This function is to stop video and audio recording and do not save results.
418 * @param[in] hstreamrecorder Specifies the streamrecorder handle
419 * @return This function returns zero on success, or negative value with error code.
420 * @remarks This function can be called successfully when current state is @n
421 * MM_streamrecorder_STATE_PAUSED or MM_streamrecorder_STATE_RECORDING and @n
422 * the state of the streamrecorder will become MM_streamrecorder_STATE_PREPARE. @n
423 * Otherwise, this function will return MM_ERROR_streamrecorder_INVALID_CONDITION.
424 * @see _mmstreamrecorder_commit
426 int _mmstreamrecorder_cancel(MMHandleType hstreamrecorder);
429 * This function is to set callback for receiving messages from streamrecorder.
431 * @param[in] hstreamrecorder Specifies the streamrecorder handle
432 * @param[in] callback Specifies the function pointer of callback function
433 * @param[in] user_data Specifies the user poiner for passing to callback function
435 * @return This function returns zero on success, or negative value with error code.
436 * @remarks typedef bool (*mm_message_callback) (int msg, mm_messageType *param, void *user_param);@n
451 * } mm_message_type; @n
453 * If a message value for mm_message_callback is MM_MESSAGE_STATE_CHANGED, @n
454 * state value in mm_message_type will be a mmstreamrecorder_state_type enum value;@n
456 * If a message value for mm_message_callback is MM_MESSAGE_ERROR, @n
457 * the code value in mm_message_type will be a mmplayer_error_type enum value;
459 * @see mm_message_type, mmstreamrecorder_state_type, mmstreamrecorder_error_type
461 int _mmstreamrecorder_set_message_callback(MMHandleType hstreamrecorder, MMMessageCallback callback, void *user_data);
464 * This function allocates structure of subsidiary attributes.
466 * @param[in] type Allocation type of streamrecorder context.
467 * @return This function returns structure pointer on success, NULL value on failure.
469 * @see _mmstreamrecorder_dealloc_subcontext()
472 int _mmstreamrecorder_alloc_subcontext(MMHandleType handle);
474 int _mmstreamrecorder_alloc_subcontext_videoinfo(MMHandleType handle);
476 int _mmstreamrecorder_alloc_subcontext_audioinfo(MMHandleType handle);
478 int _mmstreamrecorder_alloc_subcontext_fileinfo(MMHandleType handle);
481 * This function releases structure of subsidiary attributes.
483 * @param[in] sc Handle of streamrecorder subcontext.
486 * @see _mmstreamrecorder_alloc_subcontext()
489 void _mmstreamrecorder_dealloc_subcontext(MMHandleType handle);
492 * This function sets command function according to the type.
494 * @param[in] handle Handle of streamrecorder context.
495 * @param[in] type Allocation type of streamrecorder context.
496 * @return This function returns MM_ERROR_NONE on success, or other values with error code.
498 * @see __mmstreamrecorder_video_command(), __mmstreamrecorder_audio_command(), __mmstreamrecorder_image_command()
501 void _mmstreamrecorder_set_functions(MMHandleType handle);
503 void _mmstreamrecorder_unset_functions(MMHandleType handle);
508 #endif /* __MM_streamrecorder_INTERNAL_H__ */