am 51af4af2: am e28df5b9: Merge "Store correct time stamps in recorded mp4 files"

* commit '51af4af2414cdaaf0185b971a41fc0b4f8c0b7e4':
  Store correct time stamps in recorded mp4 files
diff --git a/include/media/stagefright/MPEG4Writer.h b/include/media/stagefright/MPEG4Writer.h
index cd4e129..3596b38 100644
--- a/include/media/stagefright/MPEG4Writer.h
+++ b/include/media/stagefright/MPEG4Writer.h
@@ -186,6 +186,8 @@
     void release();
     status_t reset();
 
+    static uint32_t getMpeg4Time();
+
     MPEG4Writer(const MPEG4Writer &);
     MPEG4Writer &operator=(const MPEG4Writer &);
 };
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index 86477ba..8b52e15 100755
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -317,11 +317,11 @@
     void writeDrefBox();
     void writeDinfBox();
     void writeDamrBox();
-    void writeMdhdBox(time_t now);
+    void writeMdhdBox(uint32_t now);
     void writeSmhdBox();
     void writeVmhdBox();
     void writeHdlrBox();
-    void writeTkhdBox(time_t now);
+    void writeTkhdBox(uint32_t now);
     void writeMp4aEsdsBox();
     void writeMp4vEsdsBox();
     void writeAudioFourCCBox();
@@ -821,8 +821,17 @@
     return err;
 }
 
-void MPEG4Writer::writeMvhdBox(int64_t durationUs) {
+uint32_t MPEG4Writer::getMpeg4Time() {
     time_t now = time(NULL);
+    // MP4 file uses time counting seconds since midnight, Jan. 1, 1904
+    // while time function returns Unix epoch values which starts
+    // at 1970-01-01. Lets add the number of seconds between them
+    uint32_t mpeg4Time = now + (66 * 365 + 17) * (24 * 60 * 60);
+    return mpeg4Time;
+}
+
+void MPEG4Writer::writeMvhdBox(int64_t durationUs) {
+    uint32_t now = getMpeg4Time();
     beginBox("mvhd");
     writeInt32(0);             // version=0, flags=0
     writeInt32(now);           // creation time
@@ -2473,7 +2482,7 @@
     ALOGV("%s track time scale: %d",
         mIsAudio? "Audio": "Video", mTimeScale);
 
-    time_t now = time(NULL);
+    uint32_t now = getMpeg4Time();
     mOwner->beginBox("trak");
         writeTkhdBox(now);
         mOwner->beginBox("mdia");
@@ -2686,7 +2695,7 @@
     mOwner->endBox();  // esds
 }
 
-void MPEG4Writer::Track::writeTkhdBox(time_t now) {
+void MPEG4Writer::Track::writeTkhdBox(uint32_t now) {
     mOwner->beginBox("tkhd");
     // Flags = 7 to indicate that the track is enabled, and
     // part of the presentation
@@ -2755,7 +2764,7 @@
     mOwner->endBox();
 }
 
-void MPEG4Writer::Track::writeMdhdBox(time_t now) {
+void MPEG4Writer::Track::writeMdhdBox(uint32_t now) {
     int64_t trakDurationUs = getDurationUs();
     mOwner->beginBox("mdhd");
     mOwner->writeInt32(0);             // version=0, flags=0