Skip to content

Instantly share code, notes, and snippets.

@takuya
Created November 7, 2014 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuya/086b652dd6e18c3c4239 to your computer and use it in GitHub Desktop.
Save takuya/086b652dd6e18c3c4239 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <librtmp/rtmp.h>
int main(){
int swfAge = 30; /* 30 days for SWF cache by default */
int swfVfy = 0;
unsigned char hash[RTMP_SWF_HASHLEN];
uint32_t bufferTime = (10 * 60 * 60 * 1000);
// return 0;
RTMP rtmp = { 0 };
int protocol = RTMP_PROTOCOL_UNDEFINED;
AVal hostname = { 0, 0 };
int port = 1935;
AVal playpath = { "NetRadio_R1_flash@63346", 23 };
AVal sockshost = { 0, 0 };
AVal tcUrl = { "rtmpe://netradio-r1-flash.nhk.jp:1935/live", 42};
AVal swfUrl = { "http://www3.nhk.or.jp/netradio/files/swf/rtmpe.swf", 50 };
AVal pageUrl = { 0, 0 };
AVal app = { "live", 0 };
AVal auth = { 0, 0 };
AVal swfHash = { 0, 0 };
uint32_t swfSize = 0;
AVal flashVer = { 0, 0 };
AVal subscribepath = { 0, 0 };
AVal usherToken = { 0, 0 };
uint32_t dSeek = 0;
uint32_t dStopOffset = 0;
int bLiveStream = TRUE;
long int timeout = 10;
int parsedProtocol = RTMP_PROTOCOL_UNDEFINED;
AVal parsedHost, parsedApp, parsedPlaypath;
unsigned int parsedPort = 0;
char *url = "rtmpe://netradio-r1-flash.nhk.jp";
RTMP_ParseURL(url, &parsedProtocol, &parsedHost, &parsedPort,
&parsedPlaypath, &parsedApp);
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;
}
RTMP_HashSWF(swfUrl.av_val, &swfSize, hash, swfAge);
swfHash.av_val = (char *)hash;
swfHash.av_len = RTMP_SWF_HASHLEN;
// printf("protocol=%d\n",protocol);
// printf("hostname=%s\n",hostname);
// printf("port=%d\n",port);
// printf("sockshost=%s\n",sockshost);
// printf("playpath=%s\n",playpath);
// printf("tcUrl=%s\n",tcUrl);
// printf("swfUrl=%s\n",swfUrl);
// printf("pageUrl=%s\n",pageUrl);
// printf("app=%s\n",app);
// printf("auth=%s\n",auth);
// printf("swfHash=%d\n",swfHash);
// printf("swfSize=%d\n",swfSize);
// printf("flashVer=%s\n",flashVer);
// printf("subscribepath=%s\n",subscribepath);
// printf("usherToken=%s\n",usherToken);
// printf("dSeek=%s\n",dSeek);
// printf("dStopOffset=%s\n",dStopOffset);
// printf("bLiveStream=%d\n",bLiveStream);
// printf("timeout=%d\n",timeout);
// exit(0);
RTMP_Init(&rtmp);
RTMP_SetupURL(&rtmp, url);
RTMP_SetupStream(&rtmp, protocol, &hostname, port, &sockshost, &playpath,
&tcUrl, &swfUrl, &pageUrl, &app, &auth, &swfHash, swfSize,
&flashVer, &subscribepath, &usherToken, dSeek, dStopOffset, bLiveStream, timeout);
RTMP_SetBufferMS(&rtmp, bufferTime);
RTMP_Connect(&rtmp, NULL);
RTMP_ConnectStream(&rtmp, dSeek);
//printf("接続:%d\n",RTMP_IsConnected(&rtmp));exit(0);
int bufferSize = 64 * 1024;
char *buffer;
int nRead = 0;
buffer = (char *) malloc(bufferSize);
do {
nRead = RTMP_Read(&rtmp, buffer, bufferSize);
if (nRead > 0) {
if(fwrite(buffer, sizeof(unsigned char), nRead, stdout) !=(size_t) nRead){
free(buffer);
return 1;
}
}
} while ( nRead > -1 && RTMP_IsConnected(&rtmp) && !RTMP_IsTimedout(&rtmp) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment