1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
// RTP Protocol.cpp*: définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "time.h"
bool Connection_Internet=FALSE;
LPCWSTR TOOL_Convert_to_LPC(char *ctl);
SOCKET IDP;
#define RD_SUCCESS 0
#define RD_FAILED 1
#define RD_INCOMPLETE 2
#define RD_NO_CONNECT 3
bool BETA_MODE=FALSE;
#define IR_SIZE_PAGE 10000
#define DEF_TIMEOUT 30 /* seconds */
#define DEF_BUFTIME (10 * 60 * 60 * 1000) /* 10 hours default */
char *IPage[20];
char Temporary[1024];
#include "rtmp.h"
FILE *file;
int nStatus = 1;
double percent = 0;
int nSkipKeyFrames = 0; // skip this number of keyframes when resuming
int bOverrideBufferTime = FALSE; // if the user specifies a buffer time override this is true
int bStdoutMode = TRUE; // if true print the stream directly to stdout, messages go to stderr
int bResume = FALSE; // true in resume mode
uint32_t dSeek = 0; // seek position in resume mode, 0 otherwise
uint32_t bufferTime = DEF_BUFTIME;
// meta header and initial frame for the resume mode (they are read from the file and compared with
// the stream we are trying to continue
char *metaHeader = 0;
uint32_t nMetaHeaderSize = 0;
// video keyframe for matching
char *initialFrame = 0;
uint32_t nInitialFrameSize = 0;
int initialFrameType = 0; // tye: audio or video
AVal hostname = { 0, 0 };
AVal playpath = { 0, 0 };
AVal subscribepath = { 0, 0 };
AVal usherToken = { 0, 0 }; //Justin.tv auth token
int port = -1;
int protocol = RTMP_PROTOCOL_UNDEFINED;
int retries = 0;
int bLiveStream = FALSE; // is it a live stream? then we can't seek/resume
int bHashes = FALSE; // display byte counters not hashes by default
long int timeout = DEF_TIMEOUT; // timeout connection after 120 seconds
uint32_t dStartOffset = 0; // seek position in non-live mode
uint32_t dStopOffset = 0;
RTMP rtmp = { 0 };
AVal swfUrl = { 0, 0 };
AVal tcUrl = { 0, 0 };
AVal pageUrl = { 0, 0 };
AVal app = { 0, 0 };
AVal auth = { 0, 0 };
AVal swfHash = { 0, 0 };
uint32_t swfSize = 0;
AVal flashVer = { 0, 0 };
AVal sockshost = { 0, 0 };
int Download(RTMP * rtmp, // connected RTMP object
FILE * file, uint32_t dSeek, uint32_t dStopOffset, double duration, int bResume, char *metaHeader, uint32_t nMetaHeaderSize, char *initialFrame, int initialFrameType, uint32_t nInitialFrameSize, int nSkipKeyFrames, int bStdoutMode, int bLiveStream, int bHashes, int bOverrideBufferTime, uint32_t bufferTime, double *percent);
int DownloadODC(RTMP * rtmp);
int DownloadB(RTMP * rtmp, // connected RTMP object
FILE * file, uint32_t dSeek, uint32_t dStopOffset, double duration, int bResume, char *metaHeader, uint32_t nMetaHeaderSize, char *initialFrame, int initialFrameType, uint32_t nInitialFrameSize, int nSkipKeyFrames, int bStdoutMode, int bLiveStream, int bHashes, int bOverrideBufferTime, uint32_t bufferTime, double *percent);
#include "Internet.cpp"
//typedef void RTMP_Init(RTMP *r);
RTMPPacket rtmpack;
time_t Ref_Time=0,CTime=0,Cursor=0,ITime=0,SynopsisTime=0,BGTime=0;
long duration=1;
long Essai=0;
long ATC=0;
long NTC=0;
typedef void ORTMP_INIT(RTMP *r);
//typedef void ORTMP_INIT(RTMP *r);
typedef int ORTMP_LIBVERSION(void);
typedef RTMP *ORTMP_ALLOC(void);
typedef int ORTMP_CONNECT(RTMP *r, RTMPPacket *cp);
typedef int ORTMP_SETUPURL(RTMP *r, char *url);
typedef int ORTMP_CONNECTSTREAM(RTMP *r, int seekTime);
typedef int ORTMP_READ(RTMP *r, char *buf, int size);
typedef int ORTMP_PARSEURL(const char *url, int *protocol, AVal *host,unsigned int *port, AVal *playpath, AVal *app);
typedef void ORTMP_SETUPSTREAM(RTMP *r, int protocol,AVal *hostname, unsigned int port, AVal *sockshost,AVal *playpath, AVal *tcUrl,AVal *swfUrl,AVal *pageUrl, AVal *app, AVal *auth, AVal *swfSHA256Hash,uint32_t swfSize,AVal *flashVer,AVal *subscribepath,AVal *usherToken,int dStart,int dStop, int bLiveStream, long int timeout);
typedef void ORTMP_SETBUFFERMS(RTMP *r, int size);
typedef double ORTMP_GETDURATION(RTMP *r);
typedef int ORTMP_ISCONNECTED(RTMP *r);
typedef int ORTMP_SOCKET(RTMP *r);
typedef int ORTMP_ISTIMEDOUT(RTMP *r);
typedef void ORTMP_UPDATEBUFFERMS(RTMP *r);
typedef void ORTMP_CLOSE(RTMP *r);
ORTMP_ISCONNECTED * ORTMP_IsConnected;
ORTMP_ISTIMEDOUT * ORTMP_IsTimedout;
ORTMP_SETBUFFERMS * ORTMP_SetBufferMS ;
ORTMP_UPDATEBUFFERMS * ORTMP_UpdateBufferMS;
ORTMP_READ * ORTMP_Read;
ORTMP_GETDURATION * ORTMP_GetDuration;
int _tmain(int argc, _TCHAR* argv[]){
HMODULE module;
module = LoadLibrary(L"librtmp.dll");
ORTMP_INIT * ORTMP_Init = (ORTMP_INIT *)GetProcAddress(module, "RTMP_Init");
ORTMP_LIBVERSION * ORTMP_LibVersion = (ORTMP_LIBVERSION *)GetProcAddress(module, "RTMP_LibVersion");
ORTMP_ALLOC * ORTMP_Alloc = (ORTMP_ALLOC *)GetProcAddress(module, "RTMP_Alloc");
ORTMP_SETUPURL * ORTMP_SetupURL = (ORTMP_SETUPURL *)GetProcAddress(module, "RTMP_SetupURL");
ORTMP_CONNECT * ORTMP_Connect = (ORTMP_CONNECT *)GetProcAddress(module, "RTMP_Connect");
ORTMP_CONNECTSTREAM * ORTMP_ConnectStream = (ORTMP_CONNECTSTREAM *)GetProcAddress(module, "RTMP_ConnectStream");
ORTMP_Read = (ORTMP_READ *)GetProcAddress(module, "RTMP_Read");
ORTMP_PARSEURL * ORTMP_ParseURL = (ORTMP_PARSEURL *)GetProcAddress(module, "RTMP_ParseURL");
ORTMP_SETUPSTREAM * ORTMP_SetupStream = (ORTMP_SETUPSTREAM *)GetProcAddress(module, "RTMP_SetupStream");
ORTMP_SetBufferMS = (ORTMP_SETBUFFERMS *)GetProcAddress(module, "RTMP_SetBufferMS");
ORTMP_GetDuration = (ORTMP_GETDURATION *)GetProcAddress(module, "RTMP_GetDuration");
ORTMP_IsConnected = (ORTMP_ISCONNECTED *)GetProcAddress(module, "RTMP_IsConnected");
ORTMP_SOCKET * ORTMP_Socket = (ORTMP_SOCKET *)GetProcAddress(module, "RTMP_Socket");
ORTMP_IsTimedout = (ORTMP_ISTIMEDOUT *)GetProcAddress(module, "RTMP_IsTimedout");
ORTMP_UpdateBufferMS=(ORTMP_UPDATEBUFFERMS *)GetProcAddress(module,"RTMP_UpdateBufferMS");
ORTMP_CLOSE * ORTMP_Close = (ORTMP_CLOSE *)GetProcAddress(module, "RTMP_Close");
uint32_t dSeek = 0; // seek position in resume mode, 0 otherwise
long duration = 0.0;
char BUFFER[10240];
InitWinsockV3();
ORTMP_Alloc();
ORTMP_Init(&rtmp);
AVal parsedHost, parsedApp, parsedPlaypath;
unsigned int parsedPort = 0;
int parsedProtocol = RTMP_PROTOCOL_UNDEFINED;
if (!ORTMP_ParseURL
("rtmp://stream2.lgdf.yacast.net/gulli_replay/12015VOD258.mp4", &parsedProtocol, &parsedHost, &parsedPort,&parsedPlaypath, &parsedApp))
{
}
else
{
if (!hostname.av_len)
hostname = parsedHost;
if (port == -1)
port = parsedPort;
if (playpath.av_len == 0 && parsedPlaypath.av_len)
{
playpath = parsedPlaypath;
}
if (protocol == RTMP_PROTOCOL_UNDEFINED)
protocol = parsedProtocol;
if (app.av_len == 0 && parsedApp.av_len)
{
app = parsedApp;
}
}
ORTMP_SetupStream(&rtmp, protocol, &hostname, port, &sockshost, &playpath,
&tcUrl, &swfUrl, &pageUrl, &app, &auth, &swfHash, swfSize,
&flashVer, &subscribepath, &usherToken, dSeek, dStopOffset, bLiveStream, timeout);
long count=0;
ORTMP_SetBufferMS(&rtmp, bufferTime);
duration=1;
while(Essai<3){
if(Essai==1)file = fopen("c:\\ITWORKS.mp4", "w+b");
ATC=GetTickCount();
ORTMP_Connect(&rtmp, NULL);
ORTMP_ConnectStream(&rtmp, dSeek);
dSeek=DownloadODC(&rtmp);
ORTMP_Close(&rtmp);
Essai++;
if(Essai>3)break;
}
if(Essai==1)fclose(file);
free(initialFrame);
initialFrame = NULL;
printf("count : %d",count);
return 0;
}
int DownloadODC(RTMP * rtmp){
int32_t now=0, lastUpdate=0;
int bufferSize = 256 * 1024;
char *buffer;
int nRead = 0;
off_t size = 0;
unsigned long lastPercent = 0;
buffer = (char *) malloc(bufferSize);
while(dSeek<=duration*1000){
lastUpdate=GetTickCount();
while(abs(now-lastUpdate)>10){
now=GetTickCount();
nRead = ORTMP_Read(rtmp, buffer, bufferSize);
duration=ORTMP_GetDuration(rtmp);
dSeek=rtmp->m_read.timestamp;
NTC=GetTickCount();
printf("Duration %d/%d - - - %d\n\r",dSeek/1000,duration,(NTC-ATC)/1000);
if (nRead > 0){
dSeek=rtmp->m_read.timestamp;
if (Essai==1 && fwrite(buffer, sizeof(unsigned char), nRead, file) !=
(size_t) nRead)
{
printf("%s: Failed writing, exiting!", __FUNCTION__);
free(buffer);
return -2;
}
size += nRead;
}
else return dSeek;
}
}
return 0;
}
int DownloadB(RTMP * rtmp, // connected RTMP object
FILE * file, uint32_t dSeek, uint32_t dStopOffset, double duration, int bResume, char *metaHeader, uint32_t nMetaHeaderSize, char *initialFrame, int initialFrameType, uint32_t nInitialFrameSize, int nSkipKeyFrames, int bStdoutMode, int bLiveStream, int bHashes, int bOverrideBufferTime, uint32_t bufferTime, double *percent) // percentage downloaded [out]
{
bool STOPIT=0;
int32_t now=0, lastUpdate=0;
int bufferSize = 64 * 1024;
char *buffer;
int nRead = 0;
off_t size = 0;
unsigned long lastPercent = 0;
buffer = (char *) malloc(bufferSize);
while(STOPIT==0){
nRead = ORTMP_Read(rtmp, buffer, bufferSize);
if(nRead==0)Sleep(2000);
printf("%d / %d\r\n\r",nRead, size);
if (nRead > 0)
{
dSeek=rtmp->m_read.timestamp;
if (fwrite(buffer, sizeof(unsigned char), nRead, file) !=
(size_t) nRead)
{
printf("%s: Failed writing, exiting!", __FUNCTION__);
free(buffer);
return -2;
}
size += nRead;
}
else STOPIT=1;
}
return 0;
}
int Download(RTMP * rtmp, // connected RTMP object
FILE * file, uint32_t dSeek, uint32_t dStopOffset, double duration, int bResume, char *metaHeader, uint32_t nMetaHeaderSize, char *initialFrame, int initialFrameType, uint32_t nInitialFrameSize, int nSkipKeyFrames, int bStdoutMode, int bLiveStream, int bHashes, int bOverrideBufferTime, uint32_t bufferTime, double *percent) // percentage downloaded [out]
{
int32_t now=0, lastUpdate=0;
int bufferSize = 64 * 1024;
char *buffer;
int nRead = 0;
off_t size = ftell(file);
unsigned long lastPercent = 0;
rtmp->m_read.timestamp = dSeek;
*percent = 0.0;
if (rtmp->m_read.timestamp)
{
printf("Continuing at TS: %d ms\n\r", rtmp->m_read.timestamp);
}
if (bLiveStream)
{
printf("Starting Live Stream\n\r");
}
else
{
// print initial status
// Workaround to exit with 0 if the file is fully (> 99.9%) downloaded
if (duration > 0)
{
if ((double) rtmp->m_read.timestamp >= (double) duration * 999.0)
{
printf("Already Completed at: %.3f sec Duration=%.3f sec\n\r",
(double) rtmp->m_read.timestamp / 1000.0,
(double) duration / 1000.0);
return 1;
}
else
{
*percent = ((double) rtmp->m_read.timestamp) / (duration * 1000.0) * 100.0;
*percent = ((double) (int) (*percent * 10.0)) / 10.0;
printf("%s download at: %.3f kB / %.3f sec (%.1f%%)\n\r",
bResume ? "Resuming" : "Starting",
(double) size / 1024.0, (double) rtmp->m_read.timestamp / 1000.0,
*percent);
}
}
else
{
printf("%s download at: %.3f kB\n\r",
bResume ? "Resuming" : "Starting",
(double) size / 1024.0);
}
}
if (dStopOffset > 0)
printf("For duration: %.3f sec\n\r", (double) (dStopOffset - dSeek) / 1000.0);
if (bResume && nInitialFrameSize > 0)
rtmp->m_read.flags |= RTMP_READ_RESUME;
rtmp->m_read.initialFrameType = initialFrameType;
rtmp->m_read.nResumeTS = dSeek;
rtmp->m_read.metaHeader = metaHeader;
rtmp->m_read.initialFrame = initialFrame;
rtmp->m_read.nMetaHeaderSize = nMetaHeaderSize;
rtmp->m_read.nInitialFrameSize = nInitialFrameSize;
buffer = (char *) malloc(bufferSize);
now = GetTickCount();
lastUpdate = now - 1000;
do{
nRead = ORTMP_Read(rtmp, buffer, bufferSize);
//RTMP_LogPrintf("nRead: %d\n\r", nRead);
if (nRead > 0)
{
if (fwrite(buffer, sizeof(unsigned char), nRead, file) !=
(size_t) nRead)
{
printf("%s: Failed writing, exiting!", __FUNCTION__);
free(buffer);
return -2;
}
size += nRead;
if (duration <= 0) // if duration unknown try to get it from the stream (onMetaData)
duration = ORTMP_GetDuration(rtmp);
if (duration > 0){
// make sure we claim to have enough buffer time!
if (!bOverrideBufferTime && bufferTime < (duration * 1000.0)) {
bufferTime = (uint32_t) (duration * 1000.0) + 5000; // extra 5sec to make sure we've got enough
printf("Detected that buffer time is less than duration, resetting to: %dms",bufferTime);
ORTMP_SetBufferMS(rtmp, bufferTime);
ORTMP_UpdateBufferMS(rtmp);
}
*percent = ((double) rtmp->m_read.timestamp) / (duration * 1000.0) * 100.0;
*percent = ((double) (int) (*percent * 10.0)) / 10.0;
if (bHashes) {
if (lastPercent + 1 <= *percent) {
// RTMP_LogStatus("#");
lastPercent = (unsigned long) *percent;
}
}
else {
if (abs(now - lastUpdate) > 200) {
printf("\r%.3f kB / %.2f sec (%.1f%%)",(double) size / 1024.0,(double) (rtmp->m_read.timestamp) / 1000.0, *percent);
lastUpdate = now;
}
}
}
else {
if (abs(now - lastUpdate) > 200) {
if (bHashes)
printf("#");
else
printf("\r%.3f kB / %.2f sec", (double) size / 1024.0,
(double) (rtmp->m_read.timestamp) / 1000.0);
lastUpdate = now;
}
}
}
#ifdef _DEBUG
else {
}
#endif
}
while ( nRead > -1 && ORTMP_IsConnected(rtmp));
free(buffer);
if (nRead < 0)
nRead = rtmp->m_read.status;
/* Final status update */
if (!bHashes)
{
if (duration > 0)
{
*percent = ((double) rtmp->m_read.timestamp) / (duration * 1000.0) * 100.0;
*percent = ((double) (int) (*percent * 10.0)) / 10.0;
printf("\r%.3f kB / %.2f sec (%.1f%%)",(double) size / 1024.0, (double) (rtmp->m_read.timestamp) / 1000.0, *percent);
}
else
{
printf("\r%.3f kB / %.2f sec", (double) size / 1024.0, (double) (rtmp->m_read.timestamp) / 1000.0);
}
}
if (bResume && nRead == -2)
{
printf("Couldn't resume FLV file, try --skip %d\n\n\r",nSkipKeyFrames + 1);
return -1;
}
if (nRead == -3)
return 0;
/*
if ((duration > 0 && *percent < 99.9) || nRead < 0
|| ORTMP_IsTimedout(rtmp))
{
return -666;
}*/
return 0;
} |
Partager