1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <glib.h>
#include <Ecore.h>
#include <tizen.h>
#include <service_app.h>
#include <camera.h>
#include <pthread.h>
#include "controller.h"
#include "controller_image.h"
#include "controller_telegram.h"
#include "log.h"
#include "resource_camera.h"
#define THRESHOLD_VALID_EVENT_COUNT 5
#define VALID_EVENT_INTERVAL_MS 200
#define TELEGRAM_EVENT_INTERVAL_MS 5000
#define IMAGE_FILE_PREFIX "CAM_"
//#define TEMP_IMAGE_FILENAME "/opt/usr/home/owner/apps_rw/org.tizen.smart-surveillance-camera/shared/data/tmp.jpg"
//#define LATEST_IMAGE_FILENAME "/opt/usr/home/owner/apps_rw/org.tizen.smart-surveillance-camera/shared/data/latest.jpg"
typedef struct app_data_s {
long long int last_valid_event_time;
int valid_event_count;
unsigned int latest_image_width;
unsigned int latest_image_height;
unsigned char *latest_image_buffer;
unsigned char *latest_encoded_image_buffer;
unsigned int latest_encoded_image_buffer_size;
Ecore_Thread *image_writter_thread;
pthread_mutex_t mutex;
char* temp_image_filename;
char* latest_image_filename;
Ecore_Thread *telegram_thread;
char* telegram_message;
unsigned char* telegram_image_buffer;
unsigned long long telegram_image_buffer_size;
} app_data;
static long long int __get_monotonic_ms(void)
{
long long int ret_time = 0;
struct timespec time_s;
if (0 == clock_gettime(CLOCK_MONOTONIC, &time_s))
ret_time = time_s.tv_sec* 1000 + time_s.tv_nsec / 1000000;
else
_E("Failed to get time");
return ret_time;
}
static void __terminate_telegram_thread(void *data)
{
app_data *ad = (app_data *)data;
_D("Telegram Thread Terminated!");
ad->telegram_thread = NULL;
}
static void __thread_telegram_task(void *data, Ecore_Thread *th)
{
app_data *ad = (app_data *)data;
_D("Telegram Thread Start!");
controller_telegram_send_message(ad->telegram_message);
controller_telegram_send_image(ad->telegram_image_buffer, ad->telegram_image_buffer_size);
}
static void __thread_telegram_task_end_cb(void *data, Ecore_Thread *th)
{
_D("Telegram Thread End!");
ecore_main_loop_thread_safe_call_async(__terminate_telegram_thread, (app_data *)data);
}
static void __send_telegram_message(const char* msg, app_data *ad)
{
if (!msg)
return;
static long long int last_event_time = 0;;
long long int now = __get_monotonic_ms();
unsigned char* last_buffer;
char* last_message;
char* new_message;
if (now < last_event_time + TELEGRAM_EVENT_INTERVAL_MS) {
return;
}
last_event_time = now;
new_message = strdup(msg);
pthread_mutex_lock(&ad->mutex);
last_buffer = ad->telegram_image_buffer;
last_message = ad->telegram_message;
ad->telegram_message = new_message;
ad->telegram_image_buffer = ad->latest_encoded_image_buffer;
ad->latest_encoded_image_buffer = NULL;
ad->telegram_image_buffer_size = ad->latest_encoded_image_buffer_size;
pthread_mutex_unlock(&ad->mutex);
free(last_buffer);
free(last_message);
if (!ad->telegram_thread) {
ad->telegram_thread = ecore_thread_run(__thread_telegram_task,
__thread_telegram_task_end_cb,
__thread_telegram_task_end_cb,
ad);
} else {
_E("Telegram Thread is running NOW");
}
}
static void __thread_write_image_file(void *data, Ecore_Thread *th)
{
app_data *ad = (app_data *)data;
unsigned int width = 0;
unsigned int height = 0;
unsigned char *buffer = NULL;
unsigned char *encoded_buffer = NULL;
unsigned long long encoded_size = 0;
char *image_info = NULL;
int ret = 0;
pthread_mutex_lock(&ad->mutex);
width = ad->latest_image_width;
height = ad->latest_image_height;
buffer = ad->latest_image_buffer;
ad->latest_image_buffer = NULL;
pthread_mutex_unlock(&ad->mutex);
char newFileName[PATH_MAX] = {0, };
char timeInfo[PATH_MAX] = {0, };
snprintf(timeInfo, PATH_MAX, "Time: %lld", __get_monotonic_ms());
snprintf(newFileName, PATH_MAX, "%s_%lld.jpg", ad->temp_image_filename, __get_monotonic_ms());
image_info = strdup(timeInfo);
ret = controller_image_save_image_file(newFileName, width, height, buffer,
&encoded_buffer, &encoded_size, image_info, strlen(image_info));
// if (ret) {
// _E("failed to save image file");
// } else {
// ret = rename(ad->temp_image_filename, ad->latest_image_filename);
// if (ret != 0 )
// _E("Rename fail");
// }
pthread_mutex_lock(&ad->mutex);
unsigned char *temp = ad->latest_encoded_image_buffer;
ad->latest_encoded_image_buffer = encoded_buffer;
ad->latest_encoded_image_buffer_size = encoded_size;
pthread_mutex_unlock(&ad->mutex);
free(temp);
free(image_info);
free(buffer);
}
static void __thread_write_image_file_end_cb(void *data, Ecore_Thread *th)
{
app_data *ad = (app_data *)data;
pthread_mutex_lock(&ad->mutex);
ad->image_writter_thread = NULL;
pthread_mutex_unlock(&ad->mutex);
}
static void __thread_write_image_file_cancel_cb(void *data, Ecore_Thread *th)
{
app_data *ad = (app_data *)data;
unsigned char *buffer = NULL;
_E("Thread %p got cancelled.\n", th);
pthread_mutex_lock(&ad->mutex);
buffer = ad->latest_image_buffer;
ad->latest_image_buffer = NULL;
ad->image_writter_thread = NULL;
pthread_mutex_unlock(&ad->mutex);
free(buffer);
}
static void __copy_image_buffer(image_buffer_data_s *image_buffer, app_data *ad)
{
unsigned char *buffer = NULL;
pthread_mutex_lock(&ad->mutex);
ad->latest_image_height = image_buffer->image_height;
ad->latest_image_width = image_buffer->image_width;
buffer = ad->latest_image_buffer;
ad->latest_image_buffer = image_buffer->buffer;
pthread_mutex_unlock(&ad->mutex);
free(buffer);
}
static void __preview_image_buffer_created_cb(void *data)
{
image_buffer_data_s *image_buffer = data;
app_data *ad = (app_data *)image_buffer->user_data;
ret_if(!image_buffer);
ret_if(!ad);
__copy_image_buffer(image_buffer, ad);
free(image_buffer);
pthread_mutex_lock(&ad->mutex);
if (!ad->image_writter_thread) {
ad->image_writter_thread = ecore_thread_run(__thread_write_image_file,
__thread_write_image_file_end_cb,
__thread_write_image_file_cancel_cb,
ad);
} else {
_E("Thread is running NOW");
}
pthread_mutex_unlock(&ad->mutex);
return;
}
static void _start_camera(void)
{
if (resource_camera_start_preview() == -1) {
_E("Failed to start camera preview");
}
}
static void _stop_camera(void)
{
if (resource_camera_stop_preview() == -1) {
_E("Failed to stop camera preview");
}
}
static bool service_app_create(void *data)
{
app_data *ad = (app_data *)data;
char* shared_data_path = app_get_shared_data_path();
if (shared_data_path == NULL) {
_E("Failed to get shared data path");
goto ERROR;
}
ad->temp_image_filename = g_strconcat(shared_data_path, "tmp.jpg", NULL);
ad->latest_image_filename = g_strconcat(shared_data_path, "latest.jpg", NULL);
free(shared_data_path);
_D("%s", ad->temp_image_filename);
_D("%s", ad->latest_image_filename);
controller_image_initialize();
pthread_mutex_init(&ad->mutex, NULL);
if (resource_camera_init(__preview_image_buffer_created_cb, ad) == -1) {
_E("Failed to init camera");
goto ERROR;
}
return true;
ERROR:
resource_camera_close();
controller_image_finalize();
pthread_mutex_destroy(&ad->mutex);
return false;
}
static void service_app_terminate(void *data)
{
app_data *ad = (app_data *)data;
Ecore_Thread *thread_id = NULL;
unsigned char *buffer = NULL;
unsigned char *encoded_image_buffer = NULL;
char *info = NULL;
gchar *temp_image_filename;
gchar *latest_image_filename;
_D("App Terminated - enter");
resource_camera_close();
pthread_mutex_lock(&ad->mutex);
thread_id = ad->image_writter_thread;
ad->image_writter_thread = NULL;
pthread_mutex_unlock(&ad->mutex);
if (thread_id)
ecore_thread_wait(thread_id, 3.0); // wait for 3 second
if(ad->telegram_thread)
ecore_thread_wait(ad->telegram_thread, 3.0); // wait for 3 second
ad->telegram_thread = NULL;
free(ad->telegram_message);
free(ad->telegram_image_buffer);
controller_image_finalize();
pthread_mutex_lock(&ad->mutex);
buffer = ad->latest_image_buffer;
ad->latest_image_buffer = NULL;
encoded_image_buffer = ad->latest_encoded_image_buffer;
ad->latest_encoded_image_buffer = NULL;
temp_image_filename = ad->temp_image_filename;
ad->temp_image_filename = NULL;
latest_image_filename = ad->latest_image_filename;
ad->latest_image_filename = NULL;
pthread_mutex_unlock(&ad->mutex);
free(buffer);
free(encoded_image_buffer);
free(info);
g_free(temp_image_filename);
g_free(latest_image_filename);
pthread_mutex_destroy(&ad->mutex);
free(ad);
_D("App Terminated - leave");
}
static void service_app_control(app_control_h app_control, void *data)
{
int ret = 0;
char *command = NULL;
_D("App control");
ret = app_control_get_extra_data(app_control, "command", &command);
if (ret != APP_CONTROL_ERROR_NONE) {
_D("Failed to app_control_get_extra_data() From command key [0x%x]", ret);
} else {
_D("command = [%s]", command);
if (!strncmp("send", command, sizeof("send"))) {
__send_telegram_message("TEST MESSAGE", data);
} else if (!strncmp("on", command, sizeof("on"))) {
_start_camera();
} else if (!strncmp("off", command, sizeof("off"))) {
_stop_camera();
}
free(command);
}
}
int main(int argc, char* argv[])
{
app_data *ad = NULL;
int ret = 0;
service_app_lifecycle_callback_s event_callback;
ad = calloc(1, sizeof(app_data));
retv_if(!ad, -1);
event_callback.create = service_app_create;
event_callback.terminate = service_app_terminate;
event_callback.app_control = service_app_control;
ret = service_app_main(argc, argv, &event_callback, ad);
return ret;
}
|