int main(int argc, char *argv[])
{
    if(argc < 2) {
        std::cout << USAGE << std::endl;
        exit(1);
    }
    std::ifstream  ifs;
    char *inp_filename = argv[1];
    unsigned char  ts_pkt[256];
    int  n_pkts = 0;
    uint64_t       curr_pcr;
    uint64_t       pcr_ext;
    int            inp_pid;

    ifs.open(inp_filename, std::ios::in);
    if(!ifs.is_open()) {
        std::cout << "Unable to open " << inp_filename;
        exit(1);
    }

    while(!ifs.eof()) {
        ifs.read((char*)ts_pkt, 188);
        inp_pid = ts_get_pid(ts_pkt);

        if(ts_has_adaptation(ts_pkt))
        {
            printf("Adaptation=%d\n", (ts_pkt[3] & 0x30) >> 4);
            if(tsaf_has_discontinuity(ts_pkt))
            {
                printf("PCR Discontinuity %ld %d %d\n", (int64_t)ifs.tellg()/188, inp_pid, ts_get_adaptation(ts_pkt));
            }
        }
        if(ts_has_adaptation(ts_pkt) && ts_get_adaptation(ts_pkt) > 0)
        {

            if(tsaf_has_pcr(ts_pkt))
            {
                curr_pcr = tsaf_get_pcr(ts_pkt);
                pcr_ext = tsaf_get_pcrext(ts_pkt);
                curr_pcr = (300 * curr_pcr) + pcr_ext;
                printf("%d %lld %lld %d\n", inp_pid, curr_pcr/(300 * 90), (int64_t)ifs.tellg()/188, ts_get_unitstart(ts_pkt));
            }
        }
        n_pkts++;
        //std::cout << n_pkts <<std::endl;
    }
    std::cout << "Completed" << std::endl;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    if(argc < 4) {
        std::cout << USAGE << std::endl;
        exit(1);
    }
    std::ifstream  ifs;
    std::ofstream  ofs;
    char *inp_filename = argv[1];
    char *out_filename = argv[2];
    std::vector<int>  pids_vec;
    int  i;
    unsigned char  ts_pkt[256];
    int  loc_pid;
    int  n_pkts = 0;

    ifs.open(inp_filename, std::ios::in);
    if(!ifs.is_open()) {
        std::cout << "Unable to open " << inp_filename;
        exit(1);
    }

    ofs.open(out_filename, std::ios::out);
    if(!ofs.is_open()) {
        std::cout << "Unable to open " << out_filename;
        exit(1);
    }


    for(i = 0; i < (argc - 3); i++) {
        //pids_vec[i] = atoi(argv[3+i]);
        pids_vec.push_back(atoi(argv[3+i]));
    }
    std::cout << "H1" << std::endl;

    while(!ifs.eof()) {
        ifs.read((char*)ts_pkt, 188);
        loc_pid = ts_get_pid(ts_pkt);
        if(std::find(pids_vec.begin(), pids_vec.end(), loc_pid)!=pids_vec.end())
        {
            ofs.write((char*)ts_pkt, 188);
        }
        n_pkts++;
        //std::cout << n_pkts <<std::endl;
    }
    std::cout << "Completed" << std::endl;
}
Exemplo n.º 3
0
/*****************************************************************************
 * handle_psi_packet
 *****************************************************************************/
static void handle_psi_packet(uint8_t *p_ts)
{
    uint16_t i_pid = ts_get_pid(p_ts);
    ts_pid_t *p_pid = &p_pids[i_pid];
    uint8_t i_cc = ts_get_cc(p_ts);
    const uint8_t *p_payload;
    uint8_t i_length;

    if (ts_check_duplicate(i_cc, p_pid->i_last_cc) || !ts_has_payload(p_ts))
        return;

    if (p_pid->i_last_cc != -1
          && ts_check_discontinuity(i_cc, p_pid->i_last_cc))
        psi_assemble_reset(&p_pid->p_psi_buffer, &p_pid->i_psi_buffer_used);

    p_payload = ts_section(p_ts);
    i_length = p_ts + TS_SIZE - p_payload;

    if (!psi_assemble_empty(&p_pid->p_psi_buffer, &p_pid->i_psi_buffer_used)) {
        uint8_t *p_section = psi_assemble_payload(&p_pid->p_psi_buffer,
                                                  &p_pid->i_psi_buffer_used,
                                                  &p_payload, &i_length);
        if (p_section != NULL)
            handle_section(i_pid, p_section);
    }

    p_payload = ts_next_section( p_ts );
    i_length = p_ts + TS_SIZE - p_payload;

    while (i_length) {
        uint8_t *p_section = psi_assemble_payload(&p_pid->p_psi_buffer,
                                                  &p_pid->i_psi_buffer_used,
                                                  &p_payload, &i_length);
        if (p_section != NULL)
            handle_section(i_pid, p_section);
    }
}
Exemplo n.º 4
0
int mediainfo (char *uri)
{
    int i;
    struct sockaddr_in si_other;
    struct ip_mreq group;
    int s, slen = sizeof (si_other);
    char *server;
    int port;
    time_t start_time;

    if (strncmp (uri, "udp://", 6) != 0) {
        exit (1);
    }
    server = uri + 6;
    for (i = 6; i < strlen (uri); i++) {
        if (*(uri + i) == ':') {
            *(uri + i) = '\0';
        }
        port = atoi (uri + i + 1);
    }
    if (i == strlen (uri)) {
        exit (1);
    }
    setvbuf(stdout, NULL, _IOLBF, 0);
    memset(p_pids, 0, sizeof(p_pids));

    if ((s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        exit (1);
    }

    if (is_multicast (uri) == 1) {
        int reuse = 1;
        if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0){
            perror("Setting SO_REUSEADDR error");
            close(s);
            exit(1);
        }
    }

    memset ((char *)&si_other, 0, sizeof (si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons (port);
    if (is_multicast (uri) == 1) {
        si_other.sin_addr.s_addr = INADDR_ANY;
    } else {
        if (inet_aton (server, &si_other.sin_addr) == 0) {
            exit (1);
        }    
    }
    if (bind (s, (struct sockaddr *)&si_other, sizeof (si_other)) == -1) {
        exit (1);
    }

    if (is_multicast (uri) == 1) {
        group.imr_multiaddr.s_addr = inet_addr(server);
        //group.imr_interface.s_addr = inet_addr("172.16.0.10");
        if(setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0){
            perror("Adding multicast group error");
            close(s);
            exit(1);
        }
    }

    for (i = 0; i < 8192; i++) {
        p_pids[i].i_last_cc = -1;
        psi_assemble_init(&p_pids[i].p_psi_buffer,
                          &p_pids[i].i_psi_buffer_used);
    }

    p_pids[PAT_PID].i_psi_refcount++;
    p_pids[TSDT_PID].i_psi_refcount++;
    p_pids[NIT_PID].i_psi_refcount++;
    p_pids[SDT_PID].i_psi_refcount++;

    psi_table_init(pp_current_pat_sections);
    psi_table_init(pp_next_pat_sections);
    psi_table_init(pp_current_tsdt_sections);
    psi_table_init(pp_next_tsdt_sections);
    psi_table_init(pp_current_nit_sections);
    psi_table_init(pp_next_nit_sections);
    psi_table_init(pp_current_sdt_sections);
    psi_table_init(pp_next_sdt_sections);
    for (i = 0; i < TABLE_END; i++)
        pb_print_table[i] = true;

    printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    printf("<TS>\n");

    start_time = time (NULL);
    while (got_pmt == 0) {
        uint8_t p_ts[TS_SIZE*10];
        uint8_t *p;
        size_t i_ret = recvfrom (s, p_ts, sizeof (p_ts), MSG_CMSG_CLOEXEC | MSG_DONTWAIT, (struct sockaddr *)&si_other, &slen);
        if (i_ret == -1) {
            usleep (10000);
        }

        p = p_ts;
        while (p < p_ts + i_ret) {
            if (!ts_validate(p)) {
                printf("<ERROR type=\"invalid_ts\"/>\n");
            } else {
                uint16_t i_pid = ts_get_pid(p);
                ts_pid_t *p_pid = &p_pids[i_pid];
                if (p_pid->i_psi_refcount)
                    handle_psi_packet(p);
                p_pid->i_last_cc = ts_get_cc(p);
            }
            p = p + TS_SIZE;
        }
        if (difftime (time (NULL), start_time) > 60) {
            printf ("Timeout\n");
            break;
        }
    }

    printf("</TS>\n");

    if (iconv_handle != (iconv_t)-1)
    iconv_close(iconv_handle);

    psi_table_free(pp_current_pat_sections);
    psi_table_free(pp_next_pat_sections);
    psi_table_free(pp_current_tsdt_sections);
    psi_table_free(pp_next_tsdt_sections);
    psi_table_free(pp_current_nit_sections);
    psi_table_free(pp_next_nit_sections);
    psi_table_free(pp_current_sdt_sections);
    psi_table_free(pp_next_sdt_sections);

    for (i = 0; i < i_nb_sids; i++) {
        psi_table_free(pp_sids[i]->pp_eit_sections);
        free(pp_sids[i]->p_current_pmt);
        free(pp_sids[i]);
    }
    free(pp_sids);

    for (i = 0; i < 8192; i++)
        psi_assemble_reset(&p_pids[i].p_psi_buffer,
                           &p_pids[i].i_psi_buffer_used);

    close (s);
    return EXIT_SUCCESS;
}
Exemplo n.º 5
0
/*****************************************************************************
 * output_Flush
 *****************************************************************************/
static void output_Flush( output_t *p_output )
{
    packet_t *p_packet = p_output->p_packets;
    int i_block_cnt = output_BlockCount( p_output );
    struct iovec p_iov[i_block_cnt + 2];
    uint8_t p_rtp_hdr[RTP_HEADER_SIZE];
    int i_iov = 0, i_payload_len, i_block;

    if ( (p_output->config.i_config & OUTPUT_RAW) )
    {
        p_iov[i_iov].iov_base = &p_output->raw_pkt_header;
        p_iov[i_iov].iov_len = sizeof(struct udprawpkt);
        i_iov++;
    }

    if ( !(p_output->config.i_config & OUTPUT_UDP) )
    {
        p_iov[i_iov].iov_base = p_rtp_hdr;
        p_iov[i_iov].iov_len = sizeof(p_rtp_hdr);

        rtp_set_hdr( p_rtp_hdr );
        rtp_set_type( p_rtp_hdr, RTP_TYPE_TS );
        rtp_set_seqnum( p_rtp_hdr, p_output->i_seqnum++ );
        rtp_set_timestamp( p_rtp_hdr,
                           p_output->i_ref_timestamp
                            + (p_packet->i_dts - p_output->i_ref_wallclock)
                               * 9 / 100 );
        rtp_set_ssrc( p_rtp_hdr, p_output->config.pi_ssrc );

        i_iov++;
    }

    for ( i_block = 0; i_block < p_packet->i_depth; i_block++ )
    {
        /* Do pid mapping here if needed.
         * save the original pid in the block.
         * set the pid to the new pid
         * later we re-instate the old pid for the next output
         */
        if ( b_do_remap || p_output->b_do_remap ) {
            block_t *p_block = p_packet->pp_blocks[i_block];
            uint16_t i_pid = ts_get_pid( p_block->p_ts );
            p_block->tmp_pid = UNUSED_PID;
            if ( p_output->pi_newpids[i_pid] != UNUSED_PID )
            {
                uint16_t i_newpid = p_output->pi_newpids[i_pid];
                /* Need to map this pid to the new pid */
                ts_set_pid( p_block->p_ts, i_newpid );
                p_block->tmp_pid = i_pid;
            }
        }

        p_iov[i_iov].iov_base = p_packet->pp_blocks[i_block]->p_ts;
        p_iov[i_iov].iov_len = TS_SIZE;
        i_iov++;
    }

    for ( ; i_block < i_block_cnt; i_block++ )
    {
        p_iov[i_iov].iov_base = p_pad_ts;
        p_iov[i_iov].iov_len = TS_SIZE;
        i_iov++;
    }

    
    if ( (p_output->config.i_config & OUTPUT_RAW) )
    {
        i_payload_len = 0;
        for ( i_block = 1; i_block < i_iov; i_block++ ) {
            i_payload_len += p_iov[i_block].iov_len; 
        }
        p_output->raw_pkt_header.udph.len = htons(sizeof(struct udpheader) + i_payload_len);
    }

    if ( writev( p_output->i_handle, p_iov, i_iov ) < 0 )
    {
        msg_Err( NULL, "couldn't writev to %s (%s)",
                 p_output->config.psz_displayname, strerror(errno) );
    }
    /* Update the wallclock because writev() can take some time. */
    i_wallclock = mdate();

    for ( i_block = 0; i_block < p_packet->i_depth; i_block++ )
    {
        p_packet->pp_blocks[i_block]->i_refcount--;
        if ( !p_packet->pp_blocks[i_block]->i_refcount )
            block_Delete( p_packet->pp_blocks[i_block] );
        else if ( b_do_remap || p_output->b_do_remap ) {
            /* still referenced so re-instate the orignial pid if remapped */
            block_t * p_block = p_packet->pp_blocks[i_block];
            if (p_block->tmp_pid != UNUSED_PID)
                ts_set_pid( p_block->p_ts, p_block->tmp_pid );
        }
    }
    p_output->p_packets = p_packet->p_next;
    free( p_packet );
    if ( p_output->p_packets == NULL )
        p_output->p_last_packet = NULL;
}
Exemplo n.º 6
0
int main(int i_argc, char **ppsz_argv)
{   
    int c;
    FILE *inputfile=stdin;
    FILE *outputfile=stdout;
    int offset=12, pid=0x0426;
    int i_last_cc = -1;
    
    static const struct option long_options[] = {
        { "pid",           required_argument, NULL, 'p' },
        { "offset",            required_argument,       NULL, 's' },
        { "input",         required_argument,       NULL, 'i' },
        { "output",          required_argument,       NULL, 'o' },
        { 0, 0, 0, 0 }
    };
    
    while ((c = getopt_long(i_argc, ppsz_argv, "p:s:i:o:h", long_options, NULL)) != -1)
    {
        switch (c) {
        case 'p':
            pid=strtoul(optarg, NULL, 0);
            if(pid >= 8192) {
                ERROR("bad pid value: %d!", pid);
                exit(1);
            }
            break;

        case 's':
            offset=strtol(optarg, NULL, 0);
            if(offset >= 184 || offset <= -4) {
                ERROR("bad offset value: %d!", offset);
                exit(1);
            }
            break;

        case 'i':
            inputfile = fopen(optarg, "r");
            if(!inputfile) {
                ERROR("cant open input file!");
                exit(1);
            }
            break;

        case 'o':
            outputfile = fopen(optarg, "w");
            if(!outputfile) {
                ERROR("cant open output file!");
                exit(1);
            }
            break;


        case 'h':
        default:
            usage(ppsz_argv[0]);
        }
    }


    INFO("Using pid: 0x%04x (%d)", pid, pid);
    unsigned long int packets=0;
    while (!feof(inputfile) && !ferror(inputfile)) {
        uint8_t p_ts[TS_SIZE];
        size_t i_ret = fread(p_ts, TS_SIZE, 1, inputfile);
        if (i_ret != 1) {
        	WARN("Can't read input ts");
        	break;
        }
        if (ts_validate(p_ts)) {
            if(offset >= 0 &&  ts_get_pid(p_ts)==pid) {
            	if(i_last_cc > 0 && (0x0f & (i_last_cc+1)) != ts_get_cc(p_ts)) {
            		WARN("TS Discontinuity");
            	}
            	i_last_cc = ts_get_cc(p_ts);
            	uint8_t *payload = ts_payload(p_ts);
            	if(offset) {
            		payload+=offset;
            	}

            	if(p_ts+TS_SIZE < payload) {
                	ERROR("payload is out of ts by %ld bytes", payload-p_ts+TS_SIZE);
                	break;
            	}

            	size_t o_ret = fwrite(payload, p_ts+TS_SIZE-payload, 1, outputfile);
                if (o_ret != 1) {
                	WARN("Can't write output ts");
                	break;
                }
                packets++;
            } else if(offset < 0) {
            	uint8_t *payload = &p_ts[TS_HEADER_SIZE + offset];
            	if(p_ts+TS_SIZE < payload || p_ts > payload) {
                	ERROR("payload is out of ts by %ld bytes", payload-p_ts+TS_SIZE);
                	break;
            	}

            	size_t o_ret = fwrite(payload, p_ts+TS_SIZE-payload, 1, outputfile);
                if (o_ret != 1) {
                	WARN("Can't write output ts");
                	break;
                }
                packets++;
            }
        } else {
            do {
                memmove(p_ts, &p_ts[1], TS_SIZE-1);
                size_t i_ret = fread(&p_ts[TS_SIZE-1], 1, 1, inputfile);
                if (i_ret != 1) {
                	WARN("Can't read input ts");
                	break;
                }
            } while (!ts_validate(p_ts) && !feof(inputfile) && !ferror(inputfile));
        }
    }

    if(packets){
    	INFO("Successfuly readed %ld ts-packets", packets);
    }


//mainErr:
    fclose(inputfile);
    fclose(outputfile);
    return 0;
}
Exemplo n.º 7
0
/** @internal @This handles input buffers.
 *
 * @param upipe description structure of the pipe
 * @param uref input buffer to handle
 * @param upump_p reference to the pump that generated the buffer
 */
static void upipe_dvbcsa_enc_input(struct upipe *upipe,
                                   struct uref *uref,
                                   struct upump **upump_p)
{
    struct upipe_dvbcsa_enc *upipe_dvbcsa_enc =
        upipe_dvbcsa_enc_from_upipe(upipe);
    struct upipe_dvbcsa_common *common =
        upipe_dvbcsa_enc_to_common(upipe_dvbcsa_enc);

    if (unlikely(!upipe_dvbcsa_enc->key))
        /* no key set, forward */
        return upipe_dvbcsa_enc_output(upipe, uref, upump_p);

    uint32_t ts_header_size = TS_HEADER_SIZE;
    uint8_t buf[TS_HEADER_SIZE];
    const uint8_t *ts_header = uref_block_peek(uref, 0, sizeof (buf), buf);
    if (unlikely(!ts_header)) {
        upipe_err(upipe, "fail to read ts header");
        uref_free(uref);
        return;
    }
    uint8_t scrambling = ts_get_scrambling(ts_header);
    bool has_payload = ts_has_payload(ts_header);
    bool has_adaptation = ts_has_adaptation(ts_header);
    uint16_t pid = ts_get_pid(ts_header);
    uref_block_peek_unmap(uref, 0, buf, ts_header);

    if (!has_payload || scrambling ||
        !upipe_dvbcsa_common_check_pid(common, pid))
        return upipe_dvbcsa_enc_output(upipe, uref, upump_p);

    if (unlikely(has_adaptation)) {
        uint8_t af_length;
        int ret = uref_block_extract(uref, ts_header_size, 1, &af_length);
        if (unlikely(!ubase_check(ret))) {
            upipe_err(upipe, "fail to extract adaptation field length");
            uref_free(uref);
            return;
        }
        if (unlikely(af_length >= 183)) {
            upipe_warn(upipe, "invalid adaptation field received");
            uref_free(uref);
            return;
        }
        ts_header_size += af_length + 1;
    }

    struct ubuf *ubuf = ubuf_block_copy(uref->ubuf->mgr, uref->ubuf, 0, -1);
    if (unlikely(!ubuf)) {
        upipe_err(upipe, "fail to allocate buffer");
        uref_free(uref);
        return;
    }
    uref_attach_ubuf(uref, ubuf);
    int size = -1;
    uint8_t *ts;
    int ret = ubuf_block_write(ubuf, 0, &size, &ts);
    if (unlikely(!ubase_check(ret))) {
        upipe_err(upipe, "fail to write buffer");
        uref_free(uref);
        return;
    }
    ts_set_scrambling(ts, 0x2);
    dvbcsa_encrypt(upipe_dvbcsa_enc->key,
                   ts + ts_header_size,
                   size - ts_header_size);
    return upipe_dvbcsa_enc_output(upipe, uref, upump_p);
}