⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isma.cpp

📁 完整的RTP RTSP代码库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is MPEG4IP. *  * The Initial Developer of the Original Code is Cisco Systems Inc. * Portions created by Cisco Systems Inc. are * Copyright (C) Cisco Systems Inc. 2001 - 2004.  All Rights Reserved. * * 3GPP features implementation is based on 3GPP's TS26.234-v5.60, * and was contributed by Ximpo Group Ltd. * * Portions created by Ximpo Group Ltd. are * Copyright (C) Ximpo Group Ltd. 2003, 2004.  All Rights Reserved. * * Contributor(s):  *		Dave Mackie		  dmackie@cisco.com *              Alix Marchandise-Franquet alix@cisco.com *              Ximpo Group Ltd.                mp4v2@ximpo.com */#include "mp4common.h"static const u_int8_t BifsV2Config[3] = {	0x00, 0x00, 0x60 // IsCommandStream = 1, PixelMetric = 1};void MP4File::MakeIsmaCompliant(bool addIsmaComplianceSdp){	ProtectWriteOperation("MP4MakeIsmaCompliant");	if (m_useIsma) {		// already done		return;	}	// find first audio and/or video tracks	MP4TrackId audioTrackId = MP4_INVALID_TRACK_ID;	try {		audioTrackId = FindTrackId(0, MP4_AUDIO_TRACK_TYPE);	}	catch (MP4Error* e) {		delete e;	}	MP4TrackId videoTrackId = MP4_INVALID_TRACK_ID;	try {		videoTrackId = FindTrackId(0, MP4_VIDEO_TRACK_TYPE);	}	catch (MP4Error* e) {		delete e;	}	if (audioTrackId == MP4_INVALID_TRACK_ID &&	    videoTrackId == MP4_INVALID_TRACK_ID) return;	const char *audio_media_data_name, *video_media_data_name;	uint8_t videoProfile = 0xff;	if (audioTrackId != MP4_INVALID_TRACK_ID) {	  audio_media_data_name = MP4GetTrackMediaDataName(this, audioTrackId);	  if (!(ATOMID(audio_media_data_name) == ATOMID("mp4a") ||		ATOMID(audio_media_data_name) == ATOMID("enca"))) {	    VERBOSE_ERROR(m_verbosity,			  printf("MakeIsmaCompliant:can't make ISMA compliant when file contains an %s track\n", audio_media_data_name);			  );	    return;	  }	}	//	// Note - might have to check for avc1 here...	if (videoTrackId != MP4_INVALID_TRACK_ID) {	  video_media_data_name = MP4GetTrackMediaDataName(this, videoTrackId);	  if (!(ATOMID(video_media_data_name) == ATOMID("mp4v") ||		ATOMID(video_media_data_name) == ATOMID("encv"))) {	    VERBOSE_ERROR(m_verbosity,			  printf("MakeIsmaCompliant:can't make ISMA compliant when file contains an %s track\n", video_media_data_name);			  );	    return;	  }	  uint32_t verb = GetVerbosity();	  SetVerbosity(verb & ~MP4_DETAILS_ERROR);	  videoProfile = MP4GetVideoProfileLevel(this, videoTrackId);	  SetVerbosity(verb);	}	m_useIsma = true;	u_int64_t fileMsDuration = 0;	fileMsDuration = 		ConvertFromMovieDuration(GetDuration(), MP4_MSECS_TIME_SCALE);	// delete any existing OD track	if (m_odTrackId != MP4_INVALID_TRACK_ID) {		DeleteTrack(m_odTrackId);	}	if (m_pRootAtom->FindAtom("moov.iods") == NULL) {	  (void)AddChildAtom("moov", "iods");	}	(void)AddODTrack();	SetODProfileLevel(0xFF);	if (audioTrackId != MP4_INVALID_TRACK_ID) {		AddTrackToOd(audioTrackId);		MP4SetAudioProfileLevel(this, 0xf);	}	if (videoTrackId != MP4_INVALID_TRACK_ID) {		AddTrackToOd(videoTrackId);		MP4SetVideoProfileLevel(this, videoProfile);	}	// delete any existing scene track	MP4TrackId sceneTrackId = MP4_INVALID_TRACK_ID;	try {		sceneTrackId = FindTrackId(0, MP4_SCENE_TRACK_TYPE);	}	catch (MP4Error *e) {		delete e;	}	if (sceneTrackId != MP4_INVALID_TRACK_ID) {		DeleteTrack(sceneTrackId);	}	// add scene track	sceneTrackId = AddSceneTrack();	SetSceneProfileLevel(0xFF);	SetGraphicsProfileLevel(0xFF);	SetTrackIntegerProperty(sceneTrackId, 		"mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr.objectTypeId", 		MP4SystemsV2ObjectType);		SetTrackESConfiguration(sceneTrackId, 		BifsV2Config, sizeof(BifsV2Config));	u_int8_t* pBytes = NULL;	u_int64_t numBytes = 0;	// write OD Update Command	CreateIsmaODUpdateCommandFromFileForFile(		m_odTrackId, 		audioTrackId, 		videoTrackId,		&pBytes, 		&numBytes);	WriteSample(m_odTrackId, pBytes, numBytes, fileMsDuration);	MP4Free(pBytes);	pBytes = NULL;	// write BIFS Scene Replace Command	CreateIsmaSceneCommand(		MP4_IS_VALID_TRACK_ID(audioTrackId), 		MP4_IS_VALID_TRACK_ID(videoTrackId),		&pBytes, 		&numBytes);	WriteSample(sceneTrackId, pBytes, numBytes, fileMsDuration);	MP4Free(pBytes);	pBytes = NULL;	// add session level sdp 	CreateIsmaIodFromFile(		m_odTrackId, 		sceneTrackId, 		audioTrackId, 		videoTrackId,		&pBytes, 		&numBytes);	char* iodBase64 = MP4ToBase64(pBytes, numBytes);	uint sdpBufLen = strlen(iodBase64) + 256;	uint used;	char* sdpBuf = (char*)MP4Calloc(sdpBufLen);	if (addIsmaComplianceSdp) {	  strncpy(sdpBuf, "a=isma-compliance:1,1.0,1\015\012", sdpBufLen);	}	used = strlen(sdpBuf);	sdpBufLen -= used;	snprintf(&sdpBuf[used], sdpBufLen, 		"a=mpeg4-iod: \042data:application/mpeg4-iod;base64,%s\042\015\012",		iodBase64);	SetSessionSdp(sdpBuf);	VERBOSE_ISMA(GetVerbosity(),		printf("IOD SDP = %s\n", sdpBuf));	MP4Free(iodBase64);	iodBase64 = NULL;	MP4Free(pBytes);	pBytes = NULL;	MP4Free(sdpBuf);	sdpBuf = NULL;}static void CloneIntegerProperty(	MP4Descriptor* pDest, 	MP4DescriptorProperty* pSrc,	const char* name){	MP4IntegerProperty* pGetProperty;	MP4IntegerProperty* pSetProperty;	if (!pSrc->FindProperty(name, (MP4Property**)&pGetProperty)) return;	if (!pDest->FindProperty(name, (MP4Property**)&pSetProperty)) return;	pSetProperty->SetValue(pGetProperty->GetValue());} void MP4File::CreateIsmaIodFromFile(	MP4TrackId odTrackId,	MP4TrackId sceneTrackId,	MP4TrackId audioTrackId, 	MP4TrackId videoTrackId,	u_int8_t** ppBytes,	u_int64_t* pNumBytes){	MP4Descriptor* pIod = new MP4IODescriptor();	pIod->SetTag(MP4IODescrTag);	pIod->Generate();	MP4Atom* pIodsAtom = FindAtom("moov.iods");	ASSERT(pIodsAtom);	MP4DescriptorProperty* pSrcIod = 		(MP4DescriptorProperty*)pIodsAtom->GetProperty(2);	CloneIntegerProperty(pIod, pSrcIod, "objectDescriptorId");	CloneIntegerProperty(pIod, pSrcIod, "ODProfileLevelId");	CloneIntegerProperty(pIod, pSrcIod, "sceneProfileLevelId");	CloneIntegerProperty(pIod, pSrcIod, "audioProfileLevelId");	CloneIntegerProperty(pIod, pSrcIod, "visualProfileLevelId");	CloneIntegerProperty(pIod, pSrcIod, "graphicsProfileLevelId");	// mutate esIds from MP4ESIDIncDescrTag to MP4ESDescrTag	MP4DescriptorProperty* pEsProperty;	if (!pIod->FindProperty("esIds", (MP4Property**)&pEsProperty)) return;	pEsProperty->SetTags(MP4ESDescrTag);	MP4IntegerProperty* pSetProperty;	MP4IntegerProperty* pSceneESID;	MP4IntegerProperty* pOdESID;	// OD	MP4Descriptor* pOdEsd =		pEsProperty->AddDescriptor(MP4ESDescrTag);	pOdEsd->Generate();	if (!pOdEsd->FindProperty("ESID", (MP4Property**)&pOdESID)) return;	// we set the OD ESID to a non-zero unique value	pOdESID->SetValue(m_odTrackId);	if (pOdEsd->FindProperty("URLFlag", 				 (MP4Property**)&pSetProperty))	  pSetProperty->SetValue(1);	u_int8_t* pBytes;	u_int64_t numBytes;	CreateIsmaODUpdateCommandFromFileForStream(		audioTrackId, 		videoTrackId,		&pBytes, 		&numBytes);	VERBOSE_ISMA(GetVerbosity(),		printf("OD data =\n"); MP4HexDump(pBytes, numBytes));	char* odCmdBase64 = MP4ToBase64(pBytes, numBytes);	uint urlBufLen = strlen(odCmdBase64) + 64;	char* urlBuf = (char*)MP4Malloc(urlBufLen);	snprintf(urlBuf, urlBufLen,		"data:application/mpeg4-od-au;base64,%s",		odCmdBase64);	MP4StringProperty* pUrlProperty;	if (pOdEsd->FindProperty("URL", 				 (MP4Property**)&pUrlProperty))	  pUrlProperty->SetValue(urlBuf);	VERBOSE_ISMA(GetVerbosity(),		printf("OD data URL = \042%s\042\n", urlBuf));	MP4Free(odCmdBase64);	odCmdBase64 = NULL;	MP4Free(pBytes);	pBytes = NULL;	MP4Free(urlBuf);	urlBuf = NULL;	MP4DescriptorProperty* pSrcDcd = NULL;	// HACK temporarily point to scene decoder config	(void)FindProperty(MakeTrackName(odTrackId, 		"mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr"),		(MP4Property**)&pSrcDcd);	ASSERT(pSrcDcd);	MP4Property* pOrgOdEsdProperty = 		pOdEsd->GetProperty(8);	pOdEsd->SetProperty(8, pSrcDcd);	// bufferSizeDB needs to be set appropriately	MP4BitfieldProperty* pBufferSizeProperty = NULL;	if (pOdEsd->FindProperty("decConfigDescr.bufferSizeDB",				 (MP4Property**)&pBufferSizeProperty)) {	  ASSERT(pBufferSizeProperty);	  pBufferSizeProperty->SetValue(numBytes);	}	// SL config needs to change from 2 (file) to 1 (null)	if (pOdEsd->FindProperty("slConfigDescr.predefined", 				 (MP4Property**)&pSetProperty))	  pSetProperty->SetValue(1);	// Scene	MP4Descriptor* pSceneEsd =		pEsProperty->AddDescriptor(MP4ESDescrTag);	pSceneEsd->Generate();	if (pSceneEsd->FindProperty("ESID", 				    (MP4Property**)&pSceneESID)) {	  // we set the Scene ESID to a non-zero unique value	  pSceneESID->SetValue(sceneTrackId);	}	if (pSceneEsd->FindProperty("URLFlag", 				    (MP4Property**)&pSetProperty))	  pSetProperty->SetValue(1);	CreateIsmaSceneCommand(		MP4_IS_VALID_TRACK_ID(audioTrackId), 		MP4_IS_VALID_TRACK_ID(videoTrackId),		&pBytes, 		&numBytes);	VERBOSE_ISMA(GetVerbosity(),		printf("Scene data =\n"); MP4HexDump(pBytes, numBytes));	char *sceneCmdBase64 = MP4ToBase64(pBytes, numBytes);	urlBuf = (char*)MP4Malloc(strlen(sceneCmdBase64) + 64);	snprintf(urlBuf, strlen(sceneCmdBase64) + 64,	  "data:application/mpeg4-bifs-au;base64,%s",	  sceneCmdBase64);	if (pSceneEsd->FindProperty("URL", 				    (MP4Property**)&pUrlProperty))	pUrlProperty->SetValue(urlBuf);	VERBOSE_ISMA(GetVerbosity(),		printf("Scene data URL = \042%s\042\n", urlBuf));	MP4Free(sceneCmdBase64);	sceneCmdBase64 = NULL;	MP4Free(urlBuf);	urlBuf = NULL;	MP4Free(pBytes);	pBytes = NULL;	// HACK temporarily point to scene decoder config	ASSERT(FindProperty(MakeTrackName(sceneTrackId, 		"mdia.minf.stbl.stsd.mp4s.esds.decConfigDescr"),			 (MP4Property**)&pSrcDcd));	ASSERT(pSrcDcd);	MP4Property* pOrgSceneEsdProperty = 		pSceneEsd->GetProperty(8);	pSceneEsd->SetProperty(8, pSrcDcd);	// bufferSizeDB needs to be set	pBufferSizeProperty = NULL;	if (pSceneEsd->FindProperty("decConfigDescr.bufferSizeDB",				    (MP4Property**)&pBufferSizeProperty)) {	ASSERT(pBufferSizeProperty);	pBufferSizeProperty->SetValue(numBytes);	}	// SL config needs to change from 2 (file) to 1 (null)	if (pSceneEsd->FindProperty("slConfigDescr.predefined", 				    (MP4Property**)&pSetProperty))	pSetProperty->SetValue(1);	// finally get the whole thing written to a memory 	pIod->WriteToMemory(this, ppBytes, pNumBytes);	// now carefully replace esd properties before destroying	pOdEsd->SetProperty(8, pOrgOdEsdProperty);	pSceneEsd->SetProperty(8, pOrgSceneEsdProperty);	pSceneESID->SetValue(0); // restore 0 value	pOdESID->SetValue(0);	delete pIod;	VERBOSE_ISMA(GetVerbosity(),		printf("IOD data =\n"); MP4HexDump(*ppBytes, *pNumBytes));}void MP4File::CreateIsmaIodFromParams(	u_int8_t videoProfile,	u_int32_t videoBitrate,	u_int8_t* videoConfig,	u_int32_t videoConfigLength,	u_int8_t audioProfile,	u_int32_t audioBitrate,	u_int8_t* audioConfig,	u_int32_t audioConfigLength,	u_int8_t** ppIodBytes,	u_int64_t* pIodNumBytes){	MP4IntegerProperty* pInt;	u_int8_t* pBytes = NULL;	u_int64_t numBytes;	// Create the IOD	MP4Descriptor* pIod = new MP4IODescriptor();	pIod->SetTag(MP4IODescrTag);	pIod->Generate();		// Set audio and video profileLevels	if (pIod->FindProperty("audioProfileLevelId", 			       (MP4Property**)&pInt))	pInt->SetValue(audioProfile);	if (pIod->FindProperty("visualProfileLevelId", 			       (MP4Property**)&pInt))	    pInt->SetValue(videoProfile);	// Mutate esIds from MP4ESIDIncDescrTag to MP4ESDescrTag	MP4DescriptorProperty* pEsProperty;	if (!pIod->FindProperty("esIds", (MP4Property**)&pEsProperty)) return;	pEsProperty->SetTags(MP4ESDescrTag);	// Add ES Descriptors	// Scene	CreateIsmaSceneCommand(		(audioProfile != 0xFF),		(videoProfile != 0xFF),		&pBytes, 		&numBytes);	VERBOSE_ISMA(GetVerbosity(),		printf("Scene data =\n"); MP4HexDump(pBytes, numBytes));	char* sceneCmdBase64 = MP4ToBase64(pBytes, numBytes);	char* urlBuf = 		(char*)MP4Malloc(strlen(sceneCmdBase64) + 64);	  snprintf(urlBuf, strlen(sceneCmdBase64) + 64,		  "data:application/mpeg4-bifs-au;base64,%s",		  sceneCmdBase64);	  	  VERBOSE_ISMA(GetVerbosity(),		       printf("Scene data URL = \042%s\042\n", urlBuf));	  	  /* MP4Descriptor* pSceneEsd = */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -