/***************************************************************************
  
  c_mediasample.c
  
  (c) Benoît Minisini <benoit.minisini@gambas-basic.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.
  
***************************************************************************/

#define __C_MEDIASAMPLE_C

#include "c_mediasample.h"

CMEDIASAMPLE *MEDIA_create_sample(GstSample *sample)
{
	CMEDIASAMPLE *_object;
	
	_object = (CMEDIASAMPLE *)GB.New(CLASS_MediaSample, NULL, NULL);
	THIS->sample = sample;
	gst_sample_ref(sample);
	//fprintf(stderr, "MEDIA_create_sample -> %p\n", THIS);
	return THIS;
}

static void get_sample_data(void *_object)
{
	if (!THIS->memory)
	{
		THIS->memory = gst_buffer_get_all_memory(gst_sample_get_buffer(THIS->sample));
		gst_memory_map(THIS->memory, &THIS->info, GST_MAP_READ);
		fprintf(stderr, "get_sample_data: %p / %p %ld\n", THIS, THIS->info.data, THIS->info.size);
	}
}

#if 0
static bool _convert(CMEDIASAMPLE *_object, GB_TYPE type, GB_VALUE *conv)
{
	if (THIS && (type == GB_T_STRING || type == GB_T_CSTRING))
	{
		get_sample_data(THIS);
		
		conv->_string.value.addr = GB.NewString((char *)THIS->info.data, THIS->info.size);
		conv->_string.value.start = 0;
		conv->_string.value.len = THIS->info.size;
		return FALSE;
	}

	return TRUE;
}
#endif

//-------------------------------------------------------------------------

BEGIN_METHOD_VOID(MediaSample_free)

	//fprintf(stderr, "MediaSample_free: %p / %p\n", THIS, THIS->memory);

	if (THIS->memory)
	{
		gst_memory_unmap(THIS->memory, &THIS->info);
		gst_memory_unref(THIS->memory);
		THIS->memory = NULL;
	}
	
	gst_sample_unref(THIS->sample);

END_METHOD

BEGIN_PROPERTY(MediaSample_Data)

	get_sample_data(THIS);
	GB.ReturnConstString((char *)THIS->info.data, THIS->info.size);

END_PROPERTY

BEGIN_PROPERTY(MediaSample_Size)

	get_sample_data(THIS);
	GB.ReturnInteger(THIS->info.size);

END_PROPERTY

BEGIN_PROPERTY(MediaSample_Caps)

	GB.ReturnObject(MEDIA_get_caps(gst_sample_get_caps(THIS->sample)));

END_PROPERTY

//-------------------------------------------------------------------------

GB_DESC MediaSampleDesc[] = 
{
	GB_DECLARE("MediaSample", sizeof(CMEDIASAMPLE)),
	GB_NOT_CREATABLE(),
	
	GB_METHOD("_free", NULL, MediaSample_free, NULL),
	
	GB_PROPERTY_READ("Data", "s", MediaSample_Data),
	GB_PROPERTY_READ("Size", "i", MediaSample_Size),
	
	GB_PROPERTY_READ("Info", "Collection", MediaSample_Caps),
	
	GB_END_DECLARE
};

