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 /*=======================================================================================
24 ========================================================================================*/
30 #include "mm_streamrecorder_internal.h"
31 #include "mm_streamrecorder_recorder.h"
32 #include "mm_streamrecorder_attribute.h"
33 #include "mm_streamrecorder_gstdispatch.h"
35 #include <asm/types.h>
37 /*---------------------------------------------------------------------------------------
38 | GLOBAL VARIABLE DEFINITIONS for internal |
39 ---------------------------------------------------------------------------------------*/
41 /*---------------------------------------------------------------------------------------
42 | LOCAL VARIABLE DEFINITIONS for internal |
43 ---------------------------------------------------------------------------------------*/
44 #define __MMSTREAMRECORDER_CMD_ITERATE_MAX 3
46 /*---------------------------------------------------------------------------------------
47 | LOCAL FUNCTION PROTOTYPES: |
48 ---------------------------------------------------------------------------------------*/
49 void _mmstreamrecorder_set_state(MMHandleType handle, int state);
52 /*=======================================================================================
53 | FUNCTION DEFINITIONS |
54 =======================================================================================*/
56 /*---------------------------------------------------------------------------------------
57 | GLOBAL FUNCTION DEFINITIONS: |
58 ---------------------------------------------------------------------------------------*/
60 /* Internal command functions {*/
61 int _mmstreamrecorder_create(MMHandleType *handle)
63 int ret = MM_ERROR_NONE;
64 /* char *err_attr_name = NULL; */
65 mmf_streamrecorder_t *hstreamrecorder = NULL;
67 _mmstreamrec_dbg_log("Entered");
68 mmf_return_val_if_fail(handle, MM_ERROR_STREAMRECORDER_INVALID_ARGUMENT);
70 /* Create mmf_streamrecorder_t handle and initialize every variable */
71 hstreamrecorder = (mmf_streamrecorder_t *) malloc(sizeof(mmf_streamrecorder_t));
72 mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_LOW_MEMORY);
73 memset(hstreamrecorder, 0x00, sizeof(mmf_streamrecorder_t));
76 hstreamrecorder->sub_context = NULL;
78 pthread_mutex_init(&((hstreamrecorder->mtsafe).lock), NULL);
79 pthread_cond_init(&((hstreamrecorder->mtsafe).cond), NULL);
81 pthread_mutex_init(&((hstreamrecorder->mtsafe).cmd_lock), NULL);
82 pthread_mutex_init(&((hstreamrecorder->mtsafe).state_lock), NULL);
83 pthread_mutex_init(&((hstreamrecorder->mtsafe).gst_state_lock), NULL);
84 pthread_mutex_init(&((hstreamrecorder->mtsafe).message_cb_lock), NULL);
86 ret = _mm_streamrecorder_ini_load(&hstreamrecorder->ini);
87 if (ret != MM_ERROR_NONE) {
88 _mmstreamrec_dbg_warn("failed to load ini file\n");
89 goto _ERR_INITIAL_INI;
92 hstreamrecorder->attributes = _mmstreamrecorder_alloc_attribute((MMHandleType) hstreamrecorder);
94 if (!(hstreamrecorder->attributes)) {
95 _mmstreamrec_dbg_err("_mmstreamrecorder_create::alloc attribute error.");
97 ret = MM_ERROR_STREAMRECORDER_RESOURCE_CREATION;
98 goto _ERR_ALLOC_ATTRIBUTE;
101 _mmstreamrecorder_alloc_subcontext((MMHandleType) hstreamrecorder);
102 if (!hstreamrecorder->sub_context) {
103 ret = MM_ERROR_STREAMRECORDER_RESOURCE_CREATION;
104 goto _ERR_ALLOC_SUBCONTEXT;
107 /* Initial GSTREAMER */
108 ret = _mmstreamrecorder_gstreamer_init();
111 _mmstreamrec_dbg_err("Failed to initialize gstreamer!!");
112 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
113 goto _ERR_INITIAL_GSTREAMER;
116 _mmstreamrecorder_set_state((MMHandleType)hstreamrecorder, MM_STREAMRECORDER_STATE_CREATED);
118 *handle = (MMHandleType) hstreamrecorder;
120 return MM_ERROR_NONE;
122 _ERR_INITIAL_GSTREAMER:
123 _mmstreamrecorder_dealloc_subcontext((MMHandleType) hstreamrecorder);
124 _ERR_ALLOC_ATTRIBUTE:
125 _ERR_ALLOC_SUBCONTEXT:
127 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).lock));
128 pthread_cond_destroy(&((hstreamrecorder->mtsafe).cond));
129 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).cmd_lock));
130 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).state_lock));
131 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).gst_state_lock));
132 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).message_cb_lock));
134 memset(hstreamrecorder, 0x00, sizeof(mmf_streamrecorder_t));
135 free(hstreamrecorder);
140 MMStreamRecorderStateType _mmstreamrecorder_get_state(MMHandleType handle)
143 mmf_streamrecorder_t *streamrecorder = MMF_STREAMRECORDER(handle);
145 mmf_return_val_if_fail(streamrecorder, MM_ERROR_STREAMRECORDER_INVALID_ARGUMENT);
147 _MMSTREAMRECORDER_LOCK_STATE(handle);
149 state = streamrecorder->state;
151 _MMSTREAMRECORDER_UNLOCK_STATE(handle);
156 void _mmstreamrecorder_set_state(MMHandleType handle, int state)
159 mmf_streamrecorder_t *streamrecorder = MMF_STREAMRECORDER(handle);
160 _MMStreamRecorderMsgItem msg;
162 mmf_return_if_fail(streamrecorder);
164 /*_mmstreamrec_dbg_log("");*/
166 _MMSTREAMRECORDER_LOCK_STATE(handle);
168 old_state = streamrecorder->state;
170 if (old_state != state) {
171 streamrecorder->state = state;
173 _mmstreamrec_dbg_log("set state[%d] and send state-changed message", state);
175 msg.id = MM_MESSAGE_STREAMRECORDER_STATE_CHANGED;
176 msg.param.state.code = MM_ERROR_NONE;
178 msg.param.state.previous = old_state;
179 msg.param.state.current = state;
181 /*_mmstreamrec_dbg_log("_mmstreamcorder_send_message : msg : %p, id:%x", &msg, msg.id);*/
182 _mmstreamrecorder_send_message(handle, &msg);
185 _MMSTREAMRECORDER_UNLOCK_STATE(handle);
192 int _mmstreamrecorder_destroy(MMHandleType handle)
194 int ret = MM_ERROR_NONE;
196 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
198 _mmstreamrec_dbg_log("");
200 if (!hstreamrecorder) {
201 _mmstreamrec_dbg_err("Not initialized");
202 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
203 goto _ERR_STREAMRECORDER_CMD_PRECON;
206 if (!_MMSTREAMRECORDER_TRYLOCK_CMD(hstreamrecorder)) {
207 _mmstreamrec_dbg_err("Another command is running.");
208 ret = MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
209 goto _ERR_STREAMRECORDER_CMD_PRECON;
212 /* Release SubContext and pipeline */
213 if (hstreamrecorder->sub_context) {
214 if (hstreamrecorder->sub_context->encode_element)
215 _mmstreamrecorder_destroy_pipeline(handle);
217 _mmstreamrecorder_dealloc_subcontext(handle);
220 /* Remove attributes */
221 if (hstreamrecorder->attributes) {
222 _mmstreamrecorder_dealloc_attribute(hstreamrecorder->attributes);
223 hstreamrecorder->attributes = 0;
226 /* Remove messages which are not called yet */
227 _mmstreamrecorder_remove_message_all(handle);
229 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
231 /* Release lock, cond */
232 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).lock));
233 pthread_cond_destroy(&((hstreamrecorder->mtsafe).cond));
234 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).cmd_lock));
235 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).state_lock));
236 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).gst_state_lock));
237 pthread_mutex_destroy(&((hstreamrecorder->mtsafe).message_cb_lock));
240 memset(hstreamrecorder, 0x00, sizeof(mmf_streamrecorder_t));
241 free(hstreamrecorder);
243 return MM_ERROR_NONE;
245 _ERR_STREAMRECORDER_CMD_PRECON:
247 _mmstreamrec_dbg_err("Destroy fail (ret %x)", ret);
252 int _mmstreamrecorder_realize(MMHandleType handle)
254 int ret = MM_ERROR_NONE;
255 /* int errorcode = MM_ERROR_NONE;
256 char *videosink_element_type = NULL;
257 char *videosink_name = NULL; */
259 _mmstreamrec_dbg_log("");
261 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
263 if (!hstreamrecorder) {
264 _mmstreamrec_dbg_err("Not initialized");
265 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
269 if (!_MMSTREAMRECORDER_TRYLOCK_CMD(hstreamrecorder)) {
270 _mmstreamrec_dbg_err("Another command is running.");
271 ret = MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
272 goto _ERR_STREAMRECORDER_CMD_PRECON;
275 /* create pipeline */
276 ret = _mmstreamrecorder_create_pipeline(handle);
277 if (ret != MM_ERROR_NONE) {
278 /* check internal error of gstreamer */
279 if (hstreamrecorder->sub_context->error_code != MM_ERROR_NONE) {
280 ret = hstreamrecorder->sub_context->error_code;
281 _mmstreamrec_dbg_log("gstreamer error is occurred. return it %x", ret);
283 /* release sub context */
284 _mmstreamrecorder_dealloc_subcontext(handle);
285 goto _ERR_STREAMRECORDER_CMD;
288 /* set command function */
289 _mmstreamrecorder_set_functions(handle);
291 _mmstreamrecorder_set_state(handle, MM_STREAMRECORDER_STATE_PREPARED);
293 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
295 return MM_ERROR_NONE;
297 _ERR_STREAMRECORDER_CMD:
298 /* _ERR_STREAMRECORDER_CMD_PRECON_AFTER_LOCK: */
299 /* Security thread release */
300 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
302 _ERR_STREAMRECORDER_CMD_PRECON:
303 /* _mmstreamrec_dbg_err("Realize fail (type %d, state %d, ret %x)", hstreamrecorder->type, state, ret); */
308 int _mmstreamrecorder_unrealize(MMHandleType handle)
310 int ret = MM_ERROR_NONE;
312 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
314 _mmstreamrec_dbg_log("");
316 if (!hstreamrecorder) {
317 _mmstreamrec_dbg_err("Not initialized");
318 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
322 if (!_MMSTREAMRECORDER_TRYLOCK_CMD(hstreamrecorder)) {
323 _mmstreamrec_dbg_err("Another command is running.");
324 ret = MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
325 goto _ERR_STREAMRECORDER_CMD_PRECON;
328 /* Release SubContext */
329 if (hstreamrecorder->sub_context) {
330 /* destroy pipeline */
331 _mmstreamrecorder_destroy_pipeline(handle);
332 /* Deallocate SubContext */
335 /* Deinitialize main context member */
336 _mmstreamrecorder_unset_functions(handle);
338 _mmstreamrecorder_set_state(handle, MM_STREAMRECORDER_STATE_CREATED);
340 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
342 return MM_ERROR_NONE;
344 _ERR_STREAMRECORDER_CMD_PRECON:
349 int _mmstreamrecorder_record(MMHandleType handle)
351 int ret = MM_ERROR_NONE;
353 _MMStreamRecorderSubContext *sc = NULL;
354 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
356 _mmstreamrec_dbg_log("");
358 if (!hstreamrecorder) {
359 _mmstreamrec_dbg_err("Not initialized");
360 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
364 sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
365 mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
367 /* initialize error code */
368 hstreamrecorder->sub_context->error_code = MM_ERROR_NONE;
370 ret = hstreamrecorder->command((MMHandleType) hstreamrecorder, _MM_STREAMRECORDER_CMD_RECORD);
371 if (ret != MM_ERROR_NONE) {
372 _mmstreamrec_dbg_err("_mmstreamrecorder_record does not work!");
373 goto _ERR_STREAMRECORDER_CMD;
376 _mmstreamrecorder_set_state(handle, MM_STREAMRECORDER_STATE_RECORDING);
378 return MM_ERROR_NONE;
380 _ERR_STREAMRECORDER_CMD:
381 /* check internal error of gstreamer */
382 if (hstreamrecorder->sub_context->error_code != MM_ERROR_NONE) {
383 ret = hstreamrecorder->sub_context->error_code;
384 hstreamrecorder->sub_context->error_code = MM_ERROR_NONE;
386 _mmstreamrec_dbg_log("gstreamer error is occurred. return it %x", ret);
391 int _mmstreamrecorder_push_stream_buffer(MMHandleType handle, MMStreamRecorderStreamType streamtype, unsigned long timestamp, void *buffer, int size)
393 int ret = MM_ERROR_NONE;
397 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
399 /* _mmstreamrec_dbg_log(""); */
401 if (!hstreamrecorder) {
402 _mmstreamrec_dbg_err("Not initialized");
403 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
406 mm_streamrecorder_get_attributes(handle, NULL,
407 MMSTR_VIDEO_SOURCE_FORMAT, &format,
409 GstStreamRecorderBuffer *stream_buffer = NULL;
410 stream_buffer = (GstStreamRecorderBuffer *) malloc(sizeof(GstStreamRecorderBuffer));
411 if (stream_buffer == NULL) {
412 _mmstreamrec_dbg_err("stream buffer allocation fail");
413 return MM_ERROR_STREAMRECORDER_LOW_MEMORY;
415 stream_buffer->str_handle = handle;
416 stream_buffer->buffer = gst_buffer_new();
417 if (stream_buffer->buffer == NULL) {
419 stream_buffer = NULL;
420 _mmstreamrec_dbg_err("gst buffer allocation fail");
421 return MM_ERROR_STREAMRECORDER_LOW_MEMORY;
423 stream_buffer->user_buffer = buffer;
424 /* Get Media Packet to Surface to MMVideoBuffer */
426 stream_buffer->buffer->pts = timestamp;
427 GST_BUFFER_DURATION(stream_buffer->buffer) = GST_CLOCK_TIME_NONE;
429 gst_buffer_map(stream_buffer->buffer, &map, GST_MAP_READWRITE);
430 if (streamtype == MM_STREAM_TYPE_VIDEO) {
431 if (format == MM_STREAMRECORDER_INPUT_FORMAT_NV12 || format == MM_STREAMRECORDER_INPUT_FORMAT_NV21) {
433 MMVideoBuffer *video_buf = (MMVideoBuffer *)buffer;
434 /* Buffer at 0th position */
435 gst_buffer_append_memory(stream_buffer->buffer, gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY,
436 video_buf->handle.paddr[0], size, 0, size, video_buf->handle.paddr[0], NULL));
437 /* Buffer at 1st position */
438 gst_buffer_append_memory(stream_buffer->buffer, gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY,
439 video_buf, sizeof(MMVideoBuffer), 0, sizeof(MMVideoBuffer), stream_buffer, _mmstreamrecorder_buffer_destroy));
441 gst_buffer_append_memory(stream_buffer->buffer, gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY,
442 buffer, size, 0, size, stream_buffer, _mmstreamrecorder_buffer_destroy));
444 ret = _mmstreamrecorder_push_videostream_buffer(handle, timestamp, stream_buffer->buffer, size);
445 } else if (streamtype == MM_STREAM_TYPE_AUDIO) {
446 gst_buffer_append_memory(stream_buffer->buffer, gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY,
447 buffer, size, 0, size, stream_buffer, _mmstreamrecorder_buffer_destroy));
448 ret = _mmstreamrecorder_push_audiostream_buffer(handle, timestamp, stream_buffer->buffer, size);
450 gst_buffer_unmap(stream_buffer->buffer, &map);
451 gst_object_unref(stream_buffer->buffer);
453 stream_buffer = NULL;
454 return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
456 gst_buffer_unmap(stream_buffer->buffer, &map);
461 int _mmstreamrecorder_pause(MMHandleType handle)
463 int ret = MM_ERROR_NONE;
464 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
466 _mmstreamrec_dbg_log("");
468 if (!hstreamrecorder) {
469 _mmstreamrec_dbg_err("Not initialized");
470 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
474 if (!_MMSTREAMRECORDER_TRYLOCK_CMD(hstreamrecorder)) {
475 _mmstreamrec_dbg_err("Another command is running.");
476 ret = MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
477 goto _ERR_STREAMRECORDER_CMD_PRECON;
480 ret = hstreamrecorder->command((MMHandleType) hstreamrecorder, _MM_STREAMRECORDER_CMD_PAUSE);
481 if (ret != MM_ERROR_NONE)
482 goto _ERR_STREAMRECORDER_CMD;
484 _mmstreamrecorder_set_state(handle, MM_STREAMRECORDER_STATE_PAUSED);
486 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
488 return MM_ERROR_NONE;
490 _ERR_STREAMRECORDER_CMD:
491 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
492 _ERR_STREAMRECORDER_CMD_PRECON:
497 int _mmstreamrecorder_commit(MMHandleType handle)
499 int ret = MM_ERROR_NONE;
501 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
503 _mmstreamrec_dbg_log("");
505 if (!hstreamrecorder) {
506 _mmstreamrec_dbg_err("Not initialized");
507 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
511 if (!_MMSTREAMRECORDER_TRYLOCK_CMD(hstreamrecorder)) {
512 _mmstreamrec_dbg_err("Another command is running.");
513 ret = MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
514 goto _ERR_STREAMRECORDER_CMD_PRECON;
517 ret = hstreamrecorder->command((MMHandleType) hstreamrecorder, _MM_STREAMRECORDER_CMD_COMMIT);
518 if (ret != MM_ERROR_NONE)
519 goto _ERR_STREAMRECORDER_CMD;
521 _mmstreamrecorder_set_state(handle, MM_STREAMRECORDER_STATE_PREPARED);
523 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
525 return MM_ERROR_NONE;
527 _ERR_STREAMRECORDER_CMD:
528 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
529 _ERR_STREAMRECORDER_CMD_PRECON:
535 int _mmstreamrecorder_cancel(MMHandleType handle)
537 int ret = MM_ERROR_NONE;
539 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
541 _mmstreamrec_dbg_log("");
543 if (!hstreamrecorder) {
544 _mmstreamrec_dbg_err("Not initialized");
545 ret = MM_ERROR_STREAMRECORDER_NOT_INITIALIZED;
549 if (!_MMSTREAMRECORDER_TRYLOCK_CMD(hstreamrecorder)) {
550 _mmstreamrec_dbg_err("Another command is running.");
551 ret = MM_ERROR_STREAMRECORDER_CMD_IS_RUNNING;
552 goto _ERR_STREAMRECORDER_CMD_PRECON;
555 ret = hstreamrecorder->command((MMHandleType) hstreamrecorder, _MM_STREAMRECORDER_CMD_CANCEL);
556 if (ret != MM_ERROR_NONE)
557 goto _ERR_STREAMRECORDER_CMD;
559 _mmstreamrecorder_set_state(handle, MM_STREAMRECORDER_STATE_PREPARED);
561 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
563 return MM_ERROR_NONE;
565 _ERR_STREAMRECORDER_CMD:
566 _MMSTREAMRECORDER_UNLOCK_CMD(hstreamrecorder);
567 _ERR_STREAMRECORDER_CMD_PRECON:
573 /* } Internal command functions */
575 int _mmstreamrecorder_set_message_callback(MMHandleType handle, MMMessageCallback callback, void *user_data)
577 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
579 _mmstreamrec_dbg_log("%p", hstreamrecorder);
581 mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
583 if (callback == NULL) {
584 _mmstreamrec_dbg_warn("Message Callback is disabled");
585 _mmstreamrec_dbg_warn("Application sets callback as NULL");
588 if (!_MMSTREAMRECORDER_TRYLOCK_MESSAGE_CALLBACK(hstreamrecorder)) {
589 _mmstreamrec_dbg_warn("Application's message callback is running now");
590 return MM_ERROR_STREAMRECORDER_INVALID_CONDITION;
593 /* set message callback to message handle */
594 hstreamrecorder->msg_cb = callback;
595 hstreamrecorder->msg_cb_param = user_data;
597 _MMSTREAMRECORDER_UNLOCK_MESSAGE_CALLBACK(hstreamrecorder);
599 return MM_ERROR_NONE;
602 int _mmstreamrecorder_alloc_subcontext(MMHandleType handle)
605 int ret = MM_ERROR_NONE;
606 _MMStreamRecorderSubContext *sc = NULL;
608 _mmstreamrec_dbg_log("");
610 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
612 mmf_return_val_if_fail(hstreamrecorder, MM_ERROR_STREAMRECORDER_RESOURCE_CREATION);
614 /* alloc container */
615 sc = (_MMStreamRecorderSubContext *) malloc(sizeof(_MMStreamRecorderSubContext));
616 mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_LOW_MEMORY);
619 memset(sc, 0x00, sizeof(_MMStreamRecorderSubContext));
621 sc->encode_element_num = _MMSTREAMRECORDER_ENCODE_PIPELINE_ELEMENT_NUM;
623 /* alloc element array */
624 sc->encode_element = (_MMStreamRecorderGstElement *) malloc(sizeof(_MMStreamRecorderGstElement) * sc->encode_element_num);
625 if (!sc->encode_element) {
626 _mmstreamrec_dbg_err("Failed to alloc encode element structure");
627 goto ALLOC_SUBCONTEXT_FAILED;
630 for (i = 0; i < sc->encode_element_num; i++) {
631 sc->encode_element[i].id = _MMSTREAMRECORDER_ENCODE_NONE;
632 sc->encode_element[i].gst = NULL;
635 sc->fourcc = 0x80000000;
637 hstreamrecorder->sub_context = sc;
639 ret = _mmstreamrecorder_alloc_subcontext_fileinfo((MMHandleType) hstreamrecorder);
640 if (ret != MM_ERROR_NONE) {
641 _mmstreamrec_dbg_err("Failed to allocate subcontext fileinfo");
642 goto ALLOC_SUBCONTEXT_FAILED;
645 ret = _mmstreamrecorder_alloc_subcontext_videoinfo((MMHandleType) hstreamrecorder);
646 if (ret != MM_ERROR_NONE) {
647 _mmstreamrec_dbg_err("Failed to allocate subcontext videoinfo");
648 goto ALLOC_SUBCONTEXT_FAILED;
651 _mmstreamrecorder_alloc_subcontext_audioinfo((MMHandleType) hstreamrecorder);
652 if (ret != MM_ERROR_NONE) {
653 _mmstreamrec_dbg_err("Failed to allocate subcontext audioinfo");
654 goto ALLOC_SUBCONTEXT_FAILED;
657 return MM_ERROR_NONE;
659 ALLOC_SUBCONTEXT_FAILED:
662 if (sc->encode_element) {
663 free(sc->encode_element);
664 sc->encode_element = NULL;
669 if (hstreamrecorder->sub_context != NULL)
670 hstreamrecorder->sub_context = NULL;
671 return MM_ERROR_STREAMRECORDER_LOW_MEMORY;
674 int _mmstreamrecorder_alloc_subcontext_videoinfo(MMHandleType handle)
676 _MMStreamRecorderSubContext *sc = NULL;
678 _mmstreamrec_dbg_log("");
680 sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
681 mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
683 /* alloc info for each mode */
685 sc->info_video = malloc(sizeof(_MMStreamRecorderVideoInfo));
686 if (!sc->info_video) {
687 _mmstreamrec_dbg_err("Failed to alloc info structure");
688 return MM_ERROR_STREAMRECORDER_LOW_MEMORY;
690 memset(sc->info_video, 0x00, sizeof(_MMStreamRecorderVideoInfo));
692 return MM_ERROR_NONE;
695 int _mmstreamrecorder_alloc_subcontext_audioinfo(MMHandleType handle)
697 _MMStreamRecorderSubContext *sc = NULL;
699 _mmstreamrec_dbg_log("");
701 sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
702 mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
704 /* alloc info for each mode */
705 sc->info_audio = malloc(sizeof(_MMStreamRecorderAudioInfo));
706 if (!sc->info_audio) {
707 _mmstreamrec_dbg_err("Failed to alloc info structure");
708 return MM_ERROR_STREAMRECORDER_LOW_MEMORY;
710 memset(sc->info_audio, 0x00, sizeof(_MMStreamRecorderAudioInfo));
712 return MM_ERROR_NONE;
715 int _mmstreamrecorder_alloc_subcontext_fileinfo(MMHandleType handle)
717 _MMStreamRecorderSubContext *sc = NULL;
719 _mmstreamrec_dbg_log("");
721 sc = MMF_STREAMRECORDER_SUBCONTEXT(handle);
722 mmf_return_val_if_fail(sc, MM_ERROR_STREAMRECORDER_NOT_INITIALIZED);
724 /* alloc info for each mode */
725 sc->info_file = malloc(sizeof(_MMStreamRecorderFileInfo));
726 if (!sc->info_file) {
727 _mmstreamrec_dbg_err("Failed to alloc info structure");
728 return MM_ERROR_STREAMRECORDER_LOW_MEMORY;
730 memset(sc->info_file, 0x00, sizeof(_MMStreamRecorderFileInfo));
732 return MM_ERROR_NONE;
735 void _mmstreamrecorder_dealloc_subcontext(MMHandleType handle)
737 _MMStreamRecorderSubContext *sc = NULL;
739 _mmstreamrec_dbg_log("");
741 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
743 mmf_return_if_fail(hstreamrecorder);
744 mmf_return_if_fail(hstreamrecorder->sub_context);
746 sc = hstreamrecorder->sub_context;
750 if (sc->encode_element) {
751 _mmstreamrec_dbg_log("release encode_element");
752 free(sc->encode_element);
753 sc->encode_element = NULL;
756 if (sc->info_video) {
757 _mmstreamrec_dbg_log("release info_video");
758 free(sc->info_video);
759 sc->info_video = NULL;
762 if (sc->info_audio) {
763 _mmstreamrec_dbg_log("release info_audio");
764 free(sc->info_audio);
765 sc->info_audio = NULL;
769 _mmstreamrec_dbg_log("release info_file");
771 sc->info_file = NULL;
777 if (hstreamrecorder->sub_context != NULL)
778 hstreamrecorder->sub_context = NULL;
783 void _mmstreamrecorder_set_functions(MMHandleType handle)
785 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
787 _mmstreamrec_dbg_log("");
789 /* Now only video type */
791 hstreamrecorder->command = _mmstreamrecorder_video_command;
796 void _mmstreamrecorder_unset_functions(MMHandleType handle)
798 mmf_streamrecorder_t *hstreamrecorder = MMF_STREAMRECORDER(handle);
800 _mmstreamrec_dbg_log("");
802 hstreamrecorder->command = NULL;