Esempio n. 1
0
void process_packet(unsigned char *arg, const struct pcap_pkthdr *pkthdr, const unsigned char *packetptr) {
	ProcessorData *processor;
	Packet packet;
	InPacket *bpkt;

	processor = (ProcessorData*)arg;

	parse_packet(processor, packetptr, &packet);

	bpkt = buffer_packet(&packet, pkthdr, packetptr, 0);
	packet_buffer_enqueue(&(processor->queues[processor->next_queue]), bpkt);

	processor->next_queue = (processor->next_queue + 1) % (processor->num_workers);
}
Esempio n. 2
0
void
TAO::DCPS::ReliableMulticast::detail::SenderLogic::send(
  const TAO::DCPS::ReliableMulticast::detail::Packet& p,
  PacketVector& delivered
  )
{
  delivered.clear();
  if (
    p.type_ == TAO::DCPS::ReliableMulticast::detail::Packet::DATA_INTERMEDIATE ||
    p.type_ == TAO::DCPS::ReliableMulticast::detail::Packet::DATA_END_OF_MESSAGE
    )
  {
    buffer_packet(p, delivered);
  }
}
Esempio n. 3
0
void
OpenDDS::DCPS::ReliableMulticast::detail::ReceiverLogic::receive(
  const Packet& p,
  PacketVector& nacks,
  PacketVector& delivered
  )
{
  delivered.clear();

  // todo: validate
  if (!seen_last_delivered_)
  {
    if (
      p.type_ == Packet::DATA_INTERMEDIATE ||
      p.type_ == Packet::DATA_END_OF_MESSAGE
      )
    {
      last_delivered_id_ = p.id_ - 1;
      seen_last_delivered_ = true;
    }
    else
    {
      return;
    }
  }

  if (
    p.type_ == Packet::DATA_INTERMEDIATE ||
    p.type_ == Packet::DATA_END_OF_MESSAGE ||
    p.type_ == Packet::DATA_NOT_AVAILABLE
    )
  {
    bool prior_nack_canceled = nacker_.cancel(p.id_);

    if (in_range(p.id_, 1, receiver_buffer_size_ + receiver_buffer_size_))
    {
      if (p.id_ == last_delivered_id_ + 1)
      {
        Packet tmp_packet;

        deliver(delivered, p);
        while (get_and_remove_buffered_packet(
          last_delivered_id_ + 1,
          tmp_packet
          ))
        {
          deliver(delivered, tmp_packet);
        }
      }
      else if (!is_buffered(p))
      {
        if (
          p.type_ == Packet::DATA_INTERMEDIATE ||
          p.type_ == Packet::DATA_END_OF_MESSAGE
          )
        {
          buffer_packet(p, delivered);
          if (!prior_nack_canceled)
          {
            nacker_.nack_range(find_previous_received(p.id_) + 1, p.id_);
          }
        }
        else if (p.type_ == Packet::DATA_NOT_AVAILABLE)
        {
          handle_unreliable_operation(delivered);
        }
      }
    }
  }
  else if (p.type_ == Packet::HEARTBEAT)
  {
    if (!in_range(p.id_, 0 - 2 * receiver_buffer_size_, 0))
    {
      // NACK the last packet, which will send it along and
      // then trigger the above NACK code...
      nacker_.nack_range(p.id_, p.id_ + 1);
    }
  }

  nacker_.get_nacks(nacks);
}
Esempio n. 4
0
/*
 * append_file()
 *
 * Opens the file "filename" for reading. Parses the FLV header and checks
 * everything looks normal. Then reads FLV data packets from the file
 * continuously in a loop until no more data can be read from the file.
 * The header of each FLV packet is parsed and the various fields stored in an
 * FLVpacket struct. Non-video or audio packets (e.g. metadata packets) are
 * skipped over and not parsed any further than the header. Video and audio
 * packets have their data payload and closing back pointer also stored in
 * memory.
 * If the starting timestamp for the current output file has already been
 * determined, write_packet() is then called to write the packet to the output
 * file. Otherwise the packets are buffered using buffer_packet() until the
 * first video packet has been read from the input file. The starting timestamp
 * is calculated based on the timestamp of the first video packet, the
 * video framerate, and the timestamp of the last video packet read from the
 * previous input file. The packet buffer is then flushed and operation
 * reverts to simply reading packets from input and writing to output.
 * When no more data can be read from the input file, it is closed and the
 * function returns.
 */
static void append_file(const char *filename, unsigned int mark_in, unsigned int mark_out)
{
    static unsigned char *buff;
    static size_t buffsize = 13;
    static char first_time = 1;
    static char metadata_extracted;
    long file_start_timestamp = -999999;
    long first_keyframe_timestamp = -1;
    unsigned int lastfile_video_timestamp = last_video_timestamp;
    unsigned char signature[] = { 'F', 'L', 'V' };
    FILE *infile;

    if(!quiet)
        fprintf(stderr, "Opening \"%s\"\n", filename);

    if( !(infile = fopen(filename, "rb")) )
    {
        fprintf(stderr, "ERROR while opening input file %s for reading: %s\n",
                filename, strerror(errno));
        return;
    }

    if( !buff )
        buff = malloc(buffsize);

    /* 9B = normal length of header */
    if( fread( buff, 1, 9, infile ) != 9 )
    {
        fprintf(stderr, "ERROR reading header from input file %s: %s\n",
                filename, strerror(errno));
        return;
    }
    if( memcmp(buff, signature, 3) == 0 )
    {
        /* This file has a header; we'll do some brief checks and then skip it */
        size_t header_length, extra_length;

        if( memcmp(buff, signature, 3) != 0 )
        {
            fprintf(stderr, "ERROR: Signature bytes \"FLV\" not present at start of file\n");
            exit(1);
        }

        if( buff[3] != 1 )
            fprintf(stderr, "WARNING: FLV version %d detected (only tested with v. 1)\n", buff[3]);

        if( !(buff[4] & 4) )
            fprintf(stderr, "WARNING: No audio stream present in input file\n");

        if( !(buff[4] & 1) )
            fprintf(stderr, "WARNING: No video stream present in input file\n");

        header_length = (size_t)conv_ui32(&buff[5]);
        if( (header_length + 4) > buffsize ) /* Add 4 to include 1st back-pointer */
        {
            buffsize = header_length + 4;
            buff = realloc( buff, buffsize );
        }
        extra_length = header_length - 9;
        fread( buff + 9, 1, extra_length + 4, infile );
    }
    else
        rewind(infile); /* It looks like the file contains raw FLV packets; rewind and start again. */

    while( !feof(infile) )
    {
        static struct FLVpacket packet;
        static size_t max_datasize = 0;
        size_t size;
        char key_frame = 0;

        /* Read the tag header (11 bytes) */
        size = fread( buff, 1, 11, infile );

        if( size == 0 )
        {
            if( !quiet )
                fprintf(stderr, "0 bytes read; stopping reading %s\n", filename);
            break;
        }

        /* FLV packet is structured as follows:
         * 1 byte packet type
         * 3 bytes datasize
         * 4 bytes timestamp
         * 3 bytes streamid
         * data payload (size specified previously)
         *      (for a video packet, first nibble of payload indicates frame type;
         *       however H.264 sequence headers are marked as if they were key
         *       frames - so first two bytes need to be checked to determine whether
         *       or not the video packet contains a keyframe)
         * 4 bytes backpointer
         */
        packet.type = buff[0];
        packet.datasize = conv_ui24(&buff[1], 0);
        packet.timestamp = conv_ui24(&buff[4], buff[7]);
        packet.streamid = conv_ui24(&buff[8], 0);
        if( packet.datasize > max_datasize )
        {
            max_datasize = packet.datasize;
            /* packet declared as static so packet.data will have been initialised to NULL */
            packet.data = realloc( packet.data, max_datasize );
        }
        fread( packet.data, 1, packet.datasize, infile );
        /* Read back-pointer */
        size = fread( buff, 1, 4, infile );
        /* backptr should equal the number of bytes in the whole packet including the payload and the
         * header; could use it as a sanity check if necessary */
        packet.backptr = conv_ui32(buff);

        if(packet.type == 18) /* Script data */
        {
            if(!metadata_extracted && !no_meta)
            {
                /* Attempt to extract metadata from this packet */
                metadata_extracted = extract_metadata(&packet);
                if(!quiet && metadata_extracted)
                    fprintf(stderr, "Metadata successfully extracted.\n");
            }
            continue; /* Jump to next packet */
        }

        if(packet.type == 9 &&
                (packet.data[0] & 0x0f) == 7 && packet.data[1] == 0) /* AVC sequence header */
        {
            if(!seq_header_pkt.data)
            {
                seq_header_pkt = packet;
                seq_header_pkt.data = malloc(packet.datasize);
                memcpy(seq_header_pkt.data, packet.data, packet.datasize);
            }
            continue; /* Jump to next packet */
        }

        if(packet.timestamp < mark_in ||             /* Before mark in point */
                packet.timestamp >= mark_out ||           /* After mark out point */
                (packet.type != 8 && packet.type != 9))   /* Non video or audio packet */
            continue; /* Jump to next packet */

        if( packet.type == 8 )
            key_frame = 1; /* All audio packets are keyframes */
        else
        {
            unsigned char frame_type = (packet.data[0] & 0xf0) >> 4;
            if(frame_type == 1)
                key_frame = 1;
        }
        if(first_keyframe_timestamp == -1 && key_frame)
            first_keyframe_timestamp = packet.timestamp;

        if( file_start_timestamp == -999999 )
        {
            if( packet.type == 9 ) /* Video packet */
            {
                if(key_frame)
                {
                    if(first_time)
                    {
                        /* First packet processed (either audio or video) is effectively the start of file */
                        file_start_timestamp = -first_keyframe_timestamp;
                        first_time = 0;
                    }
                    else
                        /* Calculate starting timestamp based on video framerate */
                        file_start_timestamp = lastfile_video_timestamp + frame_interval - packet.timestamp;
                    if(!quiet)
                        fprintf(stderr, "%s: File start timestamp set to %ld (First video keyframe %d)\n",
                                filename, file_start_timestamp, packet.timestamp);
                    buffer_packet( &packet, file_start_timestamp, 1); /* Flush buffer this time */
                }
                /* Discard non-keyframe video packets received before first keyframe packet */
            }
            else
                /* Buffer packets until we get our first video keyframe that
                 * we can calculate the starting timestamp from */
                buffer_packet( &packet, -1, 0 );
        }
        else
            /* Write this packet to output stream */
            write_packet( &packet, file_start_timestamp );

    }

    if( !quiet )
        fprintf(stderr, "Closing %s\n", filename);
    if( fclose(infile) != 0 )
        fprintf(stderr, "ERROR while closing input file %s: %s\n",
                filename, strerror(errno));

    return;
}