//------------------------------- // dir should end with / int CNativeWrapper::init(JNIEnv * env, void * bitmap, jobject * callback, int width, int height, const char * cfg_dir, const char * modules_dir, const char * cache_dir, const char * font_dir, const char * urlToLoad) { LOGI("Initializing GPAC with URL=%s...", urlToLoad); strcpy(m_cfg_dir, cfg_dir); strcpy(m_modules_dir, modules_dir); strcpy(m_cache_dir, cache_dir); strcpy(m_font_dir, font_dir); char m_cfg_filename[GF_MAX_PATH]; strcpy(m_cfg_filename, m_cfg_dir); strcat(m_cfg_filename, "GPAC.cfg"); int m_Width = width; int m_Height = height; int first_launch = 0; const char *opt; m_window = env; m_session = bitmap; if (!mainJavaEnv) mainJavaEnv = (JavaEnvTh *) gf_malloc(sizeof(JavaEnvTh)); memset(mainJavaEnv, 0, sizeof(JavaEnvTh)); setJavaEnv(mainJavaEnv, env, env->NewGlobalRef(*callback)); if (pthread_setspecific( jni_thread_env_key, mainJavaEnv)) { LOGE("Failed to set specific thread data to jni_thread_env_key=%p for main thread !", jni_thread_env_key); } m_mx = gf_mx_new("Osmo4"); //load config file LOGI("Loading User Config %s...", "GPAC.cfg"); m_user.config = gf_cfg_force_new(cfg_dir, "GPAC.cfg"); gf_set_progress_callback(this, Osmo4_progress_cbk); opt = gf_cfg_get_key(m_user.config, "General", "ModulesDirectory"); if (!opt) { FILE * fstart; char msg[256]; LOGI("First launch, initializing new Config %s...", "GPAC.cfg"); /*hardcode module directory*/ gf_cfg_set_key(m_user.config, "Downloader", "CleanCache", "yes"); /*startup file*/ snprintf(msg, 256, "%sgui/gui.bt", cfg_dir); fstart = fopen(msg, "r"); if (fstart) { fclose(fstart); gf_cfg_set_key(m_user.config, "General", "StartupFile", msg); } else { gf_cfg_set_key(m_user.config, "General", "#StartupFile", msg); } gf_cfg_set_key(m_user.config, "GUI", "UnhideControlPlayer", "1"); /*setup UDP traffic autodetect*/ gf_cfg_set_key(m_user.config, "Network", "AutoReconfigUDP", "yes"); gf_cfg_set_key(m_user.config, "Network", "UDPTimeout", "10000"); gf_cfg_set_key(m_user.config, "Network", "BufferLength", "3000"); gf_cfg_set_key(m_user.config, "Compositor", "TextureTextMode", "Default"); //gf_cfg_set_key(m_user.config, "Compositor", "FrameRate", "30"); gf_cfg_set_key(m_user.config, "Audio", "ForceConfig", "no"); gf_cfg_set_key(m_user.config, "Audio", "NumBuffers", "1"); gf_cfg_set_key(m_user.config, "FontEngine", "FontReader", "ft_font"); } /* All of this has to be done for every instance */ gf_cfg_set_key(m_user.config, "General", "ModulesDirectory", modules_dir ? modules_dir : GPAC_MODULES_DIR); gf_cfg_set_key(m_user.config, "General", "CacheDirectory", cache_dir ? cache_dir : GPAC_CACHE_DIR); gf_cfg_set_key(m_user.config, "General", "LastWorkingDir", cfg_dir); gf_cfg_set_key(m_user.config, "FontEngine", "FontDirectory", GPAC_FONT_DIR); gf_cfg_set_key(m_user.config, "Video", "DriverName", "Android Video Output"); gf_cfg_set_key(m_user.config, "Audio", "DriverName", "Android Audio Output"); opt = gf_cfg_get_key(m_user.config, "General", "ModulesDirectory"); LOGI("loading modules in directory %s...", opt); m_user.modules = gf_modules_new(opt, m_user.config); if (!m_user.modules || !gf_modules_get_count(m_user.modules)) { LOGE("No modules found in directory %s !", opt); if (m_user.modules) gf_modules_del(m_user.modules); gf_cfg_del(m_user.config); m_user.config = NULL; return Quit(KErrGeneral); } /*we don't thread the visual compositor to be able to minimize the app and still have audio running*/ m_user.init_flags = GF_TERM_NO_COMPOSITOR_THREAD; m_user.opaque = this; m_user.os_window_handler = m_window; m_user.os_display = m_session; m_user.EventProc = GPAC_EventProc; if (!javaVM) { LOGE("NO JAVA VM FOUND, m_user=%p !!!!\n", &m_user); return Quit(KErrGeneral); } LOGD("Loading GPAC terminal, m_user=%p...", &m_user); gf_sys_init(GF_FALSE); gf_fm_request_set_callback(this, on_fm_request); SetupLogs(); m_term = gf_term_new(&m_user); if (!m_term) { LOGE("Cannot load GPAC Terminal with m_user=%p", m_user); MessageBox("Cannot load GPAC terminal", "Fatal Error", GF_SERVICE_ERROR); gf_modules_del(m_user.modules); m_user.modules = NULL; gf_cfg_del(m_user.config); m_user.config = NULL; return Quit(KErrGeneral); } //setAudioEnvironment(javaVM); LOGD("Setting term size m_user=%p...", &m_user); gf_term_set_size(m_term, m_Width, m_Height); opt = gf_cfg_get_key(m_user.config, "General", "StartupFile"); LOGD("File loaded at startup=%s.", opt); if (!urlToLoad) urlToLoad = opt; if (urlToLoad) { LOGI("Connecting to %s...", urlToLoad); gf_term_connect(m_term, urlToLoad); } debug_log("init end"); LOGD("Saving config file %s...\n", m_cfg_filename); gf_cfg_save(m_user.config); LOGI("Initialization complete, config file saved as %s.\n", m_cfg_filename); return 0; }
DownloadedCacheEntry gf_cache_create_entry ( GF_DownloadManager * dm, const char * cache_directory, const char * url , u64 start_range, u64 end_range, Bool mem_storage) { char tmp[_CACHE_TMP_SIZE]; u8 hash[_CACHE_HASH_SIZE]; int sz; char ext[_CACHE_MAX_EXTENSION_SIZE]; DownloadedCacheEntry entry = NULL; if ( !dm || !url || !cache_directory) { GF_LOG(GF_LOG_WARNING, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry :%d, dm=%p, url=%s cache_directory=%s, aborting.\n", __LINE__, dm, url, cache_directory)); return entry; } sz = (u32) strlen ( url ); if ( sz > _CACHE_TMP_SIZE ) { GF_LOG(GF_LOG_WARNING, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry:%d : ERROR, URL is too long (%d chars), more than %d chars.\n", __LINE__, sz, _CACHE_TMP_SIZE )); return entry; } tmp[0] = '\0'; /*generate hash of the full url*/ if (start_range && end_range) { sprintf(tmp, "%s_"LLD"-"LLD, url, start_range, end_range ); } else { strcpy ( tmp, url ); } gf_sha1_csum ((u8*) tmp, (u32) strlen ( tmp ), hash ); tmp[0] = 0; { int i; for ( i=0; i<20; i++ ) { char t[3]; t[2] = 0; sprintf ( t, "%02X", hash[i] ); strcat ( tmp, t ); } } assert ( strlen ( tmp ) == (_CACHE_HASH_SIZE * 2) ); GF_SAFEALLOC(entry, struct __DownloadedCacheEntryStruct); if ( !entry ) { GF_LOG(GF_LOG_WARNING, GF_LOG_NETWORK, ("gf_cache_create_entry:%d : OUT of memory !\n", __LINE__)); return NULL; } GF_LOG(GF_LOG_DEBUG, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry:%d, entry=%p\n", __LINE__, entry)); entry->url = gf_strdup ( url ); entry->hash = gf_strdup ( tmp ); entry->memory_stored = mem_storage; entry->cacheSize = 0; entry->contentLength = 0; entry->serverETag = NULL; entry->diskETag = NULL; entry->flags = NO_VALIDATION; entry->validity = 0; entry->diskLastModified = NULL; entry->serverLastModified = NULL; entry->dm = dm; entry->range_start = start_range; entry->range_end = end_range; #ifdef ENABLE_WRITE_MX { char name[1024]; snprintf(name, sizeof(name), "CachedEntryWriteMx=%p, url=%s", (void*) entry, url); entry->write_mutex = gf_mx_new(name); assert(entry->write_mutex); } #endif entry->deletableFilesOnDelete = 0; entry->write_session = NULL; entry->sessions = gf_list_new(); if (entry->memory_stored) { entry->cache_filename = gf_malloc ( strlen ("gmem://") + 8 + strlen("@") + 16 + 1); } else { /* Sizeof cache directory + hash + possible extension */ entry->cache_filename = gf_malloc ( strlen ( cache_directory ) + strlen(cache_file_prefix) + strlen(tmp) + _CACHE_MAX_EXTENSION_SIZE + 1); } if ( !entry->hash || !entry->url || !entry->cache_filename || !entry->sessions) { GF_Err err; /* Probably out of memory */ GF_LOG(GF_LOG_WARNING, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry:%d, aborting due to OUT of MEMORY !\n", __LINE__)); err = gf_cache_delete_entry ( entry ); assert ( err == GF_OK ); return NULL; } if (entry->memory_stored) { sprintf(entry->cache_filename, "gmem://%d@%p", entry->contentLength, entry->mem_storage); return entry; } tmp[0] = '\0'; strcpy ( entry->cache_filename, cache_directory ); strcat( entry->cache_filename, cache_file_prefix ); strcat ( entry->cache_filename, entry->hash ); strcpy ( tmp, url ); { char * parser; parser = strrchr ( tmp, '?' ); if ( parser ) parser[0] = '\0'; parser = strrchr ( tmp, '#' ); if ( parser ) parser[0] = '\0'; parser = strrchr ( tmp, '.' ); if ( parser && ( strlen ( parser ) < _CACHE_MAX_EXTENSION_SIZE ) ) strncpy(ext, parser, _CACHE_MAX_EXTENSION_SIZE); else strncpy(ext, default_cache_file_suffix, _CACHE_MAX_EXTENSION_SIZE); assert (strlen(ext)); strcat( entry->cache_filename, ext); } tmp[0] = '\0'; strcpy( tmp, cache_file_prefix); strcat( tmp, entry->hash ); strcat( tmp , ext); strcat ( tmp, cache_file_info_suffix ); entry->properties = gf_cfg_force_new ( cache_directory, tmp ); if ( !entry->properties ) { GF_Err err; /* OUT of memory ? */ GF_LOG(GF_LOG_WARNING, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry:%d, aborting due to OUT of MEMORY !\n", __LINE__)); err = gf_cache_delete_entry ( entry ); assert ( err == GF_OK ); return NULL; } gf_cache_set_etag_on_disk(entry, gf_cfg_get_key(entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_ETAG)); gf_cache_set_etag_on_server(entry, gf_cfg_get_key(entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_ETAG)); gf_cache_set_mime_type(entry, gf_cfg_get_key(entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_MIME_TYPE)); gf_cache_set_last_modified_on_disk(entry, gf_cfg_get_key(entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_LAST_MODIFIED)); gf_cache_set_last_modified_on_server(entry, gf_cfg_get_key(entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_LAST_MODIFIED)); { const char * keyValue = gf_cfg_get_key ( entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_URL ); if ( keyValue == NULL || stricmp ( url, keyValue ) ) entry->flags |= CORRUPTED; keyValue = gf_cfg_get_key(entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_RANGE); if (keyValue) { u64 s, e; sscanf(keyValue, LLD"-"LLD, &s, &e); /*mark as corrupted if not same range (we don't support this for the time being ...*/ if ((s!=entry->range_start) || (e!=entry->range_end)) entry->flags |= CORRUPTED; } } gf_cache_check_if_cache_file_is_corrupted(entry); return entry; }
int dc_parse_command(int i_argc, char ** p_argv, CmdData * p_cmdd) { int i; char * psz_command_usage = "Usage: DashCast [options]\n" "\n" "Options:\n" "\n" " -a <string> audio stream input source\n" " -v <string> video stream input source\n" " -av <string> multiplexed audio and video input source\n" " -vf <string> input video format (if necessary)\n" " -vfr <int> input video framerate (if necessary)\n" " -vres <intxint> input video resolution (if necessary)\n" " -af <string> input audio format (if necessary)\n" " -live indicates that the system is live\n" " -conf <string> configuration file [default=dashcast.conf]\n" " -seg-dur <int> segment duration in millisecond [default=1000]\n" " -frag-dur <int> fragment duration in millisecond [default=1000]\n" " -mpd <string> MPD file name [default=dashcast.mpd]\n" " -out <string> output data directory name [default=output]\n" "\n"; char * psz_command_error = "\33[31mUnknown option or missing mandatory argument.\33[0m\n"; if (i_argc == 1) { printf("%s", psz_command_usage); return -1; } i = 1; while (i < i_argc) { if (strcmp(p_argv[i], "-a") == 0 || strcmp(p_argv[i], "-v") == 0 || strcmp(p_argv[i], "-av") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (strcmp(p_argv[i - 1], "-a") == 0 || strcmp(p_argv[i - 1], "-av") == 0) { if (strcmp(p_cmdd->adata.psz_name, "") != 0) { printf("Audio source has been already specified.\n"); printf("%s", psz_command_usage); return -1; } //p_cmdd->psz_asrc = malloc(strlen(p_argv[i]) + 1); strcpy(p_cmdd->adata.psz_name, p_argv[i]); strcat(p_cmdd->adata.psz_name, "\0"); } if (strcmp(p_argv[i - 1], "-v") == 0 || strcmp(p_argv[i - 1], "-av") == 0) { if (strcmp(p_cmdd->vdata.psz_name, "") != 0) { printf("Video source has been already specified.\n"); printf("%s", psz_command_usage); return -1; } //p_cmdd->psz_vsrc = malloc(strlen(p_argv[i]) + 1); strcpy(p_cmdd->vdata.psz_name, p_argv[i]); strcat(p_cmdd->vdata.psz_name, "\0"); } i++; } else if (strcmp(p_argv[i], "-af") == 0 || strcmp(p_argv[i], "-vf") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (strcmp(p_argv[i - 1], "-af") == 0) { if (strcmp(p_cmdd->adata.psz_format, "") != 0) { printf("Audio format has been already specified.\n"); printf("%s", psz_command_usage); return -1; } //p_cmdd->psz_af = malloc(strlen(p_argv[i]) + 1); strcpy(p_cmdd->adata.psz_format, p_argv[i]); strcat(p_cmdd->adata.psz_format, "\0"); } if (strcmp(p_argv[i - 1], "-vf") == 0) { if (strcmp(p_cmdd->vdata.psz_format, "") != 0) { printf("Video format has been already specified.\n"); printf("%s", psz_command_usage); return -1; } //p_cmdd->psz_vf = malloc(strlen(p_argv[i]) + 1); strcpy(p_cmdd->vdata.psz_format, p_argv[i]); strcat(p_cmdd->vdata.psz_format, "\0"); } i++; } else if (strcmp(p_argv[i], "-vfr") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (p_cmdd->vdata.i_framerate != -1) { printf("Video framerate has been already specified.\n"); printf("%s", psz_command_usage); return -1; } //p_cmdd->psz_vfr = malloc(strlen(p_argv[i]) + 1); p_cmdd->vdata.i_framerate = atoi(p_argv[i]); //strcpy(p_cmdd->psz_vfr, p_argv[i]); //strcat(p_cmdd->psz_vfr, "\0"); i++; } else if (strcmp(p_argv[i], "-vres") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (p_cmdd->vdata.i_height != -1 && p_cmdd->vdata.i_width != -1 ) { printf("Video framerate has been already specified.\n"); printf("%s", psz_command_usage); return -1; } dc_str_to_resolution(p_argv[i], &p_cmdd->vdata.i_width, &p_cmdd->vdata.i_height); i++; } else if (strcmp(p_argv[i], "-conf") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } p_cmdd->p_conf = gf_cfg_force_new(NULL, p_argv[i]); i++; } else if (strcmp(p_argv[i], "-out") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } strcpy(p_cmdd->psz_out, p_argv[i]); strcat(p_cmdd->psz_out, "\0"); i++; } else if (strcmp(p_argv[i], "-mpd") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (strcmp(p_cmdd->psz_mpd, "") != 0) { printf("MPD file has been already specified.\n"); printf("%s", psz_command_usage); return -1; } strncpy(p_cmdd->psz_mpd, p_argv[i], GF_MAX_PATH); i++; } else if (strcmp(p_argv[i], "-seg-dur") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (p_cmdd->i_seg_dur != 0) { printf("Segment duration has been already specified.\n"); printf("%s", psz_command_usage); return -1; } p_cmdd->i_seg_dur = atoi(p_argv[i]); i++; } else if (strcmp(p_argv[i], "-frag-dur") == 0) { i++; if (i >= i_argc) { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } if (p_cmdd->i_frag_dur != 0) { printf("Fragment duration has been already specified.\n"); printf("%s", psz_command_usage); return -1; } p_cmdd->i_frag_dur = atoi(p_argv[i]); i++; } else if (strcmp(p_argv[i], "-live") == 0) { p_cmdd->i_live = 1; i++; } else { printf("%s", psz_command_error); printf("%s", psz_command_usage); return -1; } } if (strcmp(p_cmdd->psz_mpd, "") == 0) { strcpy(p_cmdd->psz_mpd, "dashcast.mpd"); } if (strcmp(p_cmdd->psz_out, "") == 0) { strcpy(p_cmdd->psz_out, "output/"); struct stat status; if (stat(p_cmdd->psz_out, &status) != 0) { mkdir(p_cmdd->psz_out, 0777); } } if (strcmp(p_cmdd->vdata.psz_name, "") == 0 && strcmp(p_cmdd->adata.psz_name, "") == 0) { printf("Audio/Video source must be specified.\n"); printf("%s", psz_command_usage); return -1; } if(p_cmdd->i_seg_dur == 0) { p_cmdd->i_seg_dur = 1000; } if(p_cmdd->i_frag_dur == 0) { p_cmdd->i_frag_dur = p_cmdd->i_seg_dur; } printf("\33[34m\33[1m"); printf("Options:\n"); printf(" video source: %s\n", p_cmdd->vdata.psz_name); if (strcmp(p_cmdd->vdata.psz_format, "") != 0) { printf(" video format: %s\n", p_cmdd->vdata.psz_format); } if (p_cmdd->vdata.i_framerate != -1) { printf(" video framerate: %d\n", p_cmdd->vdata.i_framerate); } if (p_cmdd->vdata.i_height != -1 && p_cmdd->vdata.i_width != -1) { printf(" video resolution: %dx%d\n", p_cmdd->vdata.i_width, p_cmdd->vdata.i_height); } printf(" audio source: %s\n", p_cmdd->adata.psz_name); if (strcmp(p_cmdd->adata.psz_format, "") != 0) { printf(" audio format: %s\n", p_cmdd->adata.psz_format); } printf("\33[0m"); fflush(stdout); if (!p_cmdd->p_conf) { p_cmdd->p_conf = gf_cfg_force_new(NULL, "dashcast.conf"); } dc_read_configuration(p_cmdd); return 0; }