<commit_before>/* * 
 *  This file is part of Feng
 * 
 * Copyright (C) 2007 by LScube team <<EMAIL>> 
 * See AUTHORS for more details 
 *  
 * Feng is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * 
 * Feng is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License
 * along with Feng; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *  
 * */

#include <stdio.h>
#include <sys/time.h>

#include <pthread.h>
#include <fenice/schedule.h>
#include <fenice/rtcp.h>
#include <fenice/utils.h>
#include <fenice/debug.h>
#include <fenice/fnc_log.h>

#define SCHEDULER_TIMING 16000 //16ms. Sleep time suggested by Intel

extern schedule_list sched[ONE_FORK_MAX_CONNECTION];
extern int stop_schedule;

void *schedule_do(void *nothing)
{
    int i = 0, utime = SCHEDULER_TIMING, res;
    double now;
do {
    // Fake waiting. Break the while loop to achieve fair kernel (re)scheduling and fair CPU loads.
    usleep(utime);
    utime = SCHEDULER_TIMING;
    for (i=0; i<ONE_FORK_MAX_CONNECTION; ++i) {
        pthread_mutex_lock(&sched[i].mux);
        if (sched[i].valid && sched[i].rtp_session->started) {
            RTP_session * session = sched[i].rtp_session;
            Track *tr = r_selected_track(session->track_selector);
            if (!session->pause || tr->properties->media_source == MS_live) {
                now = gettimeinseconds(NULL);
		res = ERR_NOERROR;
                while (res == ERR_NOERROR && now >= session->start_time &&
                    now >= session->start_time + session->send_time) {
#if 0
                        fprintf(stderr, "[SCH] PT: %d Sendtime: %f Timestamp: %f Delta: %f\n",
                                tr->properties->payload_type, session->send_time, session->timestamp - session->seek_time,
                                session->send_time - (session->timestamp - session->seek_time));
#endif
#if 1
//TODO DSC will be implemented WAY later.
#else
                    stream_change(session,
                        change_check(session));
#endif
                // Send an RTP packet
                    res = sched[i].play_action(session);
                    switch (res) {
                        case ERR_NOERROR: // All fine
                            break;
                        case ERR_EOF:
                            if(tr->properties->media_source != MS_live) {
                                fnc_log(FNC_LOG_INFO,"[SCH] Stream Finished");
                                RTCP_send_packet(session, SR);
                                RTCP_send_packet(session, BYE);
                                RTCP_flush(session);
                            }
                            break;
                        case ERR_ALLOC:
                            fnc_log(FNC_LOG_WARN,"[SCH] Cannot allocate memory");
                            schedule_stop(i);
                            break;
                        default:
                            fnc_log(FNC_LOG_WARN,"[SCH] Packet Lost");
                            break;
                    }
                    RTCP_handler(session);
                    /*if RTCP_handler return ERR_GENERIC what do i have to do?*/
                }
                if (res == ERR_NOERROR) {
                    int next_send_time = fabs((session->start_time +
                                        session->send_time) - now) * 1000000;
                    utime = min(utime, next_send_time);
                }
            }
        }
        pthread_mutex_unlock(&sched[i].mux);
    }
} while (!stop_schedule);

    stop_schedule=0;
    return ERR_NOERROR;
}

<commit_msg>make usleep time in scheduler unsigned.<commit_after>/* * 
 *  This file is part of Feng
 * 
 * Copyright (C) 2007 by LScube team <<EMAIL>> 
 * See AUTHORS for more details 
 *  
 * Feng is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * 
 * Feng is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License
 * along with Feng; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *  
 * */

#include <stdio.h>
#include <sys/time.h>

#include <pthread.h>
#include <fenice/schedule.h>
#include <fenice/rtcp.h>
#include <fenice/utils.h>
#include <fenice/debug.h>
#include <fenice/fnc_log.h>

#define SCHEDULER_TIMING 16000 //16ms. Sleep time suggested by Intel

extern schedule_list sched[ONE_FORK_MAX_CONNECTION];
extern int stop_schedule;

void *schedule_do(void *nothing)
{
    int i = 0, res;
    unsigned utime = SCHEDULER_TIMING;
    double now;
do {
    // Fake waiting. Break the while loop to achieve fair kernel (re)scheduling and fair CPU loads.
    usleep(utime);
    utime = SCHEDULER_TIMING;
    for (i=0; i<ONE_FORK_MAX_CONNECTION; ++i) {
        pthread_mutex_lock(&sched[i].mux);
        if (sched[i].valid && sched[i].rtp_session->started) {
            RTP_session * session = sched[i].rtp_session;
            Track *tr = r_selected_track(session->track_selector);
            if (!session->pause || tr->properties->media_source == MS_live) {
                now = gettimeinseconds(NULL);
		res = ERR_NOERROR;
                while (res == ERR_NOERROR && now >= session->start_time &&
                    now >= session->start_time + session->send_time) {
#if 0
                        fprintf(stderr, "[SCH] PT: %d Sendtime: %f Timestamp: %f Delta: %f\n",
                                tr->properties->payload_type, session->send_time, session->timestamp - session->seek_time,
                                session->send_time - (session->timestamp - session->seek_time));
#endif
#if 1
//TODO DSC will be implemented WAY later.
#else
                    stream_change(session,
                        change_check(session));
#endif
                // Send an RTP packet
                    res = sched[i].play_action(session);
                    switch (res) {
                        case ERR_NOERROR: // All fine
                            break;
                        case ERR_EOF:
                            if(tr->properties->media_source != MS_live) {
                                fnc_log(FNC_LOG_INFO,"[SCH] Stream Finished");
                                RTCP_send_packet(session, SR);
                                RTCP_send_packet(session, BYE);
                                RTCP_flush(session);
                            }
                            break;
                        case ERR_ALLOC:
                            fnc_log(FNC_LOG_WARN,"[SCH] Cannot allocate memory");
                            schedule_stop(i);
                            break;
                        default:
                            fnc_log(FNC_LOG_WARN,"[SCH] Packet Lost");
                            break;
                    }
                    RTCP_handler(session);
                    /*if RTCP_handler return ERR_GENERIC what do i have to do?*/
                }
                if (res == ERR_NOERROR) {
                    int next_send_time = fabs((session->start_time +
                                        session->send_time) - now) * 1000000;
                    utime = min(utime, next_send_time);
                }
            }
        }
        pthread_mutex_unlock(&sched[i].mux);
    }
} while (!stop_schedule);

    stop_schedule=0;
    return ERR_NOERROR;
}


{'fraction_non_alphanumeric': 0.09363204675514428, 'fraction_numerical': 0.008523073176671132, 'mean_word_length': 1.6790606653620352, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5593641', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2201, 'n_words': 650}