box.cpp herschreven voor snelheid en bugloosheid. Plus, coole edits in het algemeen.
This commit is contained in:
		
							parent
							
								
									c12c5d1f97
								
							
						
					
					
						commit
						95f62fde10
					
				
					 4 changed files with 27 additions and 69 deletions
				
			
		| 
						 | 
					@ -285,7 +285,7 @@ int mainHandler(int CONN_fd){
 | 
				
			||||||
                  FlashBuf.append(Audio_Init.data, Audio_Init.len);
 | 
					                  FlashBuf.append(Audio_Init.data, Audio_Init.len);
 | 
				
			||||||
                  FlashFirstAudio = false;
 | 
					                  FlashFirstAudio = false;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                #if DEBUG >= 4
 | 
					                #if DEBUG >= 5
 | 
				
			||||||
                fprintf(stderr, "Received a tag of type %2hhu and length %i\n", tag->data[0], tag->len);
 | 
					                fprintf(stderr, "Received a tag of type %2hhu and length %i\n", tag->data[0], tag->len);
 | 
				
			||||||
                #endif
 | 
					                #endif
 | 
				
			||||||
                FlashBuf.append(tag->data,tag->len);
 | 
					                FlashBuf.append(tag->data,tag->len);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,20 +10,22 @@ int main(){
 | 
				
			||||||
  HTTPReader H;
 | 
					  HTTPReader H;
 | 
				
			||||||
  FLV_Pack * F = 0;
 | 
					  FLV_Pack * F = 0;
 | 
				
			||||||
  unsigned int P = 0;
 | 
					  unsigned int P = 0;
 | 
				
			||||||
 | 
					  char * Payload = 0;
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  while (H.ReadSocket(stdin) || H.CleanForNext()){
 | 
					  while (H.ReadSocket(stdin) || H.CleanForNext()){
 | 
				
			||||||
    if (H.body.size() > 10000){
 | 
					    if (H.body.size() > 10000){
 | 
				
			||||||
      Box * TestBox = new Box((uint8_t*)H.body.c_str(), H.body.size());
 | 
					      Box * TestBox = new Box((uint8_t*)H.body.c_str(), H.body.size());
 | 
				
			||||||
      printf("First bytes: %2hhu %2hhu %2hhu %2hhu\n", ((char*)TestBox->Payload)[0], ((char*)TestBox->Payload)[1], ((char*)TestBox->Payload)[2], ((char*)TestBox->Payload)[3]);
 | 
					      Payload = (char*)TestBox->GetPayload();
 | 
				
			||||||
 | 
					      printf("First bytes: %2hhu %2hhu %2hhu %2hhu\n", Payload[0], Payload[1], Payload[2], Payload[3]);
 | 
				
			||||||
      P = 0;
 | 
					      P = 0;
 | 
				
			||||||
      while (TestBox->PayloadSize > P){
 | 
					      while (TestBox->GetPayloadSize() > P){
 | 
				
			||||||
        if (FLV_GetPacket(F, (char*)TestBox->Payload, TestBox->PayloadSize, P)){
 | 
					        if (FLV_GetPacket(F, Payload, TestBox->GetPayloadSize(), P)){
 | 
				
			||||||
          std::cout << "Got a " << F->len << " bytes " << F->tagType() << " FLV tag of time " << F->tagTime() << "." << std::endl;
 | 
					          std::cout << "Got a " << F->len << " bytes " << F->tagType() << " FLV tag of time " << F->tagTime() << "." << std::endl;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      delete TestBox;
 | 
					      delete TestBox;
 | 
				
			||||||
    }else{
 | 
					    }else{
 | 
				
			||||||
      std::cout << "Skipped too small fragment" << std::endl;
 | 
					      std::cout << "Skipped too small fragment of size " << H.body.size() << std::endl;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,11 +9,6 @@
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct BoxHeader {
 | 
					 | 
				
			||||||
  uint32_t TotalSize;
 | 
					 | 
				
			||||||
  uint32_t BoxType;
 | 
					 | 
				
			||||||
};//BoxHeader struct
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class Box {
 | 
					class Box {
 | 
				
			||||||
  public:
 | 
					  public:
 | 
				
			||||||
    Box();
 | 
					    Box();
 | 
				
			||||||
| 
						 | 
					@ -31,73 +26,48 @@ class Box {
 | 
				
			||||||
    static uint8_t * uint32_to_uint8( uint32_t data );
 | 
					    static uint8_t * uint32_to_uint8( uint32_t data );
 | 
				
			||||||
    static uint8_t * uint16_to_uint8( uint16_t data );
 | 
					    static uint8_t * uint16_to_uint8( uint16_t data );
 | 
				
			||||||
    static uint8_t * uint8_to_uint8( uint8_t data );
 | 
					    static uint8_t * uint8_to_uint8( uint8_t data );
 | 
				
			||||||
    BoxHeader GetHeader( );
 | 
					 | 
				
			||||||
    void ResetPayload( );
 | 
					    void ResetPayload( );
 | 
				
			||||||
 | 
					  private:
 | 
				
			||||||
    uint8_t * Payload;
 | 
					    uint8_t * Payload;
 | 
				
			||||||
    BoxHeader header;
 | 
					 | 
				
			||||||
    uint32_t PayloadSize;
 | 
					    uint32_t PayloadSize;
 | 
				
			||||||
};//Box Class
 | 
					};//Box Class
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Box::Box() {
 | 
					Box::Box() {
 | 
				
			||||||
  Payload = NULL;
 | 
					  Payload = (uint8_t *)malloc(8);
 | 
				
			||||||
  PayloadSize = 0;
 | 
					  PayloadSize = 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Box::Box(uint32_t BoxType) {
 | 
					Box::Box(uint32_t BoxType) {
 | 
				
			||||||
  header.BoxType = BoxType;
 | 
					  Payload = (uint8_t *)malloc(8);
 | 
				
			||||||
  Payload = NULL;
 | 
					  SetBoxType(BoxType);
 | 
				
			||||||
  PayloadSize = 0;
 | 
					  PayloadSize = 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Box::Box(uint8_t * Content, uint32_t length) {
 | 
					Box::Box(uint8_t * Content, uint32_t length) {
 | 
				
			||||||
  header.TotalSize = (Content[0] << 24) + (Content[1] << 16) + (Content[2] << 8) + (Content[3]);
 | 
					 | 
				
			||||||
  if(header.TotalSize != length) { std::cerr << "Warning: length sizes differ\n"; }
 | 
					 | 
				
			||||||
  header.BoxType = (Content[4] << 24) + (Content[5] << 16) + (Content[6] << 8) + (Content[7]);
 | 
					 | 
				
			||||||
  std::cerr << "Created new box with type \""
 | 
					 | 
				
			||||||
            << (char)(header.BoxType >> 24)
 | 
					 | 
				
			||||||
            << (char)((header.BoxType << 8) >> 24)
 | 
					 | 
				
			||||||
            << (char)((header.BoxType << 16) >> 24)
 | 
					 | 
				
			||||||
            << (char)((header.BoxType << 24) >> 24)
 | 
					 | 
				
			||||||
            << "\"\n";
 | 
					 | 
				
			||||||
  PayloadSize = length-8;
 | 
					  PayloadSize = length-8;
 | 
				
			||||||
  Payload = new uint8_t[PayloadSize];
 | 
					  Payload = (uint8_t *)malloc(length);
 | 
				
			||||||
  memcpy( Payload, &Content[8], PayloadSize );
 | 
					  memcpy(Payload, Content, length);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Box::~Box() {
 | 
					Box::~Box() {
 | 
				
			||||||
 | 
					  if (Payload) free(Payload);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Box::SetBoxType(uint32_t BoxType) {
 | 
					void Box::SetBoxType(uint32_t BoxType) {
 | 
				
			||||||
  header.BoxType = BoxType;
 | 
					  ((unsigned int*)Payload)[1] = htonl(BoxType);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint32_t Box::GetBoxType() {
 | 
					uint32_t Box::GetBoxType() {
 | 
				
			||||||
  return header.BoxType;
 | 
					  return ntohl(((unsigned int*)Payload)[1]);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Box::SetPayload(uint32_t Size, uint8_t * Data, uint32_t Index) {
 | 
					void Box::SetPayload(uint32_t Size, uint8_t * Data, uint32_t Index) {
 | 
				
			||||||
  uint8_t * tempchar = NULL;
 | 
					 | 
				
			||||||
  if ( Index + Size > PayloadSize ) {
 | 
					  if ( Index + Size > PayloadSize ) {
 | 
				
			||||||
    if ( Payload ) {
 | 
					 | 
				
			||||||
      tempchar = new uint8_t[PayloadSize];
 | 
					 | 
				
			||||||
      memcpy( tempchar, Payload, PayloadSize );
 | 
					 | 
				
			||||||
      delete Payload;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    PayloadSize = Index + Size;
 | 
					    PayloadSize = Index + Size;
 | 
				
			||||||
    Payload = new uint8_t[PayloadSize];
 | 
					    ((unsigned int*)Payload)[0] = htonl(PayloadSize+8);
 | 
				
			||||||
    if( tempchar ) {
 | 
					    Payload = (uint8_t *)realloc(Payload, PayloadSize + 8);
 | 
				
			||||||
      memcpy( Payload, tempchar, Index );
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      for(uint32_t i = 0; i < Index; i++) { Payload[i] = 0; }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    memcpy( &Payload[Index], Data, Size );
 | 
					 | 
				
			||||||
    header.TotalSize = PayloadSize + 8;
 | 
					 | 
				
			||||||
    if( tempchar ) {
 | 
					 | 
				
			||||||
      delete tempchar;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    memcpy( &Payload[Index], Data, Size );
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  memcpy(Payload + 8 + Index, Data, Size);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint32_t Box::GetPayloadSize() {
 | 
					uint32_t Box::GetPayloadSize() {
 | 
				
			||||||
| 
						 | 
					@ -105,29 +75,21 @@ uint32_t Box::GetPayloadSize() {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t * Box::GetPayload() {
 | 
					uint8_t * Box::GetPayload() {
 | 
				
			||||||
  uint8_t * temp = new uint8_t[PayloadSize];
 | 
					  return Payload+8;
 | 
				
			||||||
  memcpy( temp, Payload, PayloadSize );
 | 
					 | 
				
			||||||
  return temp;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t * Box::GetPayload(uint32_t Index, uint32_t & Size) {
 | 
					uint8_t * Box::GetPayload(uint32_t Index, uint32_t & Size) {
 | 
				
			||||||
  if(Index > PayloadSize) { return NULL; }
 | 
					  if(Index > PayloadSize) {Size = 0;}
 | 
				
			||||||
  if(Index + Size > PayloadSize) { Size = PayloadSize - Index; }
 | 
					  if(Index + Size > PayloadSize) { Size = PayloadSize - Index; }
 | 
				
			||||||
  uint8_t * temp = new uint8_t[Size - Index];
 | 
					  return Payload + 8 + Index;
 | 
				
			||||||
  memcpy( temp, &Payload[Index], Size - Index );
 | 
					 | 
				
			||||||
  return temp;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint32_t Box::GetBoxedDataSize() {
 | 
					uint32_t Box::GetBoxedDataSize() {
 | 
				
			||||||
  return header.TotalSize;
 | 
					  return ntohl(((unsigned int*)Payload)[0]);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t * Box::GetBoxedData( ) {
 | 
					uint8_t * Box::GetBoxedData( ) {
 | 
				
			||||||
  uint8_t * temp = new uint8_t[header.TotalSize];
 | 
					  return Payload;
 | 
				
			||||||
  memcpy( temp, uint32_to_uint8(header.TotalSize), 4 );
 | 
					 | 
				
			||||||
  memcpy( &temp[4], uint32_to_uint8(header.BoxType), 4 );
 | 
					 | 
				
			||||||
  memcpy( &temp[8], Payload, PayloadSize );
 | 
					 | 
				
			||||||
  return temp;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -153,15 +115,8 @@ uint8_t * Box::uint8_to_uint8( uint8_t data ) {
 | 
				
			||||||
   return temp;
 | 
					   return temp;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
BoxHeader Box::GetHeader( ) {
 | 
					 | 
				
			||||||
  return header;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void Box::ResetPayload( ) {
 | 
					void Box::ResetPayload( ) {
 | 
				
			||||||
  header.TotalSize -= PayloadSize;
 | 
					 | 
				
			||||||
  PayloadSize = 0;
 | 
					  PayloadSize = 0;
 | 
				
			||||||
  if(Payload) {
 | 
					  Payload = (uint8_t *)realloc(Payload, PayloadSize + 8);
 | 
				
			||||||
    delete Payload;
 | 
					  ((unsigned int*)Payload)[0] = htonl(0);
 | 
				
			||||||
    Payload = NULL;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -582,5 +582,6 @@ std::string Interface::mdatFold(std::string data){
 | 
				
			||||||
  std::string Result;
 | 
					  std::string Result;
 | 
				
			||||||
  mdat->SetContent((uint8_t*)data.c_str(), data.size());
 | 
					  mdat->SetContent((uint8_t*)data.c_str(), data.size());
 | 
				
			||||||
  Result.append((char*)mdat->GetBox()->GetBoxedData(), (int)mdat->GetBox()->GetBoxedDataSize());
 | 
					  Result.append((char*)mdat->GetBox()->GetBoxedData(), (int)mdat->GetBox()->GetBoxedDataSize());
 | 
				
			||||||
 | 
					  delete mdat;
 | 
				
			||||||
  return Result;
 | 
					  return Result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue