From 9369f5f08f5615cd76d7892f34ac506fb57000b7 Mon Sep 17 00:00:00 2001
From: Erik Zandvliet <erik.zandvliet@ddvtech.com>
Date: Sun, 31 Jul 2011 16:28:14 +0200
Subject: [PATCH] Debug commit

---
 Connector_TS/main.cpp | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/Connector_TS/main.cpp b/Connector_TS/main.cpp
index eb10db8d..c020898e 100644
--- a/Connector_TS/main.cpp
+++ b/Connector_TS/main.cpp
@@ -21,6 +21,8 @@ class Transport_Packet {
     void SetPesHeader( );
     void Write( );
     void Write( Socket::Connection conn );
+    void SetContinuityCounter( int Counter );
+    void SetMessageLength( int MsgLen );
   private:
     int PID;
     char Buffer[188];
@@ -34,6 +36,15 @@ Transport_Packet::Transport_Packet( bool NALUStart, int PID ) {
   Buffer[3] = (char)0x00;
 }
 
+void Transport_Packet::SetMessageLength( int MsgLen ) {
+  Buffer[8] = ( MsgLen & 0xFF00 ) >> 8;
+  Buffer[9] = ( MsgLen & 0xFF );
+}
+
+void Transport_Packet::SetContinuityCounter( int Counter ) {
+  Buffer[3] = (char)0x00 + ( Counter & 0x0F );
+}
+
 void Transport_Packet::SetPesHeader( ) {
   Buffer[4] = (char)0x00;
   Buffer[5] = (char)0x00;
@@ -52,19 +63,24 @@ void Transport_Packet::SetPayload( char * Payload, int PayloadLen, int Offset )
 }
 
 std::vector<Transport_Packet> WrapNalus( FLV::Tag tag ) {
+  static int ContinuityCounter = 0;
   Transport_Packet TS;
   int PacketAmount = ( ( tag.len - (188 - 17 ) ) / 184 ) + 2;
   std::cerr << "Wrapping a tag of length " << tag.len << " into " << PacketAmount << " TS Packet(s)\n";
   std::vector<Transport_Packet> Result;
   char LeadIn[4] = { (char)0x00, (char)0x00, (char)0x00, (char)0x01 };
   TS = Transport_Packet( true );
+  TS.SetContinuityCounter( ContinuityCounter );
+  ContinuityCounter = ( ( ContinuityCounter + 1 ) & 0x0F );
   TS.SetPesHeader( );
+  TS.SetMessageLength( tag.len - 16 );
   TS.SetPayload( LeadIn, 4, 13 );
   TS.SetPayload( &tag.data[16], 171, 17 );
   Result.push_back( TS );
   for( int i = 0; i < (PacketAmount - 1); i++ ) {
-//  std::cerr << i << "<" << (PacketAmount - 1) << "?  " << ( i < ( PacketAmount - 1 ) ) << "\n";
     TS = Transport_Packet( false );
+    TS.SetContinuityCounter( ContinuityCounter );
+    ContinuityCounter = ( ( ContinuityCounter + 1 ) & 0x0F );
     TS.SetPayload( &tag.data[187+(184*i)], 184, 4 );
     Result.push_back( TS );
   }
@@ -99,6 +115,7 @@ int TS_Handler( Socket::Connection conn ) {
       #endif
       inited = true;
     }
+    
     switch (ss.ready()){
       case -1:
         conn.close();
@@ -124,7 +141,9 @@ int TS_Handler( Socket::Connection conn ) {
             }
           }
           if( tag.data[ 0 ] == 0x08 ) {
-            fprintf(stderr, "Audio Tag Read\n");
+            if( ( tag.data[ 11 ] == 0xAF ) && ( tag.data[ 12 ] == 0x01 ) ) {
+              fprintf(stderr, "Audio Contains Raw AAC\n");
+            }
           }          
         }
         break;