1

OMX provides a struct with following definition

/* Parameter specifying the content URI to use. */
typedef struct OMX_PARAM_CONTENTURITYPE
{
OMX_U32 nSize;
/**< size of the structure in bytes */
OMX_VERSIONTYPE nVersion; /**< OMX specification version information */
OMX_U8 contentURI[1];     /**< The URI name*/
}OMX_PARAM_CONTENTURITYPE;
OMX_IndexParamContentURI,
/**< The URI that identifies the target content. Data type is OMX_PARAM_CONTENTURITYPE. */

I have got a constant char array to set.

char* filename = "/test.bmp";

As far as I understood I need somehow to set memcopy filename to struct.contentURI and then to update the struct.size accordingly. How can I do it?

Best regards

Stasik
  • 2,568
  • 1
  • 25
  • 44

1 Answers1

1

First you need to allocate enough memory to contain the fixed-size parts and the filename:

size_t uri_size = strlen(filename) + 1;
size_t param_size = sizeof(OMX_PARAM_CONTENTURITYPE) + uri_size - 1;
OMX_PARAM_CONTENTURITYPE * param = malloc(param_size);

adding 1 to include the termination character, and subtracting 1 because the structure already contains an array of one byte.

In C++, you'll need a cast, and you should use a smart pointer or a vector for exception safety:

std::vector<char> memory(param_size);
OMX_PARAM_CONTENTURITYPE * param = 
    reinterpret_cast<OMX_PARAM_CONTENTURITYPE *>(&memory[0]);

Then you can fill in the fields:

param->nSize = param_size;
param->nVersion = whatever;
memcpy(param->contentURI, filename, uri_size);

and don't forget to free(param) once you've finished with it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • should i subtract one? what about the terminating \0? – Stasik Dec 21 '12 at 15:57
  • 1
    And rememver that this is officially undefined behavior (and I've heard of debugging implementations where it would fail). – James Kanze Dec 21 '12 at 15:58
  • @Stasik That depends on whether the library expects it to be terminated or not. I guessed it didn't, since there's a length field that makes termination redundant; but if it does, then don't subtract one, and do add one to the amount copied (or, equivalently, add one to `uri_size` to include the terminator there). – Mike Seymour Dec 21 '12 at 16:00
  • sorry, i can not assume that you have read that in OMX "Note that, in common with other variable length structures, the nSize parameter indicates the total size of the structure including the URI name. This includes any required termination byte(s)." – Stasik Dec 21 '12 at 16:02
  • @Stasik: No, I'm not familiar with the library. In that case, I'll adjust the answer accordingly. – Mike Seymour Dec 21 '12 at 16:02
  • @Stasik Maybe. The technique was widely used at one time, but the C committee decided, for various reasons, to disallow it. – James Kanze Dec 22 '12 at 00:24