Пример #1
0
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;
}
Пример #2
0
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;
}
Пример #3
0
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);
}
Пример #4
0
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;
}
Пример #5
0
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;
}
Пример #6
0
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;
}
Пример #7
0
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;
}
Пример #8
0
/**
 * 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;
}
Пример #9
0
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;
}
Пример #10
0
/*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;
}
Пример #11
0
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;
}
Пример #12
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;
}
Пример #13
0
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;
}
Пример #14
0
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;
}
Пример #15
0
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;
}
Пример #16
0
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;
} 
Пример #17
0
Файл: live.c Проект: Bevara/GPAC
u32 grab_live_m2ts(const char *grab_m2ts, const char *outName)
{
	char data[0x80000];
	u32 check = 50;
	u64 nb_pck;
	Bool first_run, is_rtp;
	FILE *output;
#ifndef GPAC_DISABLE_STREAMING
	u16 seq_num;
	GF_RTPReorder *ch = NULL;
#endif
	GF_Socket *sock;
	GF_Err e = gf_m2ts_get_socket(grab_m2ts, NULL, 0x80000, &sock);

	if (e) {
		fprintf(stderr, "Cannot open %s: %s\n", grab_m2ts, gf_error_to_string(e));
		return 1;
	}
	output = gf_f64_open(outName, "wb");
	if (!output) {
		fprintf(stderr, "Cannot open %s: check path and rights\n", outName);
		gf_sk_del(sock);
		return 1;
	}

	fprintf(stderr, "Dumping %s stream to %s - press q to abort\n", grab_m2ts, outName);

	first_run = 1;
	is_rtp = 0;
	while (1) {
		u32 size = 0;

		check--;
		if (!check) {
			if (gf_prompt_has_input()) {
				char c = (char) gf_prompt_get_char();
				if (c=='q') break;
			}
			check = 50;
		}

		/*m2ts chunks by chunks*/
		e = gf_sk_receive(sock, data, 0x40000, 0, &size);
		if (!size || e) {
			gf_sleep(1);
			continue;
		}
		if (first_run) {
			first_run = 0;
			/*FIXME: we assume only simple RTP packaging (no CSRC nor extensions)*/
			if ((data[0] != 0x47) && ((data[1] & 0x7F) == 33) ) {
				is_rtp = 1;
#ifndef GPAC_DISABLE_STREAMING
				ch = gf_rtp_reorderer_new(100, 500);
#endif
			}
		}
		/*process chunk*/
		if (is_rtp) {
#ifndef GPAC_DISABLE_STREAMING
			char *pck;
			seq_num = ((data[2] << 8) & 0xFF00) | (data[3] & 0xFF);
			gf_rtp_reorderer_add(ch, (void *) data, size, seq_num);

			pck = (char *) gf_rtp_reorderer_get(ch, &size);
			if (pck) {
				fwrite(pck+12, size-12, 1, output);
				gf_free(pck);
			}
#else
			fwrite(data+12, size-12, 1, output);
#endif
		} else {
			fwrite(data, size, 1, output);
		}
	}
	nb_pck = gf_f64_tell(output);
	nb_pck /= 188;
	fprintf(stderr, "Captured "LLU" TS packets\n", nb_pck );
	fclose(output);
	gf_sk_del(sock);

#ifndef GPAC_DISABLE_STREAMING
	if (ch)
		gf_rtp_reorderer_del(ch);
#endif
	return 0;
}
Пример #18
0
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;
}
Пример #19
0
GF_Err gf_isom_add_meta_item(GF_ISOFile *file, Bool root_meta, u32 track_num, Bool self_reference, char *resource_path, const char *item_name, const char *mime_type, const char *content_encoding, const char *URL, const char *URN)
{
	GF_Err e;
	GF_ItemLocationEntry *location_entry;
	GF_ItemInfoEntryBox *infe;
	GF_MetaBox *meta;
	u32 lastItemID = 0;

	if (!self_reference && !item_name && !resource_path) return GF_BAD_PARAM;
	e = CanAccessMovie(file, GF_ISOM_OPEN_WRITE);
	if (e) return e;
	meta = gf_isom_get_meta(file, root_meta, track_num);
	if (!meta) {
		GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("Trying to add item, but missing meta box"));
		return GF_BAD_PARAM;
	}

	e = FlushCaptureMode(file);
	if (e) return e;

	/*check file exists */
	if (!URN && !URL && !self_reference) {
		FILE *src = gf_f64_open(resource_path, "rb");
		if (!src) return GF_URL_ERROR;
		fclose(src);
	}

	if (meta->item_infos) {
		u32 i;
		u32 item_count = gf_list_count(meta->item_infos->item_infos);
		for (i = 0; i < item_count; i++) {
			GF_ItemInfoEntryBox *e= (GF_ItemInfoEntryBox *)gf_list_get(meta->item_infos->item_infos, i);
			if (e->item_ID > lastItemID) lastItemID = e->item_ID;
		}
	}

	infe = (GF_ItemInfoEntryBox *)infe_New();
	infe->item_ID = ++lastItemID;

	/*get relative name*/
	if (item_name) {
		infe->item_name = gf_strdup(item_name);
	} else if (resource_path) {
		if (strrchr(resource_path, GF_PATH_SEPARATOR)) {
			infe->item_name = gf_strdup(strrchr(resource_path, GF_PATH_SEPARATOR) + 1);
		} else {
			infe->item_name = gf_strdup(resource_path);
		}
	}

	if (mime_type) {
		infe->content_type = gf_strdup(mime_type);
	} else {
		infe->content_type = gf_strdup("application/octet-stream");
	}
	if (content_encoding) infe->content_encoding = gf_strdup(content_encoding);

	/*Creation of the ItemLocation */
	location_entry = (GF_ItemLocationEntry*)gf_malloc(sizeof(GF_ItemLocationEntry));
	if (!location_entry) {
		gf_isom_box_del((GF_Box *)infe);
		return GF_OUT_OF_MEM;
	}
	memset(location_entry, 0, sizeof(GF_ItemLocationEntry));
	location_entry->extent_entries = gf_list_new();
		
	/*Creates an mdat if it does not exist*/
	if (!file->mdat) {
		file->mdat = (GF_MediaDataBox *)mdat_New();
		gf_list_add(file->TopBoxes, file->mdat);
	}

	/*Creation an ItemLocation Box if it does not exist*/
	if (!meta->item_locations) meta->item_locations = (GF_ItemLocationBox *)iloc_New();
	gf_list_add(meta->item_locations->location_entries, location_entry);
	location_entry->item_ID = lastItemID;

	if (!meta->item_infos) meta->item_infos = (GF_ItemInfoBox *) iinf_New();
	e = gf_list_add(meta->item_infos->item_infos, infe);
	if (e) return e;

	/*0: the current file*/
	location_entry->data_reference_index = 0;
	if (self_reference) {
		GF_ItemExtentEntry *entry;
		GF_SAFEALLOC(entry, GF_ItemExtentEntry);
		gf_list_add(location_entry->extent_entries, entry);
		if (!infe->item_name) infe->item_name = gf_strdup("");
		return GF_OK;
	}

	/*file not copied, just referenced*/
	if (URL || URN) {
		u32 dataRefIndex;
		if (!meta->file_locations) meta->file_locations = (GF_DataInformationBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_DINF);
		if (!meta->file_locations->dref) meta->file_locations->dref = (GF_DataReferenceBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_DREF);
		e = Media_FindDataRef(meta->file_locations->dref, (char *) URL, (char *) URN, &dataRefIndex);
		if (e) return e;
		if (!dataRefIndex) {
			e = Media_CreateDataRef(meta->file_locations->dref, (char *) URL, (char *) URN, &dataRefIndex);
			if (e) return e;
		}
		location_entry->data_reference_index = dataRefIndex;
	}

	/*capture mode, write to disk*/
	if ((file->openMode == GF_ISOM_OPEN_WRITE) && !location_entry->data_reference_index) {
		FILE *src;
		GF_ItemExtentEntry *entry;
		GF_SAFEALLOC(entry, GF_ItemExtentEntry);

		location_entry->base_offset = gf_bs_get_position(file->editFileMap->bs);

		/*update base offset size*/
		if (location_entry->base_offset>0xFFFFFFFF) meta->item_locations->base_offset_size = 8;
		else if (location_entry->base_offset && !meta->item_locations->base_offset_size) meta->item_locations->base_offset_size = 4;

		entry->extent_length = 0;
		entry->extent_offset = 0;
		gf_list_add(location_entry->extent_entries, entry);

		src = gf_f64_open(resource_path, "rb");
		if (src) {
			char cache_data[4096];
			u64 remain;
			gf_f64_seek(src, 0, SEEK_END);
			entry->extent_length = gf_f64_tell(src);
			gf_f64_seek(src, 0, SEEK_SET);

			remain = entry->extent_length;
			while (remain) {
				u32 size_cache = (remain>4096) ? 4096 : (u32) remain;
				size_cache = fread(cache_data, 1, size_cache, src);
				gf_bs_write_data(file->editFileMap->bs, cache_data, size_cache);
				remain -= size_cache;
			}
			fclose(src);

			/*update length size*/
			if (entry->extent_length>0xFFFFFFFF) meta->item_locations->length_size = 8;
			else if (entry->extent_length && !meta->item_locations->length_size) meta->item_locations->length_size = 4;
		}
	}
	/*store full path for info*/
	else if (!location_entry->data_reference_index) {
		infe->full_path = gf_strdup(resource_path);
	}
	return GF_OK;
}
Пример #20
0
static GF_Err MP3_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;
	u32 hdr, start_from;
	MP3Reader *read = plug->priv;

	if (read->ch != channel)
		return GF_STREAM_NOT_FOUND;

	*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;

	/*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;
		}
		*is_new_data = 1;

		pos = gf_f64_tell(read->stream);
		hdr = gf_mp3_get_next_header(read->stream);
		if (!hdr) {
			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;
		}
		read->data_size = gf_mp3_frame_size(hdr);
		if (!read->data_size) {
			*out_reception_status = GF_EOS;
			read->done = 1;
			return GF_OK;
		}


		/*we're seeking*/
		if (read->start_range && read->duration) {
			read->current_time = 0;
			start_from = (u32) (read->start_range * read->sample_rate);
			gf_f64_seek(read->stream, 0, SEEK_SET);
			while (read->current_time<start_from) {
				hdr = gf_mp3_get_next_header(read->stream);
				if (!hdr) {
					read->start_range = 0;
					*out_reception_status = GF_EOS;
					return GF_OK;
				}
				read->current_time += gf_mp3_window_size(hdr);
				read->data_size = gf_mp3_frame_size(hdr);
				gf_f64_seek(read->stream, read->data_size-4, SEEK_CUR);
			}
			read->start_range = 0;
			GF_LOG(GF_LOG_DEBUG, GF_LOG_NETWORK, ("[MP3Demux] Seeking to frame size %d - TS %d - file pos %d\n", read->data_size, read->current_time, gf_f64_tell(read->stream)));
		}

		read->sl_hdr.compositionTimeStamp = read->current_time;
		GF_LOG(GF_LOG_DEBUG, GF_LOG_NETWORK, ("[MP3Demux] Found new frame size %d - TS %d - file pos %d\n", read->data_size, read->current_time, gf_f64_tell(read->stream)));

		read->current_time += gf_mp3_window_size(hdr);

		read->data = gf_malloc(sizeof(char) * (read->data_size+read->pad_bytes));
		read->data[0] = (hdr >> 24) & 0xFF;
		read->data[1] = (hdr >> 16) & 0xFF;
		read->data[2] = (hdr >> 8) & 0xFF;
		read->data[3] = (hdr ) & 0xFF;
		/*end of file*/
		if (fread(&read->data[4], 1, read->data_size - 4, read->stream) != read->data_size-4) {
			gf_free(read->data);
			read->data = NULL;
			if (read->is_remote) {
				gf_f64_seek(read->stream, pos, SEEK_SET);
				*out_reception_status = GF_OK;
			} else {
				*out_reception_status = GF_EOS;
			}
			return GF_OK;
		}
		if (read->pad_bytes) memset(read->data + read->data_size, 0, sizeof(char) * read->pad_bytes);
	}
	*out_sl_hdr = read->sl_hdr;
	*out_data_ptr = read->data;
	*out_data_size = read->data_size;
	return GF_OK;
}
Пример #21
0
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;
}
Пример #22
0
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);
}
Пример #23
0
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;
}
Пример #24
0
GF_Err gf_webvtt_parser_parse(GF_WebVTTParser *parser, u32 duration)
{
    char            szLine[2048];
    char            *sOK;
    u32             len;
    GF_Err          e;
    Bool            do_parse = GF_TRUE;
    GF_WebVTTCue    *cue = NULL;
    u32             start = 0;
    u32             end = 0;
    char            *prevLine = NULL;
    char            *header = NULL;
    u32             header_len = 0;
    Bool            had_marks = GF_FALSE;

    if (!parser) return GF_BAD_PARAM;
    if (parser->is_srt) {
        parser->on_header_parsed(parser->user, gf_strdup("WEBVTT\n"));
    }
    while (do_parse) {
        sOK = gf_text_get_utf8_line(szLine, 2048, parser->vtt_in, parser->unicode_type);
        REM_TRAIL_MARKS(szLine, "\r\n")
        len = (u32) strlen(szLine);
        switch (parser->state) {
        case WEBVTT_PARSER_STATE_WAITING_SIGNATURE:
            if (!sOK || len < 6 || strnicmp(szLine, "WEBVTT", 6) || (len > 6 && szLine[6] != ' ' && szLine[6] != '\t')) {
                e = GF_CORRUPTED_DATA;
                parser->report_message(parser->user, e, "Bad WEBVTT file signature %s", szLine);
                goto exit;
            } else {
                if (had_marks) { szLine[len] = '\n'; len++; }
                header = gf_strdup(szLine);
                header_len = len;
                parser->state = WEBVTT_PARSER_STATE_WAITING_HEADER;
            }
            break; /* proceed to next line */
        case WEBVTT_PARSER_STATE_WAITING_HEADER:
            if (prevLine) {
                u32 prev_len = (u32) strlen(prevLine);
                header = (char *)gf_realloc(header, header_len + prev_len + 1);
                strcpy(header+header_len,prevLine);
                header_len += prev_len;
                gf_free(prevLine);
                prevLine = NULL;
            }
            if (sOK && len) {
                if (strstr(szLine, "-->")) {
                    parser->on_header_parsed(parser->user, header);
                    /* continue to the next state without breaking */
                    parser->state = WEBVTT_PARSER_STATE_WAITING_CUE_TIMESTAMP;
                    /* no break, continue to the next state*/
                } else {
                    if (had_marks) { szLine[len] = '\n'; len++; }
                    prevLine = gf_strdup(szLine);
                    break; /* proceed to next line */
                }
            } else {
                parser->on_header_parsed(parser->user, header);
                if (!sOK) {
                    /* end of file, parsing is done */
                    do_parse = GF_FALSE;
                    break;
                } else {
                    /* empty line means end of header */
                    parser->state = WEBVTT_PARSER_STATE_WAITING_CUE;
                    /* no break, continue to the next state*/
                }
            }
        case WEBVTT_PARSER_STATE_WAITING_CUE:
            if (sOK && len) {
                if (strstr(szLine, "-->")) {
                    parser->state = WEBVTT_PARSER_STATE_WAITING_CUE_TIMESTAMP;
                    /* continue to the next state without breaking */
                } else {
                    /* discard the previous line */
                    /* should we do something with it ? callback ?*/
                    if (prevLine) {
                        gf_free(prevLine);
                        prevLine = NULL;
                    }
                    /* save this new line */
                    if (had_marks) { szLine[len] = '\n'; len++; }
                    prevLine = gf_strdup(szLine);
                    /* stay in the same state */
                    break;
                }
            } else {
                /* discard the previous line */
                /* should we do something with it ? callback ?*/
                if (prevLine) {
                    gf_free(prevLine);
                    prevLine = NULL;
                }
                if (!sOK) {
                    do_parse = GF_FALSE;
                    break;
                } else {
                    /* remove empty lines and stay in the same state */
                    break;
                }
            }
        case WEBVTT_PARSER_STATE_WAITING_CUE_TIMESTAMP:
            if (sOK && len) {
                if (cue == NULL) {
                    cue   = gf_webvtt_cue_new();
                }
                if (prevLine) {
                    gf_webvtt_cue_add_property(cue, WEBVTT_ID, prevLine, (u32) strlen(prevLine));
                    gf_free(prevLine);
                    prevLine = NULL;
                }
                e = gf_webvtt_parser_parse_timings_settings(parser, cue, szLine, len);
                if (e) {
                    if (cue) gf_webvtt_cue_del(cue);
                    cue = NULL;
                    parser->state = WEBVTT_PARSER_STATE_WAITING_CUE;
                } else {
                    start = (u32)gf_webvtt_timestamp_get(&cue->start);
                    end   = (u32)gf_webvtt_timestamp_get(&cue->end);
                    parser->state = WEBVTT_PARSER_STATE_WAITING_CUE_PAYLOAD;
                }
            } else {
                /* not possible */
                assert(0);
            }
            break;
        case WEBVTT_PARSER_STATE_WAITING_CUE_PAYLOAD:
            if (sOK && len) {
                if (had_marks) { szLine[len] = '\n'; len++; }
                gf_webvtt_cue_add_property(cue, WEBVTT_PAYLOAD, szLine, len);
                /* remain in the same state as a cue payload can have multiple lines */
                break;
            } else {
                /* end of the current cue */
                gf_webvtt_add_cue_to_samples(parser, parser->samples, cue);
                cue = NULL;

                gf_set_progress("Importing WebVTT", gf_f64_tell(parser->vtt_in), parser->file_size);
                if ((duration && (end >= duration)) || !sOK) {
                    do_parse = GF_FALSE;
                    break;
                } else {
                    /* empty line, move to next cue */
                    parser->state = WEBVTT_PARSER_STATE_WAITING_CUE;
                    break;
                }
            }
        }
        if (duration && (start >= duration)) {
            do_parse = GF_FALSE;
            break;
        }
    }


    /* no more cues to come, flush everything */
    if (cue) {
        gf_webvtt_add_cue_to_samples(parser, parser->samples, cue);
        cue = NULL;
    }
    while (gf_list_count(parser->samples) > 0) {
        GF_WebVTTSample *sample = (GF_WebVTTSample *)gf_list_get(parser->samples, 0);
        parser->last_duration = sample->end - sample->start;
        gf_list_rem(parser->samples, 0);
        parser->on_sample_parsed(parser->user, sample);
    }
    gf_set_progress("Importing WebVTT", parser->file_size, parser->file_size);
    e = GF_OK;
exit:
    if (cue) gf_webvtt_cue_del(cue);
    if (prevLine) gf_free(prevLine);
    if (header) gf_free(header);
    return e;
}