提交 705e7639 authored 作者: blu's avatar blu

init

上级 9ee1230c
...@@ -9,6 +9,9 @@ extern "C" { ...@@ -9,6 +9,9 @@ extern "C" {
#undef av_err2str #undef av_err2str
#define av_err2str(errnum) av_make_error_string((char*)__builtin_alloca(AV_ERROR_MAX_STRING_SIZE), AV_ERROR_MAX_STRING_SIZE, errnum) #define av_err2str(errnum) av_make_error_string((char*)__builtin_alloca(AV_ERROR_MAX_STRING_SIZE), AV_ERROR_MAX_STRING_SIZE, errnum)
#define PS_MARK_E "DEADBEEF"
#define PS_MARK_S "BEEFDEAD"
void logThrow(void * avcl, int lvl, const char *fmt, ...) void logThrow(void * avcl, int lvl, const char *fmt, ...)
{ {
(void) avcl; (void) avcl;
...@@ -24,7 +27,9 @@ namespace AVPacketSerializer { ...@@ -24,7 +27,9 @@ namespace AVPacketSerializer {
int encode(AVPacket &pkt, char **bytes) { int encode(AVPacket &pkt, char **bytes) {
int cnt = 0; int cnt = 0;
//data //data
int wholeSize = sizeof(pkt.size) + pkt.size; char mark_s[] = PS_MARK_S;
char mark_e[] = PS_MARK_E;
int wholeSize = sizeof(mark_s) - 1 + sizeof(pkt.size) + pkt.size;
//side data //side data
wholeSize +=sizeof(pkt.side_data_elems); wholeSize +=sizeof(pkt.side_data_elems);
if(pkt.side_data_elems != 0) { if(pkt.side_data_elems != 0) {
...@@ -34,9 +39,11 @@ namespace AVPacketSerializer { ...@@ -34,9 +39,11 @@ namespace AVPacketSerializer {
} }
// 4 + 8: wholeSize + DEADBEAF // 4 + 8: wholeSize + DEADBEAF
wholeSize += sizeof(pkt.pts) * 5 + sizeof(pkt.flags) + sizeof(pkt.stream_index) + sizeof(wholeSize) + 8; wholeSize += sizeof(pkt.pts) * 5 + sizeof(pkt.flags) + sizeof(pkt.stream_index) + sizeof(wholeSize) + sizeof(mark_e) -1;
*bytes = (char*)malloc(wholeSize); *bytes = (char*)malloc(wholeSize);
memcpy((*bytes)+cnt, mark_s, sizeof(mark_s) -1);
cnt += sizeof(mark_s) -1;
// data // data
memcpy((*bytes)+cnt, &(pkt.size), sizeof(pkt.size)); memcpy((*bytes)+cnt, &(pkt.size), sizeof(pkt.size));
cnt +=sizeof(pkt.size); cnt +=sizeof(pkt.size);
...@@ -73,8 +80,8 @@ namespace AVPacketSerializer { ...@@ -73,8 +80,8 @@ namespace AVPacketSerializer {
cnt+=sizeof(pkt.stream_index); cnt+=sizeof(pkt.stream_index);
memcpy((*bytes )+cnt,&wholeSize, sizeof(wholeSize)); memcpy((*bytes )+cnt,&wholeSize, sizeof(wholeSize));
cnt+=sizeof(wholeSize); cnt+=sizeof(wholeSize);
memcpy((*bytes )+cnt, (char*)"DEADBEEF", 8); memcpy((*bytes )+cnt, mark_e, sizeof(mark_e) -1);
cnt+=8; cnt+=sizeof(mark_e) -1;
av_log_set_level(AV_LOG_DEBUG); av_log_set_level(AV_LOG_DEBUG);
assert(cnt == wholeSize); assert(cnt == wholeSize);
av_log(NULL, AV_LOG_DEBUG, "\n\n\npkt origin size %d, serialized size: %d, elems:%d\n\n\n", pkt.size, wholeSize, pkt.side_data_elems); av_log(NULL, AV_LOG_DEBUG, "\n\n\npkt origin size %d, serialized size: %d, elems:%d\n\n\n", pkt.size, wholeSize, pkt.side_data_elems);
...@@ -86,11 +93,15 @@ namespace AVPacketSerializer { ...@@ -86,11 +93,15 @@ namespace AVPacketSerializer {
//AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket)); //AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket));
int ret = 0; int ret = 0;
int got = 0; int got = 0;
if(strncmp("DEADBEEF", bytes + len - 8, 8) != 0) { char mark_s[] = PS_MARK_S;
char mark_e[] = PS_MARK_E;
if(memcmp(mark_e, bytes + len - sizeof(mark_e) + 1, sizeof(mark_e) - 1) != 0 || memcmp(mark_s, bytes, sizeof(mark_s) - 1)) {
av_log(NULL, AV_LOG_ERROR, "invalid packet"); av_log(NULL, AV_LOG_ERROR, "invalid packet");
return -1; return -1;
} }
memcpy(&(pkt->size), bytes, sizeof(pkt->size)); //skip mark_s
got += sizeof(mark_s) - 1;
memcpy(&(pkt->size), bytes + got, sizeof(pkt->size));
got += sizeof(pkt->size); got += sizeof(pkt->size);
av_new_packet(pkt, pkt->size); av_new_packet(pkt, pkt->size);
memcpy(pkt->data, bytes + got, pkt->size); memcpy(pkt->data, bytes + got, pkt->size);
...@@ -136,17 +147,52 @@ void mqPacketFree(void *data, void*hint) { ...@@ -136,17 +147,52 @@ void mqPacketFree(void *data, void*hint) {
free(data); free(data);
} }
namespace AVFormatSerializer { namespace AVFormatCtxSerializer {
int encode(AVFormatContext &context, char **bytes) { struct AVFormatCtx {
AVFormatCtx() {};
AVFormatCtx(char *bytes, int len) {
_decode(bytes, len);
}
AVFormatCtx(AVFormatContext *pCtx):pCtx(pCtx) {
}
~AVFormatCtx() {
}
int toBytes(char **bytes) {
return _encode(pCtx, bytes);
}
int fromBytes(char *bytes, int len) {
return _decode(bytes, len);
}
private:
int nb_streams;
AVStream *streams = NULL;
AVFormatContext * pCtx = NULL;
int _encode(AVFormatContext *ctx, char **bytes) {
int ret = 0; int ret = 0;
int wholeSize = 0; int wholeSize = 0;
int got = 0; int got = 0;
char mark_s[] = PS_MARK_S;
char mark_e[] = PS_MARK_E;
wholeSize += sizeof(mark_s) - 1;
// num streams // num streams
wholeSize += sizeof(context.nb_streams); wholeSize += sizeof(ctx.nb_streams);
for(int i = 0; i < wholeSize; i++) { for(int i = 0; i < ctx.nb_streams; i++) {
wholeSize += sizeof(AVStream);
}
wholeSize += sizeof(wholeSize);
} }
int _decode(char *bytes, int len) {
} }
};
} }
#endif #endif
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论