4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jeongmo Yang <jm80.yang@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 =======================================================================================*/
29 #include <sys/vfs.h> /* struct statfs */
30 #include <sys/time.h> /* gettimeofday */
32 #include <gst/video/video-info.h>
35 #include "mm_camcorder_internal.h"
36 #include "mm_camcorder_util.h"
37 #include "mm_camcorder_sound.h"
38 #include <mm_util_image.h>
39 #include <mm_util_imgp.h>
40 #include <mm_util_jpeg.h>
42 /*-----------------------------------------------------------------------
43 | GLOBAL VARIABLE DEFINITIONS for internal |
44 -----------------------------------------------------------------------*/
45 static int mmcam_log_level = MM_CAMCORDER_LOG_LEVEL_INFO;
47 /*-----------------------------------------------------------------------
48 | LOCAL VARIABLE DEFINITIONS for internal |
49 -----------------------------------------------------------------------*/
50 #define TIME_STRING_MAX_LEN 64
51 #define __MMCAMCORDER_CAPTURE_WAIT_TIMEOUT 5
52 #define __MMCAMCORDER_MAX_WIDTH 8192
53 #define __MMCAMCORDER_MAX_HEIGHT 8192
55 #define FPUTC_CHECK(x_char, x_file) \
57 if (fputc(x_char, x_file) == EOF) { \
58 MMCAM_LOG_ERROR("[Critical] fputc() returns fail.\n"); \
62 #define FPUTS_CHECK(x_str, x_file) \
64 if (fputs(x_str, x_file) == EOF) { \
65 MMCAM_LOG_ERROR("[Critical] fputs() returns fail.\n"); \
71 /*---------------------------------------------------------------------------
72 | LOCAL FUNCTION PROTOTYPES: |
73 ---------------------------------------------------------------------------*/
74 /* STATIC INTERNAL FUNCTION */
76 //static gint skip_mdat(FILE *f);
77 static guint16 get_language_code(const char *str);
78 static gchar* str_to_utf8(const gchar *str);
79 static inline gboolean write_tag(FILE *f, const gchar *tag);
80 static inline gboolean write_to_32(FILE *f, guint val);
81 static inline gboolean write_to_16(FILE *f, guint val);
82 static inline gboolean write_to_24(FILE *f, guint val);
83 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
84 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
85 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
88 /*===========================================================================================
90 | FUNCTION DEFINITIONS |
92 ========================================================================================== */
93 /*---------------------------------------------------------------------------
94 | GLOBAL FUNCTION DEFINITIONS: |
95 ---------------------------------------------------------------------------*/
97 static int __gdbus_method_call_sync(GDBusConnection *conn, const char *bus_name,
98 const char *object, const char *iface, const char *method,
99 GVariant *args, GVariant **result, bool is_sync)
101 int ret = MM_ERROR_NONE;
102 GVariant *dbus_reply = NULL;
104 if (!conn || !object || !iface || !method) {
105 MMCAM_LOG_ERROR("Invalid Argument %p %p %p %p",
106 conn, object, iface, method);
107 return MM_ERROR_INVALID_ARGUMENT;
110 MMCAM_LOG_WARNING("Dbus call - obj [%s], iface [%s], method [%s]", object, iface, method);
113 dbus_reply = g_dbus_connection_call_sync(conn,
114 bus_name, object, iface, method, args, NULL,
115 G_DBUS_CALL_FLAGS_NONE, G_DBUS_TIMEOUT, NULL, NULL);
117 MMCAM_LOG_WARNING("Method Call '%s.%s' Success", iface, method);
118 *result = dbus_reply;
120 MMCAM_LOG_ERROR("dbus method call sync reply failed");
121 ret = MM_ERROR_CAMCORDER_INTERNAL;
124 g_dbus_connection_call(conn, bus_name, object, iface, method, args, NULL,
125 G_DBUS_CALL_FLAGS_NONE, G_DBUS_TIMEOUT, NULL, NULL, NULL);
128 MMCAM_LOG_WARNING("done");
133 static int __gdbus_subscribe_signal(GDBusConnection *conn,
134 const char *object_name, const char *iface_name, const char *signal_name,
135 GDBusSignalCallback signal_cb, guint *subscribe_id, void *userdata)
139 if (!conn || !object_name || !iface_name || !signal_name || !signal_cb || !subscribe_id) {
140 MMCAM_LOG_ERROR("Invalid Argument %p %p %p %p %p %p",
141 conn, object_name, iface_name, signal_name, signal_cb, subscribe_id);
142 return MM_ERROR_INVALID_ARGUMENT;
145 MMCAM_LOG_INFO("subscirbe signal Obj %s, iface_name %s, sig_name %s",
146 object_name, iface_name, signal_name);
148 subs_id = g_dbus_connection_signal_subscribe(conn,
149 NULL, iface_name, signal_name, object_name, NULL,
150 G_DBUS_SIGNAL_FLAGS_NONE, signal_cb, userdata, NULL);
152 MMCAM_LOG_ERROR("g_dbus_connection_signal_subscribe() failed");
153 return MM_ERROR_CAMCORDER_INTERNAL;
155 *subscribe_id = subs_id;
156 MMCAM_LOG_INFO("subs_id %u", subs_id);
159 return MM_ERROR_NONE;
163 static void __gdbus_stream_eos_cb(GDBusConnection *connection,
164 const gchar *sender_name, const gchar *object_path, const gchar *interface_name,
165 const gchar *signal_name, GVariant *param, gpointer user_data)
168 _MMCamcorderGDbusCbInfo *gdbus_info = NULL;
169 mmf_camcorder_t *hcamcorder = NULL;
171 MMCAM_LOG_WARNING("entered");
173 if (!param || !user_data) {
174 MMCAM_LOG_ERROR("invalid parameter %p %p", param, user_data);
178 gdbus_info = (_MMCamcorderGDbusCbInfo *)user_data;
179 hcamcorder = (mmf_camcorder_t *)gdbus_info->mm_handle;
181 g_variant_get(param, "(i)", &played_idx);
183 g_mutex_lock(&gdbus_info->sync_mutex);
185 MMCAM_LOG_WARNING("gdbus_info->param %d, played_idx : %d, handle : %p",
186 gdbus_info->param, played_idx, hcamcorder);
188 if (gdbus_info->param == played_idx) {
189 g_dbus_connection_signal_unsubscribe(connection, gdbus_info->subscribe_id);
191 gdbus_info->is_playing = FALSE;
192 gdbus_info->subscribe_id = 0;
193 gdbus_info->param = 0;
195 g_cond_signal(&gdbus_info->sync_cond);
198 g_mutex_unlock(&gdbus_info->sync_mutex);
200 MMCAM_LOG_WARNING("done");
205 static int __gdbus_wait_for_cb_return(_MMCamcorderGDbusCbInfo *gdbus_info, int time_out)
207 int ret = MM_ERROR_NONE;
211 MMCAM_LOG_ERROR("invalid info");
212 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
215 g_mutex_lock(&gdbus_info->sync_mutex);
217 MMCAM_LOG_WARNING("entered");
219 if (gdbus_info->is_playing == FALSE) {
220 MMCAM_LOG_INFO("callback is already returned");
221 g_mutex_unlock(&gdbus_info->sync_mutex);
222 return MM_ERROR_NONE;
225 end_time = g_get_monotonic_time() + (time_out * G_TIME_SPAN_MILLISECOND);
227 if (g_cond_wait_until(&gdbus_info->sync_cond, &gdbus_info->sync_mutex, end_time)) {
228 MMCAM_LOG_WARNING("wait signal received");
230 MMCAM_LOG_ERROR("wait time is expired");
231 ret = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
234 g_mutex_unlock(&gdbus_info->sync_mutex);
236 MMCAM_LOG_WARNING("done");
242 gint32 _mmcamcorder_double_to_fix(gdouble d_number)
244 return (gint32) (d_number * 65536.0);
247 // find top level tag only, do not use this function for finding sub level tags
248 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc, gboolean do_rewind)
250 size_t read_item = 0;
256 while ((read_item = fread(&buf, sizeof(guchar), 8, f)) > 0) {
257 uint64_t buf_size = 0;
258 uint32_t buf_fourcc = 0;
261 MMCAM_LOG_ERROR("fread failed : %zu", read_item);
265 buf_fourcc = MMCAM_FOURCC(buf[4], buf[5], buf[6], buf[7]);
267 if (tag_fourcc == buf_fourcc) {
268 MMCAM_LOG_INFO("find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
271 MMCAM_LOG_INFO("skip [%c%c%c%c] tag", MMCAM_FOURCC_ARGS(buf_fourcc));
273 buf_size = _mmcamcorder_get_container_size(buf);
275 /* if size of mdat is 1, it means largesize is used.(bigger than 4GB) */
276 if (buf_fourcc == MMCAM_FOURCC('m', 'd', 'a', 't') &&
278 read_item = fread(&buf, sizeof(guchar), 8, f);
280 MMCAM_LOG_ERROR("fread failed");
284 buf_size = _mmcamcorder_get_container_size64(buf);
285 buf_size = buf_size - 16; /* include tag and large file flag(size 1) */
287 buf_size = buf_size - 8; /* include tag */
290 MMCAM_LOG_INFO("seek %"PRIu64, buf_size);
291 if (fseeko(f, (off_t)buf_size, SEEK_CUR) != 0) {
292 MMCAM_LOG_ERROR("fseeko() fail");
298 MMCAM_LOG_INFO("cannot find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
303 gboolean _mmcamcorder_find_fourcc(FILE *f, guint32 tag_fourcc, gboolean do_rewind)
305 size_t read_item = 0;
311 while ((read_item = fread(&buf, sizeof(guchar), 8, f)) > 0) {
312 uint64_t buf_size = 0;
313 uint32_t buf_fourcc = 0;
316 MMCAM_LOG_ERROR("fread failed : %zu", read_item);
320 buf_fourcc = MMCAM_FOURCC(buf[4], buf[5], buf[6], buf[7]);
322 if (tag_fourcc == buf_fourcc) {
323 MMCAM_LOG_INFO("find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
325 } else if (buf_fourcc == MMCAM_FOURCC('m', 'o', 'o', 'v') &&
326 tag_fourcc != buf_fourcc) {
327 if (_mmcamcorder_find_fourcc(f, tag_fourcc, FALSE))
332 MMCAM_LOG_INFO("skip [%c%c%c%c] tag", MMCAM_FOURCC_ARGS(buf_fourcc));
334 buf_size = _mmcamcorder_get_container_size(buf);
336 /* if size of mdat is 1, it means largesize is used.(bigger than 4GB) */
337 if (buf_fourcc == MMCAM_FOURCC('m', 'd', 'a', 't') &&
339 read_item = fread(&buf, sizeof(guchar), 8, f);
341 MMCAM_LOG_ERROR("fread failed");
345 buf_size = _mmcamcorder_get_container_size64(buf);
346 buf_size = buf_size - 16; /* include tag and large file flag(size 1) */
348 buf_size = buf_size - 8; /* include tag */
351 MMCAM_LOG_INFO("seek %"PRIu64, buf_size);
352 if (fseeko(f, (off_t)buf_size, SEEK_CUR) != 0) {
353 MMCAM_LOG_ERROR("fseeko() fail");
359 MMCAM_LOG_INFO("cannot find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
364 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos)
366 MMCAM_LOG_INFO("size : %"G_GINT64_FORMAT"", curr_pos-prev_pos);
367 if (fseeko(f, prev_pos, SEEK_SET) != 0) {
368 MMCAM_LOG_ERROR("fseeko() fail");
372 if (!write_to_32(f, curr_pos -prev_pos))
375 if (fseeko(f, curr_pos, SEEK_SET) != 0) {
376 MMCAM_LOG_ERROR("fseeko() fail");
383 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info)
385 gint64 current_pos, pos;
390 if ((pos = ftello(f)) < 0) {
391 MMCAM_LOG_ERROR("ftello() returns negative value");
395 if (!write_to_32(f, 0)) //size
398 if (!write_tag(f, "loci")) // type
401 FPUTC_CHECK(0, f); // version
403 if (!write_to_24(f, 0)) // flags
406 if (!write_to_16(f, get_language_code("eng"))) // language
409 str = str_to_utf8("location_name");
411 FPUTS_CHECK(str, f); // name
414 FPUTC_CHECK('\0', f);
415 FPUTC_CHECK(0, f); //role
417 if (!write_to_32(f, info.longitude)) // Longitude
420 if (!write_to_32(f, info.latitude)) // Latitude
423 if (!write_to_32(f, info.altitude)) // Altitude
426 str = str_to_utf8("Astronomical_body");
427 FPUTS_CHECK(str, f);//Astronomical_body
430 FPUTC_CHECK('\0', f);
432 str = str_to_utf8("Additional_notes");
433 FPUTS_CHECK(str, f); // Additional_notes
436 FPUTC_CHECK('\0', f);
438 if ((current_pos = ftello(f)) < 0) {
439 MMCAM_LOG_ERROR("ftello() returns negative value");
443 if (!_mmcamcorder_update_size(f, pos, current_pos))
449 void _mmcamcorder_write_Latitude(FILE *f, int value)
453 int l_below_decimal = 0;
455 l_decimal = value / 10000;
458 snprintf(s_latitude, sizeof(s_latitude), "-%.2d.", l_decimal);
460 snprintf(s_latitude, sizeof(s_latitude), "%.2d.", l_decimal);
462 snprintf(s_latitude, sizeof(s_latitude), "+%.2d.", l_decimal);
465 l_below_decimal = value - (l_decimal * 10000);
466 if (l_below_decimal < 0)
467 l_below_decimal = -l_below_decimal;
469 snprintf(&s_latitude[4], sizeof(s_latitude) - 4, "%.4d", l_below_decimal);
471 write_tag(f, s_latitude);
474 void _mmcamcorder_write_Longitude(FILE *f, int value)
476 char s_longitude[24];
478 int l_below_decimal = 0;
480 l_decimal = value / 10000;
483 snprintf(s_longitude, sizeof(s_longitude), "-%.3d.", l_decimal);
485 snprintf(s_longitude, sizeof(s_longitude), "%.3d.", l_decimal);
487 snprintf(s_longitude, sizeof(s_longitude), "+%.3d.", l_decimal);
490 l_below_decimal = value - (l_decimal * 10000);
491 if (l_below_decimal < 0)
492 l_below_decimal = -l_below_decimal;
494 snprintf(&s_longitude[5], sizeof(s_longitude) - 5, "%.4d", l_below_decimal);
496 write_tag(f, s_longitude);
499 #define D_GEOGRAPH "\xA9xyz"
500 // 0x0012 -> latitude(8) + longitude(9) + seperator(1) = 18
501 // 0x15c7 -> encode in english
502 #define D_INFO_GEOGRAPH 0x001215c7
504 gboolean _mmcamcorder_write_geodata(FILE *f, _MMCamcorderLocationInfo info)
506 gint64 current_pos, pos;
510 if ((pos = ftello(f)) < 0) {
511 MMCAM_LOG_ERROR("ftello() returns negative value");
515 if (!write_to_32(f, 0)) //size
518 if (!write_tag(f, D_GEOGRAPH)) // type
521 if (!write_to_32(f, D_INFO_GEOGRAPH))
524 _mmcamcorder_write_Latitude(f, info.latitude);
525 _mmcamcorder_write_Longitude(f, info.longitude);
527 FPUTC_CHECK(0x2F, f);
529 if ((current_pos = ftello(f)) < 0) {
530 MMCAM_LOG_ERROR("ftello() returns negative value");
534 if (!_mmcamcorder_update_size(f, pos, current_pos))
541 gboolean _mmcamcorder_write_udta(FILE *f, int gps_enable, _MMCamcorderLocationInfo info, _MMCamcorderLocationInfo geotag)
543 gint64 current_pos, pos;
545 MMCAM_LOG_INFO("gps enable : %d", gps_enable);
546 if (gps_enable == FALSE) {
547 MMCAM_LOG_INFO("no need to write udta");
551 if ((pos = ftello(f)) < 0) {
552 MMCAM_LOG_ERROR("ftello() returns negative value");
557 if (!write_to_32(f, 0)) {
558 MMCAM_LOG_ERROR("failed to write size");
563 if (!write_tag(f, "udta")) {
564 MMCAM_LOG_ERROR("failed to write type udta");
569 if (!_mmcamcorder_write_loci(f, info)) {
570 MMCAM_LOG_ERROR("failed to write loci");
574 if (!_mmcamcorder_write_geodata(f, geotag)) {
575 MMCAM_LOG_ERROR("failed to write geodata");
580 if ((current_pos = ftello(f)) < 0) {
581 MMCAM_LOG_ERROR("ftello() returns negative value");
585 if (!_mmcamcorder_update_size(f, pos, current_pos)) {
586 MMCAM_LOG_ERROR("failed to update size");
590 MMCAM_LOG_INFO("done");
596 guint64 _mmcamcorder_get_container_size(const guchar *size)
604 result = result | (temp << 16);
606 result = result | (temp << 8);
607 result = result | size[3];
609 MMCAM_LOG_INFO("result : %"G_GUINT64_FORMAT, result);
615 guint64 _mmcamcorder_get_container_size64(const guchar *size)
623 result = result | (temp << 48);
625 result = result | (temp << 40);
627 result = result | (temp << 32);
629 result = result | (temp << 24);
631 result = result | (temp << 16);
633 result = result | (temp << 8);
634 result = result | size[7];
636 MMCAM_LOG_INFO("result : %"G_GUINT64_FORMAT, result);
642 gboolean _mmcamcorder_update_composition_matrix(FILE *f, int orientation)
645 guint32 a = 0x00010000;
648 guint32 d = 0x00010000;
650 switch (orientation) {
651 case MM_CAMCORDER_TAG_VIDEO_ORT_90:/* 90 degree */
657 case MM_CAMCORDER_TAG_VIDEO_ORT_180:/* 180 degree */
661 case MM_CAMCORDER_TAG_VIDEO_ORT_270:/* 270 degree */
667 case MM_CAMCORDER_TAG_VIDEO_ORT_NONE:/* 0 degree */
680 write_to_32(f, 0x40000000);
682 MMCAM_LOG_INFO("orientation : %d, write data 0x%x 0x%x 0x%x 0x%x",
683 orientation, a, b, c, d);
689 static int __mmcamcorder_storage_supported_cb(int storage_id, storage_type_e type,
690 storage_state_e state, const char *path, void *user_data)
692 _MMCamcorderStorageInfo *info = (_MMCamcorderStorageInfo *)user_data;
695 MMCAM_LOG_ERROR("NULL info");
699 if (type == info->type) {
700 info->id = storage_id;
708 int _mmcamcorder_get_storage_validity(MMHandleType handle, const char *filename, guint64 min_space, gboolean *storage_validity)
710 int ret = MM_ERROR_NONE;
712 char *dir_name = NULL;
713 guint64 free_space = 0;
714 mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
716 mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
717 mmf_return_val_if_fail(storage_validity, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
720 MMCAM_LOG_WARNING("NULL filename, but keep going...");
721 *storage_validity = TRUE;
722 return MM_ERROR_NONE;
725 dir_name = g_path_get_dirname(filename);
726 mmf_return_val_if_fail(dir_name, MM_ERROR_OUT_OF_STORAGE);
728 err = _mmcamcorder_get_storage_info(dir_name, hcamcorder->root_directory, &hcamcorder->storage_info);
730 MMCAM_LOG_ERROR("get storage info failed");
731 ret = MM_ERROR_CAMCORDER_INTERNAL;
735 err = _mmcamcorder_get_freespace(hcamcorder->storage_info.type, &free_space);
737 MMCAM_LOG_ERROR("get free space failed");
738 ret = MM_ERROR_CAMCORDER_INTERNAL;
742 MMCAM_LOG_WARNING("current free space - %s [%" G_GUINT64_FORMAT "]", dir_name, free_space);
747 if (ret == MM_ERROR_NONE && free_space > min_space) {
748 *storage_validity = TRUE;
749 MMCAM_LOG_INFO("validity and free space of storage : OK");
751 *storage_validity = FALSE;
752 MMCAM_LOG_ERROR("OUT of STORAGE [err:%d or free space [%"G_GUINT64_FORMAT"] is smaller than [%"G_GUINT64_FORMAT"]",
753 err, free_space, min_space);
760 void _mmcamcorder_adjust_recording_max_size(const char *filename, guint64 *max_size)
762 int file_system_type = 0;
763 char *dir_name = NULL;
765 mmf_return_if_fail(max_size);
766 mmf_return_if_fail(filename);
768 dir_name = g_path_get_dirname(filename);
769 mmf_return_if_fail(dir_name);
771 if (_mmcamcorder_get_file_system_type(dir_name, &file_system_type) == 0) {
772 /* MSDOS_SUPER_MAGIC : 0x4d44 */
773 if (file_system_type == MSDOS_SUPER_MAGIC &&
774 (*max_size == 0 || *max_size > FAT32_FILE_SYSTEM_MAX_SIZE)) {
775 MMCAM_LOG_WARNING("FAT32 and too large max[%"G_GUINT64_FORMAT"], set max as %lu",
776 *max_size, FAT32_FILE_SYSTEM_MAX_SIZE);
777 *max_size = FAT32_FILE_SYSTEM_MAX_SIZE;
779 MMCAM_LOG_WARNING("file system 0x%x, max size %"G_GUINT64_FORMAT,
780 file_system_type, *max_size);
788 int _mmcamcorder_get_storage_info(const gchar *path, const gchar *root_directory, _MMCamcorderStorageInfo *info)
791 struct stat stat_path;
792 struct stat stat_root;
794 if (!path || !root_directory || !info) {
795 MMCAM_LOG_ERROR("invalid parameter %p %p %p", path, root_directory, info);
799 if (strlen(root_directory) > 0) {
800 if (stat(path, &stat_path) != 0) {
801 MMCAM_LOG_ERROR("failed to stat for [%s][errno %d]", path, errno);
805 if (stat(root_directory, &stat_root) != 0) {
806 MMCAM_LOG_ERROR("failed to stat for [%s][errno %d]", root_directory, errno);
810 if (stat_path.st_dev == stat_root.st_dev)
811 info->type = STORAGE_TYPE_INTERNAL;
813 info->type = STORAGE_TYPE_EXTERNAL;
815 info->type = STORAGE_TYPE_INTERNAL;
816 MMCAM_LOG_WARNING("invalid length of root directory, assume that it's internal storage.");
819 ret = storage_foreach_device_supported((storage_device_supported_cb)__mmcamcorder_storage_supported_cb, info);
820 if (ret != STORAGE_ERROR_NONE) {
821 MMCAM_LOG_ERROR("storage_foreach_device_supported failed 0x%x", ret);
825 MMCAM_LOG_INFO("storage info - type %d, id %d", info->type, info->id);
831 int _mmcamcorder_get_freespace(storage_type_e type, guint64 *free_space)
836 if (type == STORAGE_TYPE_INTERNAL)
837 ret = storage_get_internal_memory_size(&vfs);
839 ret = storage_get_external_memory_size(&vfs);
841 if (ret != STORAGE_ERROR_NONE) {
843 MMCAM_LOG_ERROR("get memory size failed [type %d] 0x%x", type, ret);
847 *free_space = vfs.f_bsize * vfs.f_bavail;
849 MMCAM_LOG_INFO("vfs.f_bsize [%lu], vfs.f_bavail [%lu]", vfs.f_bsize, vfs.f_bavail);
850 MMCAM_LOG_INFO("memory size %"G_GUINT64_FORMAT" [%s]", *free_space, path);
856 int _mmcamcorder_get_file_system_type(const gchar *path, int *file_system_type)
862 if (!g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
863 MMCAM_LOG_INFO("File(%s) doesn't exist.", path);
867 if (-1 == statfs(path, &fs)) {
868 MMCAM_LOG_INFO("statfs failed.(%s)", path);
872 *file_system_type = (int)fs.f_type;
878 int _mmcamcorder_get_device_led_brightness(GDBusConnection *conn, int *brightness)
881 int ret = MM_ERROR_NONE;
882 GVariant *params = NULL;
883 GVariant *result = NULL;
885 ret = __gdbus_method_call_sync(conn, "org.tizen.system.deviced",
886 "/Org/Tizen/System/DeviceD/Led", "org.tizen.system.deviced.Led",
887 "GetBrightnessForCamera", params, &result, TRUE);
888 if (ret != MM_ERROR_NONE) {
889 MMCAM_LOG_ERROR("Dbus Call on Client Error");
894 g_variant_get(result, "(i)", &get_value);
895 *brightness = get_value;
896 MMCAM_LOG_INFO("flash brightness : %d", *brightness);
897 g_variant_unref(result);
899 MMCAM_LOG_ERROR("replied result is null");
900 ret = MM_ERROR_CAMCORDER_INTERNAL;
907 int _mmcamcorder_send_sound_play_message(GDBusConnection *conn, _MMCamcorderGDbusCbInfo *gdbus_info,
908 const char *sample_name, const char *stream_role, const char *volume_gain, int sync_play)
911 int ret = MM_ERROR_NONE;
912 GVariant *params = NULL;
913 GVariant *result = NULL;
916 if (!conn || !gdbus_info) {
917 MMCAM_LOG_ERROR("Invalid parameter %p %p", conn, gdbus_info);
918 return MM_ERROR_CAMCORDER_INTERNAL;
921 params = g_variant_new("(sss)", sample_name, stream_role, volume_gain);
923 ret = __gdbus_method_call_sync(conn, "org.pulseaudio.Server",
924 "/org/pulseaudio/SoundPlayer", "org.pulseaudio.SoundPlayer",
925 "SamplePlay", params, &result, TRUE);
926 if (ret != MM_ERROR_NONE) {
927 MMCAM_LOG_ERROR("Dbus Call on Client Error");
932 g_variant_get(result, "(i)", &get_value);
933 MMCAM_LOG_INFO("played index : %d", get_value);
934 g_variant_unref(result);
936 MMCAM_LOG_ERROR("replied result is null");
937 return MM_ERROR_CAMCORDER_INTERNAL;
940 g_mutex_lock(&gdbus_info->sync_mutex);
942 if (gdbus_info->subscribe_id > 0) {
943 MMCAM_LOG_WARNING("subscribe_id[%u] is remained. remove it.", gdbus_info->subscribe_id);
945 g_dbus_connection_signal_unsubscribe(conn, gdbus_info->subscribe_id);
947 gdbus_info->subscribe_id = 0;
950 gdbus_info->is_playing = TRUE;
951 gdbus_info->param = get_value;
953 ret = __gdbus_subscribe_signal(conn,
954 "/org/pulseaudio/SoundPlayer", "org.pulseaudio.SoundPlayer", "EOS",
955 __gdbus_stream_eos_cb, &subs_id, gdbus_info);
957 if (ret == MM_ERROR_NONE)
958 gdbus_info->subscribe_id = subs_id;
960 g_mutex_unlock(&gdbus_info->sync_mutex);
962 if (sync_play && ret == MM_ERROR_NONE)
963 ret = __gdbus_wait_for_cb_return(gdbus_info, G_DBUS_TIMEOUT);
969 void _mmcamcorder_request_dpm_popup(GDBusConnection *conn, const char *restricted_policy)
971 int ret = MM_ERROR_NONE;
972 gboolean get_value = 0;
973 GVariant *params = NULL;
974 GVariant *result = NULL;
976 if (!conn || !restricted_policy) {
977 MMCAM_LOG_ERROR("Invalid parameter %p %p", conn, restricted_policy);
981 params = g_variant_new("(s)", restricted_policy);
983 ret = __gdbus_method_call_sync(conn,
984 "org.tizen.DevicePolicyManager",
985 "/org/tizen/DevicePolicyManager/Syspopup",
986 "org.tizen.DevicePolicyManager.Syspopup",
987 "show", params, &result, TRUE);
988 if (ret != MM_ERROR_NONE) {
989 MMCAM_LOG_ERROR("Dbus Call on Client Error 0x%x", ret);
994 g_variant_get(result, "(b)", &get_value);
995 MMCAM_LOG_INFO("request result : %d", get_value);
996 g_variant_unref(result);
998 MMCAM_LOG_ERROR("replied result is null");
1005 int _mmcamcorder_get_file_size(const char *filename, guint64 *size)
1009 if (stat(filename, &buf) != 0)
1011 *size = (guint64)buf.st_size;
1016 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category)
1018 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1020 MMCamcorderHandlerItem *item = NULL;
1022 mmf_return_if_fail(hcamcorder);
1024 if (!hcamcorder->buffer_probes) {
1025 MMCAM_LOG_WARNING("list for buffer probe is NULL");
1029 MMCAM_LOG_INFO("start - category : 0x%x", category);
1031 list = hcamcorder->buffer_probes;
1035 MMCAM_LOG_ERROR("Remove buffer probe failed, the item is NULL");
1036 list = g_list_next(list);
1040 if (item->category & category) {
1041 if (item->object && GST_IS_PAD(item->object)) {
1042 MMCAM_LOG_INFO("Remove buffer probe on [%s:%s] - [ID : %lu], [Category : %x]",
1043 GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category);
1044 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
1046 MMCAM_LOG_WARNING("Remove buffer probe failed, the pad is null or not pad, just remove item from list and free it");
1049 list = g_list_next(list);
1050 hcamcorder->buffer_probes = g_list_remove(hcamcorder->buffer_probes, item);
1053 MMCAM_LOG_INFO("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1054 list = g_list_next(list);
1058 if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1059 g_list_free(hcamcorder->buffer_probes);
1060 hcamcorder->buffer_probes = NULL;
1063 MMCAM_LOG_INFO("done");
1069 void _mmcamcorder_remove_one_buffer_probe(MMHandleType handle, void *object)
1071 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1073 MMCamcorderHandlerItem *item = NULL;
1075 mmf_return_if_fail(hcamcorder);
1077 if (!hcamcorder->buffer_probes) {
1078 MMCAM_LOG_WARNING("list for buffer probe is NULL");
1082 MMCAM_LOG_INFO("start - object : %p", object);
1084 list = hcamcorder->buffer_probes;
1088 MMCAM_LOG_ERROR("Remove buffer probe failed, the item is NULL");
1089 list = g_list_next(list);
1093 if (item->object && item->object == object) {
1094 if (GST_IS_PAD(item->object)) {
1095 MMCAM_LOG_INFO("Remove buffer probe on [%s:%s] - [ID : %lu], [Category : %x]",
1096 GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category);
1097 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
1099 MMCAM_LOG_WARNING("Remove buffer probe failed, the pad is null or not pad, just remove item from list and free it");
1102 list = g_list_next(list);
1103 hcamcorder->buffer_probes = g_list_remove(hcamcorder->buffer_probes, item);
1108 MMCAM_LOG_INFO("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1109 list = g_list_next(list);
1113 MMCAM_LOG_INFO("done");
1119 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category)
1121 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1123 MMCamcorderHandlerItem *item = NULL;
1125 mmf_return_if_fail(hcamcorder);
1127 if (!hcamcorder->event_probes) {
1128 MMCAM_LOG_WARNING("list for event probe is NULL");
1132 MMCAM_LOG_INFO("start - category : 0x%x", category);
1134 list = hcamcorder->event_probes;
1138 MMCAM_LOG_ERROR("Remove event probe failed, the item is NULL");
1139 list = g_list_next(list);
1143 if (item->category & category) {
1144 if (item->object && GST_IS_PAD(item->object)) {
1145 MMCAM_LOG_INFO("Remove event probe on [%s:%s] - [ID : %lu], [Category : %x]",
1146 GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category);
1147 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
1149 MMCAM_LOG_WARNING("Remove event probe failed, the pad is null or not pad, just remove item from list and free it");
1152 list = g_list_next(list);
1153 hcamcorder->event_probes = g_list_remove(hcamcorder->event_probes, item);
1156 MMCAM_LOG_INFO("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1157 list = g_list_next(list);
1161 if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1162 g_list_free(hcamcorder->event_probes);
1163 hcamcorder->event_probes = NULL;
1166 MMCAM_LOG_INFO("done");
1172 void _mmcamcorder_disconnect_signal(MMHandleType handle, _MMCamcorderHandlerCategory category)
1174 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1176 MMCamcorderHandlerItem *item = NULL;
1178 mmf_return_if_fail(hcamcorder);
1180 if (!hcamcorder->signals) {
1181 MMCAM_LOG_WARNING("list for signal is NULL");
1185 MMCAM_LOG_INFO("start - category : 0x%x", category);
1187 list = hcamcorder->signals;
1191 MMCAM_LOG_ERROR("Fail to Disconnecting signal, the item is NULL");
1192 list = g_list_next(list);
1196 if (item->category & category) {
1197 if (item->object && GST_IS_ELEMENT(item->object)) {
1198 if (g_signal_handler_is_connected(item->object, item->handler_id)) {
1199 MMCAM_LOG_INFO("Disconnect signal from [%s] : [ID : %lu], [Category : %x]",
1200 GST_OBJECT_NAME(item->object), item->handler_id, item->category);
1201 g_signal_handler_disconnect(item->object, item->handler_id);
1203 MMCAM_LOG_WARNING("Signal was not connected, cannot disconnect it : [%s] [ID : %lu], [Category : %x]",
1204 GST_OBJECT_NAME(item->object), item->handler_id, item->category);
1207 MMCAM_LOG_ERROR("Fail to Disconnecting signal, the element is null or not element, just remove item from list and free it");
1210 list = g_list_next(list);
1211 hcamcorder->signals = g_list_remove(hcamcorder->signals, item);
1214 MMCAM_LOG_INFO("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1215 list = g_list_next(list);
1219 if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1220 g_list_free(hcamcorder->signals);
1221 hcamcorder->signals = NULL;
1224 MMCAM_LOG_INFO("done");
1230 void _mmcamcorder_remove_all_handlers(MMHandleType handle, _MMCamcorderHandlerCategory category)
1232 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1234 MMCAM_LOG_INFO("ENTER");
1236 if (hcamcorder->signals)
1237 _mmcamcorder_disconnect_signal((MMHandleType)hcamcorder, category);
1238 if (hcamcorder->event_probes)
1239 _mmcamcorder_remove_event_probe((MMHandleType)hcamcorder, category);
1240 if (hcamcorder->buffer_probes)
1241 _mmcamcorder_remove_buffer_probe((MMHandleType)hcamcorder, category);
1243 MMCAM_LOG_INFO("LEAVE");
1247 void _mmcamcorder_element_release_noti(gpointer data, GObject *where_the_object_was)
1250 _MMCamcorderSubContext *sc = (_MMCamcorderSubContext *)data;
1252 mmf_return_if_fail(sc);
1253 mmf_return_if_fail(sc->element);
1255 for (i = 0 ; i < _MMCAMCORDER_PIPELINE_ELEMENT_NUM ; i++) {
1256 if (sc->element[i].gst && (G_OBJECT(sc->element[i].gst) == where_the_object_was)) {
1257 MMCAM_LOG_WARNING("The element[%d][%p] is finalized", sc->element[i].id, sc->element[i].gst);
1258 sc->element[i].gst = NULL;
1259 sc->element[i].id = _MMCAMCORDER_NONE;
1264 mmf_return_if_fail(sc->encode_element);
1266 for (i = 0 ; i < _MMCAMCORDER_ENCODE_PIPELINE_ELEMENT_NUM ; i++) {
1267 if (sc->encode_element[i].gst && (G_OBJECT(sc->encode_element[i].gst) == where_the_object_was)) {
1268 MMCAM_LOG_WARNING("The encode element[%d][%p] is finalized", sc->encode_element[i].id, sc->encode_element[i].gst);
1269 sc->encode_element[i].gst = NULL;
1270 sc->encode_element[i].id = _MMCAMCORDER_ENCODE_NONE;
1275 MMCAM_LOG_WARNING("there is no matching element %p", where_the_object_was);
1281 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1282 gboolean _mmcamcorder_msg_callback(void *data)
1284 _MMCamcorderMsgItem *item = (_MMCamcorderMsgItem*)data;
1285 mmf_camcorder_t *hcamcorder = NULL;
1286 mmf_return_val_if_fail(item, FALSE);
1288 g_mutex_lock(&item->lock);
1290 hcamcorder = MMF_CAMCORDER(item->handle);
1291 if (hcamcorder == NULL) {
1292 MMCAM_LOG_WARNING("msg id:0x%x, item:%p, handle is NULL", item->id, item);
1293 goto MSG_CALLBACK_DONE;
1296 /*MMCAM_LOG_INFO("msg id:%x, msg_cb:%p, msg_data:%p, item:%p", item->id, hcamcorder->msg_cb, hcamcorder->msg_data, item);*/
1298 _MMCAMCORDER_LOCK(hcamcorder);
1300 /* remove item from msg data */
1301 if (hcamcorder->msg_data) {
1302 /*MMCAM_LOG_INFO("remove item %p", item);*/
1303 hcamcorder->msg_data = g_list_remove(hcamcorder->msg_data, item);
1305 MMCAM_LOG_WARNING("msg_data is NULL but item[%p] will be removed", item);
1308 _MMCAMCORDER_UNLOCK(hcamcorder);
1310 _MMCAMCORDER_LOCK_MESSAGE_CALLBACK(hcamcorder);
1312 if ((hcamcorder) && (hcamcorder->msg_cb))
1313 hcamcorder->msg_cb(item->id, (MMMessageParamType*)(&(item->param)), hcamcorder->msg_cb_param);
1315 _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1317 _MMCAMCORDER_SIGNAL(hcamcorder);
1320 /* release allocated memory */
1321 if (item->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1322 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)item->param.data;
1324 SAFE_G_FREE(cam_fd_info->face_info);
1325 SAFE_G_FREE(cam_fd_info);
1327 item->param.data = NULL;
1328 item->param.size = 0;
1330 } else if (item->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED || item->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1331 MMCamRecordingReport *report = (MMCamRecordingReport *)item->param.data;
1333 SAFE_G_FREE(report->recording_filename);
1336 item->param.data = NULL;
1340 g_mutex_unlock(&item->lock);
1341 g_mutex_clear(&item->lock);
1345 /* For not being called again */
1348 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1351 gboolean _mmcamcorder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data)
1353 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1354 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1355 _MMCamcorderMsgItem *item = NULL;
1356 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1358 mmf_return_val_if_fail(hcamcorder, FALSE);
1359 mmf_return_val_if_fail(data, FALSE);
1362 case MM_MESSAGE_CAMCORDER_STATE_CHANGED:
1363 case MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_RM:
1364 data->param.union_type = MM_MSG_UNION_STATE;
1366 case MM_MESSAGE_CAMCORDER_RECORDING_STATUS:
1367 data->param.union_type = MM_MSG_UNION_RECORDING_STATUS;
1369 case MM_MESSAGE_CAMCORDER_FIRMWARE_UPDATE:
1370 data->param.union_type = MM_MSG_UNION_FIRMWARE;
1372 case MM_MESSAGE_CAMCORDER_CURRENT_VOLUME:
1373 data->param.union_type = MM_MSG_UNION_REC_VOLUME_DB;
1375 case MM_MESSAGE_CAMCORDER_TIME_LIMIT:
1376 case MM_MESSAGE_CAMCORDER_MAX_SIZE:
1377 case MM_MESSAGE_CAMCORDER_NO_FREE_SPACE:
1378 case MM_MESSAGE_CAMCORDER_ERROR:
1379 case MM_MESSAGE_CAMCORDER_FOCUS_CHANGED:
1380 case MM_MESSAGE_CAMCORDER_CAPTURED:
1381 case MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED:
1382 case MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED:
1383 case MM_MESSAGE_READY_TO_RESUME:
1385 data->param.union_type = MM_MSG_UNION_CODE;
1389 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1390 item = g_malloc(sizeof(_MMCamcorderMsgItem));
1392 memcpy(item, data, sizeof(_MMCamcorderMsgItem));
1393 item->handle = handle;
1394 g_mutex_init(&item->lock);
1396 _MMCAMCORDER_LOCK(handle);
1397 hcamcorder->msg_data = g_list_append(hcamcorder->msg_data, item);
1398 /*MMCAM_LOG_INFO("item[%p]", item);*/
1400 /* Use DEFAULT priority */
1401 g_idle_add_full(G_PRIORITY_DEFAULT, _mmcamcorder_msg_callback, item, NULL);
1403 _MMCAMCORDER_UNLOCK(handle);
1404 #else /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1405 _MMCAMCORDER_LOCK_MESSAGE_CALLBACK(hcamcorder);
1407 if (hcamcorder->msg_cb)
1408 hcamcorder->msg_cb(data->id, (MMMessageParamType*)(&(data->param)), hcamcorder->msg_cb_param);
1410 MMCAM_LOG_INFO("message callback is NULL. message id %d", data->id);
1412 _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1414 /* release allocated memory */
1415 if (data->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1416 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)data->param.data;
1418 SAFE_G_FREE(cam_fd_info->face_info);
1419 data->param.data = NULL;
1420 g_free(cam_fd_info);
1422 } else if (data->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED || data->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1423 MMCamRecordingReport *report = (MMCamRecordingReport *)data->param.data;
1425 SAFE_G_FREE(report->recording_filename);
1426 data->param.data = NULL;
1430 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1436 void _mmcamcorder_remove_message_all(MMHandleType handle)
1438 mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1439 gboolean ret = TRUE;
1440 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1441 _MMCamcorderMsgItem *item = NULL;
1443 gint64 end_time = 0;
1444 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1446 mmf_return_if_fail(hcamcorder);
1448 _MMCAMCORDER_LOCK(handle);
1450 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1451 if (!hcamcorder->msg_data) {
1452 MMCAM_LOG_INFO("No message data is remained.");
1454 list = hcamcorder->msg_data;
1458 list = g_list_next(list);
1461 MMCAM_LOG_ERROR("Fail to remove message. The item is NULL");
1463 if (g_mutex_trylock(&(item->lock))) {
1464 ret = g_idle_remove_by_data(item);
1466 MMCAM_LOG_INFO("remove msg item[%p], ret[%d]", item, ret);
1470 MMCAM_LOG_WARNING("failed to remove msg cb for item %p, it will be called later with NULL handle", item);
1473 /* release allocated memory */
1474 if (item->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1475 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)item->param.data;
1477 SAFE_G_FREE(cam_fd_info->face_info);
1478 item->param.size = 0;
1480 SAFE_G_FREE(cam_fd_info);
1481 } else if (item->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED || item->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1482 MMCamRecordingReport *report = (MMCamRecordingReport *)item->param.data;
1484 SAFE_G_FREE(report->recording_filename);
1486 SAFE_G_FREE(report);
1489 hcamcorder->msg_data = g_list_remove(hcamcorder->msg_data, item);
1491 g_mutex_unlock(&(item->lock));
1494 g_mutex_clear(&item->lock);
1498 MMCAM_LOG_INFO("remove msg done");
1501 MMCAM_LOG_WARNING("item lock failed. it's being called...");
1503 end_time = g_get_monotonic_time() + (100 * G_TIME_SPAN_MILLISECOND);
1505 if (_MMCAMCORDER_WAIT_UNTIL(handle, end_time))
1506 MMCAM_LOG_WARNING("signal received");
1508 MMCAM_LOG_WARNING("timeout");
1513 g_list_free(hcamcorder->msg_data);
1514 hcamcorder->msg_data = NULL;
1516 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1518 /* remove idle function for playing capture sound */
1520 ret = g_idle_remove_by_data(hcamcorder);
1521 MMCAM_LOG_INFO("remove idle function for playing capture sound. ret[%d]", ret);
1524 _MMCAMCORDER_UNLOCK(handle);
1530 int _mmcamcorder_get_pixel_format(GstCaps *caps)
1532 const char *media_type;
1534 MMPixelFormatType type = MM_PIXEL_FORMAT_INVALID;
1536 mmf_return_val_if_fail(caps != NULL, MM_PIXEL_FORMAT_INVALID);
1538 media_type = gst_structure_get_name(gst_caps_get_structure(caps, 0));
1539 if (media_type == NULL) {
1540 MMCAM_LOG_ERROR("failed to get media_type");
1541 return MM_PIXEL_FORMAT_INVALID;
1544 if (!gst_video_info_from_caps(&vinfo, caps)) {
1545 MMCAM_LOG_ERROR("get video info failed[media type:%s]", media_type);
1546 return MM_PIXEL_FORMAT_INVALID;
1549 if (GST_VIDEO_INFO_IS_YUV(&vinfo))
1550 type = _mmcamcorder_get_pixtype(gst_video_format_to_fourcc(GST_VIDEO_INFO_FORMAT(&vinfo)));
1551 else if (GST_VIDEO_INFO_IS_RGB(&vinfo))
1552 type = MM_PIXEL_FORMAT_RGB888;
1553 else if (!strcmp(media_type, "image/jpeg"))
1554 type = MM_PIXEL_FORMAT_ENCODED;
1555 else if (!strcmp(media_type, "video/x-h264"))
1556 type = MM_PIXEL_FORMAT_ENCODED_H264;
1557 else if (!strcmp(media_type, "video/x-jpeg"))
1558 type = MM_PIXEL_FORMAT_ENCODED_MJPEG;
1559 else if (!strcmp(media_type, "video/x-vp8"))
1560 type = MM_PIXEL_FORMAT_ENCODED_VP8;
1561 else if (!strcmp(media_type, "video/x-vp9"))
1562 type = MM_PIXEL_FORMAT_ENCODED_VP9;
1564 if (type == MM_PIXEL_FORMAT_INVALID)
1565 MMCAM_LOG_ERROR("Not supported format[%s,GST:%d]", media_type, GST_VIDEO_INFO_FORMAT(&vinfo));
1567 MMCAM_LOG_DEBUG("Type[%d]", type);
1573 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format)
1575 unsigned int fourcc = 0;
1577 /*MMCAM_LOG_INFO("pixtype(%d)", pixtype);*/
1580 case MM_PIXEL_FORMAT_NV12:
1581 if (use_zero_copy_format)
1582 fourcc = GST_MAKE_FOURCC('S', 'N', '1', '2');
1584 fourcc = GST_MAKE_FOURCC('N', 'V', '1', '2');
1587 case MM_PIXEL_FORMAT_NV21:
1588 if (use_zero_copy_format)
1589 fourcc = GST_MAKE_FOURCC('S', 'N', '2', '1');
1591 fourcc = GST_MAKE_FOURCC('N', 'V', '2', '1');
1594 case MM_PIXEL_FORMAT_YUYV:
1595 if (use_zero_copy_format)
1596 fourcc = GST_MAKE_FOURCC('S', 'U', 'Y', 'V');
1598 fourcc = GST_MAKE_FOURCC('Y', 'U', 'Y', '2');
1601 case MM_PIXEL_FORMAT_UYVY:
1602 if (use_zero_copy_format)
1603 fourcc = GST_MAKE_FOURCC('S', 'Y', 'V', 'Y');
1605 fourcc = GST_MAKE_FOURCC('U', 'Y', 'V', 'Y');
1608 case MM_PIXEL_FORMAT_I420:
1609 if (use_zero_copy_format)
1610 fourcc = GST_MAKE_FOURCC('S', '4', '2', '0');
1612 fourcc = GST_MAKE_FOURCC('I', '4', '2', '0');
1615 case MM_PIXEL_FORMAT_YV12:
1616 fourcc = GST_MAKE_FOURCC('Y', 'V', '1', '2');
1618 case MM_PIXEL_FORMAT_422P:
1619 fourcc = GST_MAKE_FOURCC('4', '2', '2', 'P');
1621 case MM_PIXEL_FORMAT_RGB565:
1622 fourcc = GST_MAKE_FOURCC('R', 'G', 'B', 'P');
1624 case MM_PIXEL_FORMAT_RGB888:
1625 fourcc = GST_MAKE_FOURCC('R', 'G', 'B', ' ');
1627 case MM_PIXEL_FORMAT_RGBA:
1628 fourcc = GST_MAKE_FOURCC('B', 'G', 'R', 'x');
1630 case MM_PIXEL_FORMAT_ARGB:
1631 fourcc = GST_MAKE_FOURCC('x', 'R', 'G', 'B');
1633 case MM_PIXEL_FORMAT_ENCODED:
1634 if (codectype == MM_IMAGE_CODEC_JPEG) {
1635 fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G');
1636 } else if (codectype == MM_IMAGE_CODEC_JPEG_SRW) {
1637 fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G'); /*TODO: JPEG+SamsungRAW format */
1638 } else if (codectype == MM_IMAGE_CODEC_SRW) {
1639 fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G'); /*TODO: SamsungRAW format */
1640 } else if (codectype == MM_IMAGE_CODEC_PNG) {
1641 fourcc = GST_MAKE_FOURCC('P', 'N', 'G', ' ');
1643 /* Please let us know what other fourcces are. ex) BMP, GIF?*/
1644 fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G');
1647 case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1648 fourcc = GST_MAKE_FOURCC('I', 'T', 'L', 'V');
1650 case MM_PIXEL_FORMAT_ENCODED_H264:
1651 fourcc = GST_MAKE_FOURCC('H', '2', '6', '4');
1653 case MM_PIXEL_FORMAT_INVZ:
1654 fourcc = GST_MAKE_FOURCC('I', 'N', 'V', 'Z');
1656 case MM_PIXEL_FORMAT_ENCODED_MJPEG:
1657 fourcc = GST_MAKE_FOURCC ('M', 'J', 'P', 'G');
1659 case MM_PIXEL_FORMAT_ENCODED_VP8:
1660 fourcc = GST_MAKE_FOURCC ('V', 'P', '8', '0');
1662 case MM_PIXEL_FORMAT_ENCODED_VP9:
1663 fourcc = GST_MAKE_FOURCC ('V', 'P', '9', '0');
1666 MMCAM_LOG_INFO("Not proper pixel type[%d]. Set default - I420", pixtype);
1667 if (use_zero_copy_format)
1668 fourcc = GST_MAKE_FOURCC('S', '4', '2', '0');
1670 fourcc = GST_MAKE_FOURCC('I', '4', '2', '0');
1679 int _mmcamcorder_get_pixtype(unsigned int fourcc)
1681 int pixtype = MM_PIXEL_FORMAT_INVALID;
1683 char *pfourcc = (char*)&fourcc;
1685 MMCAM_LOG_INFO("fourcc(%c%c%c%c)",
1686 pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
1690 case GST_MAKE_FOURCC('S', 'N', '1', '2'):
1692 case GST_MAKE_FOURCC('N', 'V', '1', '2'):
1693 pixtype = MM_PIXEL_FORMAT_NV12;
1695 case GST_MAKE_FOURCC('S', 'N', '2', '1'):
1697 case GST_MAKE_FOURCC('N', 'V', '2', '1'):
1698 pixtype = MM_PIXEL_FORMAT_NV21;
1700 case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
1702 case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
1704 case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
1705 pixtype = MM_PIXEL_FORMAT_YUYV;
1707 case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
1709 case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
1710 pixtype = MM_PIXEL_FORMAT_UYVY;
1712 case GST_MAKE_FOURCC('S', '4', '2', '0'):
1714 case GST_MAKE_FOURCC('I', '4', '2', '0'):
1715 pixtype = MM_PIXEL_FORMAT_I420;
1717 case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
1718 pixtype = MM_PIXEL_FORMAT_YV12;
1720 case GST_MAKE_FOURCC('4', '2', '2', 'P'):
1721 pixtype = MM_PIXEL_FORMAT_422P;
1723 case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
1724 pixtype = MM_PIXEL_FORMAT_RGB565;
1726 case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
1727 pixtype = MM_PIXEL_FORMAT_RGB888;
1729 case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
1731 case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
1732 pixtype = MM_PIXEL_FORMAT_ARGB;
1734 case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
1736 case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
1738 case GST_MAKE_FOURCC('S', 'R', '3', '2'):
1739 pixtype = MM_PIXEL_FORMAT_RGBA;
1741 case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
1742 pixtype = MM_PIXEL_FORMAT_ENCODED;
1745 case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
1746 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
1748 case GST_MAKE_FOURCC('H', '2', '6', '4'):
1749 pixtype = MM_PIXEL_FORMAT_ENCODED_H264;
1751 case GST_MAKE_FOURCC ('M', 'J', 'P', 'G'):
1752 pixtype = MM_PIXEL_FORMAT_ENCODED_MJPEG;
1754 case GST_MAKE_FOURCC ('V', 'P', '8', '0'):
1756 case GST_MAKE_FOURCC ('v', 'p', '0', '8'):
1757 pixtype = MM_PIXEL_FORMAT_ENCODED_VP8;
1759 case GST_MAKE_FOURCC ('V', 'P', '9', '0'):
1761 case GST_MAKE_FOURCC ('v', 'p', '0', '9'):
1762 pixtype = MM_PIXEL_FORMAT_ENCODED_VP9;
1764 case GST_MAKE_FOURCC('I', 'N', 'V', 'Z'):
1765 pixtype = MM_PIXEL_FORMAT_INVZ;
1768 MMCAM_LOG_INFO("Not supported fourcc type(%c%c%c%c)", fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
1769 pixtype = MM_PIXEL_FORMAT_INVALID;
1777 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list)
1779 GList *local_list = element_list;
1780 _MMCamcorderGstElement *element = NULL;
1782 mmf_return_val_if_fail(bin && local_list, FALSE);
1784 while (local_list) {
1785 element = (_MMCamcorderGstElement*)local_list->data;
1786 if (element && element->gst) {
1787 if (!gst_bin_add(bin, GST_ELEMENT(element->gst))) {
1788 MMCAM_LOG_ERROR("Add element [%s] to bin [%s] FAILED",
1789 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1790 GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1793 MMCAM_LOG_INFO("Add element [%s] to bin [%s] OK",
1794 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1795 GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1798 local_list = local_list->next;
1804 gboolean _mmcamcorder_link_elements(GList *element_list)
1806 GList *local_list = element_list;
1807 _MMCamcorderGstElement *element = NULL;
1808 _MMCamcorderGstElement *pre_element = NULL;
1810 mmf_return_val_if_fail(local_list, FALSE);
1812 pre_element = (_MMCamcorderGstElement*)local_list->data;
1813 local_list = local_list->next;
1815 while (local_list) {
1816 element = (_MMCamcorderGstElement*)local_list->data;
1817 if (pre_element && pre_element->gst && element && element->gst) {
1818 if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(pre_element->gst), GST_ELEMENT(element->gst))) {
1819 MMCAM_LOG_INFO("Link [%s] to [%s] OK",
1820 GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1821 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1823 MMCAM_LOG_ERROR("Link [%s] to [%s] FAILED",
1824 GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1825 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1830 pre_element = element;
1831 local_list = local_list->next;
1837 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
1838 unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, size_t *dst_length)
1840 int mm_ret = MM_ERROR_NONE;
1841 int input_format = MM_UTIL_COLOR_YUV420;
1842 mm_util_image_h src_image = NULL;
1843 mm_util_image_h dst_image = NULL;
1845 if (!src_data || !dst_data || !dst_width || !dst_height || !dst_length) {
1846 MMCAM_LOG_ERROR("something is NULL %p,%p,%p,%p,%p",
1847 src_data, dst_data, dst_width, dst_height, dst_length);
1851 /* set input format for mm-util */
1852 switch (src_format) {
1853 case MM_PIXEL_FORMAT_I420:
1854 input_format = MM_UTIL_COLOR_I420;
1856 case MM_PIXEL_FORMAT_YV12:
1857 input_format = MM_UTIL_COLOR_YUV420;
1859 case MM_PIXEL_FORMAT_NV12:
1860 input_format = MM_UTIL_COLOR_NV12;
1862 case MM_PIXEL_FORMAT_YUYV:
1863 input_format = MM_UTIL_COLOR_YUYV;
1865 case MM_PIXEL_FORMAT_UYVY:
1866 input_format = MM_UTIL_COLOR_UYVY;
1868 case MM_PIXEL_FORMAT_RGB888:
1869 input_format = MM_UTIL_COLOR_RGB24;
1872 MMCAM_LOG_ERROR("NOT supported format [%d]", src_format);
1876 MMCAM_LOG_INFO("src size %dx%d -> dst size %dx%d", src_width, src_height, *dst_width, *dst_height);
1878 mm_ret = mm_image_create_image(src_width, src_height, input_format, src_data, (size_t)src_length, &src_image);
1879 if (mm_ret != MM_ERROR_NONE) {
1880 MMCAM_LOG_ERROR("mm_image_create_image failed 0x%x", mm_ret);
1884 mm_ret = mm_util_resize_image(src_image, *dst_width, *dst_height, &dst_image);
1885 mm_image_destroy_image(src_image);
1886 if (mm_ret != MM_ERROR_NONE) {
1887 MMCAM_LOG_ERROR("mm_util_resize_image failed 0x%x", mm_ret);
1891 mm_ret = mm_image_get_image(dst_image, dst_width, dst_height, NULL, dst_data, dst_length);
1892 mm_image_destroy_image(dst_image);
1893 if (mm_ret != MM_ERROR_NONE) {
1894 MMCAM_LOG_ERROR("mm_image_get_image failed 0x%x", mm_ret);
1898 MMCAM_LOG_INFO("resize done %dx%d -> %dx%d, %p, length %zu",
1899 src_width, src_height, *dst_width, *dst_height, *dst_data, *dst_length);
1905 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
1906 int src_format, unsigned int src_length, unsigned int jpeg_quality,
1907 void **result_data, unsigned int *result_length)
1909 int ret = MM_UTIL_ERROR_NONE;
1910 gboolean ret_conv = TRUE;
1911 mm_util_color_format_e jpeg_format = MM_UTIL_COLOR_NUM;
1912 unsigned char *converted_src = NULL;
1913 unsigned int converted_src_size = 0;
1914 mm_util_image_h decoded = NULL;
1915 void *encoded_data = NULL;
1916 size_t encoded_data_size = 0;
1918 mmf_return_val_if_fail(src_data && result_data && result_length, FALSE);
1920 switch (src_format) {
1921 case MM_PIXEL_FORMAT_NV12:
1922 //jpeg_format = MM_UTIL_COLOR_NV12;
1923 ret_conv = _mmcamcorder_convert_NV12_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1924 jpeg_format = MM_UTIL_COLOR_YUV420;
1926 case MM_PIXEL_FORMAT_NV16:
1927 jpeg_format = MM_UTIL_COLOR_NV16;
1928 converted_src = src_data;
1930 case MM_PIXEL_FORMAT_NV21:
1931 jpeg_format = MM_UTIL_COLOR_NV21;
1932 converted_src = src_data;
1934 case MM_PIXEL_FORMAT_YUYV:
1935 //jpeg_format = MM_UTIL_COLOR_YUYV;
1936 ret_conv = _mmcamcorder_convert_YUYV_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1937 jpeg_format = MM_UTIL_COLOR_YUV420;
1939 case MM_PIXEL_FORMAT_UYVY:
1940 //jpeg_format = MM_UTIL_COLOR_UYVY;
1941 ret_conv = _mmcamcorder_convert_UYVY_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1942 jpeg_format = MM_UTIL_COLOR_YUV420;
1944 case MM_PIXEL_FORMAT_I420:
1945 jpeg_format = MM_UTIL_COLOR_YUV420;
1946 converted_src = src_data;
1948 case MM_PIXEL_FORMAT_RGB888:
1949 jpeg_format = MM_UTIL_COLOR_RGB24;
1950 converted_src = src_data;
1952 case MM_PIXEL_FORMAT_RGBA:
1953 jpeg_format = MM_UTIL_COLOR_RGBA;
1954 converted_src = src_data;
1956 case MM_PIXEL_FORMAT_ARGB:
1957 jpeg_format = MM_UTIL_COLOR_ARGB;
1958 converted_src = src_data;
1960 case MM_PIXEL_FORMAT_422P:
1961 jpeg_format = MM_UTIL_COLOR_YUV422; // not supported
1963 case MM_PIXEL_FORMAT_NV12T:
1964 case MM_PIXEL_FORMAT_YV12:
1965 case MM_PIXEL_FORMAT_RGB565:
1966 case MM_PIXEL_FORMAT_ENCODED:
1967 case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1972 if (ret_conv == FALSE) {
1973 if (converted_src &&
1974 converted_src != src_data) {
1975 free(converted_src);
1976 converted_src = NULL;
1978 MMCAM_LOG_ERROR("color convert error source format[%d], jpeg format[%d]", src_format, jpeg_format);
1982 converted_src_size = (converted_src && (converted_src != src_data)) ? converted_src_size : src_length;
1983 ret = mm_image_create_image(src_width, src_height, jpeg_format, converted_src, (size_t)converted_src_size, &decoded);
1984 if (ret != MM_UTIL_ERROR_NONE) {
1985 MMCAM_LOG_ERROR("mm_image_create_image error [%d]", ret);
1986 if (converted_src && (converted_src != src_data))
1987 free(converted_src);
1991 ret = mm_util_encode_to_jpeg_memory(decoded, jpeg_quality, &encoded_data, &encoded_data_size);
1992 mm_image_destroy_image(decoded);
1994 if (converted_src && (converted_src != src_data)) {
1995 free(converted_src);
1996 converted_src = NULL;
1999 if (ret != MM_UTIL_ERROR_NONE) {
2000 MMCAM_LOG_ERROR("No encoder supports %d format, error code %x", src_format, ret);
2006 *result_data = encoded_data;
2007 *result_length = (unsigned int)encoded_data_size;
2013 /* make UYVY smaller as multiple size. ex: 640x480 -> 320x240 or 160x120 ... */
2014 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
2015 unsigned char **dst, unsigned int dst_width, unsigned int dst_height)
2021 int ratio_width = 0;
2022 int ratio_height = 0;
2026 unsigned char *result = NULL;
2028 if (src == NULL || dst == NULL) {
2029 MMCAM_LOG_ERROR("src[%p] or dst[%p] is NULL", src, dst);
2033 result = (unsigned char *)malloc((dst_width * dst_height)<<1);
2035 MMCAM_LOG_ERROR("failed to alloc dst data");
2039 ratio_width = src_width / dst_width;
2040 ratio_height = src_height / dst_height;
2041 line_width = src_width << 1;
2042 jump_width = ratio_width << 1;
2044 MMCAM_LOG_WARNING("[src %dx%d] [dst %dx%d] [line width %d] [ratio width %d, height %d]",
2045 src_width, src_height, dst_width, dst_height, line_width, ratio_width, ratio_height);
2047 for (i = 0 ; i < src_height ; i += ratio_height) {
2048 line_base = i * line_width;
2049 for (j = 0 ; j < line_width ; j += jump_width) {
2050 src_index = line_base + j;
2051 result[k++] = src[src_index];
2052 result[k++] = src[src_index+1];
2055 src_index = line_base + j;
2056 if (src_index % 4 == 0)
2057 result[k++] = src[src_index+2];
2059 result[k++] = src[src_index];
2061 result[k++] = src[src_index+1];
2067 MMCAM_LOG_WARNING("converting done - result %p", result);
2073 static guint16 get_language_code(const char *str)
2075 return (guint16)(((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
2078 static gchar * str_to_utf8(const gchar *str)
2080 return g_convert(str, -1, "UTF-8", "ASCII", NULL, NULL, NULL);
2083 static inline gboolean write_tag(FILE *f, const gchar *tag)
2086 FPUTC_CHECK(*tag++, f);
2091 static inline gboolean write_to_32(FILE *f, guint val)
2093 FPUTC_CHECK(val >> 24, f);
2094 FPUTC_CHECK(val >> 16, f);
2095 FPUTC_CHECK(val >> 8, f);
2096 FPUTC_CHECK(val, f);
2100 static inline gboolean write_to_16(FILE *f, guint val)
2102 FPUTC_CHECK(val >> 8, f);
2103 FPUTC_CHECK(val, f);
2107 static inline gboolean write_to_24(FILE *f, guint val)
2109 write_to_16(f, val >> 8);
2110 FPUTC_CHECK(val, f);
2114 void *_mmcamcorder_util_task_thread_func(void *data)
2116 int ret = MM_ERROR_NONE;
2117 mmf_camcorder_t *hcamcorder = (mmf_camcorder_t *)data;
2120 MMCAM_LOG_ERROR("handle is NULL");
2124 MMCAM_LOG_WARNING("start thread");
2126 g_mutex_lock(&hcamcorder->task_thread_lock);
2128 while (hcamcorder->task_thread_state != _MMCAMCORDER_TASK_THREAD_STATE_EXIT) {
2129 switch (hcamcorder->task_thread_state) {
2130 case _MMCAMCORDER_TASK_THREAD_STATE_NONE:
2131 MMCAM_LOG_WARNING("wait for task signal");
2132 g_cond_wait(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock);
2133 MMCAM_LOG_WARNING("task signal received : state %d", hcamcorder->task_thread_state);
2135 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_PLAY_START:
2136 _mmcamcorder_sound_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE02, FALSE);
2137 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2139 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_SOLO_PLAY_START:
2140 _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE01, FALSE);
2141 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2143 case _MMCAMCORDER_TASK_THREAD_STATE_ENCODE_PIPE_CREATE:
2144 ret = _mmcamcorder_video_prepare_record((MMHandleType)hcamcorder);
2146 /* Play record start sound */
2147 _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_REC_START, FALSE);
2149 MMCAM_LOG_INFO("_mmcamcorder_video_prepare_record return 0x%x", ret);
2150 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2152 case _MMCAMCORDER_TASK_THREAD_STATE_CHECK_CAPTURE_IN_RECORDING:
2154 gint64 end_time = 0;
2156 MMCAM_LOG_WARNING("wait for capture data in recording. wait signal...");
2158 end_time = g_get_monotonic_time() + (5 * G_TIME_SPAN_SECOND);
2160 if (g_cond_wait_until(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock, end_time)) {
2161 MMCAM_LOG_WARNING("signal received");
2163 _MMCamcorderMsgItem message;
2165 memset(&message, 0x0, sizeof(_MMCamcorderMsgItem));
2167 MMCAM_LOG_ERROR("capture data wait time out, send error message");
2169 message.id = MM_MESSAGE_CAMCORDER_ERROR;
2170 message.param.code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2172 _mmcamcorder_send_message((MMHandleType)hcamcorder, &message);
2174 hcamcorder->capture_in_recording = FALSE;
2177 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2181 MMCAM_LOG_WARNING("invalid task thread state %d", hcamcorder->task_thread_state);
2182 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_EXIT;
2187 g_mutex_unlock(&hcamcorder->task_thread_lock);
2189 MMCAM_LOG_WARNING("exit thread");
2195 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2200 int dst_y_offset = 0;
2201 int dst_u_offset = 0;
2202 int dst_v_offset = 0;
2203 int loop_length = 0;
2204 unsigned int dst_size = 0;
2205 unsigned char *dst_data = NULL;
2207 if (!src || !dst || !dst_len) {
2208 MMCAM_LOG_ERROR("NULL pointer %p, %p, %p", src, dst, dst_len);
2212 dst_size = (width * height * 3) >> 1;
2214 MMCAM_LOG_INFO("YUVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2216 dst_data = (unsigned char *)malloc(dst_size);
2218 MMCAM_LOG_ERROR("failed to alloc dst_data. size %d", dst_size);
2222 loop_length = width << 1;
2223 dst_u_offset = width * height;
2224 dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2226 MMCAM_LOG_INFO("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2228 for (i = 0 ; i < height ; i++) {
2229 for (j = 0 ; j < loop_length ; j += 2) {
2230 dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2234 dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2236 dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2244 *dst_len = dst_size;
2246 MMCAM_LOG_INFO("DONE: YUVY -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2252 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2257 int dst_y_offset = 0;
2258 int dst_u_offset = 0;
2259 int dst_v_offset = 0;
2260 int loop_length = 0;
2261 unsigned int dst_size = 0;
2262 unsigned char *dst_data = NULL;
2264 if (!src || !dst || !dst_len) {
2265 MMCAM_LOG_ERROR("NULL pointer %p, %p, %p", src, dst, dst_len);
2269 dst_size = (width * height * 3) >> 1;
2271 MMCAM_LOG_INFO("UYVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2273 dst_data = (unsigned char *)malloc(dst_size);
2275 MMCAM_LOG_ERROR("failed to alloc dst_data. size %d", dst_size);
2279 loop_length = width << 1;
2280 dst_u_offset = width * height;
2281 dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2283 MMCAM_LOG_INFO("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2285 for (i = 0 ; i < height ; i++) {
2286 for (j = 0 ; j < loop_length ; j += 2) {
2289 dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2291 dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2296 dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2301 *dst_len = dst_size;
2303 MMCAM_LOG_INFO("DONE: UYVY -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2309 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2313 int dst_y_offset = 0;
2314 int dst_u_offset = 0;
2315 int dst_v_offset = 0;
2316 int loop_length = 0;
2317 unsigned int dst_size = 0;
2318 unsigned char *dst_data = NULL;
2320 if (!src || !dst || !dst_len) {
2321 MMCAM_LOG_ERROR("NULL pointer %p, %p, %p", src, dst, dst_len);
2325 /* buffer overflow prevention check */
2326 if (width > __MMCAMCORDER_MAX_WIDTH || height > __MMCAMCORDER_MAX_HEIGHT) {
2327 MMCAM_LOG_ERROR("too large size %d x %d", width, height);
2331 dst_size = (width * height * 3) >> 1;
2333 MMCAM_LOG_INFO("NV12 -> I420 : %dx%d, dst size %d", width, height, dst_size);
2335 dst_data = (unsigned char *)malloc(dst_size);
2337 MMCAM_LOG_ERROR("failed to alloc dst_data. size %d", dst_size);
2341 loop_length = width << 1;
2342 dst_u_offset = width * height;
2343 dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2345 MMCAM_LOG_INFO("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2348 memcpy(dst_data, src, dst_u_offset);
2350 loop_length = dst_u_offset >> 1;
2351 src_offset = dst_u_offset;
2354 for (i = 0 ; i < loop_length ; i++) {
2356 dst_data[dst_u_offset++] = src[src_offset++];
2358 dst_data[dst_v_offset++] = src[src_offset++];
2362 *dst_len = dst_size;
2364 MMCAM_LOG_INFO("DONE: NV12 -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2370 void _mmcamcorder_emit_dbus_signal(GDBusConnection *conn, const char *object_name,
2371 const char *interface_name, const char *signal_name, int value)
2373 if (!conn || !object_name || !interface_name || !signal_name) {
2374 MMCAM_LOG_ERROR("NULL pointer %p %p %p %p",
2375 conn, object_name, interface_name, signal_name);
2379 if (!g_dbus_connection_emit_signal(conn, NULL,
2380 object_name, interface_name, signal_name,
2381 g_variant_new("(i)", value), NULL)) {
2382 MMCAM_LOG_WARNING("failed to emit signal");
2384 MMCAM_LOG_INFO("emit signal done - value 0x%.8x", value);
2385 g_dbus_connection_flush(conn, NULL, NULL, NULL);
2386 MMCAM_LOG_INFO("signal flush done");
2393 int _mmcamcorder_get_audiosrc_blocksize(int samplerate, int format, int channel, int interval, int *blocksize)
2398 MMCAM_LOG_ERROR("NULL ptr");
2402 if (samplerate == 0 || channel == 0 || interval == 0) {
2403 MMCAM_LOG_ERROR("invalid param %d %d %d", samplerate, channel, interval);
2407 if (format == MM_CAMCORDER_AUDIO_FORMAT_PCM_S16_LE)
2410 *blocksize = samplerate * depth * channel * interval / 8000;
2415 gboolean _mmcamcorder_is_encoded_preview_pixel_format(int pixel_format)
2417 return (pixel_format == MM_PIXEL_FORMAT_ENCODED_H264 ||
2418 pixel_format == MM_PIXEL_FORMAT_ENCODED_MJPEG ||
2419 pixel_format == MM_PIXEL_FORMAT_ENCODED_VP8 ||
2420 pixel_format == MM_PIXEL_FORMAT_ENCODED_VP9);
2423 void _mmcamcorder_set_log_level(int level)
2425 MMCAM_LOG_WARNING("set log level[%d]", level);
2426 mmcam_log_level = level;
2429 int _mmcamcorder_get_log_level(void)
2431 return mmcam_log_level;
2434 void _mmcamcorder_measure_fps(void *data)
2437 int frame_count = 0;
2438 struct timeval current_video_time;
2439 _MMCamcorderKPIMeasure *kpi = (_MMCamcorderKPIMeasure *)data;
2441 mmf_return_if_fail(kpi);
2443 if (!timerisset(&kpi->init_video_time) && !timerisset(&kpi->last_video_time)) {
2444 MMCAM_LOG_INFO("START to measure FPS");
2445 gettimeofday(&(kpi->init_video_time), NULL);
2448 frame_count = ++(kpi->video_framecount);
2450 gettimeofday(¤t_video_time, NULL);
2452 diff_sec = current_video_time.tv_sec - kpi->last_video_time.tv_sec;
2456 kpi->current_fps = (frame_count - kpi->last_framecount) / diff_sec;
2458 if ((current_video_time.tv_sec - kpi->init_video_time.tv_sec) != 0)
2459 kpi->average_fps = kpi->video_framecount / (current_video_time.tv_sec - kpi->init_video_time.tv_sec);
2461 kpi->last_framecount = frame_count;
2462 kpi->last_video_time.tv_sec = current_video_time.tv_sec;
2463 kpi->last_video_time.tv_usec = current_video_time.tv_usec;
2465 MMCAM_LOG_INFO("current fps[%d], average[%d]", kpi->current_fps, kpi->average_fps);