static u32 check_existing_file(char *base_file, char *ext, char *data, u32 data_size, u32 idx) { char szFile[GF_MAX_PATH]; u64 fsize; FILE *f; sprintf(szFile, "%s%04X%s", base_file, idx, ext); f = gf_f64_open(szFile, "rb"); if (!f) return 0; gf_f64_seek(f, 0, SEEK_END); fsize = gf_f64_tell(f); if (fsize==data_size) { u32 offset=0; char cache[1024]; gf_f64_seek(f, 0, SEEK_SET); while (fsize) { u32 read = (u32) fread(cache, 1, 1024, f); fsize -= read; if (memcmp(cache, data+offset, sizeof(char)*read)) break; offset+=read; } fclose(f); f = NULL; /*same file*/ if (!fsize) return 2; } if (f) fclose(f); return 1; }
static GF_Err IMG_ConnectService(GF_InputService *plug, GF_ClientService *serv, const char *url) { char *sExt; IMGLoader *read = (IMGLoader *)plug->priv; read->service = serv; if (!url) return GF_BAD_PARAM; sExt = strrchr(url, '.'); if (!stricmp(sExt, ".jpeg") || !stricmp(sExt, ".jpg")) read->img_type = IMG_JPEG; else if (!stricmp(sExt, ".png")) read->img_type = IMG_PNG; else if (!stricmp(sExt, ".pngd")) read->img_type = IMG_PNGD; else if (!stricmp(sExt, ".pngds")) read->img_type = IMG_PNGDS; else if (!stricmp(sExt, ".pngs")) read->img_type = IMG_PNGS; else if (!stricmp(sExt, ".bmp")) read->img_type = IMG_BMP; if (read->dnload) gf_service_download_del(read->dnload); read->dnload = NULL; /*remote fetch*/ if (!jp_is_local(url)) { jp_download_file(plug, url); return GF_OK; } read->stream = fopen(url, "rb"); if (read->stream) { gf_f64_seek(read->stream, 0, SEEK_END); read->data_size = (u32) gf_f64_tell(read->stream); gf_f64_seek(read->stream, 0, SEEK_SET); } gf_service_connect_ack(serv, NULL, read->stream ? GF_OK : GF_URL_ERROR); if (read->stream && read->is_inline) IMG_SetupObject(read); return GF_OK; }
GF_EXPORT char *gf_rtp_streamer_format_sdp_header(char *app_name, char *ip_dest, char *session_name, char *iod64) { u64 size; char *sdp; FILE *tmp = gf_temp_file_new(); if (!tmp) return NULL; /* write SDP header*/ fprintf(tmp, "v=0\n"); fprintf(tmp, "o=%s 3326096807 1117107880000 IN IP%d %s\n", app_name, gf_net_is_ipv6(ip_dest) ? 6 : 4, ip_dest); fprintf(tmp, "s=%s\n", (session_name ? session_name : "GPAC Scene Streaming Session")); fprintf(tmp, "c=IN IP%d %s\n", gf_net_is_ipv6(ip_dest) ? 6 : 4, ip_dest); fprintf(tmp, "t=0 0\n"); if (iod64) fprintf(tmp, "a=mpeg4-iod:\"data:application/mpeg4-iod;base64,%s\"\n", iod64); gf_f64_seek(tmp, 0, SEEK_END); size = gf_f64_tell(tmp); gf_f64_seek(tmp, 0, SEEK_SET); sdp = gf_malloc(sizeof(char) * (size_t)(size+1)); size = fread(sdp, 1, (size_t)size, tmp); sdp[size] = 0; fclose(tmp); return sdp; }
void IMG_NetIO(void *cbk, GF_NETIO_Parameter *param) { GF_Err e; const char *szCache; IMGLoader *read = (IMGLoader *) cbk; if (!read->dnload) return; /*handle service message*/ gf_service_download_update_stats(read->dnload); e = param->error; /*wait to get the whole file*/ if (!e && (param->msg_type!=GF_NETIO_DATA_TRANSFERED)) return; if (param->msg_type==GF_NETIO_DATA_TRANSFERED) { szCache = gf_dm_sess_get_cache_name(read->dnload); if (!szCache) e = GF_IO_ERR; else { read->stream = gf_f64_open((char *) szCache, "rb"); if (!read->stream) e = GF_SERVICE_ERROR; else { e = GF_OK; gf_f64_seek(read->stream, 0, SEEK_END); read->data_size = (u32) gf_f64_tell(read->stream); gf_f64_seek(read->stream, 0, SEEK_SET); } } } /*OK confirm*/ gf_service_connect_ack(read->service, NULL, e); if (!e) IMG_SetupObject(read); }
/** * Returns TRUE if file is ready to be read, FALSE otherwise * @param read Reader * @param minSizeToRead How much bytes do we need to start reading at minimum */ static Bool MP3_ConfigureFromFile(MP3Reader *read, u32 *minSizeToRead) { unsigned char id3v2[10]; u32 hdr, size; u64 pos; if (!read->stream) return 0; /* ID3VVFFFFSIZE = 13bytes * ID3 string * VV = Version * F = Flags * SIZE = 32bits size with first Most Significant bit set to 0 -> 28 bits * Size starts AFTER this header, meaning we have to add 10 bytes */ pos = fread(id3v2, sizeof(unsigned char), 10, read->stream); *minSizeToRead = 0; if (pos == 10){ /* Did we read an ID3v2 ? */ if (id3v2[0] == 'I' && id3v2[1] == 'D' && id3v2[2] == '3'){ int sz = 10 + ((id3v2[9] & 0x7f) + ((id3v2[8] & 0x7f) << 7) + ((id3v2[7] & 0x7f) << 14) + ((id3v2[6] & 0x7f) << 21)); //printf("Size of id3v2 header = %d\n", sz); *minSizeToRead = sz; } } gf_f64_seek(read->stream, 0, SEEK_SET); hdr = gf_mp3_get_next_header(read->stream); if (!hdr) return 0; read->sample_rate = gf_mp3_sampling_rate(hdr); read->oti = gf_mp3_object_type_indication(hdr); gf_f64_seek(read->stream, 0, SEEK_SET); if (!read->oti) return 0; /*we don't have the full file...*/ if (read->is_remote) return 1; // return 1; gf_f64_seek(read->stream, 0, SEEK_SET); read->duration = 0; while (1) { hdr = gf_mp3_get_next_header(read->stream); if (!hdr) break; read->duration += gf_mp3_window_size(hdr); size = gf_mp3_frame_size(hdr); pos = gf_f64_tell(read->stream); gf_f64_seek(read->stream, pos + size - 4, SEEK_SET); } gf_f64_seek(read->stream, 0, SEEK_SET); return 1; }
static Bool AC3_ConfigureFromFile(AC3Reader *read) { Bool sync; GF_BitStream *bs; GF_AC3Header hdr; memset(&hdr, 0, sizeof(GF_AC3Header)); if (!read->stream) return 0; bs = gf_bs_from_file(read->stream, GF_BITSTREAM_READ); sync = gf_ac3_parser_bs(bs, &hdr, GF_TRUE); if (!sync) { gf_bs_del(bs); return GF_FALSE; } read->nb_ch = hdr.channels; read->sample_rate = hdr.sample_rate; read->duration = 0; if (!read->is_remote) { read->duration = 1536; gf_bs_skip_bytes(bs, hdr.framesize); while (gf_ac3_parser_bs(bs, &hdr, GF_FALSE)) { read->duration += 1536; gf_bs_skip_bytes(bs, hdr.framesize); } } gf_bs_del(bs); gf_f64_seek(read->stream, 0, SEEK_SET); return 1; }
Bool gf_cache_check_if_cache_file_is_corrupted(const DownloadedCacheEntry entry) { FILE *the_cache = gf_f64_open ( entry->cache_filename, "rb" ); if ( the_cache ) { char * endPtr; const char * keyValue = gf_cfg_get_key ( entry->properties, CACHE_SECTION_NAME, CACHE_SECTION_NAME_CONTENT_SIZE ); gf_f64_seek ( the_cache, 0, SEEK_END ); entry->cacheSize = ( u32 ) gf_f64_tell ( the_cache ); fclose ( the_cache ); if (keyValue) { entry->contentLength = strtoul( keyValue, &endPtr, 10); if (*endPtr!='\0' || entry->contentLength != entry->cacheSize) { entry->flags |= CORRUPTED; GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry:%d, Cache corrupted: file and cache info size mismatch.\n", __LINE__)); } } else { entry->flags |= CORRUPTED; GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, ("[CACHE] gf_cache_create_entry:%d, CACHE is corrupted !\n", __LINE__)); } } else { entry->flags |= CORRUPTED; } return entry->flags & CORRUPTED; }
GF_Err gf_img_file_dec(char *png_filename, u32 *hint_oti, u32 *width, u32 *height, u32 *pixel_format, char **dst, u32 *dst_size) { u32 fsize, read, oti; FILE *f; char *data; GF_Err e; f = gf_f64_open(png_filename, "rb"); if (!f) return GF_URL_ERROR; oti = 0; if (!hint_oti || ! *hint_oti) { char *ext = strrchr(png_filename, '.'); if (!ext) return GF_NOT_SUPPORTED; if (!stricmp(ext, ".png")) oti = GPAC_OTI_IMAGE_PNG; else if (!stricmp(ext, ".jpg") || !stricmp(ext, ".jpeg")) oti = GPAC_OTI_IMAGE_JPEG; } gf_f64_seek(f, 0, SEEK_END); fsize = (u32)gf_f64_tell(f); gf_f64_seek(f, 0, SEEK_SET); data = gf_malloc(fsize); read = fread(data, sizeof(char), fsize, f); fclose( f ); if (read != fsize) return GF_IO_ERR; e = GF_NOT_SUPPORTED; *dst_size = 0; if (oti == GPAC_OTI_IMAGE_JPEG) { #ifdef GPAC_HAS_JPEG e = gf_img_jpeg_dec(data, fsize, width, height, pixel_format, NULL, dst_size, 0); if (*dst_size) { *dst = gf_malloc(*dst_size); return gf_img_jpeg_dec(data, fsize, width, height, pixel_format, NULL, dst_size, 0); } #endif } else if (oti == GPAC_OTI_IMAGE_PNG) { #ifdef GPAC_HAS_PNG e = gf_img_png_dec(data, fsize, width, height, pixel_format, NULL, dst_size); if (*dst_size) { *dst = gf_malloc(*dst_size); return gf_img_png_dec(data, fsize, width, height, pixel_format, *dst, dst_size); } #endif } return e; }
static Bool svg_check_download(SVGIn *svgin) { u64 size; FILE *f = gf_f64_open(svgin->file_name, "rb"); if (!f) return 0; gf_f64_seek(f, 0, SEEK_END); size = gf_f64_tell(f); fclose(f); if (size==svgin->file_size) return 1; return 0; }
static void SAF_CheckFile(SAFIn *read) { u32 nb_streams, i, cts, au_size, au_type, stream_id, ts_res; GF_BitStream *bs; StreamInfo si[1024]; gf_f64_seek(read->stream, 0, SEEK_SET); bs = gf_bs_from_file(read->stream, GF_BITSTREAM_READ); nb_streams=0; while (gf_bs_available(bs)) { gf_bs_read_u16(bs); gf_bs_read_int(bs, 2); cts = gf_bs_read_int(bs, 30); au_size = gf_bs_read_int(bs, 16); au_type = gf_bs_read_int(bs, 4); stream_id = gf_bs_read_int(bs, 12); au_size-=2; ts_res = 0; for (i=0; i<nb_streams; i++) { if (si[i].stream_id==stream_id) ts_res = si[i].ts_res; } if (!ts_res) { if ((au_type==1) || (au_type==2) || (au_type==7)) { gf_bs_read_u16(bs); ts_res = gf_bs_read_u24(bs); au_size -= 5; si[nb_streams].stream_id = stream_id; si[nb_streams].ts_res = ts_res; nb_streams++; } } if (ts_res && (au_type==4)) { Double ts = cts; ts /= ts_res; if (ts>read->duration) read->duration = ts; } gf_bs_skip_bytes(bs, au_size); } gf_bs_del(bs); gf_f64_seek(read->stream, 0, SEEK_SET); }
GF_DataMap *gf_isom_fdm_new(const char *sPath, u8 mode) { u8 bs_mode; GF_FileDataMap *tmp = (GF_FileDataMap *) gf_malloc(sizeof(GF_FileDataMap)); if (!tmp) return NULL; memset(tmp, 0, sizeof(GF_FileDataMap)); tmp->type = GF_ISOM_DATA_FILE; tmp->mode = mode; #ifndef GPAC_DISABLE_ISOM_WRITE //open a temp file if (!strcmp(sPath, "mp4_tmp_edit")) { //create a temp file (that only occurs in EDIT/WRITE mode) tmp->stream = gf_temp_file_new(); bs_mode = GF_BITSTREAM_READ; } #endif switch (mode) { case GF_ISOM_DATA_MAP_READ: if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "rb"); bs_mode = GF_BITSTREAM_READ; break; ///we open the file in READ/WRITE mode, in case case GF_ISOM_DATA_MAP_WRITE: if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "w+b"); if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "wb"); bs_mode = GF_BITSTREAM_WRITE; break; ///we open the file in CAT mode, in case case GF_ISOM_DATA_MAP_CAT: if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "a+b"); if (tmp->stream) gf_f64_seek(tmp->stream, 0, SEEK_END); bs_mode = GF_BITSTREAM_WRITE; break; default: gf_free(tmp); return NULL; } if (!tmp->stream) { gf_free(tmp); return NULL; } tmp->bs = gf_bs_from_file(tmp->stream, bs_mode); if (!tmp->bs) { fclose(tmp->stream); gf_free(tmp); return NULL; } return (GF_DataMap *)tmp; }
static GF_Err IMG_ChannelGetSLP(GF_InputService *plug, LPNETCHANNEL channel, char **out_data_ptr, u32 *out_data_size, GF_SLHeader *out_sl_hdr, Bool *sl_compressed, GF_Err *out_reception_status, Bool *is_new_data) { IMGLoader *read = (IMGLoader *)plug->priv; *out_reception_status = GF_OK; *sl_compressed = 0; *is_new_data = 0; memset(&read->sl_hdr, 0, sizeof(GF_SLHeader)); read->sl_hdr.randomAccessPointFlag = 1; read->sl_hdr.compositionTimeStampFlag = 1; *out_sl_hdr = read->sl_hdr; /*fetching es data*/ if (read->ch == channel) { if (read->done) { *out_reception_status = GF_EOS; return GF_OK; } if (!read->data) { if (!read->stream) { *out_data_ptr = NULL; *out_data_size = 0; return GF_OK; } *is_new_data = 1; gf_f64_seek(read->stream, 0, SEEK_SET); read->data = (char*) gf_malloc(sizeof(char) * (read->data_size + read->pad_bytes)); read->data_size = (u32) fread(read->data, sizeof(char), read->data_size, read->stream); gf_f64_seek(read->stream, 0, SEEK_SET); if (read->pad_bytes) memset(read->data + read->data_size, 0, sizeof(char) * read->pad_bytes); } *out_data_ptr = read->data; *out_data_size = read->data_size; return GF_OK; } return GF_STREAM_NOT_FOUND; }
GF_Err gf_isom_set_meta_xml(GF_ISOFile *file, Bool root_meta, u32 track_num, char *XMLFileName, Bool IsBinaryXML) { GF_Err e; FILE *xmlfile; GF_XMLBox *xml; GF_MetaBox *meta; e = CanAccessMovie(file, GF_ISOM_OPEN_WRITE); if (e) return e; meta = gf_isom_get_meta(file, root_meta, track_num); if (!meta) return GF_BAD_PARAM; e = gf_isom_remove_meta_xml(file, root_meta, track_num); if (e) return e; xml = (GF_XMLBox *)xml_New(); if (!xml) return GF_OUT_OF_MEM; gf_list_add(meta->other_boxes, xml); if (IsBinaryXML) xml->type = GF_ISOM_BOX_TYPE_BXML; /*assume 32bit max size = 4Go should be sufficient for a DID!!*/ xmlfile = gf_f64_open(XMLFileName, "rb"); if (!xmlfile) return GF_URL_ERROR; gf_f64_seek(xmlfile, 0, SEEK_END); assert(gf_f64_tell(xmlfile) < 1<<31); xml->xml_length = (u32) gf_f64_tell(xmlfile); gf_f64_seek(xmlfile, 0, SEEK_SET); xml->xml = (char*)gf_malloc(sizeof(unsigned char)*xml->xml_length); xml->xml_length = fread(xml->xml, 1, sizeof(unsigned char)*xml->xml_length, xmlfile); if (ferror(xmlfile)) { gf_free(xml->xml); xml->xml = NULL; return GF_BAD_PARAM; } fclose(xmlfile); return GF_OK; }
GF_Err gf_webvtt_parser_init(GF_WebVTTParser *parser, const char *input_file, void *user, GF_Err (*report_message)(void *, GF_Err, char *, const char *), void (*on_sample_parsed)(void *, GF_WebVTTSample *), void (*on_header_parsed)(void *, const char *)) { const char *ext; if (parser) { parser->state = WEBVTT_PARSER_STATE_WAITING_SIGNATURE; ext = strrchr(input_file, '.'); if (ext) { ext++; if (!strcmp(ext, "srt")) { parser->is_srt = GF_TRUE; parser->state = WEBVTT_PARSER_STATE_WAITING_CUE; } } parser->vtt_in = gf_f64_open(input_file, "rt"); gf_f64_seek(parser->vtt_in, 0, SEEK_END); parser->file_size = gf_f64_tell(parser->vtt_in); gf_f64_seek(parser->vtt_in, 0, SEEK_SET); parser->unicode_type = gf_text_get_utf_type(parser->vtt_in); if (parser->unicode_type<0) { fclose(parser->vtt_in); return GF_NOT_SUPPORTED; } parser->user = user; parser->report_message = report_message; parser->on_sample_parsed = on_sample_parsed; parser->on_header_parsed = on_header_parsed; return GF_OK; } return GF_BAD_PARAM; }
static Bool IsLargeFile(char *path) { #ifndef _WIN32_WCE FILE *stream; s64 size; stream = gf_f64_open(path, "rb"); if (!stream) return 0; gf_f64_seek(stream, 0, SEEK_END); size = gf_f64_tell(stream); fclose(stream); if (size == -1L) return 0; if (size > 0xFFFFFFFF) return 1; #endif return 0; }
GF_Err gf_cache_open_write_cache( const DownloadedCacheEntry entry, const GF_DownloadSession * sess ) { CHECK_ENTRY; if (!sess) return GF_BAD_PARAM; #ifdef ENABLE_WRITE_MX GF_LOG(GF_LOG_DEBUG, GF_LOG_NETWORK,("[CACHE] Locking write mutex %p for entry=%s\n", (void*) (entry->write_mutex), entry->url) ); gf_mx_p(entry->write_mutex); #endif entry->write_session = sess; if (!entry->continue_file) { assert( ! entry->writeFilePtr); entry->written_in_cache = 0; } entry->flags &= ~CORRUPTED; if (entry->memory_stored) { GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, ("[CACHE] Opening cache file %s for write (%s)...\n", entry->cache_filename, entry->url)); if (!entry->mem_allocated || (entry->mem_allocated < entry->contentLength)) { if (entry->contentLength) entry->mem_allocated = entry->contentLength; else if (!entry->mem_allocated) entry->mem_allocated = 81920; entry->mem_storage = gf_realloc(entry->mem_storage, sizeof(char)* (entry->mem_allocated + 2) ); } if (!entry->mem_allocated) { GF_LOG(GF_LOG_ERROR, GF_LOG_NETWORK, ("[CACHE] Failed to create memory storage for file %s\n", entry->url)); return GF_OUT_OF_MEM; } sprintf(entry->cache_filename, "gmem://%d@%p", entry->contentLength, entry->mem_storage); return GF_OK; } GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, ("[CACHE] Opening cache file %s for write (%s)...\n", entry->cache_filename, entry->url)); entry->writeFilePtr = gf_f64_open(entry->cache_filename, entry->continue_file ? "a+b" : "wb"); if (!entry->writeFilePtr) { GF_LOG(GF_LOG_ERROR, GF_LOG_NETWORK, ("[CACHE] Error while opening cache file %s for writting.\n", entry->cache_filename)); entry->write_session = NULL; #ifdef ENABLE_WRITE_MX gf_mx_v(entry->write_mutex); #endif return GF_IO_ERR; } entry->file_exists = 1; if (entry->continue_file ) gf_f64_seek(entry->writeFilePtr, 0, SEEK_END); return GF_OK; }
/*Dummy input just send a file name, no multitrack to handle so we don't need to check sub_url nor expected type*/ static GF_Descriptor *DC_GetServiceDesc(GF_InputService *plug, u32 expect_type, const char *sub_url) { u32 size = 0; char *uri; GF_ESD *esd; GF_BitStream *bs; DCReader *read = (DCReader *) plug->priv; GF_InitialObjectDescriptor *iod = (GF_InitialObjectDescriptor *) gf_odf_desc_new(GF_ODF_IOD_TAG); iod->scene_profileAndLevel = 1; iod->graphics_profileAndLevel = 1; iod->OD_profileAndLevel = 1; iod->audio_profileAndLevel = 0xFE; iod->visual_profileAndLevel = 0xFE; iod->objectDescriptorID = 1; if (read->is_views_url) { iod->URLString = gf_strdup(read->url); return (GF_Descriptor *)iod; } esd = gf_odf_desc_esd_new(0); esd->slConfig->timestampResolution = 1000; esd->slConfig->useTimestampsFlag = 1; esd->ESID = 0xFFFE; esd->decoderConfig->streamType = GF_STREAM_PRIVATE_SCENE; esd->decoderConfig->objectTypeIndication = read->oti; if (read->dnload) { uri = (char *) gf_dm_sess_get_cache_name(read->dnload); gf_dm_sess_get_stats(read->dnload, NULL, NULL, &size, NULL, NULL, NULL); } else { FILE *f = gf_f64_open(read->url, "rt"); gf_f64_seek(f, 0, SEEK_END); size = (u32) gf_f64_tell(f); fclose(f); uri = read->url; } bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); gf_bs_write_u32(bs, size); gf_bs_write_data(bs, uri, (u32) strlen(uri)); gf_bs_get_content(bs, &esd->decoderConfig->decoderSpecificInfo->data, &esd->decoderConfig->decoderSpecificInfo->dataLength); gf_bs_del(bs); gf_list_add(iod->ESDescriptors, esd); return (GF_Descriptor *)iod; }
int main(int argc, char **argv) { char data[188]; u32 size, fsize, fdone; GF_M2TS_Demuxer *ts; FILE *src = fopen(argv[1], "rb"); ts = gf_m2ts_demux_new(); ts->on_event = on_m2ts_event; fseek(src, 0, SEEK_END); fsize = ftell(src); fseek(src, 0, SEEK_SET); fdone = 0; while (!feof(src)) { size = fread(data, 1, 188, src); if (size<188) break; gf_m2ts_process_data(ts, data, size); if (has_seen_pat) break; } dest = fopen("pes.mp3", "wb"); gf_m2ts_reset_parsers(ts); gf_f64_seek(src, 0, SEEK_SET); fdone = 0; while (!feof(src)) { size = fread(data, 1, 188, src); if (size<188) break; gf_m2ts_process_data(ts, data, size); fdone += size; gf_set_progress("MPEG-2 TS Parsing", fdone, fsize); } gf_set_progress("MPEG-2 TS Parsing", fsize, fsize); fclose(src); gf_m2ts_demux_del(ts); if (dest) fclose(dest); return 0; }
u32 SAF_Run(void *_p) { GF_NETIO_Parameter par; char data[1024]; SAFIn *read = (SAFIn *)_p; par.msg_type = GF_NETIO_DATA_EXCHANGE; par.data = data; gf_f64_seek(read->stream, 0, SEEK_SET); read->saf_size=0; read->run_state = 1; while (read->run_state && !feof(read->stream) ) { par.size = (u32) fread(data, 1, 1024, read->stream); if (!par.size) break; SAF_NetIO(read, &par); } read->run_state = 2; return 0; }
static Bool CTXLoad_CheckDownload(CTXLoadPriv *priv) { u64 size; FILE *f; u32 now = gf_sys_clock(); if (!priv->file_size && (now - priv->last_check_time < 1000) ) return 0; f = gf_f64_open(priv->file_name, "rt"); if (!f) return 0; gf_f64_seek(f, 0, SEEK_END); size = gf_f64_tell(f); fclose(f); /*we MUST have a complete file for now ...*/ if (!priv->file_size) { if (priv->last_check_size == size) return 1; priv->last_check_size = size; priv->last_check_time = now; } else { if (size==priv->file_size) return 1; } return 0; }
s64 gf_cache_reader_seek_at( GF_CacheReader reader, u64 seekPosition) { if (!reader) return -1; reader->readPosition = gf_f64_seek(reader->readPtr, seekPosition, SEEK_SET); return reader->readPosition; }
static GF_Err gf_seng_encode_dims_au(GF_SceneEngine *seng, u16 ESID, GF_List *commands, char **data, u32 *size) { #ifndef GPAC_DISABLE_SCENE_DUMP GF_SceneDumper *dumper = NULL; #endif GF_Err e; char rad_name[4096]; char file_name[4096]; FILE *file = NULL; u64 fsize; char *buffer = NULL; GF_BitStream *bs = NULL; u8 dims_header; Bool compress_dims; #ifdef DUMP_DIMS_LOG_WITH_TIME u32 do_dump_with_time = 1; #endif u32 buffer_len; char *cache_dir, *dump_name; if (!data) return GF_BAD_PARAM; e = GF_OK; if (!seng->dump_path) cache_dir = gf_get_default_cache_directory(); else cache_dir = seng->dump_path; dump_name = "gpac_scene_engine_dump"; compress_dims = 1; #ifdef DUMP_DIMS_LOG_WITH_TIME start: #endif if (commands && gf_list_count(commands)) { sprintf(rad_name, "%s%c%s%s", cache_dir, GF_PATH_SEPARATOR, dump_name, "_update"); } else { #ifndef DUMP_DIMS_LOG_WITH_TIME sprintf(rad_name, "%s%c%s%s", cache_dir, GF_PATH_SEPARATOR, "rap_", dump_name); #else char date_str[100], time_str[100]; time_t now; struct tm *tm_tot; now = time(NULL); tm_tot = localtime(&now); strftime(date_str, 100, "%Yy%mm%dd", tm_tot); strftime(time_str, 100, "%Hh%Mm%Ss", tm_tot); sprintf(rad_name, "%s%c%s-%s-%s%s", cache_dir, GF_PATH_SEPARATOR, date_str, time_str, "rap_", dump_name); #endif } #ifndef GPAC_DISABLE_SCENE_DUMP dumper = gf_sm_dumper_new(seng->ctx->scene_graph, rad_name, ' ', GF_SM_DUMP_SVG); if (!dumper) { GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[SceneEngine] Cannot create SVG dumper for %s.svg\n", rad_name)); e = GF_IO_ERR; goto exit; } if (commands && gf_list_count(commands)) { e = gf_sm_dump_command_list(dumper, commands, 0, 0); } else { e = gf_sm_dump_graph(dumper, 0, 0); } gf_sm_dumper_del(dumper); if(seng->dump_rap){ GF_SceneDumper *dumper = NULL; sprintf(rad_name, "%s%c%s%s", cache_dir, GF_PATH_SEPARATOR, "rap_", dump_name); dumper = gf_sm_dumper_new(seng->ctx->scene_graph, rad_name, ' ', GF_SM_DUMP_SVG); if (!dumper) { GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[SceneEngine] Cannot create SVG dumper for %s.svg\n", rad_name)); e = GF_IO_ERR; goto exit; } e = gf_sm_dump_graph(dumper, 0, 0); gf_sm_dumper_del(dumper); } if (e) { GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[SceneEngine] Cannot dump DIMS Commands\n")); goto exit; } #endif #ifdef DUMP_DIMS_LOG_WITH_TIME if (do_dump_with_time) { do_dump_with_time = 0; goto start; } #endif sprintf(file_name, "%s.svg", rad_name); file = gf_f64_open(file_name, "rb"); if (!file) { GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[SceneEngine] Cannot open SVG dump file %s\n", file_name)); e = GF_IO_ERR; goto exit; } gf_f64_seek(file, 0, SEEK_END); fsize = gf_f64_tell(file); if (fsize == 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[SceneEngine] SVG dump %s is empty\n", file_name)); goto exit; } /* First, read the dump in a buffer */ buffer = gf_malloc((size_t)fsize * sizeof(char)); gf_f64_seek(file, 0, SEEK_SET); fsize = fread(buffer, sizeof(char), (size_t)fsize, file); fclose(file); file = NULL; /* Then, set DIMS unit header - TODO: notify redundant units*/ dims_header = 0; if (commands && gf_list_count(commands)) { dims_header = GF_DIMS_UNIT_P; /* streamer->all_non_rap_critical ? 0 : GF_DIMS_UNIT_P;*/ } else { /*redundant RAP with complete scene*/ dims_header = GF_DIMS_UNIT_M | GF_DIMS_UNIT_S | GF_DIMS_UNIT_I | GF_DIMS_UNIT_P; } /* Then, if compression is asked, we do it */ buffer_len = (u32)fsize; assert(fsize < 1<<31); GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[SceneEngine] Sending DIMS data - sizes: raw (%d)", buffer_len)); if (compress_dims) { dims_header |= GF_DIMS_UNIT_C; e = gf_gz_compress_payload(&buffer, buffer_len, &buffer_len); GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("/ compressed (%d)", buffer_len)); if (e) goto exit; } GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("\n")); /* Then, prepare the DIMS data using a bitstream instead of direct manipulation for endianness The new bitstream size should be: the size of the (compressed) data + 1 bytes for the header + 2 bytes for the size + 4 bytes if the size is greater than 65535 */ bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); if (buffer_len > 65535) { GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[SceneEngine] Warning: DIMS Unit size too big !!!\n")); gf_bs_write_u16(bs, 0); /* internal GPAC hack to indicate that the size is larger than 65535 */ gf_bs_write_u32(bs, buffer_len+1); } else { gf_bs_write_u16(bs, buffer_len+1); } gf_bs_write_u8(bs, dims_header); gf_bs_write_data(bs, buffer, buffer_len); gf_free(buffer); buffer = NULL; gf_bs_get_content(bs, data, size); gf_bs_del(bs); exit: if (!seng->dump_path) gf_free(cache_dir); if (buffer) gf_free(buffer); if (file) fclose(file); return e; }
static GF_Err AC3_ChannelGetSLP(GF_InputService *plug, LPNETCHANNEL channel, char **out_data_ptr, u32 *out_data_size, GF_SLHeader *out_sl_hdr, Bool *sl_compressed, GF_Err *out_reception_status, Bool *is_new_data) { u64 pos, start_from; Bool sync; GF_BitStream *bs; GF_AC3Header hdr; AC3Reader *read = plug->priv; *out_reception_status = GF_OK; *sl_compressed = 0; *is_new_data = 0; memset(&hdr, 0, sizeof(GF_AC3Header)); memset(&read->sl_hdr, 0, sizeof(GF_SLHeader)); read->sl_hdr.randomAccessPointFlag = 1; read->sl_hdr.compositionTimeStampFlag = 1; if (read->ch != channel) return GF_STREAM_NOT_FOUND; /*fetching es data*/ if (read->done) { *out_reception_status = GF_EOS; return GF_OK; } if (!read->data) { if (!read->stream) { *out_data_ptr = NULL; *out_data_size = 0; return GF_OK; } bs = gf_bs_from_file(read->stream, GF_BITSTREAM_READ); *is_new_data = 1; fetch_next: pos = gf_f64_tell(read->stream); sync = gf_ac3_parser_bs(bs, &hdr, GF_FALSE); if (!sync) { gf_bs_del(bs); if (!read->dnload) { *out_reception_status = GF_EOS; read->done = 1; } else { gf_f64_seek(read->stream, pos, SEEK_SET); *out_reception_status = GF_OK; } return GF_OK; } if (!hdr.framesize) { gf_bs_del(bs); *out_reception_status = GF_EOS; read->done = 1; return GF_OK; } read->data_size = hdr.framesize; read->nb_samp = 1536; /*we're seeking*/ if (read->start_range && read->duration) { start_from = (u32) (read->start_range * read->sample_rate); if (read->current_time + read->nb_samp < start_from) { read->current_time += read->nb_samp; goto fetch_next; } else { read->start_range = 0; } } read->sl_hdr.compositionTimeStamp = read->current_time; read->data = gf_malloc(sizeof(char) * (read->data_size+read->pad_bytes)); gf_bs_read_data(bs, (char *) read->data, read->data_size); if (read->pad_bytes) memset(read->data + read->data_size, 0, sizeof(char) * read->pad_bytes); gf_bs_del(bs); } *out_sl_hdr = read->sl_hdr; *out_data_ptr =(char *) read->data; *out_data_size = read->data_size; return GF_OK; }
static GF_Err AC3_ServiceCommand(GF_InputService *plug, GF_NetworkCommand *com) { AC3Reader *read = plug->priv; if (com->base.command_type==GF_NET_SERVICE_INFO) { com->info.name = read->icy_track_name ? read->icy_track_name : read->icy_name; com->info.comment = read->icy_genre; return GF_OK; } if (!com->base.on_channel) { /*if live session we may cache*/ if (read->is_live && (com->command_type==GF_NET_IS_CACHABLE)) return GF_OK; return GF_NOT_SUPPORTED; } switch (com->command_type) { case GF_NET_CHAN_SET_PULL: if ((read->ch == com->base.on_channel) && read->is_live) return GF_NOT_SUPPORTED; return GF_OK; case GF_NET_CHAN_INTERACTIVE: if ((read->ch == com->base.on_channel) && read->is_live) return GF_NOT_SUPPORTED; return GF_OK; case GF_NET_CHAN_BUFFER: if ((read->ch == com->base.on_channel) && read->is_live) { if (com->buffer.max<1000) com->buffer.max = 1000; com->buffer.min = com->buffer.max/2; } return GF_OK; case GF_NET_CHAN_SET_PADDING: read->pad_bytes = com->pad.padding_bytes; return GF_OK; case GF_NET_CHAN_DURATION: com->duration.duration = read->duration; com->duration.duration /= read->sample_rate; return GF_OK; case GF_NET_CHAN_PLAY: read->start_range = com->play.start_range; read->end_range = com->play.end_range; read->current_time = 0; if (read->stream) gf_f64_seek(read->stream, 0, SEEK_SET); if (read->ch == com->base.on_channel) { read->done = 0; /*PLAY after complete download, estimate duration*/ if (!read->is_remote && !read->duration) { AC3_ConfigureFromFile(read); if (read->duration) { GF_NetworkCommand rcfg; rcfg.base.on_channel = read->ch; rcfg.base.command_type = GF_NET_CHAN_DURATION; rcfg.duration.duration = read->duration; rcfg.duration.duration /= read->sample_rate; gf_service_command(read->service, &rcfg, GF_OK); } } } return GF_OK; case GF_NET_CHAN_STOP: return GF_OK; default: return GF_OK; } }
GF_Err gf_bifs_enc_sf_field(GF_BifsEncoder *codec, GF_BitStream *bs, GF_Node *node, GF_FieldInfo *field) { GF_Err e; if (node) { e = gf_bifs_enc_quant_field(codec, bs, node, field); if (e != GF_EOS) return e; } switch (field->fieldType) { case GF_SG_VRML_SFBOOL: GF_BIFS_WRITE_INT(codec, bs, * ((SFBool *)field->far_ptr), 1, "SFBool", NULL); break; case GF_SG_VRML_SFCOLOR: BE_WriteSFFloat(codec, ((SFColor *)field->far_ptr)->red, bs, "color.red"); BE_WriteSFFloat(codec, ((SFColor *)field->far_ptr)->green, bs, "color.green"); BE_WriteSFFloat(codec, ((SFColor *)field->far_ptr)->blue, bs, "color.blue"); break; case GF_SG_VRML_SFFLOAT: BE_WriteSFFloat(codec, * ((SFFloat *)field->far_ptr), bs, NULL); break; case GF_SG_VRML_SFINT32: GF_BIFS_WRITE_INT(codec, bs, * ((SFInt32 *)field->far_ptr), 32, "SFInt32", NULL); break; case GF_SG_VRML_SFROTATION: BE_WriteSFFloat(codec, ((SFRotation *)field->far_ptr)->x, bs, "rot.x"); BE_WriteSFFloat(codec, ((SFRotation *)field->far_ptr)->y, bs, "rot.y"); BE_WriteSFFloat(codec, ((SFRotation *)field->far_ptr)->z, bs, "rot.z"); BE_WriteSFFloat(codec, ((SFRotation *)field->far_ptr)->q, bs, "rot.theta"); break; case GF_SG_VRML_SFSTRING: if (node && (node->sgprivate->tag==TAG_MPEG4_CacheTexture) && (field->fieldIndex<=2)) { u32 size, val; char buf[4096]; FILE *f = gf_f64_open(((SFString*)field->far_ptr)->buffer, "rb"); if (!f) return GF_URL_ERROR; gf_f64_seek(f, 0, SEEK_END); size = (u32) gf_f64_tell(f); val = gf_get_bit_size(size); GF_BIFS_WRITE_INT(codec, bs, val, 5, "nbBits", NULL); GF_BIFS_WRITE_INT(codec, bs, size, val, "length", NULL); gf_f64_seek(f, 0, SEEK_SET); while (size) { u32 read = fread(buf, 1, 4096, f); gf_bs_write_data(bs, buf, read); size -= read; } } else { u32 i, len, val; char *str = (char *) ((SFString*)field->far_ptr)->buffer; if (node && (node->sgprivate->tag==TAG_MPEG4_BitWrapper) ) { len = ((M_BitWrapper*)node)->buffer_len; } else { len = str ? strlen(str) : 0; } val = gf_get_bit_size(len); GF_BIFS_WRITE_INT(codec, bs, val, 5, "nbBits", NULL); GF_BIFS_WRITE_INT(codec, bs, len, val, "length", NULL); for (i=0; i<len; i++) gf_bs_write_int(bs, str[i], 8); GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[BIFS] string\t\t%d\t\t%s\n", 8*len, str) ); } break; case GF_SG_VRML_SFTIME: gf_bs_write_double(bs, *((SFTime *)field->far_ptr)); GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[BIFS] SFTime\t\t%d\t\t%g\n", 64, *((SFTime *)field->far_ptr))); break; case GF_SG_VRML_SFVEC2F: BE_WriteSFFloat(codec, ((SFVec2f *)field->far_ptr)->x, bs, "vec2f.x"); BE_WriteSFFloat(codec, ((SFVec2f *)field->far_ptr)->y, bs, "vec2f.y"); break; case GF_SG_VRML_SFVEC3F: BE_WriteSFFloat(codec, ((SFVec3f *)field->far_ptr)->x, bs, "vec3f.x"); BE_WriteSFFloat(codec, ((SFVec3f *)field->far_ptr)->y, bs, "vec3f.y"); BE_WriteSFFloat(codec, ((SFVec3f *)field->far_ptr)->z, bs, "vec3f.z"); break; case GF_SG_VRML_SFURL: { SFURL *url = (SFURL *) field->far_ptr; GF_BIFS_WRITE_INT(codec, bs, (url->OD_ID>0) ? 1 : 0, 1, "hasODID", "SFURL"); if (url->OD_ID>0) { GF_BIFS_WRITE_INT(codec, bs, url->OD_ID, 10, "ODID", "SFURL"); } else { u32 i; u32 len = url->url ? strlen(url->url) : 0; u32 val = gf_get_bit_size(len); GF_BIFS_WRITE_INT(codec, bs, val, 5, "nbBits", NULL); GF_BIFS_WRITE_INT(codec, bs, len, val, "length", NULL); for (i=0; i<len; i++) gf_bs_write_int(bs, url->url[i], 8); GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[BIFS] string\t\t%d\t\t%s\t\t//SFURL\n", 8*len, url->url)); } } break; case GF_SG_VRML_SFIMAGE: { u32 size, i; SFImage *img = (SFImage *)field->far_ptr; GF_BIFS_WRITE_INT(codec, bs, img->width, 12, "width", "SFImage"); GF_BIFS_WRITE_INT(codec, bs, img->height, 12, "height", "SFImage"); GF_BIFS_WRITE_INT(codec, bs, img->numComponents - 1, 2, "nbComp", "SFImage"); size = img->width * img->height * img->numComponents; for (i=0; i<size; i++) gf_bs_write_int(bs, img->pixels[i], 8); GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[BIFS] pixels\t\t%d\t\tnot dumped\t\t//SFImage\n", 8*size)); } break; case GF_SG_VRML_SFCOMMANDBUFFER: { SFCommandBuffer *cb = (SFCommandBuffer *) field->far_ptr; if (cb->buffer) gf_free(cb->buffer); cb->buffer = NULL; cb->bufferSize = 0; if (gf_list_count(cb->commandList)) { u32 i, nbBits; GF_BitStream *bs_cond = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[BIFS] /*SFCommandBuffer*/\n" )); e = gf_bifs_enc_commands(codec, cb->commandList, bs_cond); if (!e) gf_bs_get_content(bs_cond, (char**)&cb->buffer, &cb->bufferSize); gf_bs_del(bs_cond); if (e) return e; GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[BIFS] /*End SFCommandBuffer*/\n")); nbBits = gf_get_bit_size(cb->bufferSize); GF_BIFS_WRITE_INT(codec, bs, nbBits, 5, "NbBits", NULL); GF_BIFS_WRITE_INT(codec, bs, cb->bufferSize, nbBits, "BufferSize", NULL); for (i=0; i<cb->bufferSize; i++) GF_BIFS_WRITE_INT(codec, bs, cb->buffer[i], 8, "buffer byte", NULL); } /*empty command buffer*/ else { GF_BIFS_WRITE_INT(codec, bs, 0, 5, "NbBits", NULL); } } break; case GF_SG_VRML_SFNODE: return gf_bifs_enc_node(codec, *((GF_Node **)field->far_ptr), field->NDTtype, bs); case GF_SG_VRML_SFSCRIPT: #ifdef GPAC_HAS_SPIDERMONKEY codec->LastError = SFScript_Encode(codec, (SFScript *)field->far_ptr, bs, node); #else return GF_NOT_SUPPORTED; #endif break; case GF_SG_VRML_SFATTRREF: { u32 idx=0; SFAttrRef *ar = (SFAttrRef *)field->far_ptr; u32 nbBitsDEF = gf_get_bit_size(gf_node_get_num_fields_in_mode(ar->node, GF_SG_FIELD_CODING_DEF) - 1); GF_BIFS_WRITE_INT(codec, bs, gf_node_get_id(ar->node) - 1, codec->info->config.NodeIDBits, "NodeID", NULL); gf_bifs_field_index_by_mode(ar->node, ar->fieldIndex, GF_SG_FIELD_CODING_DEF, &idx); GF_BIFS_WRITE_INT(codec, bs, idx, nbBitsDEF, "field", NULL); } break; default: return GF_NOT_SUPPORTED; } return codec->LastError; }
static GF_Err xml_sax_read_file(GF_SAXParser *parser) { GF_Err e = GF_EOS; unsigned char szLine[XML_INPUT_SIZE+2]; #ifdef NO_GZIP if (!parser->f_in) return GF_BAD_PARAM; #else if (!parser->gz_in) return GF_BAD_PARAM; #endif while (!parser->suspended) { #ifdef NO_GZIP s32 read = fread(szLine, 1, XML_INPUT_SIZE, parser->f_in); #else s32 read = gzread(parser->gz_in, szLine, XML_INPUT_SIZE); #endif if ((read<=0) /*&& !parser->node_depth*/) break; szLine[read] = 0; szLine[read+1] = 0; e = gf_xml_sax_parse(parser, szLine); if (e) break; if (parser->file_pos > parser->file_size) parser->file_size = parser->file_pos + 1; if (parser->on_progress) parser->on_progress(parser->sax_cbck, parser->file_pos, parser->file_size); } #ifdef NO_GZIP if (feof(parser->f_in)) { #else if (gzeof(parser->gz_in)) { #endif if (!e) e = GF_EOS; if (parser->on_progress) parser->on_progress(parser->sax_cbck, parser->file_size, parser->file_size); #ifdef NO_GZIP fclose(parser->f_in); parser->f_in = NULL; #else gzclose(parser->gz_in); parser->gz_in = 0; #endif parser->elt_start_pos = parser->elt_end_pos = 0; parser->elt_name_start = parser->elt_name_end = 0; parser->att_name_start = 0; parser->current_pos = 0; parser->line_size = 0; parser->att_sep = 0; parser->file_pos = 0; parser->file_size = 0; parser->line_size = 0; } return e; } GF_EXPORT GF_Err gf_xml_sax_parse_file(GF_SAXParser *parser, const char *fileName, gf_xml_sax_progress OnProgress) { FILE *test; GF_Err e; #ifndef NO_GZIP gzFile gzInput; #endif unsigned char szLine[6]; /*check file exists and gets its size (zlib doesn't support SEEK_END)*/ test = gf_f64_open(fileName, "rb"); if (!test) return GF_URL_ERROR; gf_f64_seek(test, 0, SEEK_END); assert(gf_f64_tell(test) < 1<<31); parser->file_size = (u32) gf_f64_tell(test); fclose(test); parser->on_progress = OnProgress; #ifdef NO_GZIP parser->f_in = gf_f64_open(fileName, "rt"); fread(szLine, 1, 4, parser->f_in); #else gzInput = gzopen(fileName, "rb"); if (!gzInput) return GF_IO_ERR; parser->gz_in = gzInput; /*init SAX parser (unicode setup)*/ gzread(gzInput, szLine, 4); #endif szLine[4] = szLine[5] = 0; e = gf_xml_sax_init(parser, szLine); if (e) return e; parser->file_pos = 4; /* souchay : not sure for next 2 lines, but it works better it seems */ parser->elt_start_pos = 0; parser->current_pos = 0; return xml_sax_read_file(parser); }
GF_DataMap *gf_isom_fdm_new(const char *sPath, u8 mode) { u8 bs_mode; GF_FileDataMap *tmp = (GF_FileDataMap *) gf_malloc(sizeof(GF_FileDataMap)); if (!tmp) return NULL; memset(tmp, 0, sizeof(GF_FileDataMap)); tmp->type = GF_ISOM_DATA_FILE; tmp->mode = mode; #ifndef GPAC_DISABLE_ISOM_WRITE //open a temp file if (!strcmp(sPath, "mp4_tmp_edit")) { //create a temp file (that only occurs in EDIT/WRITE mode) tmp->stream = gf_temp_file_new(); bs_mode = GF_BITSTREAM_READ; } #endif if (!strncmp(sPath, "gmem://", 7)) { u32 size; void *mem_address; if (sscanf(sPath, "gmem://%d@%p", &size, &mem_address) != 2) return NULL; tmp->bs = gf_bs_new((const char *)mem_address, size, GF_BITSTREAM_READ); if (!tmp->bs) { gf_free(tmp); return NULL; } return (GF_DataMap *)tmp; } switch (mode) { case GF_ISOM_DATA_MAP_READ: if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "rb"); bs_mode = GF_BITSTREAM_READ; break; ///we open the file in READ/WRITE mode, in case case GF_ISOM_DATA_MAP_WRITE: if (!strcmp(sPath, "std")) { tmp->stream = stdout; tmp->is_stdout = 1; } if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "w+b"); if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "wb"); bs_mode = GF_BITSTREAM_WRITE; break; ///we open the file in CAT mode, in case case GF_ISOM_DATA_MAP_CAT: if (!strcmp(sPath, "std")) { tmp->stream = stdout; tmp->is_stdout = 1; } if (!tmp->stream) tmp->stream = gf_f64_open(sPath, "a+b"); if (tmp->stream) gf_f64_seek(tmp->stream, 0, SEEK_END); bs_mode = GF_BITSTREAM_WRITE; break; default: gf_free(tmp); return NULL; } if (!tmp->stream) { gf_free(tmp); return NULL; } tmp->bs = gf_bs_from_file(tmp->stream, bs_mode); if (!tmp->bs) { fclose(tmp->stream); gf_free(tmp); return NULL; } if (default_write_buffering_size) { gf_bs_set_output_buffering(tmp->bs, default_write_buffering_size); } return (GF_DataMap *)tmp; }
static GF_Err gf_isom_streamer_setup_sdp(GF_ISOMRTPStreamer *streamer, char*sdpfilename, char **out_sdp_buffer) { GF_RTPTrack *track; FILE *sdp_out; char filename[GF_MAX_PATH]; char sdpLine[20000]; strcpy(filename, sdpfilename ? sdpfilename : "videosession.sdp"); sdp_out = gf_f64_open(filename, "wt"); if (!sdp_out) return GF_IO_ERR; if (!out_sdp_buffer) { sprintf(sdpLine, "v=0"); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "o=MP4Streamer 3357474383 1148485440000 IN IP%d %s", gf_net_is_ipv6(streamer->dest_ip) ? 6 : 4, streamer->dest_ip); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "s=livesession"); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "i=This is an MP4 time-sliced Streaming demo"); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "u=http://gpac.sourceforge.net"); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "e=admin@"); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "c=IN IP%d %s", gf_net_is_ipv6(streamer->dest_ip) ? 6 : 4, streamer->dest_ip); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "t=0 0"); fprintf(sdp_out, "%s\n", sdpLine); sprintf(sdpLine, "a=x-copyright: Streamed with GPAC (C)2000-200X - http://gpac.sourceforge.net\n"); fprintf(sdp_out, "%s\n", sdpLine); } track = streamer->stream; while (track) { char *sdp_media=NULL; const char *KMS = NULL; char *dsi = NULL; u32 w, h; u32 dsi_len = 0; GF_DecoderConfig *dcd = gf_isom_get_decoder_config(streamer->isom, track->track_num, 1); if (dcd && dcd->decoderSpecificInfo) { dsi = dcd->decoderSpecificInfo->data; dsi_len = dcd->decoderSpecificInfo->dataLength; } w = h = 0; if (gf_isom_get_media_type(streamer->isom, track->track_num) == GF_ISOM_MEDIA_VISUAL) { gf_isom_get_visual_info(streamer->isom, track->track_num, 1, &w, &h); } gf_isom_get_ismacryp_info(streamer->isom, track->track_num, 1, NULL, NULL, NULL, NULL, &KMS, NULL, NULL, NULL); /*TODO retrieve DIMS content encoding from track to set the flags */ gf_rtp_streamer_append_sdp_extended(track->rtp, gf_isom_get_track_id(streamer->isom, track->track_num), dsi, dsi_len, streamer->isom, track->track_num, (char *)KMS, w, h, &sdp_media); if (sdp_media) { fprintf(sdp_out, "%s", sdp_media); gf_free(sdp_media); } if (dcd) gf_odf_desc_del((GF_Descriptor *)dcd); track = track->next; } fprintf(sdp_out, "\n"); fclose(sdp_out); if (out_sdp_buffer) { u64 size; sdp_out = gf_f64_open(filename, "r"); gf_f64_seek(sdp_out, 0, SEEK_END); size = gf_f64_tell(sdp_out); gf_f64_seek(sdp_out, 0, SEEK_SET); if (*out_sdp_buffer) gf_free(*out_sdp_buffer); *out_sdp_buffer = gf_malloc(sizeof(char)*(size_t)(size+1)); size = fread(*out_sdp_buffer, 1, (size_t)size, sdp_out); fclose(sdp_out); (*out_sdp_buffer)[size]=0; } return GF_OK; }
GF_EXPORT char *gf_xml_sax_peek_node(GF_SAXParser *parser, char *att_name, char *att_value, char *substitute, char *get_attr, char *end_pattern, Bool *is_substitute) { u32 state, att_len, alloc_size; z_off_t pos; Bool from_buffer; Bool dobreak=0; char szLine1[XML_INPUT_SIZE+2], szLine2[XML_INPUT_SIZE+2], *szLine, *cur_line, *sep, *start, first_c, *result; #define CPYCAT_ALLOC(__str, __is_copy) if ( strlen(__str) + (__is_copy ? 0 : strlen(szLine))>=alloc_size) {\ alloc_size = 1+strlen(__str); \ if (!__is_copy) alloc_size += strlen(szLine); \ szLine = gf_realloc(szLine, alloc_size); \ }\ if (__is_copy) strcpy(szLine, __str); \ else strcat(szLine, __str); \ from_buffer=0; #ifdef NO_GZIP if (!parser->f_in) from_buffer=1; #else if (!parser->gz_in) from_buffer=1; #endif result = NULL; szLine1[0] = szLine2[0] = 0; pos=0; if (!from_buffer) { #ifdef NO_GZIP pos = gf_f64_tell(parser->f_in); #else pos = gztell(parser->gz_in); #endif } att_len = strlen(parser->buffer + parser->att_name_start); if (att_len<2*XML_INPUT_SIZE) att_len = 2*XML_INPUT_SIZE; alloc_size = att_len; szLine = (char *) gf_malloc(sizeof(char)*alloc_size); strcpy(szLine, parser->buffer + parser->att_name_start); cur_line = szLine; att_len = strlen(att_value); state = 0; goto retry; while (1) { u32 read; u8 sep_char; if (!from_buffer) { #ifdef NO_GZIP if (!feof(parser->f_in)) break; #else if (!gzeof(parser->gz_in)) break; #endif } if (dobreak) break; if (cur_line == szLine2) { cur_line = szLine1; } else { cur_line = szLine2; } if (from_buffer) { dobreak=1; } else { #ifdef NO_GZIP read = fread(cur_line, 1, XML_INPUT_SIZE, parser->f_in); #else read = gzread(parser->gz_in, cur_line, XML_INPUT_SIZE); #endif cur_line[read] = cur_line[read+1] = 0; CPYCAT_ALLOC(cur_line, 0); } if (end_pattern) { start = strstr(szLine, end_pattern); if (start) { start[0] = 0; dobreak = 1; } } retry: if (state == 2) goto fetch_attr; sep = strstr(szLine, att_name); if (!sep && !state) { state = 0; start = strrchr(szLine, '<'); if (start) { CPYCAT_ALLOC(start, 1); } else { CPYCAT_ALLOC(cur_line, 1); } continue; } if (!state) { state = 1; /*load next line*/ first_c = sep[0]; sep[0] = 0; start = strrchr(szLine, '<'); if (!start) goto exit; sep[0] = first_c; CPYCAT_ALLOC(start, 1); sep = strstr(szLine, att_name); } sep = strchr(sep, '='); if (!sep) { state = 0; CPYCAT_ALLOC(cur_line, 1); continue; } while (sep[0] && (sep[0] != '\"') && (sep[0] != '\'') ) sep++; if (!sep[0]) continue; sep_char = sep[0]; sep++; while (sep[0] && strchr(" \n\r\t", sep[0]) ) sep++; if (!sep[0]) continue; if (!strchr(sep, sep_char)) continue; /*found*/ if (!strncmp(sep, att_value, att_len)) { u32 pos; sep = szLine + 1; while (strchr(" \t\r\n", sep[0])) sep++; pos = 0; while (!strchr(" \t\r\n", sep[pos])) pos++; first_c = sep[pos]; sep[pos] = 0; state = 2; if (!substitute || !get_attr || strcmp(sep, substitute) ) { if (is_substitute) *is_substitute = 0; result = gf_strdup(sep); goto exit; } sep[pos] = first_c; fetch_attr: sep = strstr(szLine + 1, get_attr); if (!sep) { CPYCAT_ALLOC(cur_line, 1); continue; } sep += strlen(get_attr); while (strchr("= \t\r\n", sep[0])) sep++; sep++; pos = 0; while (!strchr(" \t\r\n/>", sep[pos])) pos++; sep[pos-1] = 0; result = gf_strdup(sep); if (is_substitute) *is_substitute = 1; goto exit; } state = 0; CPYCAT_ALLOC(sep, 1); goto retry; } exit: gf_free(szLine); if (!from_buffer) { #ifdef NO_GZIP gf_f64_seek(parser->f_in, pos, SEEK_SET); #else gzrewind(parser->gz_in); gzseek(parser->gz_in, pos, SEEK_SET); #endif } return result; }
GF_Err DoWriteMeta(GF_ISOFile *file, GF_MetaBox *meta, GF_BitStream *bs, Bool Emulation, u64 baseOffset, u64 *mdatSize) { GF_ItemExtentEntry *entry; u64 maxExtendOffset, maxExtendSize; u32 i, j, count; maxExtendOffset = 0; maxExtendSize = 0; *mdatSize = 0; if (!meta->item_locations) return GF_OK; count = gf_list_count(meta->item_locations->location_entries); for (i=0; i<count; i++) { u64 it_size; GF_ItemLocationEntry *iloc = (GF_ItemLocationEntry *)gf_list_get(meta->item_locations->location_entries, i); /*get item info*/ GF_ItemInfoEntryBox *iinf = NULL; j=0; while ((iinf = (GF_ItemInfoEntryBox *)gf_list_enum(meta->item_infos->item_infos, &j))) { if (iinf->item_ID==iloc->item_ID) break; iinf = NULL; } if (!iloc->base_offset && (gf_list_count(iloc->extent_entries)==1)) { entry = (GF_ItemExtentEntry *)gf_list_get(iloc->extent_entries, 0); if (!entry->extent_length && !entry->original_extent_offset) { entry->extent_offset = 0; continue; } } it_size = 0; /*for self contained only*/ if (!iloc->data_reference_index) { iloc->base_offset = baseOffset; /*new resource*/ if (iinf->full_path) { FILE *src=NULL; if (!iinf->data_len) { src = gf_f64_open(iinf->full_path, "rb"); if (!src) continue; gf_f64_seek(src, 0, SEEK_END); it_size = gf_f64_tell(src); gf_f64_seek(src, 0, SEEK_SET); } else { it_size = iinf->data_len; } if (maxExtendSize<it_size) maxExtendSize = it_size; if (!gf_list_count(iloc->extent_entries)) { GF_SAFEALLOC(entry, GF_ItemExtentEntry); gf_list_add(iloc->extent_entries, entry); } entry = (GF_ItemExtentEntry *)gf_list_get(iloc->extent_entries, 0); entry->extent_offset = 0; entry->extent_length = it_size; /*OK write to mdat*/ if (!Emulation) { if (src) { char cache_data[4096]; u64 remain = entry->extent_length; while (remain) { u32 size_cache = (remain>4096) ? 4096 : (u32) remain; size_cache = fread(cache_data, sizeof(char), size_cache, src); gf_bs_write_data(bs, cache_data, size_cache); remain -= size_cache; } } else { gf_bs_write_data(bs, iinf->full_path, iinf->data_len); } } if (src) fclose(src); } else if (gf_list_count(iloc->extent_entries)) { u32 j; j=0; while ((entry = (GF_ItemExtentEntry *)gf_list_enum(iloc->extent_entries, &j))) { if (j && (maxExtendOffset<it_size) ) maxExtendOffset = it_size; /*compute new offset*/ entry->extent_offset = baseOffset + it_size; it_size += entry->extent_length; if (maxExtendSize<entry->extent_length) maxExtendSize = entry->extent_length; /*Reading from the input file*/ if (!Emulation) { char cache_data[4096]; u64 remain = entry->extent_length; gf_bs_seek(file->movieFileMap->bs, entry->original_extent_offset + iloc->original_base_offset); while (remain) { u32 size_cache = (remain>4096) ? 4096 : (u32) remain; gf_bs_read_data(file->movieFileMap->bs, cache_data, size_cache); /*Writing to the output file*/ gf_bs_write_data(bs, cache_data, size_cache); remain -= size_cache; } } } } baseOffset += it_size; *mdatSize += it_size; } else { /*we MUST have at least one extent for the dref data*/ if (!gf_list_count(iloc->extent_entries)) { GF_SAFEALLOC(entry, GF_ItemExtentEntry); gf_list_add(iloc->extent_entries, entry); } entry = (GF_ItemExtentEntry *)gf_list_get(iloc->extent_entries, 0); entry->extent_offset = 0; /*0 means full length of referenced file*/ entry->extent_length = 0; } } /*update offset & size length fields*/ if (baseOffset>0xFFFFFFFF) meta->item_locations->base_offset_size = 8; else if (baseOffset) meta->item_locations->base_offset_size = 4; if (maxExtendSize>0xFFFFFFFF) meta->item_locations->length_size = 8; else if (maxExtendSize) meta->item_locations->length_size = 4; if (maxExtendOffset>0xFFFFFFFF) meta->item_locations->offset_size = 8; else if (maxExtendOffset) meta->item_locations->offset_size = 4; return GF_OK; }