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 //-------------------------------------------------------------------------------
166 //-------------------------------------------------------------------------------
168 /* f:40,c:00007b,c:01008f,c:03007b */
169 bool eParser::MetaData(std::string aMediaFileName)
171 std::string metafilename = aMediaFileName;
172 metafilename += ".meta";
174 std::ifstream ifs(metafilename.c_str());
176 if (!ifs.is_open()) {
178 LOG("metadata is not exists..");
183 size_t rc = 0, i = 0;
184 char buffer[1024] = {0};
186 ifs.getline(buffer, 1024);
189 LOG("%d [%s]", i, buffer);
191 std::vector<string> tokens = eParser::Split(buffer, ',');
192 if(tokens.size() < 3) {
194 LOG("pid count size error : %d", tokens.size());
199 int setting_done = false;
200 for (int ii = 0; ii < tokens.size(); ++ii) {
201 std::string token = tokens[ii];
202 if(token.at(0) != 'c') continue;
204 int cache_id = atoi(token.substr(2,2).c_str());
206 LOG("token : %d [%s], chcke_id : [%d]", ii, token.c_str(), cache_id);
209 case(eCacheID::cVPID):
210 gVideoPid = strtol(token.substr(4,4).c_str(), NULL, 16);
212 LOG("video pid : %d", gVideoPid);
214 setting_done = (gVideoPid && gAudioPid) ? true : false;
216 case(eCacheID::cAC3PID):
217 gAudioPid = strtol(token.substr(4,4).c_str(), NULL, 16);
219 LOG("audio pid : %d", gAudioPid);
222 case(eCacheID::cAPID):
223 gAudioPid = strtol(token.substr(4,4).c_str(), NULL, 16);
225 LOG("audio pid : %d", gAudioPid);
227 setting_done = (gVideoPid && gAudioPid) ? true : false;
230 if(setting_done) break;
238 //-------------------------------------------------------------------------------