static int dash_server_onvod(void* /*dash*/, http_session_t* session, const char* /*method*/, const char* path) { char fullpath[PATH_MAX]; int r = path_concat(path + 5 /* /vod/ */, LOCALPATH, fullpath); printf("vod: %s\n", fullpath); if (0 == r && path_testfile(fullpath)) { // MIME if (strendswith(fullpath, ".mpd")) http_server_set_content_type(session, "application/xml+dash"); else if (strendswith(fullpath, ".mp4") || strendswith(fullpath, ".m4v")) http_server_set_header(session, "content-type", "video/mp4"); else if (strendswith(fullpath, ".m4a")) http_server_set_header(session, "content-type", "audio/mp4"); //http_server_set_header(session, "Transfer-Encoding", "chunked"); // cross domain http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); return http_server_sendfile(session, fullpath, NULL, NULL); } return http_server_send(session, 404, "", 0, NULL, NULL); }
static int hls_server_ts(void* session, const std::string& path, const std::string& ts) { hls_playlist_t* playlist = s_playlists.find(path)->second; assert(playlist); std::string file = path + '/' + ts; for(auto i = playlist->files.begin(); i != playlist->files.end(); ++i) { if(i->name == file) { void* bundle = http_bundle_alloc(i->size); void* ptr = http_bundle_lock(bundle); memcpy(ptr, i->data, i->size); http_bundle_unlock(bundle, i->size); http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); http_server_send(session, 200, bundle); http_bundle_free(bundle); printf("load file %s\n", file.c_str()); return 0; } } printf("load ts file(%s) failed\n", file.c_str()); return hls_server_reply(session, 404, ""); }
static int dash_server_mpd(http_session_t* session, dash_playlist_t* dash) { http_server_set_header(session, "Content-Type", "application/xml+dash"); http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); AutoThreadLocker locker(s_locker); http_server_reply(session, 200, dash->playlist, strlen(dash->playlist)); return 0; }
static int hls_server_reply(void* session, int code, const char* msg) { void* ptr; void* bundle; bundle = http_bundle_alloc(strlen(msg) + 1); ptr = http_bundle_lock(bundle); strcpy((char*)ptr, msg); http_bundle_unlock(bundle, strlen(msg) + 1); http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); http_server_send(session, code, bundle); http_bundle_free(bundle); return 0; }
int WebSession::Send(int code, const char* contentType, const void* data, int len) { http_server_set_header(m_http, "Server", "MD WebServer 0.1"); http_server_set_header(m_http, "Connection", "keep-alive"); http_server_set_header(m_http, "Keep-Alive", "timeout=5,max=100"); http_server_set_header(m_http, "Content-Type", contentType); //http_server_set_header(m_http, "Content-Type", "text/html; charset=utf-8"); http_server_set_header_int(m_http, "Content-Length", len); int r = http_server_send(m_http, code, (void*)data, len); if(len > 0 && len < 2*1024) { dlog_log("%s\n", (const char*)data); } return r; }
static int hls_server_reply_file(void* session, const char* file) { static char buffer[4 * 1024 * 1024]; StdCFile f(file, "rb"); int r = f.Read(buffer, sizeof(buffer)); void* ptr; void* bundle; bundle = http_bundle_alloc(r); ptr = http_bundle_lock(bundle); memcpy(ptr, buffer, r); http_bundle_unlock(bundle, r); http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); http_server_send(session, 200, bundle); http_bundle_free(bundle); return 0; }
static int hls_server_m3u8(void* session, const std::string& path) { void* m3u8 = s_playlists.find(path)->second->m3u8; assert(m3u8); void* bundle = http_bundle_alloc(4 * 1024); void* ptr = http_bundle_lock(bundle); assert(0 == hls_m3u8_playlist(m3u8, 0, (char*)ptr, 4 * 1024)); http_bundle_unlock(bundle, strlen((char*)ptr)); http_server_set_header(session, "content-type", HLS_M3U8_TYPE); http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); http_server_send(session, 200, bundle); http_bundle_free(bundle); printf("load %s.m3u8 file\n", path.c_str()); return 0; }
static int dash_server_onlive(void* dash, http_session_t* session, const char* /*method*/, const char* path) { char fullpath[PATH_MAX]; int r = path_concat(path + 6 /* /live/ */, LOCALPATH, fullpath); printf("live: %s\n", fullpath); const char* name = path_basename(fullpath); if (strendswith(name, ".mpd")) { return dash_server_mpd(session, (dash_playlist_t*)dash); } else if (path_testfile(name)) { // cross domain http_server_set_header(session, "Access-Control-Allow-Origin", "*"); http_server_set_header(session, "Access-Control-Allow-Methods", "GET, POST, PUT"); return http_server_sendfile(session, name, NULL, NULL); } return http_server_send(session, 404, "", 0, NULL, NULL); }