summaryrefslogtreecommitdiff
path: root/gstplayer/GstMediaRecorder.cpp
blob: 1695cca0360f079511a5a19050b6aa033a725920 (plain)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
/*
 * =====================================================================================
 *
 *	   Filename:  GstMediaRecorder.cpp
 *
 *	Description:  
 *
 *		Version:  1.0
 *		Created:  03/31/2009 01:40:04 PM
 *	   Revision:  none
 *	   Compiler:  gcc
 *
 *		 Author:  Benjamin Gaignard
 *		Company:  STEricsson
 *
 * =====================================================================================
 */
/* 
Copyright (c) 2010, ST-Ericsson SA
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
- Neither the name of the ST-Ericsson nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define LOG_NDEBUG 0

#undef LOG_TAG
#define LOG_TAG "GstMediaRecorder"

#include <sys/ioctl.h>
#include <alsa/asoundlib.h>
#include <asnd_api.h>

#include "GstMediaRecorder.h"
#include <utils/Log.h>
#ifndef STECONF_ANDROID_VERSION_FROYO
#include <ui/CameraParameters.h>
#include <utils/Errors.h>
#include <media/mediarecorder.h>
#include <media/AudioSystem.h>
#include <ui/ISurface.h>
#include <ui/ICamera.h>
#include <ui/Camera.h>
#else
#include <camera/CameraParameters.h>
#include <utils/Errors.h>
#include <media/AudioSystem.h>
#include <surfaceflinger/ISurface.h>
#include <camera/ICamera.h>
#include <camera/Camera.h>
#endif
#include <fcntl.h>
#include <gst/app/gstappsrc.h>
#include <gst/app/gstappbuffer.h>
#include <gsticbvideo.h>
#include <binder/MemoryBase.h>
#include <cutils/properties.h>

using namespace android;

GstMediaRecorder::GstMediaRecorder ()
{
  LOGV ("GstMediaRecorder constructor");
  mCamera = NULL;
  mSurface = NULL;
  mFlags = 0;

  mVideoBin = NULL;
  mAudioBin = NULL;
  mPipeline = NULL;

  mUse_video_src = FALSE;
  mUse_audio_src = FALSE;

  mVideoSrc = NULL;
  mAudioSrc = NULL;

  mOutFilePath = NULL;

  mMaxDuration = -1;
  mMaxFileSize = -1;
  mCurrentFileSize = 0;
  mTimer = NULL;

  //default init
  mFps = 15;
  mWidth = 176;
  mHeight = 144;
  mOutput_format = OUTPUT_FORMAT_DEFAULT;
  mVideo_encoder = VIDEO_ENCODER_DEFAULT;
  mAudio_encoder = AUDIO_ENCODER_DEFAULT;
  mAudio_source = AUDIO_SOURCE_MIC;

  mAudioSampleRate = 48000;
  mAudioChannels = 2;
  mAudioBitrate = 192000;
  mVideoBitrate = 786432;
  mVTMode = 0;
  mIPeriod = 0;
  mIMBRefreshMode = 0;

  if (!g_thread_supported ()) {
    LOGV ("GstMediaRecorder GLib thread init");
    g_thread_init (NULL);
  }
  // setup callback listener
  mCameraListener = new AndroidGstCameraListener (this);

  /* create and init the EOS mutex now */
  mEOSlock = g_mutex_new ();
  g_mutex_lock (mEOSlock);
  mIsEos = FALSE;

  if (snd_hwdep_open (&mHwdep_handle, "hw:0,0", O_RDWR) < 0) {
    LOGE ("Error %d opening hwdep device\n", errno);
  }
}

GstMediaRecorder::~GstMediaRecorder ()
{
  LOGV ("GstMediaRecorder destructor");

  if (mCamera != NULL) {
    mCamera->setListener (NULL);
    if ((mFlags & FLAGS_HOT_CAMERA) == 0) {
      LOGV ("GstMediaRecorder camera was cold when we started, stopping preview");
      mCamera->stopPreview ();
    }
    if (mFlags & FLAGS_SET_CAMERA) {
      LOGV ("GstMediaRecorder unlocking camera to return to app");
      mCamera->unlock ();
    } else {
      LOGV ("GstMediaRecorder disconnect from camera");
      mCamera->disconnect ();
    }
    mCamera.clear ();
  }

  mFlags = 0;

  // don't send eos but release the pipeline
  release_pipeline ();

  if (mOutFilePath) {
    g_free (mOutFilePath);
  }
  mCameraListener.clear ();

  // free mutex
  g_mutex_free (mEOSlock);
  mEOSlock = NULL;

  // free timer
  g_timer_destroy (mTimer);
  mTimer = NULL;

  if (mHwdep_handle) {
    snd_hwdep_close (mHwdep_handle);
  }

}

status_t
GstMediaRecorder::init ()
{
  LOGV ("GstMediaRecorder init");

  return OK;
}

status_t
GstMediaRecorder::setAudioSource (audio_source as)
{
  //LOGV("GstMediaRecorder setAudioSource %s", (as==AUDIO_SOURCE_DEFAULT)?"AUDIO_SOURCE_DEFAULT":"AUDIO_SOURCE_MIC");
  mAudio_source = as;
  mUse_audio_src = TRUE;
  switch (as) {
    case AUDIO_SOURCE_DEFAULT:
      LOGV ("GstMediaRecorder setAudioSource DEFAULT (MIC)");
      //the default value is equal to AUDIO_SOURCE_MIC        
      mAudio_source = AUDIO_SOURCE_MIC;
      break;
    case AUDIO_SOURCE_MIC:
      LOGV ("GstMediaRecorder setAudioSource MIC");
      break;
    case AUDIO_SOURCE_VOICE_UPLINK:
      LOGV ("GstMediaRecorder setAudioSource VOICE_UPLINK");
      break;
    case AUDIO_SOURCE_VOICE_DOWNLINK:
      LOGV ("GstMediaRecorder setAudioSource VOICE_DOWNLINK");
      break;
    case AUDIO_SOURCE_CAMCORDER:
      LOGV ("GstMediaRecorder setAudioSource CAMCORDER");
      break;
    case AUDIO_SOURCE_VOICE_RECOGNITION:
      LOGV ("GstMediaRecorder setAudioSource VOICE_RECOGNITION");
      break;
    case AUDIO_SOURCE_VOICE_CALL:
      LOGV ("GstMediaRecorder setAudioSource VOICE_CALL");
      break;
    default:
      break;
  }
  return OK;
}

status_t
GstMediaRecorder::setVideoSource (video_source vs)
{
  LOGV ("GstMediaRecorder setVideoSource %s",
      (vs ==
          VIDEO_SOURCE_DEFAULT) ? "VIDEO_SOURCE_DEFAULT" :
      "VIDEO_SOURCE_CAMERA");
  switch (vs) {
    case VIDEO_SOURCE_DEFAULT:
      //the default value is equal to VIDEO_SOURCE_CAMERA
      mUse_video_src = TRUE;
      break;
    case VIDEO_SOURCE_CAMERA:
      mUse_video_src = TRUE;
      break;
    default:
      mUse_video_src = FALSE;
      break;
  }
  return OK;
}

status_t
GstMediaRecorder::setOutputFormat (output_format of)
{
  LOGV ("GstMediaRecorder setOutputFormat %d", of);
  mOutput_format = of;

  switch (of) {
    case OUTPUT_FORMAT_DEFAULT:
      LOGV ("GstMediaRecorder setOutputFormat DEFAULT (3GPP)");
      mOutput_format = OUTPUT_FORMAT_THREE_GPP;
      break;
    case OUTPUT_FORMAT_THREE_GPP:
      LOGV ("GstMediaRecorder setOutputFormat 3GPP");
      break;
    case OUTPUT_FORMAT_MPEG_4:
      LOGV ("GstMediaRecorder setOutputFormat MPEG4");
      break;
    case OUTPUT_FORMAT_RAW_AMR:
      LOGV ("GstMediaRecorder setOutputFormat RAW AMR (AMR NB)");
      break;
    case OUTPUT_FORMAT_LIST_END:
      break;
    case OUTPUT_FORMAT_AMR_WB:
      LOGV (" AMR WB");
      break;
    case OUTPUT_FORMAT_AAC_ADIF:
      LOGV (" AAC ADIF");
      break;
    case OUTPUT_FORMAT_AAC_ADTS:
      LOGV (" AAC ADTS");
      break;
  }
  return OK;
}

status_t
GstMediaRecorder::setAudioEncoder (audio_encoder ae)
{
  //LOGV("GstMediaRecorder setAudioEncoder %s", (ae==AUDIO_ENCODER_DEFAULT)?"AUDIO_ENCODER_DEFAULT":"AUDIO_ENCODER_AMR_NB");
  mAudio_encoder = ae;
  switch (mAudio_encoder) {
    case AUDIO_ENCODER_DEFAULT:
    case AUDIO_ENCODER_AMR_NB:
      LOGV ("GstMediaRecorder setAudioEncoder AMR NB");
      mAudio_encoder = AUDIO_ENCODER_AMR_NB;
      break;
    case AUDIO_ENCODER_AMR_WB:
      LOGV ("GstMediaRecorder setAudioEncoder AMR WB");
      break;
    case AUDIO_ENCODER_AAC:
      LOGV ("GstMediaRecorder setAudioEncoder AAC");
      break;
    case AUDIO_ENCODER_AAC_PLUS:
      LOGV ("GstMediaRecorder setAudioEncoder AAC PLUS");
      break;
    case AUDIO_ENCODER_EAAC_PLUS:
      LOGV ("GstMediaRecorder setAudioEncoder EAAC PLUS");
      break;
    default:
      LOGV ("GstMediaRecorder setAudioEncoder AMR NB");
      mAudio_encoder = AUDIO_ENCODER_AMR_NB;
      break;
  }
  return OK;
}

status_t
GstMediaRecorder::setVideoEncoder (video_encoder ve)
{
  LOGV ("GstMediaRecorder setVideoEncoder %d", ve);
  mVideo_encoder = ve;
  switch (mVideo_encoder) {
    case VIDEO_ENCODER_DEFAULT:
      LOGV ("GstMediaRecorder setVideoEncoder DEFAULT (MPEG4)");
      mVideo_encoder = VIDEO_ENCODER_MPEG_4_SP;
      break;
    case VIDEO_ENCODER_H263:
      LOGV ("GstMediaRecorder setVideoEncoder H263");
      break;
    case VIDEO_ENCODER_H264:
      LOGV ("GstMediaRecorder setVideoEncoder H264");
      break;
    case VIDEO_ENCODER_MPEG_4_SP:
      LOGV ("GstMediaRecorder setVideoEncoder MPEG4");
      break;
  }
  return OK;
}

status_t
GstMediaRecorder::setVideoSize (int width, int height)
{
  LOGV ("GstMediaRecorder setVideoSize width=%d height=%d", width, height);
  mWidth = width;
  mHeight = height;
  return OK;
}

status_t
GstMediaRecorder::setVideoFrameRate (int frames_per_second)
{
  LOGV ("GstMediaRecorder setVideoFrameRate %d fps", frames_per_second);
  mFps = frames_per_second;
  return OK;
}

status_t
GstMediaRecorder::setCamera (const sp < ICamera > &camera)
{
  LOGV ("GstMediaRecorder setCamera");

  mFlags &= ~FLAGS_SET_CAMERA | FLAGS_HOT_CAMERA;
  if (camera == NULL) {
    LOGV ("camera is NULL");
    return OK;
  }
  // Connect our client to the camera remote
  mCamera = Camera::create (camera);
  if (mCamera == NULL) {
    LOGV ("Unable to connect to camera");
    return OK;
  }

  LOGV ("Connected to camera");
  mFlags |= FLAGS_SET_CAMERA;
  if (mCamera->previewEnabled ()) {
    mFlags |= FLAGS_HOT_CAMERA;
    LOGV ("camera is hot");
  }
  mUse_video_src = TRUE;
  return OK;
}

status_t
GstMediaRecorder::setPreviewSurface (const sp < ISurface > &surface)
{
  LOGV ("GstMediaRecorder setPreviewSurface");
  mSurface = surface;
  return OK;
}

status_t
GstMediaRecorder::setOutputFile (const char *path)
{
  LOGV ("GstMediaRecorder setOutputFile %s", path);
  mOutFilePath = g_strdup_printf ("file://%s", path);
  mOutFilePath_fd = -1;
  return OK;
}

status_t
GstMediaRecorder::setOutputFile (int fd, int64_t offset, int64_t length)
{
  LOGV ("GstMediaRecorder setOutputFile for fd : fd=%d offset=%lld length=%lld",
      fd, offset, length);
  GST_UNUSED (offset);
  GST_UNUSED (length);
  mOutFilePath = g_strdup_printf ("fd://%d", fd);
  mOutFilePath_fd = fd;
  return OK;
}

status_t
GstMediaRecorder::setParameters (const String8 & params)
{
  LOGV ("GstMediaRecorder setParameters");

  if (strstr (params, "max-duration") != NULL) {
    sscanf (params, "max-duration=%lld", &mMaxDuration);
  }
  if (strstr (params, "max-filesize") != NULL) {
    sscanf (params, "max-filesize=%lld", &mMaxFileSize);
  }
  if (strstr (params, "audio-param-sampling-rate") != NULL) {
    sscanf (params, "audio-param-sampling-rate=%lld", &mAudioSampleRate);
    if ((mAudioSampleRate < 8000) || (mAudioSampleRate > 48000))
      mAudioSampleRate = 48000;

  }
  if (strstr (params, "audio-param-number-of-channels") != NULL) {
    sscanf (params, "audio-param-number-of-channels=%lld", &mAudioChannels);
    if ((mAudioChannels < 0) || (mAudioChannels > 2))
      mAudioChannels = 2;
  }
  if (strstr (params, "audio-param-encoding-bitrate") != NULL) {
    sscanf (params, "audio-param-encoding-bitrate=%lld", &mAudioBitrate);
    if ((mAudioBitrate < 0) || (mAudioBitrate > 192000))
      mAudioBitrate = 128000;
  }
  if (strstr (params, "video-param-encoding-bitrate") != NULL) {
    sscanf (params, "video-param-encoding-bitrate=%lld", &mVideoBitrate);
    if ((mVideoBitrate < 0) || (mVideoBitrate > 786432))
      mVideoBitrate = 360000;
  }
  if (strstr (params, "vt-mode") != NULL) {
    sscanf (params, "vt-mode=%d", &mVTMode);
  }
  if (strstr (params, "i-mb-refresh") != NULL) {
    sscanf (params, "i-mb-refresh=%d", &mIMBRefreshMode);
  }
  if (strstr (params, "i-period") != NULL) {
    sscanf (params, "i-period=%d", &mIPeriod);
  }
  if (strstr (params, "video-bitrate") != NULL) {
    sscanf (params, "video-bitrate=%lld", &mVideoBitrate);
  }
  //if (mCamera != NULL) {
  //send the parameters to the camera to set specific effect or others parameters
  //      mCamera->setParameters(params);
  //}
  LOGV ("GstMediaRecorder  max duration %lld max file size %lld", mMaxDuration,
      mMaxFileSize);
  return OK;
}

status_t
GstMediaRecorder::setListener (const sp < IMediaPlayerClient > &listener)
{
  LOGV ("GstMediaRecorder setListener");
  mListener = listener;
  return OK;
}

status_t
GstMediaRecorder::prepare ()
{
  LOGV ("GstMediaRecorder prepare");

  // create a camera if the app didn't supply one
  if ((mCamera == 0) && (mUse_video_src == TRUE)) {
    mCamera = Camera::connect ();
  }

  if (mCamera != NULL && mSurface != NULL) {
    LOGV ("GstMediaRecorder set preview display surface");
    mCamera->setPreviewDisplay (mSurface);
  }

  if (mCamera != NULL) {
    LOGV ("GstMediaRecorder set camera parameters width=%d height=%d fps=%d",
        mWidth, mHeight, mFps);
    String8 s = mCamera->getParameters ();
    CameraParameters p (s);
    p.setPreviewSize (mWidth, mHeight);

    if (mCamera->previewEnabled ()) {
      s = p.flatten ();
      mCamera->setParameters (s);
      mFlags |= FLAGS_HOT_CAMERA;
      LOGV ("GstMediaRecorder preview camera already enabled");
    } else {
      p.setPreviewFrameRate (mFps);
      s = p.flatten ();
      mCamera->setParameters (s);
      mCamera->startPreview ();
      mFlags &= ~FLAGS_HOT_CAMERA;
    }
  }

  return build_record_graph ();
}

typedef struct
{
  sp < IMemory > frame;
  sp < Camera > camera;
} record_callback_cookie;

static void
video_frame_release (GstICBVideoBuffer * buffer)
{
  //LOGE("GstMediaRecorder video frame release");

  record_callback_cookie *cookie = (record_callback_cookie *) (buffer->ctx);

  cookie->camera->releaseRecordingFrame (cookie->frame);

  cookie->frame.clear ();

  g_free (cookie);
}

/*static*/ void
GstMediaRecorder::record_callback (const sp < IMemory > &frame, void *cookie)
{
  ssize_t offset = 0;
  size_t size = 0;
  video_frame_t video_frame = VIDEO_FRAME_INIT;
  GstBuffer *buffer;
  GstClockTime duration;

  //LOGE("GstMediaRecorder        record callback");
  record_callback_cookie *lcookie = g_new0 (record_callback_cookie, 1);
  sp < IMemoryHeap > heap = frame->getMemory (&offset, &size);

  GstMediaRecorder *mediarecorder = (GstMediaRecorder *) cookie;
  if (mediarecorder->mVideoSrc == NULL) {
    LOGV ("GstMediaRecorder record_callback the videosrc don't exist");
    mediarecorder->mCamera->stopRecording ();
    return;
  }

  video_frame.pmem_fd = heap->getHeapID ();
  video_frame.pmem_offset = offset;
  video_frame.pmem_size = size;

  lcookie->frame = frame;
  lcookie->camera = mediarecorder->mCamera;

  buffer =
      gst_icbvideo_buffer_new (&video_frame,
      (GstMiniObjectFinalizeFunction) video_frame_release, lcookie,
      GST_ELEMENT (mediarecorder->mVideoSrc));

  GST_BUFFER_SIZE (buffer) = size;      //needed to build correct timestamp in basesrc

  duration = gst_util_uint64_scale_int (GST_SECOND, 1, mediarecorder->mFps);
  GST_BUFFER_DURATION (buffer) = duration;      //needed to build correct duration in basesrc

  gst_app_src_push_buffer (GST_APP_SRC (mediarecorder->mVideoSrc), buffer);
}

GstStateChangeReturn
GstMediaRecorder::wait_for_set_state (int timeout_msec)
{
  GstMessage *msg;
  GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;

  /* Wait for state change */
  msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (mPipeline), timeout_msec * GST_MSECOND,    /* in nanosec */
      (GstMessageType) (GST_MESSAGE_ERROR | GST_MESSAGE_ASYNC_DONE));

  if (msg) {
    if ((GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ASYNC_DONE))
      ret = GST_STATE_CHANGE_SUCCESS;

    gst_message_unref (msg);
  }

  return ret;
}

status_t
GstMediaRecorder::start ()
{
  GstStateChangeReturn ret;
  LOGV ("GstMediaRecorder start recording");

  if (mPipeline == NULL) {
    LOGV ("GstMediaRecorder start pipeline not created");
    return OK;
  }

  ret = gst_element_set_state (mPipeline, GST_STATE_PLAYING);

  // set the audio source device, open micro
  const sp < IAudioFlinger > &audioFlinger = AudioSystem::get_audio_flinger ();
  if (audioFlinger != 0) {
    LOGV ("GstMediaRecorder start recording: unmute the microphone");
    audioFlinger->setMicMute (FALSE);
  }

  if (mCamera != NULL) {
    mCamera->setListener (mCameraListener);
    mCamera->startRecording ();
  }

  if (ret == GST_STATE_CHANGE_ASYNC) {
    ret = wait_for_set_state (2000);    // wait 2 second for state change
  }

  if (ret != GST_STATE_CHANGE_SUCCESS) {
    goto bail;
  }

  LOGV ("GstMediaRecorder pipeline is in playing state");
  return OK;

bail:

  LOGV ("GstMediaRecorder start failed");

  if (mCamera != NULL) {
    mCamera->stopRecording ();
  }

  release_pipeline ();

  return OK;                    // return OK to avoid execption in java
}

status_t
GstMediaRecorder::stop ()
{
  LOGV ("GstMediaRecorder stop recording");

  if (mPipeline == NULL) {
    LOGV ("GstMediaRecorder stop pipeline not created");
    return OK;
  }

  if (mCamera != NULL) {
    mCamera->stopRecording ();
    mCamera->setListener (NULL);
  }

  /* Send audio & video Eos */
  sendEos ();

  if (mIsEos)
    g_mutex_lock (mEOSlock);

  // EOS has been receive now release the pipeline 
  return release_pipeline ();

}

status_t
GstMediaRecorder::release_pipeline ()
{
  if (mPipeline == NULL) {
    return OK;
  }

  LOGV ("GstMediaRecorder change pipeline state to NULL");
  gst_element_set_state (mPipeline, GST_STATE_NULL);
  gst_element_get_state (mPipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  LOGV ("GstMediaRecorder unref pipeline");
  gst_object_unref (mPipeline);
  mPipeline = NULL;
  mVideoBin = NULL;
  mAudioBin = NULL;
  mVideoSrc = NULL;

  if (mOutFilePath_fd > -1) {
    ::close (mOutFilePath_fd);
    mOutFilePath_fd = -1;
  }


  LOGV ("GstMediaRecorder stop exit");

  return OK;
}

status_t
GstMediaRecorder::close ()
{
  LOGV ("GstMediaRecorder close");

  return OK;
}

status_t
GstMediaRecorder::reset ()
{
  LOGV ("GstMediaRecorder reset");
  release_pipeline ();

  return OK;
}

status_t
GstMediaRecorder::getMaxAmplitude (int *max)
{
  int ioParam;

  LOGV ("GstMediaRecorder getMaxAmplitude");

  ioParam = 5;                  // device C0
  if (snd_hwdep_ioctl (mHwdep_handle, ASND_HWDEP_IOCTL_GET_MAX_AMP,
          (void *) &ioParam) < 0) {
    LOGE ("error : get max amplitude returned %d\n", errno);
    *max = 0;
    return OK;
  }
  *max = ioParam;

  return OK;
}

// create a video bin appsrc->icbvideoenc->capsfilter
GstElement *
GstMediaRecorder::create_video_bin ()
{
  GstElement *vbin;
  GstElement *video_src;
  GstElement *video_encoder, *video_format_filter;
  GstElement *video_queue;
  GstPad *pad;

  video_queue = NULL;

  if (mUse_video_src == FALSE) {
    // nothing the create in this case
    return NULL;
  }

  LOGV ("GstMediaRecorder create_video_bin");

  LOGV ("GstMediaRecorder create video appsrc");
  video_src = gst_element_factory_make ("appsrc", "videosrc");
  if (!video_src) {
    LOGV ("GstMediaRecorder can't create video src");
    return NULL;
  }

  g_object_set (G_OBJECT (video_src), "is-live", TRUE, NULL);   // it is a pseudo live source
  g_object_set (G_OBJECT (video_src), "max-bytes", (guint64) mWidth * mHeight * 3, NULL);       // max byte limit equal to 2 frames
  g_object_set (G_OBJECT (video_src), "format", 2, NULL);       // byte format 
  g_object_set (G_OBJECT (video_src), "block", true, NULL);     // Block push-buffer when max-bytes are queued

  g_object_set (G_OBJECT (video_src), "caps",
      gst_caps_new_simple ("video/x-raw-yuv",
          "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('N', 'V', '1', '2'),
          "width", G_TYPE_INT, mWidth,
          "height", G_TYPE_INT, mHeight,
          "framerate", GST_TYPE_FRACTION, mFps, 1, NULL), NULL);


  video_encoder = gst_element_factory_make ("icbvideoenc", NULL);

  if (!video_encoder) {
    LOGE ("GstMediaRecorder can't create video encoder");
    goto remove_video_src;
  }

  video_format_filter = gst_element_factory_make ("capsfilter", NULL);

  if (!video_format_filter) {
    LOGE ("GstMediaRecorder can't create video format filter");
    goto remove_video_encoder;
  }

  switch (mVideo_encoder) {
    case VIDEO_ENCODER_DEFAULT:
    case VIDEO_ENCODER_MPEG_4_SP:
      LOGV ("GstMediaRecorder set video caps: video/mpeg, width=%d, height=%d, framerate=%d/1", mWidth, mHeight, mFps);
      g_object_set (G_OBJECT (video_format_filter), "caps",
          gst_caps_new_simple ("video/mpeg",
              "width", G_TYPE_INT, mWidth, "height", G_TYPE_INT, mHeight,
//VT                                                                                                             "framerate", GST_TYPE_FRACTION, mFps, 1,
              "mpegversion", G_TYPE_INT, 4, NULL), NULL);
      break;
    case VIDEO_ENCODER_H264:
      LOGV ("GstMediaRecorder can't encode in h264");
      goto remove_video_format_filter;
    case VIDEO_ENCODER_H263:
    default:
      LOGV ("GstMediaRecorder set video caps: video/x-h263, width=%d, height=%d, framerate=%d/1", mWidth, mHeight, mFps);
      g_object_set (G_OBJECT (video_format_filter), "caps",
          gst_caps_new_simple ("video/x-h263",
              "width", G_TYPE_INT, mWidth, "height", G_TYPE_INT, mHeight,
//VT                                                                                            "framerate", GST_TYPE_FRACTION, mFps, 1,
              NULL), NULL);
      break;
  }


  /* VT support */
  {
    GValue framerate = { 0 };
    int framerate_num = mFps;
    int framerate_denom = 1;
    int bitrate = mVideoBitrate;
    int i_period = mIPeriod;
    int i_mb_refresh = mIMBRefreshMode;
    int vt_mode = mVTMode;

    if (vt_mode) {
      g_object_set (G_OBJECT (video_encoder), "vt-mode", vt_mode, NULL);
    }
    if (bitrate) {
      g_object_set (G_OBJECT (video_encoder), "bitrate", bitrate, NULL);
    }
    if (i_period) {
      /* in seconds, convert to nb of frames */
      i_period = i_period * framerate_num / framerate_denom;
      g_object_set (G_OBJECT (video_encoder), "i-period", i_period, NULL);
    }
    if (i_mb_refresh) {
      g_object_set (G_OBJECT (video_encoder), "i-mb-refresh", i_mb_refresh,
          NULL);
    }

    /* ! take care of framerate because of fraction type, 
       use g_object_set_property with a gvalue instead g_object_set */
    g_value_init (&framerate, GST_TYPE_FRACTION);
    gst_value_set_fraction (&framerate, framerate_num, framerate_denom);
    g_object_set_property (G_OBJECT (video_encoder), "framerate", &framerate);
    g_value_unset (&framerate);
  }

  video_queue = gst_element_factory_make ("queue", NULL);
  g_object_set (G_OBJECT (video_queue), "max-size-time", 2000000000, NULL);


  LOGV ("GstMediaRecorder create vbin");
  vbin = gst_bin_new ("vbin");
  if (!vbin) {
    LOGE ("GstMediaRecorder can't create vbin");
    goto remove_video_format_filter;
  }

  gst_bin_add_many (GST_BIN_CAST (vbin), video_src, video_encoder,
      video_format_filter, video_queue, NULL);

  LOGV ("GstMediaRecorder link video_src->->queue->video_encoder->video_format_filter->queue");
  if (!gst_element_link_many (video_src, video_encoder, video_format_filter,
          video_queue, NULL)) {
    LOGE ("GstMediaRecorder can't link elements");
    goto remove_vbin;
  }

  LOGV ("GstMediaRecorder create src ghost pad in vbin");
  pad = gst_element_get_static_pad (video_queue, "src");
  gst_element_add_pad (vbin, gst_ghost_pad_new ("src", pad));
  gst_object_unref (pad);

  mVideoSrc = video_src;

  return vbin;

remove_vbin:
  gst_object_unref (vbin);
remove_video_format_filter:
  gst_object_unref (video_format_filter);
  gst_object_unref (video_queue);
remove_video_encoder:
  gst_object_unref (video_encoder);
remove_video_src:
  gst_object_unref (video_src);
  return NULL;
}

// create a audio bin icbaudiosrc->icbaudioenc->capsfilter
GstElement *
GstMediaRecorder::create_audio_bin ()
{
  GstElement *abin;
  GstElement *audio_src, *audio_enc, *audio_format_filter;
  GstElement *audio_queue;
  GstPad *pad;
  gint recordsrc;

  if (mUse_audio_src == FALSE) {
    return NULL;
  }
  LOGV ("GstMediaRecorder create_audio_bin");

  LOGV ("GstMediaRecorder create audio src");
  audio_src = gst_element_factory_make ("icbaudiosrc", "icbaudiosrc0"); // do not change the element name

  if (!audio_src) {
    LOGE ("GstMediaRecorder can't create audio source");
    return NULL;
  }
  // set the audio source device
  LOGV ("GstMediaRecorder set device to audio src");
  g_object_set (G_OBJECT (audio_src), "device", "C0", NULL);

  // set the record source
  LOGV ("GstMediaRecorder set record src");
  recordsrc = mAudio_source;
  if (recordsrc > 0)
    recordsrc--;
  g_object_set (G_OBJECT (audio_src), "recordsrc", recordsrc, NULL);

  LOGV ("GstMediaRecorder create audio encoder");
  audio_enc = gst_element_factory_make ("icbaudioenc", "icbaudioenc0"); // do not change the element name

  if (!audio_enc) {
    LOGE ("GstMediaRecorder can't create audio encoder");
    goto remove_audio_src;
  }
//      g_object_set(G_OBJECT(audio_enc),"bitrate", mAudioBitrate, NULL);
//      g_object_set(G_OBJECT(audio_enc),"channel", mAudioChannels, NULL);      
//      g_object_set(G_OBJECT(audio_enc),"freq", mAudioSampleRate, NULL);

  // configure audio encoder
  LOGV ("GstMediaRecorder set properties to audio encoder");
  switch (mAudio_encoder) {
    case AUDIO_ENCODER_AMR_WB:
      // configure audio encoder for AMR-WB 
      LOGV ("GstMediaRecorder set properties to audio encoder for AMR_WB");
      if ((mOutput_format == OUTPUT_FORMAT_RAW_AMR)
          && (mUse_video_src == FALSE)) {
        // in AMR RAW format we will not have muxer after audio encoder so use the amr storage format
        g_object_set (G_OBJECT (audio_enc), "stream-type", 2, "format", 2,
            "bitrate", (gint64) (23850), "freq", (gint) 16000, "channel", 1,
            (gchar *) NULL);
      } else {
        g_object_set (G_OBJECT (audio_enc), "stream-type", 2, "format", 3,
            "bitrate", (gint64) (23850), "freq", (gint) 16000, "channel", 1,
            (gchar *) NULL);
      }
      audio_format_filter = gst_element_factory_make ("capsfilter", NULL);
      g_object_set (G_OBJECT (audio_format_filter), "caps",
          gst_caps_new_simple ("audio/AMR-WB",
              "rate", G_TYPE_INT, 16000,
              "channels", G_TYPE_INT, 1, NULL), NULL);
      break;
    case AUDIO_ENCODER_AAC:
    case AUDIO_ENCODER_AAC_PLUS:
    case AUDIO_ENCODER_EAAC_PLUS:
      // configure audio encoder for AAC 
      LOGV ("GstMediaRecorder set properties to audio encoder for AAC");
      g_object_set (G_OBJECT (audio_enc), "stream-type", 3, "format", 1,
          "bitrate", (gint64) (16000), "freq", (gint) 32000, "channel", 2,
          (gchar *) NULL);
      audio_format_filter = gst_element_factory_make ("capsfilter", NULL);
      g_object_set (G_OBJECT (audio_format_filter), "caps",
          gst_caps_new_simple ("audio/mpeg",
              "mpegversion", G_TYPE_INT, 4,
              "rate", G_TYPE_INT, 32000,
              "channels", G_TYPE_INT, 2, NULL), NULL);
      break;
    case AUDIO_ENCODER_DEFAULT:
    case AUDIO_ENCODER_AMR_NB:
    default:
      // configure audio encoder for AMR-NB 
      LOGV ("GstMediaRecorder set properties to audio encoder for AMR_NB");
      if ((mOutput_format == OUTPUT_FORMAT_RAW_AMR)
          && (mUse_video_src == FALSE)) {
        // in AMR RAW format we will not have muxer after audio encoder so use the amr storage format
        g_object_set (G_OBJECT (audio_enc), "stream-type", 1, "format", 2,
            "bitrate", (gint64) (12200), "freq", (gint) 8000, "channel", 1,
            (gchar *) NULL);
      } else {
        g_object_set (G_OBJECT (audio_enc), "stream-type", 1, "format", 3,
            "bitrate", (gint64) (12200), "freq", (gint) 8000, "channel", 1,
            (gchar *) NULL);
      }
      audio_format_filter = gst_element_factory_make ("capsfilter", NULL);
      g_object_set (G_OBJECT (audio_format_filter), "caps",
          gst_caps_new_simple ("audio/AMR",
              "rate", G_TYPE_INT, 8000, "channels", G_TYPE_INT, 1, NULL), NULL);
      break;
  }

  audio_queue = gst_element_factory_make ("queue", "audio_queue");
  g_object_set (G_OBJECT (audio_queue), "max-size-time", 2000000000, NULL);

  LOGV ("GstMediaRecorder create audio bin");
  abin = gst_bin_new ("abin");

  if (!abin) {
    LOGE ("GstMediaRecorder can't create abin");
    goto remove_audio_enc;
  }

  LOGV ("GstMediaRecorder add element to audio bin");
  gst_bin_add_many (GST_BIN_CAST (abin), audio_src, audio_enc,
      audio_format_filter, audio_queue, NULL);

  LOGV ("GstMediaRecorder link audio_src->audio_enc");
  if (!gst_element_link_many (audio_src, audio_enc, audio_format_filter,
          audio_queue, NULL)) {
    LOGE ("GstMediaRecorder can't link audio_src->audio_enc");
    goto remove_abin;
  }

  LOGV ("GstMediaRecorder create src ghost pad in abin");
  pad = gst_element_get_static_pad (audio_queue, "src");
  gst_element_add_pad (abin, gst_ghost_pad_new ("src", pad));
  gst_object_unref (pad);

  mAudioSrc = audio_src;
  return abin;

remove_abin:
  gst_object_unref (abin);
remove_audio_enc:
  gst_object_unref (audio_format_filter);
  gst_object_unref (audio_queue);
  gst_object_unref (audio_enc);
remove_audio_src:
  gst_object_unref (audio_src);
  return NULL;
}

/*static*/ GstBusSyncReply
GstMediaRecorder::bus_message (GstBus * bus, GstMessage * msg, gpointer data)
{
  GstMediaRecorder *mediarecorder = (GstMediaRecorder *) data;
  if (bus) {
    // do nothing except remove compilation warning
  }


  switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_EOS:{
      LOGV ("GstMediaRecorder bus receive message EOS");
      /* unlock mutex  */
      g_mutex_unlock (mediarecorder->mEOSlock);
      break;
    }
    case GST_MESSAGE_ERROR:{
      GError *err;
      gchar *debug;

      gst_message_parse_error (msg, &err, &debug);
      LOGE ("GstMediaRecorder bus receive message ERROR %d: %s from %s",
          err->code, err->message, debug);

      if (mediarecorder->mListener != NULL) {
        mediarecorder->mListener->notify (MEDIA_RECORDER_EVENT_ERROR,
            MEDIA_RECORDER_ERROR_UNKNOWN, err->code);
      }
      g_error_free (err);
      g_free (debug);
      break;
    }
    default:
      // do nothing
      break;
  }

  return GST_BUS_PASS;
}

void
GstMediaRecorder::sendEos ()
{
  if (!mIsEos) {
    LOGV ("GstMediaRecorder : forcing EOS");

    // only sen EOS once
    mIsEos = TRUE;

    /* stop audio recording */
    if (mAudioSrc != NULL) {
      /* send EOS */
      g_object_set (G_OBJECT (mAudioSrc), "eos", TRUE, NULL);

      /* reset mAudioSrc (will avoid to send another eos upon stop request */
      mAudioSrc = NULL;
    }

    /* stop video recording */
    if (mVideoSrc != NULL) {
      /* send EOS */
      gst_app_src_end_of_stream (GST_APP_SRC (mVideoSrc));

      /* reset mVideoSrc (will avoid to send another eos upon stop request */
      mVideoSrc = NULL;
    }
  }
}



/*static*/ void
GstMediaRecorder::handoff (GstElement * object, GstBuffer * buffer,
    gpointer user_data)
{
  GstMediaRecorder *mediarecorder = (GstMediaRecorder *) user_data;
  gulong microsecond;
  int sizeMargin = 0;
  mediarecorder->mCurrentFileSize += GST_BUFFER_SIZE (buffer);

  if (mediarecorder->mTimer == NULL) {
    mediarecorder->mTimer = g_timer_new ();
  }
  //LOGE("GstMediaRecorder handoff current file size %lld duration %lld", mediarecorder->mCurrentFileSize, (gint64)g_timer_elapsed(mediarecorder->mTimer, &microsecond)*1000);

  if ((mediarecorder->mMaxDuration != -1)
      && (mediarecorder->mMaxDuration <=
          (gint64) (g_timer_elapsed (mediarecorder->mTimer,
                  &microsecond) * 1000))) {
    LOGV ("GstMediaRecorder reached recording time limit");
    if (mediarecorder->mListener != NULL) {
      mediarecorder->mListener->notify (MEDIA_RECORDER_EVENT_INFO,
          MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
    }
    /* Send audio & video Eos */
    mediarecorder->sendEos ();

    g_object_set (object, "signal-handoffs", FALSE, NULL);
    return;
  }

  /* consider a margin before stopping (because we will still get data to flush the pipeline */
  if (mediarecorder->mAudioSrc != NULL)
    sizeMargin += 3000;         /* 3kB for Audio recording */

  if (mediarecorder->mVideoSrc != NULL)
    sizeMargin += 50000;        /* 50kB for video recording */

  if ((mediarecorder->mMaxFileSize != -1)
      && (mediarecorder->mMaxFileSize <=
          mediarecorder->mCurrentFileSize + sizeMargin)) {
    LOGV ("GstMediaRecorder reached recording size limit");
    if (mediarecorder->mListener != NULL) {
      mediarecorder->mListener->notify (MEDIA_RECORDER_EVENT_INFO,
          MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
    }
    /* Send audio & video Eos */
    mediarecorder->sendEos ();

    g_object_set (object, "signal-handoffs", FALSE, NULL);
    return;
  }
}

/*static*/ void
GstMediaRecorder::debug_log (GstDebugCategory * category, GstDebugLevel level,
    const gchar * file, const gchar * function, gint line,
    GObject * object, GstDebugMessage * message, gpointer data)
{
  gint pid;
  GstClockTime elapsed;
  GstMediaRecorder *mediarecorder = (GstMediaRecorder *) data;

  GST_UNUSED (file);
  GST_UNUSED (object);

  if (level > gst_debug_category_get_threshold (category))
    return;

  pid = getpid ();

  elapsed = GST_CLOCK_DIFF (mediarecorder->mGst_info_start_time,
      gst_util_get_timestamp ());


  g_printerr ("%" GST_TIME_FORMAT " %5d %s %s %s:%d %s\r\n",
      GST_TIME_ARGS (elapsed),
      pid,
      gst_debug_level_get_name (level),
      gst_debug_category_get_name (category), function, line,
      gst_debug_message_get (message));
}



status_t
GstMediaRecorder::build_record_graph ()
{
  GstElement *muxer, *identity, *sink;
  GstBus *bus;
  GError *err = NULL;
  int argc = 3;
  char **argv;
  char str0[] = "";
  //char str1[] =  "";
  char str2[] = "";
  char trace[PROPERTY_VALUE_MAX];

  argv = (char **) malloc (sizeof (char *) * argc);
  argv[0] = (char *) malloc (sizeof (char) * (strlen (str0) + 1));
  argv[2] = (char *) malloc (sizeof (char) * (strlen (str2) + 1));
  strcpy (argv[0], str0);
  strcpy (argv[2], str2);

  char value[PROPERTY_VALUE_MAX];
  property_get ("persist.gst.debug", value, "0");
  LOGV ("persist.gst.debug property %s", value);
  argv[1] = (char *) malloc (sizeof (char) * (strlen (value) + 1));
  strcpy (argv[1], value);

  property_get ("persist.gst.trace", trace, "/dev/console");
  LOGV ("persist.gst.trace property %s", trace);
  LOGV ("route the trace to %s", trace);
  int fd_trace = open (trace, O_RDWR);
  if (fd_trace != 1) {
    dup2 (fd_trace, 0);
    dup2 (fd_trace, 1);
    dup2 (fd_trace, 2);
    ::close (fd_trace);
  }

  mGst_info_start_time = gst_util_get_timestamp ();
  gst_debug_remove_log_function (debug_log);
  gst_debug_add_log_function (debug_log, this);
  gst_debug_remove_log_function (gst_debug_log_default);

  LOGV ("GstMediaRecorder gstreamer init check");
  // init gstreamer       
  if (!gst_init_check (&argc, &argv, &err)) {
    LOGE ("GstMediaRecorder Could not initialize GStreamer: %s\n",
        err ? err->message : "unknown error occurred");
    if (err) {
      g_error_free (err);
    }
  }

  LOGV ("GstMediaRecorder create pipeline");
  mPipeline = gst_pipeline_new (NULL);
  if (!mPipeline) {
    LOGE ("GstMediaRecorder can't create pipeline");
    goto bail;
  }
  // verbose info (as gst-launch -v)
  // Activate the trace with the command: "setprop persist.gst.verbose 1"
  property_get ("persist.gst.verbose", value, "0");
  LOGV ("persist.gst.verbose property = %s", value);
  if (value[0] == '1') {
    LOGV ("Activate deep_notify");
    g_signal_connect (mPipeline, "deep_notify",
        G_CALLBACK (gst_object_default_deep_notify), NULL);
  }

  LOGV ("GstMediaRecorder register bus callback");
  bus = gst_pipeline_get_bus (GST_PIPELINE (mPipeline));
  gst_bus_set_sync_handler (bus, bus_message, this);
  gst_object_unref (bus);

  if ((mOutput_format == OUTPUT_FORMAT_RAW_AMR) && (mUse_video_src == FALSE)) {
    // in RAW AMR format don't use any muxer
    LOGV ("GstMediaRecorder use identity as muxer in RAW_AMR format");
    muxer = gst_element_factory_make ("identity", NULL);
  } else {
    LOGV ("GstMediaRecorder use gppmux");
    muxer = gst_element_factory_make ("gppmux", NULL);
  }

  if (!muxer) {
    LOGE ("GstMediaRecorder can't create muxer");
    goto bail1;
  }

  gst_bin_add (GST_BIN_CAST (mPipeline), muxer);

  LOGV ("GstMediaRecorder create sink from uri %s", mOutFilePath);
  sink = gst_element_make_from_uri (GST_URI_SINK, mOutFilePath, NULL);
  if (!sink) {
    LOGE ("GstMediaRecorder can't create sink %s", mOutFilePath);
    goto bail1;
  }

  g_object_set (G_OBJECT (sink), "async", FALSE, NULL);

  gst_bin_add (GST_BIN_CAST (mPipeline), sink);

  LOGV ("GstMediaRecorder create identity");
  identity = gst_element_factory_make ("identity", NULL);
  if (!identity) {
    LOGE ("GstMediaRecorder can't identity element");
    goto bail1;
  }
  gst_bin_add (GST_BIN_CAST (mPipeline), identity);

  mCurrentFileSize = 0;
  g_signal_connect (identity, "handoff", G_CALLBACK (handoff), this);
  g_object_set (G_OBJECT (identity), "signal-handoffs", TRUE, NULL);

  mVideoBin = create_video_bin ();
  if (mVideoBin) {
    gst_bin_add (GST_BIN_CAST (mPipeline), mVideoBin);
    LOGV ("GstMediaRecorder link vbin to muxer");
    if (!gst_element_link (mVideoBin, muxer)) {
      LOGE ("GstMediaRecorder can't link vbin to muxer");
    }
  }

  mAudioBin = create_audio_bin ();
  if (mAudioBin) {
    gst_bin_add (GST_BIN_CAST (mPipeline), mAudioBin);
    LOGV ("GstMediaRecorder link abin to muxer");
    if (!gst_element_link (mAudioBin, muxer)) {
      LOGE ("GstMediaRecorder can't link abin to muxer");
    }
  }

  if (!mAudioBin && !mVideoBin) {
    LOGE ("GstMediaRecorder both audiobin and videobin are NULL !!!!!");
    goto bail1;
  }
  LOGV ("GstMediaRecorder link muxer->identity->sink");
  if (!gst_element_link_many (muxer, identity, sink, NULL)) {
    LOGE ("GstMediaRecorder can't link muxer->identity->sink");
  }


  gst_element_set_state (mPipeline, GST_STATE_READY);
  gst_element_get_state (mPipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  return OK;

bail1:
  LOGV ("GstMediaRecorder change pipeline state to NULL");
  gst_element_set_state (mPipeline, GST_STATE_NULL);
  gst_element_get_state (mPipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  LOGV ("GstMediaRecorder  unref pipeline");
  gst_object_unref (mPipeline);
bail:
  mPipeline = NULL;
  mVideoBin = NULL;
  mAudioBin = NULL;
  mVideoSrc = NULL;
  return UNKNOWN_ERROR;
}

void
GstMediaRecorder::postDataTimestamp (nsecs_t timestamp, int32_t msgType,
    const sp < IMemory > &dataPtr)
{
  ssize_t offset = 0;
  size_t size = 0;
  video_frame_t video_frame = VIDEO_FRAME_INIT;
  GstBuffer *buffer;
  GstClockTime duration;
  GST_UNUSED (timestamp);
  GST_UNUSED (msgType);

  //LOGV("postDataTimestamp");
  record_callback_cookie *lcookie = g_new0 (record_callback_cookie, 1);
  sp < IMemoryHeap > heap = dataPtr->getMemory (&offset, &size);

  if (mVideoSrc == NULL) {
    LOGE (" record_callback the videosrc don't exist");
    mCamera->stopRecording ();
    return;
  }

  video_frame.pmem_fd = heap->getHeapID ();
  video_frame.pmem_offset = offset;
  video_frame.pmem_size = size;

  lcookie->frame = dataPtr;
  lcookie->camera = mCamera;

  buffer =
      gst_icbvideo_buffer_new (&video_frame,
      (GstMiniObjectFinalizeFunction) video_frame_release, lcookie,
      GST_ELEMENT (mVideoSrc));
  GST_BUFFER_SIZE (buffer) = size;      //needed to build correct timestamp in basesrc
  GST_BUFFER_TIMESTAMP (buffer) = timestamp;

  /* Last frame of video not encoded */
  duration = gst_util_uint64_scale_int (GST_SECOND, 1, mFps);
  GST_BUFFER_DURATION (buffer) = duration;      //needed to build correct duration in basesrc

  gst_app_src_push_buffer (GST_APP_SRC (mVideoSrc), buffer);
}

// camera callback interface
void
AndroidGstCameraListener::postData (int32_t msgType,
    const sp < IMemory > &dataPtr)
{
  GST_UNUSED (msgType);
  GST_UNUSED (dataPtr);
}

void
AndroidGstCameraListener::postDataTimestamp (nsecs_t timestamp, int32_t msgType,
    const sp < IMemory > &dataPtr)
{
  //LOGI("AndroidGstCameraListener::postDataTimestamp %lld ns", timestamp);
  if ((mRecorder != NULL) && (msgType == CAMERA_MSG_VIDEO_FRAME)) {
    mRecorder->postDataTimestamp (timestamp, msgType, dataPtr);
  }
}