Skip to content

Commit

Permalink
add minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
williamlai committed Sep 22, 2022
1 parent a4bf2da commit c76d498
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
14 changes: 12 additions & 2 deletions samples/polly_mp3_download/polly_mp3_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static int prvInitPollySynthesizeSpeechParameter(PollySynthesizeSpeechParameter_

int main(int argc, char *argv[])
{
int res = 0;
const char *pOutputFilename = NULL;
const char *pText = NULL;
PollyServiceParameter_t xServPara = { 0 };
Expand All @@ -125,11 +126,20 @@ int main(int argc, char *argv[])
return 0;
}

xOut.callback = prvPollySynthesizeSpeechCallback;
xOut.onDataCallback = prvPollySynthesizeSpeechCallback;
xOut.pUserData = fp;
xOut.uStatusCode = 0;

printf("Polly synthesize speech begin...\n");
Polly_synthesizeSpeech(&xServPara, &xPara, &xOut);
if ((res = Polly_synthesizeSpeech(&xServPara, &xPara, &xOut)) != POLLY_ERRNO_NONE)
{
printf("Failed to execute Polly synthesize speech. (res:%d)\n", res);
if (xOut.uStatusCode != 0)
{
printf("HTTP status code: %u\n", xOut.uStatusCode);
}
}

printf("Polly synthesize speech end\n");

if (fp != NULL)
Expand Down
4 changes: 3 additions & 1 deletion src/include/polly/polly.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define POLLY_ERRNO_HTTP_100_CONTINUE_EXPECT_MORE (-8)
#define POLLY_ERRNO_HTTP_WANT_MORE (-9)
#define POLLY_ERRNO_HTTP_PARSE_FAILURE (-10)
#define POLLY_ERRNO_HTTP_REQ_FAILURE (-11)

#define AWS_POLLY_SERVICE_NAME "polly"

Expand Down Expand Up @@ -46,8 +47,9 @@ typedef struct

typedef struct
{
int (*callback)(uint8_t *pData, size_t uLen, void *pUserData);
int (*onDataCallback)(uint8_t *pData, size_t uLen, void *pUserData);
void *pUserData;
unsigned int uStatusCode;
} PollySynthesizeSpeechOutput_t;

int Polly_synthesizeSpeech(PollyServiceParameter_t *pServPara, PollySynthesizeSpeechParameter_t *pPara, PollySynthesizeSpeechOutput_t *pOut);
Expand Down
24 changes: 17 additions & 7 deletions src/source/polly.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define DEFAULT_HTTP_RECV_BUFSIZE 2048

static int prvGenDateTimeiso8601(char pDateISO8601[DATE_TIME_ISO_8601_FORMAT_STRING_SIZE])
static int prvGenDateTimeIso8601(char pDateISO8601[DATE_TIME_ISO_8601_FORMAT_STRING_SIZE])
{
int res = POLLY_ERRNO_NONE;
time_t xTimeUtcNow = {0};
Expand Down Expand Up @@ -95,7 +95,7 @@ static int prvSynthesizeSpeechRecv(NetIoHandle xNetIo, PollySynthesizeSpeechOutp
}
}

if ((res = NetIo_recv(xNetIo, (unsigned char *)(pRecvBuf + uBytesTotalReceived), uRecvBufSize - uBytesTotalReceived, &uBytesReceived)) != NETIO_ERRNO_NONE)
if (NetIo_recv(xNetIo, (unsigned char *)(pRecvBuf + uBytesTotalReceived), uRecvBufSize - uBytesTotalReceived, &uBytesReceived) != NETIO_ERRNO_NONE)
{
res = POLLY_ERRNO_NET_RECV_FAILED;
break;
Expand All @@ -111,9 +111,19 @@ static int prvSynthesizeSpeechRecv(NetIoHandle xNetIo, PollySynthesizeSpeechOutp
resHttpParser = Hp_parse(xHttpParser, pRecvBuf, uBytesTotalReceived, &uBytesParsed, &uHttpStatusCode, &pChunkLoc, &uChunkLen);
if (resHttpParser == HTTP_PARSER_ERRNO_NONE || resHttpParser == HTTP_PARSER_ERRNO_WANT_MORE_DATA)
{
if (pOut->callback != NULL && pChunkLoc != NULL && uChunkLen > 0)
if (uHttpStatusCode != 0)
{
pOut->callback((uint8_t *)pChunkLoc, uChunkLen, pOut->pUserData);
pOut->uStatusCode = uHttpStatusCode;
if (pOut->uStatusCode / 100 != 2)
{
res = POLLY_ERRNO_HTTP_REQ_FAILURE;
break;
}
}

if (pOut->onDataCallback != NULL && pChunkLoc != NULL && uChunkLen > 0)
{
pOut->onDataCallback((uint8_t *)pChunkLoc, uChunkLen, pOut->pUserData);
}

/* Move the parsed data forward */
Expand Down Expand Up @@ -155,7 +165,7 @@ int Polly_synthesizeSpeech(PollyServiceParameter_t *pServPara, PollySynthesizeSp

NetIoHandle xNetIo = NULL;

if ((res = prvGenDateTimeiso8601(pDateISO8601)) != POLLY_ERRNO_NONE)
if ((res = prvGenDateTimeIso8601(pDateISO8601)) != POLLY_ERRNO_NONE)
{
/* Propagate the error code */
}
Expand All @@ -178,7 +188,7 @@ int Polly_synthesizeSpeech(PollyServiceParameter_t *pServPara, PollySynthesizeSp
xSigV4Para.pPayload = pPayload;
xSigV4Para.uPayloadLen = uPayloadLen;

if ((res = SigV4_Sign(&xSigV4Para, &pAuth, &uAuthLen)) != SIGV4_ERRNO_NONE)
if (SigV4_Sign(&xSigV4Para, &pAuth, &uAuthLen) != SIGV4_ERRNO_NONE)
{
res = POLLY_ERRNO_SIGN_FAILURE;
}
Expand Down Expand Up @@ -229,7 +239,7 @@ int Polly_synthesizeSpeech(PollyServiceParameter_t *pServPara, PollySynthesizeSp
{
res = POLLY_ERRNO_NET_CONFIG_FAILED;
}
else if (NetIo_send(xNetIo, pHttpReq, uHttpReqLen) != NETIO_ERRNO_NONE)
else if (NetIo_send(xNetIo, (const unsigned char *)pHttpReq, uHttpReqLen) != NETIO_ERRNO_NONE)
{
res = POLLY_ERRNO_NET_SEND_FAILED;
}
Expand Down

0 comments on commit c76d498

Please sign in to comment.