请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
工具/软件:Linux
您好!
我使用的是 ti-processor-sdk-linux-am57xx-evm-03.00.00.04 SDK,我想使用硬件编码 ,我使用 appsrc 和 appsink
不幸的是内存泄漏。 有人可以帮助我吗?
#include "CGStrenCode.h"
CGStrenCode::CGStrenCode()
{
m_pSource = NULL;
m_nWidth = 0;
m_nHeight = 0;
m_bInital = false;
}
CGStrenCode::~CGStrenCode ()
{
GST_ELECK_SET_STATE (m_pipeline、GST_State_NULL);
fprintf (stderr、"删除管道\n");
GST_object_UNREF (GST_object (m_pipeline));
FREE (m_pSource);
m_pSource = NULL;
}
int CGStrenCode::Inital (const char * szname、bool bentcode、int nWidth、int nHeight)
{
if (NULL =szname || strlen (szname)==0)
返回-1;
if (nWidth <= 0|| nHeight <=0)
返回-2;
if (m_bInital)
return -99;
m_nWidth = nWidth;
m_nHeight = nHeight;
CreateName (szname、"管道");
m_pipeline = gst_pipeline_new (m_szName);//"mypipeline");
g_assert (m_pipeline);
CreateName (szname、"source");
m_appsrc = gst_elem_factory_make ("appsrc"、m_szName);//"mysource");
g_assert (m_appsrc);
m_pSource =(char *)(malloc (nWidth*nHeight*3));
if (NULL == m_pSource)
返回-3;
返回 InitalEncode (szname);
}
int CGStrenCode::startCode (char * sourcebuf、int sourceLen、char * resultbuf、int* resultLen)
{
if (sourceLen > m_nWidth* m_nHeight *3|| sourcebuf <= 0)
{
printf (在最大长度\n"/sourceLen 上为"%d);
返回-1;
}
if (NULL =sourcebuf || NULL =m_pSource || NULL == resultbuf || NULL == resultLen)
{
printf ("缓冲区为空\n");
返回-2;
}
if (!m_bInital)
返回-3;
int sourcelen = sourceLen;
memcpy (m_pSource、sourcebuf、sourcelen);
PushBufferEncode (m_pSource、sourcelen);
int reslen=0;
char * buf = pullH264Encode (&reslen);
* resultlen = reslen;
if (* resultLen!=0)
memcpy (resultbuf、buf、reslen);
删除[] buf;
返回0;
}
int CGStrenCode::InitalEncode (const char * szname)
{
gboolean bLinkOk;
GstElement *编码;
CreateName (szname、"编码");
编码= GST_ELECK_factory_make ("ducatih264en"、m_szName);//"myencode");
G_ASSERT (编码);
CreateName (szname、"sink");
m_appsink = gst_elem_factory_make ("appsink"、m_szName);//"mysink");
g_assert (m_appsink);
if (!m_pipeline ||!m_appsrc ||!encode ||!m_appsink)
{
fprintf (stderr、"不能 gst_element_factory_make、正在终止\n");
返回-4;
}
GstCaps * capsappsrc2Jpegenc;//在 appsrc 和 jpegenc 之间
capsappsrc2Jpegenc = gst_caps_new_full (
GST_structure_new ("视频/x-raw "、
"format"、G_TYPE_string、"NV12"、
"宽度"、G_TYPE_INT、m_nWidth、
"高度"、G_TYPE_INT、m_nHeight、
"帧速率"、GST_TYPE_FRATE、15、1、
null)、
GST_structure_new ("video/x-h264"、
"format"、G_TYPE_string、"NV12"、
"宽度"、G_TYPE_INT、m_nWidth、
"高度"、G_TYPE_INT、m_nHeight、
null)、
null);
gst_bin_add_many (gst_bin (m_pipeline)、m_appsrc、encode、m_appsink、NULL);
bLinkOk = gst_elem_link_filtered (m_appsrc、编码、capsappsrc2Jpegenc);
if (!bLinkOk){
G_WARNING ("链接 src 编码\n"失败);
返回-5;
}
GST_ELECK_LINK (编码、m_appsink);
G_object_set (G_object (m_appsrc)、
"stream-type"、gst_app_stream_type_stream、
"format"、gst_format_Bytes、
null);
G_object_set (G_object (m_appsink)、
"丢弃"、true、NULL);
G_object_set (G_object (编码)、
"速率预设"、1、NULL);
fprintf (stderr、"将 g_main_loop_run 设置为 gst_state_play\n");
GST_ELEGER_SET_STATE (m_F管线、GST_State_Playing);
m_bInital = true;
返回0;
}
空 CGStrenCode::PushBufferEncode (char *nv12buf、int len)
{
GstFlowReturn;
GstBuffer *缓冲区;
GstMapInfo 信息;
Buffer = GST_Buffer_new_allocate (NULL、len、NULL);
GST_buffer_map (buffer、&info、GST_MAP_WRITE);
memmove (info.data、nv12buf、len);
RET = GST_APP_src_PUSH_BUFFER (GST_APP_SRC (m_appsrc)、buffer);
if (ret =GST_flow_OK){
// printf ("PUSH OK\n");
}
GST_buffer_unmap (buffer、&info);
}
char* CGStrenCode:::PullH264Encode (int* outlen)
{
GstSample*样本;
SAMPLE = GST_APP_SINK_PULL_SAMPLE (GST_APP_SINK (m_appSINK));
if (采样== NULL)
{
printf ("=========================== null \n");
fprintf (stderr、"gst_app_sink_pull_sample 返回 null);
// GST_SAMPLE_UNREF (样本);
返回 NULL;
}
GstBuffer* buffer = GST_SAMPLE_GET_buffer (sample);
GstMapInfo MapInfo;
gst_buffer_map (buffer、&MapInfo、gst_map_read);
// printf ("拉动 buffersize:%d\n"、MapInfo.size);
char* Pret = new char[MapInfo.size];
memmove (Pret、MapInfo.data、MapInfo.size);
GST_buffer_unmap (buffer、&MapInfo);
GST_SAMPLE_UNREF (样本);
*outlen = MapInfo.size;
返回前缀
;}
void CGStrenCode::CreateName (const char * left、const char * right)
{
memset (m_szName、0、LEN_encode_name);
strcpy (m_szName、left);
strcat (m_szName、right);
}
#ifndef CGSTRENCODE_H #define CGSTRENCODE_H #include #include #include #include #include #include #include #include #define LEN_encode_name 128 类 CGStrenCode { public: CGStrenCode(); ~CGStrenCode(); int 初始值(const char * szname、bool bentcode、int nWidth、int nHeight); int startCode (char * sourcebuf、int sourceLen、char * resultbuf、int* resultLen); protected: // int InitalDecode (const char * szname); int InitalEncode (const char * szname); void PushBufferEncode (char *nv12buf,int len); CHAR* PullH264Encode (int* outlen); void CreateName (const char *、const char *); private: GstElement * m_pipeline; GstElement * m_appsrc; GstElement * m_appsink; int m_nWidth; 内部 m_n 高度; char m_szName[LEN_encode_name]; char * m_pSource; bool m_bInital; GstBuffer * m_pGstBuffer; }; #endif // CGSTRENCODE_H