4 * Created on: 2013. 9. 12.
13 #include <sys/ioctl.h>
20 #include "ePreDefine.h"
21 #include "eURIDecoder.h"
22 #include "eFilePumpThread.h"
23 #include "eNetworkPumpThread.h"
26 FILE* fpLog = fopen("/tmp/filestreamproxy.log", "w");
28 //#define LOG(X,...) { do{}while(0); }
32 //-------------------------------------------------------------------------------
36 char* ReadRequest(char* aRequest)
38 return fgets(aRequest, MAX_LINE_LENGTH-1, stdin);
40 //-------------------------------------------------------------------------------
43 int gVideoPid = 0, gAudioPid = 0;
44 std::vector<string> Split(std::string aBuffer, char aDelimiter);
45 void FileName(char* aRequest, char* aHttp, std::string& aOutData);
46 bool MetaData(std::string aMediaFileName);
48 using namespace eParser;
49 //-------------------------------------------------------------------------------
51 /* GET /file?file=/home/kos/work/workspace/filestreamproxy/data/20131023%200630%20-%20FASHION%20TV%20-%20instant%20record.ts HTTP/1.0 */
52 int main(int argc, char** argv)
54 char request[MAX_LINE_LENGTH] = {0};
56 if (!ReadRequest(request)) {
63 if (strncmp(request, "GET /", 5)) {
67 char* http = strchr(request + 5, ' ');
68 if (!http || strncmp(http, " HTTP/1.", 7)) {
69 RETURN_ERR_400("Not support request (%s).", http);
72 std::string srcfilename = "";
73 eParser::FileName(request, http, srcfilename);
75 bool isSuccessMeta = eParser::MetaData(srcfilename);
78 LOG("meta parsing result : %d, video : %d, audio : %d", isSuccessMeta, eParser::gVideoPid, eParser::gAudioPid);
81 gDeviceFd = open("/dev/bcm_enc0", O_RDWR);
84 RETURN_ERR_502("Fail to opne device.");
88 if(ioctl(gDeviceFd, 1, eParser::gVideoPid)) {
89 RETURN_ERR_502("Fail to set video pid");
91 if(ioctl(gDeviceFd, 2, eParser::gAudioPid)) {
92 RETURN_ERR_502("Fail to set audio pid");
96 eFilePumpThread filepump(gDeviceFd, srcfilename);
101 if(ioctl(gDeviceFd, 100, 0)) {
102 RETURN_ERR_502("Fail to start transcoding.");
104 eNetworkPumpThread networkpump(gDeviceFd);
118 //-------------------------------------------------------------------------------
120 std::vector<string> eParser::Split(std::string aBuffer, char aDelimiter)
122 int b = 0, i = 0, l = aBuffer.length();
123 std::vector<string> t;
126 if (aBuffer[i] == aDelimiter) {
127 t.push_back(aBuffer.substr(b, i-b));
132 t.push_back(aBuffer.substr(b, l));
137 //-------------------------------------------------------------------------------
139 void eParser::FileName(char* aRequest, char* aHttp, std::string& aOutData)
142 char* file = aRequest + 5;
143 if (strncmp(file, "file?file=", strlen("file?file="))) {
146 strncpy(tmp, file+10, aHttp-file-10);
147 aOutData = eURIDecoder().Decode(tmp);
149 //-------------------------------------------------------------------------------
151 /* f:40,c:00007b,c:01008f,c:03007b */
152 bool eParser::MetaData(std::string aMediaFileName)
154 std::string metafilename = aMediaFileName;
155 metafilename += ".meta";
157 std::ifstream ifs(metafilename.c_str());
159 if (!ifs.is_open()) {
161 LOG("metadata is not exists..");
166 size_t rc = 0, i = 0;
167 char buffer[1024] = {0};
169 ifs.getline(buffer, 1024);
172 LOG("%d [%s]", i, buffer);
174 std::vector<string> tokens = eParser::Split(buffer, ',');
175 if(tokens.size() < 3) {
177 LOG("pid count size error : %d", tokens.size());
182 for (int ii = 0; ii < tokens.size(); ++ii) {
183 std::string token = tokens[ii];
185 LOG("token : %d [%s]", ii, token.c_str());
189 gVideoPid = strtol(token.substr(4,8).c_str(), NULL, 16);
191 LOG("video pid : %d", gVideoPid);
195 gAudioPid = strtol(token.substr(4,8).c_str(), NULL, 16);
198 LOG("audio pid : %d", gAudioPid);
209 //-------------------------------------------------------------------------------