From fb7f58d4b318ef3760925f8d65b887b1a8d0adb6 Mon Sep 17 00:00:00 2001
From: Erik Zandvliet <s.p.the.ghost@gmail.com>
Date: Sun, 23 Jan 2011 13:55:21 +0100
Subject: [PATCH] First try on linking the boxes\n

---
 MP4/box_stsd.cpp  |  8 ++++----
 MP4/box_stsd.h    |  2 +-
 MP4/interface.cpp | 41 +++++++++++++++++++++++++++++++++++++++++
 MP4/main.cpp      |  4 +++-
 4 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/MP4/box_stsd.cpp b/MP4/box_stsd.cpp
index 9fa5a574..0e91122d 100644
--- a/MP4/box_stsd.cpp
+++ b/MP4/box_stsd.cpp
@@ -2,7 +2,7 @@
 
 Box_stsd::Box_stsd( ) {
   Container = new Box( 0x73747364 );
-  SetDefaults();
+  SetReserved();
 }
 
 Box_stsd::~Box_stsd() {
@@ -10,6 +10,7 @@ Box_stsd::~Box_stsd() {
 }
 
 Box * Box_stsd::GetBox() {
+  WriteContent();
   return Container;
 }
 
@@ -21,17 +22,16 @@ void Box_stsd::AddContent( Box * newcontent, uint32_t offset ) {
     delete Content[offset];
   }
   Content[offset] = newcontent;
-  WriteContent();
 }
 
-void Box_stsd::SetDefaults( ) {
+void Box_stsd::SetReserved( ) {
   Container->SetPayload((uint32_t)4,Box::uint32_to_uint8( 1 ),4);
   Container->SetPayload((uint32_t)4,Box::uint32_to_uint8( 0 ),0);
 }
 
 void Box_stsd::WriteContent( ) {
   Container->ResetPayload( );
-  SetDefaults( );
+  SetReserved( );
   Box * current;
   std::string serializedbox = "";
   for( uint32_t i = 0; i < Content.size(); i++ ) {
diff --git a/MP4/box_stsd.h b/MP4/box_stsd.h
index 287060de..3cc33356 100644
--- a/MP4/box_stsd.h
+++ b/MP4/box_stsd.h
@@ -11,7 +11,7 @@ class Box_stsd {
   private:
     Box * Container;
 
-    void SetDefaults();
+    void SetReserved();
     void WriteContent();
     std::vector<Box *> Content;
 };//Box_ftyp Class
diff --git a/MP4/interface.cpp b/MP4/interface.cpp
index 1abfe7cd..51d9c45b 100644
--- a/MP4/interface.cpp
+++ b/MP4/interface.cpp
@@ -71,3 +71,44 @@ Interface::~Interface() {
   if( moov ) { delete moov; moov = NULL; }
   if( ftyp ) { delete ftyp; ftyp = NULL; }
 }
+
+void Interface::link( ) {
+  //Linking Video Track
+  stsd_vide->AddContent(avcC_vide->GetBox());
+  stbl_vide->AddContent(stsd_vide->GetBox(),3);
+  stbl_vide->AddContent(stco_vide->GetBox(),2);
+  stbl_vide->AddContent(stsc_vide->GetBox(),1);
+  stbl_vide->AddContent(stts_vide->GetBox());
+  dref_vide->AddContent(url_vide->GetBox());
+  dinf_vide->AddContent(dref_vide->GetBox());
+  minf_vide->AddContent(stbl_vide->GetBox(),2);
+  minf_vide->AddContent(dinf_vide->GetBox(),1);
+  minf_vide->AddContent(vmhd_vide->GetBox());
+  mdia_vide->AddContent(minf_vide->GetBox(),2);
+  mdia_vide->AddContent(hdlr_vide->GetBox(),1);
+  mdia_vide->AddContent(mdhd_vide->GetBox());
+  trak_vide->AddContent(mdia_vide->GetBox(),1);
+  trak_vide->AddContent(tkhd_vide->GetBox());
+
+  //Linking Sound Track
+  stsd_soun->AddContent(esds_soun->GetBox());
+  stbl_soun->AddContent(stsd_soun->GetBox(),3);
+  stbl_soun->AddContent(stco_soun->GetBox(),2);
+  stbl_soun->AddContent(stsc_soun->GetBox(),1);
+  stbl_soun->AddContent(stts_soun->GetBox());
+  dref_soun->AddContent(url_soun->GetBox());
+  dinf_soun->AddContent(dref_soun->GetBox());
+  minf_soun->AddContent(stbl_soun->GetBox(),2);
+  minf_soun->AddContent(dinf_soun->GetBox(),1);
+  minf_soun->AddContent(smhd_soun->GetBox());
+  mdia_soun->AddContent(minf_soun->GetBox(),2);
+  mdia_soun->AddContent(hdlr_soun->GetBox(),1);
+  mdia_soun->AddContent(mdhd_soun->GetBox());
+  trak_soun->AddContent(mdia_soun->GetBox(),1);
+  trak_soun->AddContent(tkhd_soun->GetBox());
+
+  //Linking total file
+  moov->AddContent(trak_soun->GetBox(),2);
+  moov->AddContent(trak_vide->GetBox(),1);
+  moov->AddContent(mvhd->GetBox());
+}
diff --git a/MP4/main.cpp b/MP4/main.cpp
index 3f45a598..027ddb8d 100644
--- a/MP4/main.cpp
+++ b/MP4/main.cpp
@@ -4,7 +4,9 @@
 int main( ) {
   std::cout << "Creating Interface\n";
   Interface * file = new Interface();
-  std::cout << "Interface created, deleting it again\n";
+  std::cout << "Interface created, start linking them\n";
+  file->link();
+  std::cout << "Linking finished, deleting boxes\n";
   delete file;
   std::cout << "Interface deleted\n";
   return 0;