time_t MP3BroadcasterLog::WriteLogHeader(FILE *inFile)
{
    // Write a W3C compatable log header
    time_t calendarTime = ::time(NULL);
    Assert(-1 != calendarTime);
    if (-1 == calendarTime)
        return -1;

    struct tm  timeResult;
    struct tm* theLocalTime = qtss_localtime(&calendarTime, &timeResult);
    Assert(NULL != theLocalTime);
    if (NULL == theLocalTime)
        return -1;
    
    char tempBuffer[1024] = { 0 };
    qtss_strftime(tempBuffer, sizeof(tempBuffer), "#Log File Created On: %m/%d/%Y %H:%M:%S\n", theLocalTime);
    this->WriteToLog(tempBuffer, !kAllowLogToRoll);
    tempBuffer[0] = '\0';
    
    // format a date for the startup time
    
    char theDateBuffer[QTSSRollingLog::kMaxDateBufferSizeInBytes] = { 0 };
    Bool16 result = QTSSRollingLog::FormatDate(theDateBuffer, false);
    
    if (result)
    {
        qtss_sprintf(tempBuffer, sLogHeader, "MP3Broadcaster" , kVersionString, 
                            theDateBuffer, sLogTimeInGMT ? "GMT" : "local time");
        this->WriteToLog(tempBuffer, !kAllowLogToRoll);
    }
        
    return calendarTime;
}
Bool16 QTSSExpirationDate::IsSoftwareExpired()
{
    if (!sIsExpirationEnabled)
        return false;
        
    SInt32 expMonth, expDay, expYear;
    if (EOF == ::sscanf(sExpirationDate, "%" _S32BITARG_ "/%" _S32BITARG_ "/%" _S32BITARG_ "", &expMonth, &expDay, &expYear))
    {
        Assert(false);
        return true;
    }
    
    //sanity checks
    Assert((expMonth > 0) && (expMonth <= 12));
    if ((expMonth <= 0) || (expMonth > 12))
        return true;
    
    Assert((expDay > 0) && (expDay <= 31));
    if ((expDay <= 0) || (expDay > 31))
        return true;
        
    Assert(expYear >= 1998);
    if (expYear < 1998)
        return true;
    
    time_t theCurrentTime = ::time(NULL);
    Assert(theCurrentTime != -1);
    if (theCurrentTime == -1)
        return true;
        
    struct tm  timeResult;
    struct tm* theLocalTime = qtss_localtime(&theCurrentTime, &timeResult);
    Assert(theLocalTime != NULL);
    if (theLocalTime == NULL)
        return true;
        
    if (expYear > (theLocalTime->tm_year + 1900))
        return false;//ok
    if (expYear < (theLocalTime->tm_year + 1900))
        return true;//expired

    if (expMonth > (theLocalTime->tm_mon + 1))
        return false;//ok
    if (expMonth < (theLocalTime->tm_mon + 1))
        return true;//expired

    if (expDay > theLocalTime->tm_mday)
        return false;//ok
    else
        return true;//expired
}
void TestUnixTime(time_t theTime, char *ioDateBuffer)
{
	Assert(NULL != ioDateBuffer);

	//use ansi routines for getting the date.
	time_t calendarTime = theTime;
	Assert(-1 != calendarTime);
	if (-1 == calendarTime)
		return;

	struct tm  timeResult;
	struct tm* theLocalTime = qtss_localtime(&calendarTime, &timeResult);
	Assert(NULL != theLocalTime);
	if (NULL == theLocalTime)
		return;

	//date needs to look like this for common log format: 29/Sep/1998:11:34:54 -0700
	//this wonderful ANSI routine just does it for you.
	//qtss_strftime(ioDateBuffer, kMaxDateBufferSize, "%d/%b/%Y:%H:%M:%S", theLocalTime);
	qtss_strftime(ioDateBuffer, QTSSRollingLog::kMaxDateBufferSizeInBytes, "%Y-%m-%d %H:%M:%S", theLocalTime);
	return;
}
void PLBroadcastDef::ShowSettings()
{
    
    
    qtss_printf( "\n" );
    qtss_printf( "Description File Settings\n" );
    qtss_printf( "----------------------------\n" );
        
    qtss_printf( "destination_ip_address  %s\n", mOrigDestAddress );
    qtss_printf( "destination_sdp_file  %s\n", mDestSDPFile );
    qtss_printf( "destination_base_port  %s\n", mBasePort );
    qtss_printf( "play_mode  %s\n", mPlayMode );
    qtss_printf( "recent_movies_list_size  %d\n", mLimitPlayQueueLength );
    qtss_printf( "playlist_file  %s\n", mPlayListFile );
    qtss_printf( "logging  %s\n", mLogging );
    qtss_printf( "log_file  %s\n", mLogFile );
    if (mSDPReferenceMovie != NULL)
        qtss_printf( "sdp_reference_movie  %s\n", mSDPReferenceMovie );
    qtss_printf( "sdp_file  %s\n", mSDPFile );
    qtss_printf( "max_upcoming_list_size  %s\n", mMaxUpcomingMovieListSize );
    qtss_printf( "show_current  %s\n", mShowCurrent );
    qtss_printf( "show_upcoming  %s\n", mShowUpcoming );
    qtss_printf( "broadcaster_name \"%s\"\n", mName);
    qtss_printf( "broadcaster_password \"XXXXX\"\n");
    qtss_printf( "multicast_ttl %s\n",mTTL);
    qtss_printf( "rtsp_port %s\n",mRTSPPort);

    Float32 bufferDelay = 0.0;
    ::sscanf(mClientBufferDelay, "%f", &bufferDelay);
    if (bufferDelay != 0.0) 
        qtss_printf( "client_buffer_delay %.2f\n",bufferDelay);
    else
        qtss_printf( "client_buffer_delay default\n");
    
    if (mPIDFile != NULL)
        qtss_printf( "pid_file %s\n",mPIDFile);
    
    qtss_printf( "broadcast_SDP_is_dynamic  %s\n", mIsDynamic );

    UInt32 startTime = (UInt32) ::strtoul(mStartTime, NULL, 10);
    if ( startTime > 2208988800LU)
    {
        qtss_printf( "broadcast_start_time %s (NTP seconds)\n",mStartTime);

        startTime -= 2208988800LU; //1970 - 1900 secs      
        qtss_printf( "-->broadcast_start_time = %lu (unix seconds)\n",startTime);
        
        time_t tmpTime;
        tmpTime = (time_t) startTime;
        struct tm  timeResult;
        struct tm *localTM = qtss_localtime(&tmpTime, &timeResult);
        char timeBuffer[kTimeStrSize];
        char *theTime = qtss_asctime(localTM,timeBuffer, sizeof(timeBuffer));
        if (theTime[0] != 0)
            theTime[::strlen(theTime) -1] = 0;
        qtss_printf( "-->broadcast_start_time = %s (local time)\n",theTime);
        
        tmpTime = (time_t) startTime;
        struct tm *gmTM = qtss_gmtime(&tmpTime, &timeResult);
        theTime = qtss_asctime(gmTM, timeBuffer, sizeof(timeBuffer));
        if (theTime[0] != 0)
            theTime[::strlen(theTime) -1] = 0;
        qtss_printf( "-->broadcast_start_time = %s (UTC/GM time)\n",theTime);
    }
    else if (0 == startTime)
        qtss_printf( "broadcast_start_time   0 (allow all)\n");    
    else
        qtss_printf( "broadcast_start_time   %s (NTPseconds allow all)\n", mStartTime);
    
    UInt32 endTime = strtoul(mEndTime, NULL, 10);
    if (endTime > 2208988800LU)
    {
        qtss_printf( "broadcast_end_time   %s (NTP seconds)\n",mEndTime);
        
        endTime -= 2208988800LU;//convert to 1970 secs
        qtss_printf( "-->broadcast_end_time   = %lu (unix seconds)\n",endTime);
        
        time_t tmpTime = (time_t) endTime;
        struct tm  timeResult;
        struct tm *localTM = qtss_localtime(&tmpTime, &timeResult);
        char timeBuffer[kTimeStrSize];
        char *theTime = qtss_asctime(localTM,timeBuffer, sizeof(timeBuffer));
        if (theTime[0] != 0)
            theTime[::strlen(theTime) -1] = 0;
        qtss_printf( "-->broadcast_end_time   = %s (local time)\n",theTime);
        
        tmpTime = (time_t) endTime;
        struct tm *gmTM = qtss_gmtime(&tmpTime, &timeResult);
        theTime = qtss_asctime(gmTM, timeBuffer, sizeof(timeBuffer));
        if (theTime[0] != 0)
            theTime[::strlen(theTime) -1] = 0;
        qtss_printf( "-->broadcast_end_time   = %s (UTC/GM time)\n",theTime);
    }
    else if (0 == endTime)
        qtss_printf( "broadcast_end_time   0 (unbounded)\n");
    else
        qtss_printf( "broadcast_end_time   1900 + %s seconds (looks invalid)\n", mEndTime);
        
	qtss_printf( "max_err_file_k_size %lu\n", qtss_getmaxprintfcharsinK());

    
    qtss_printf( "============================\n" );
    
}
/* changed by [email protected] (see relaod.txt for info) */
int QTFileBroadcaster::Play(char *mTimeFile)
/* ***************************************************** */
{
    SInt16  err = 0;
    Float64 transmitTime = 0;
    MediaStream *theStreamPtr = NULL;   
    RTpPacket   rtpPacket;
    unsigned int sleptTime;
    SInt32 movieStartOffset = 0; //z
    Bool16      negativeTime = false;
    fMovieDuration = fRTPFilePtr->GetMovieDuration();
    fSendTimeOffset = 0.0;
    fMovieStart = true;
    fNumMoviesPlayed ++;
    
    if (fMovieEndTime > 0) // take into account the movie load time as well as the last movie early end.
    {   UInt64 timeNow = PlayListUtils::Milliseconds();
        fMovieIntervalTime = timeNow - fMovieEndTime;

        SInt32 earlySleepTimeMilli = (SInt32)(fMovieTimeDiffMilli - fMovieIntervalTime);
        earlySleepTimeMilli -= 40; // Don't sleep the entire time we need some time to execute or else we will be late
        if (earlySleepTimeMilli > 0)
        {   OSThread::Sleep( earlySleepTimeMilli);
        }
    }
    
    fMovieStartTime = PlayListUtils::Milliseconds();    
    fMediaStreamList.MovieStarted(fMovieStartTime); 
    
/* changed by [email protected] (see relaod.txt for info) */
    if(mTimeFile!=NULL)
    {
        FILE *fTimeFile = NULL;
        struct timeval start, dur, end;
        struct tm tm_start, tm_dur, tm_end, timeResult;

        memset (&start,0, sizeof(start));

        SInt64 timenow = OS::Milliseconds();
        start.tv_sec = (long) OS::TimeMilli_To_UnixTimeSecs(timenow);
        start.tv_usec = (long) ((OS::TimeMilli_To_UnixTimeMilli(timenow) - (start.tv_sec * 1000)) * 1000);

        dur.tv_sec = (long)fMovieDuration;
        dur.tv_usec = (long)((fMovieDuration - dur.tv_sec) * 1000000);
        
        end.tv_sec = start.tv_sec + dur.tv_sec + (long)((start.tv_usec + dur.tv_usec) / 1000000);
        end.tv_usec = (start.tv_usec + dur.tv_usec) % 1000000;
                time_t startSecs = start.tv_sec;
                time_t endSecs = end.tv_sec;
        memcpy(&tm_start, qtss_localtime(&startSecs, &timeResult), sizeof(struct tm));
        memcpy(&tm_end, qtss_localtime(&endSecs, &timeResult), sizeof(struct tm));

        tm_dur.tm_hour = dur.tv_sec / 3600;
        tm_dur.tm_min = (dur.tv_sec % 3600) / 60;
        tm_dur.tm_sec = (dur.tv_sec % 3600) % 60;
        
        // initialize all current movie parameters to unkown ("-").
        
        ::strcpy(fCurrentMovieName, "-");
        ::strcpy(fCurrentMovieCopyright, "-");
        ::strcpy(fCurrentMovieComment, "-");
        ::strcpy(fCurrentMovieAuthor, "-");
        ::strcpy(fCurrentMovieArtist, "-");
        ::strcpy(fCurrentMovieAlbum, "-");

        /* save start time, stop time and length of currently playing song to .current file */
        fTimeFile = fopen(mTimeFile, "a");
        if(fTimeFile)
        {   
            SimpleString *theQTTextPtr = fMovieSDPParser->fQTTextLines.Begin();
            while (theQTTextPtr != NULL)
            {
                char tmp[256];
                ::memcpy(tmp, theQTTextPtr->fTheString, theQTTextPtr->fLen);
                tmp[theQTTextPtr->fLen] = 0;
                // if this SDP parameter is needed for logging then cache it here so
                // we can log it later.
                if (::strstr(theQTTextPtr->fTheString, "a=x-qt-text-nam:")!=NULL)
                    ::strcpy(fCurrentMovieName, &tmp[16]);
                if (::strstr(theQTTextPtr->fTheString, "a=x-qt-text-cpy:")!=NULL)
                    ::strcpy(fCurrentMovieCopyright, &tmp[16]);
                if (::strstr(theQTTextPtr->fTheString, "a=x-qt-text-cmt:")!=NULL)
                    ::strcpy(fCurrentMovieComment, &tmp[16]);
                if (::strstr(theQTTextPtr->fTheString, "a=x-qt-text-aut:")!=NULL)
                    ::strcpy(fCurrentMovieAuthor, &tmp[16]);
                if (::strstr(theQTTextPtr->fTheString, "a=x-qt-text-ART:")!=NULL)
                    ::strcpy(fCurrentMovieArtist, &tmp[16]);
                if (::strstr(theQTTextPtr->fTheString, "a=x-qt-text-alb:")!=NULL)
                    ::strcpy(fCurrentMovieAlbum, &tmp[16]);
                fwrite(theQTTextPtr->fTheString,theQTTextPtr->fLen, sizeof(char),fTimeFile);
                qtss_fprintf(fTimeFile,"\n");
                theQTTextPtr = fMovieSDPParser->fQTTextLines.Next();
            }

            time_t startTime = (time_t) start.tv_sec;
            time_t endTime = (time_t) end.tv_sec;
            char buffer[kTimeStrSize];
            char *timestringStart = qtss_ctime(&startTime, buffer, sizeof(buffer));
            qtss_fprintf(fTimeFile,"b=%02d:%02d:%02d:%06d %ld %s", (int) tm_start.tm_hour, (int) tm_start.tm_min, (int) tm_start.tm_sec, (int)start.tv_usec, (long int) startTime, timestringStart);
            char *timestringEnd = qtss_ctime(&endTime, buffer, sizeof(buffer));
            qtss_fprintf(fTimeFile,"e=%02d:%02d:%02d:%06d %ld %s", (int)tm_end.tm_hour, (int) tm_end.tm_min,(int)  tm_end.tm_sec, (int) end.tv_usec,(long int) endTime, timestringEnd);
            qtss_fprintf(fTimeFile,"d=%02d:%02d:%02d:%06d %d \n", (int) tm_dur.tm_hour, (int) tm_dur.tm_min,(int)  tm_dur.tm_sec, (int) dur.tv_usec, (int)dur.tv_sec);

            fclose(fTimeFile);
        }   
    }

    while (true) 
    {
        if (fQuitImmediatePtr && *fQuitImmediatePtr){err = 0; break; } // quit now not an error
        
        if (fBroadcastDefPtr->mTheSession)
        {   UInt32 thePacketQLen = 0;
            thePacketQLen = fBroadcastDefPtr->mTheSession->GetPacketQLen();
            SInt64 maxSleep = PlayListUtils::Milliseconds() + 1000; 
            if (thePacketQLen > eMaxPacketQLen)
            {   //qtss_printf("PacketQ too big = %lu \n", (UInt32) thePacketQLen);
                while ( (eMaxPacketQLen/2) < fBroadcastDefPtr->mTheSession->GetPacketQLen())
                {   this->SleepInterval(100.0);
                    if (maxSleep < PlayListUtils::Milliseconds())
                        break;
                }
                //qtss_printf("PacketQ after sleep = %lu \n", (UInt32) fBroadcastDefPtr->mTheSession->GetPacketQLen());
                continue;
            }
        }
        
        
        transmitTime = fRTPFilePtr->GetNextPacket(&rtpPacket.fThePacket, &rtpPacket.fLength);
            theStreamPtr = (MediaStream*)fRTPFilePtr->GetLastPacketTrack()->Cookie1;
        err = fRTPFilePtr->Error();
        if (err != QTRTPFile::errNoError)   {err = eMovieFileInvalid; break; } // error getting packet
        if (NULL == rtpPacket.fThePacket)   {err = 0; break; } // end of movie not an error
        if (NULL == theStreamPtr)           {err = eMovieFileInvalid; break; }// an error

        
        transmitTime *= (Float64) PlayListUtils::eMilli; // convert to milliseconds
        if (transmitTime < 0.0 && negativeTime == false) // Deal with negative transmission times
        {   movieStartOffset += (SInt32) (transmitTime / 15.0);
            negativeTime = true;
        }
        sleptTime = (unsigned int) Sleep(transmitTime);
        
        err = theStreamPtr->Send(&rtpPacket);
            
        if (err != 0)  { break; } 
        err = fMediaStreamList.UpdateStreams();
        if (err != 0)  { break; } 
        
        if (    (fBroadcastDefPtr != NULL)
            &&  (fBroadcastDefPtr->mTheSession != NULL)
            &&  (fBroadcastDefPtr->mTheSession->GetReasonForDying() != BroadcasterSession::kDiedNormally)   
            )   
         { break; } 
    };
    
    fMovieEndTime = (SInt64) PlayListUtils::Milliseconds(); 
    fMediaStreamList.MovieEnded(fMovieEndTime);

    // see if the movie duration is greater than the time it took to send the packets.
    // the difference is a delay that we insert before playing the next movie.
    SInt64 playDurationMilli = (SInt64) fMovieEndTime - (SInt64) fMovieStartTime;
    fMovieTimeDiffMilli =  ((SInt64) ( (Float64) fMovieDuration * (Float64) PlayListUtils::eMilli)) - (SInt64) playDurationMilli;
    fMovieTimeDiffMilli-= (movieStartOffset/2);

    return err;
}