Commit 2c730355 authored by Linshizhi's avatar Linshizhi

Add MovMem Protocol.

parent 22f6a543
#include "movMemProto.h"
#include <algorithm>
#include <stdexcept>
namespace IOProto {
namespace MovMemProto {
int MovMemProto::read_packet_internal(void *priv, uint8_t *buf, int bufSize) {
if (flag == write || bufSize < 0 || buf == nullptr) {
return AVERROR(EINVAL);
}
// No Datas
if (trans.mem.data == nullptr && s.empty()) {
return AVERROR(EAGAIN);
}
if (trans.mem.data == nullptr) {
trans.mem = s.front();
// EOF arrive
if (trans.mem.data == nullptr) {
return AVERROR_EOF;
}
trans.pos = trans.mem.data.get();
trans.remain = trans.mem.size;
s.pop();
}
size_t sizeToRead = std::min((size_t)bufSize, trans.remain);
memcpy(buf, trans.pos, sizeToRead);
// Update TransContext
trans.pos += sizeToRead;
trans.remain -= sizeToRead;
// Datas of current memory piece is all
// readed, do cleaning.
if (trans.remain == 0) {
trans.mem.data = nullptr;
trans.pos = nullptr;
trans.remain = 0;
}
return sizeToRead;
}
int MovMemProto::write_packet_internal(void *priv, uint8_t *buf, int bufSize) {
throw std::runtime_error("MovMemProto not support write");
}
int64_t MovMemProto::seek_packet_internal(void *opaque, int64_t offset, int whence) {
throw std::runtime_error("MovMemProto not support seek");
}
void MovMemProto::close_internal() {
trans.mem.data = nullptr;
while (!s.empty()) {
auto mem = s.front();
s.pop();
}
}
}
}
#include "proto.h"
#include <memory>
#include <queue>
#ifndef MOVMEMPROTO_H
#define MOVMEMPROTO_H
namespace IOProto {
namespace MovMemProto {
struct PacketMem {
std::shared_ptr<uint8_t> data;
size_t size;
};
using MemPiece = PacketMem;
using Stream = std::queue<MemPiece>;
struct TransContext {
MemPiece mem;
uint8_t *pos;
size_t remain;
};
class MovMemProto: public IOProtocol<MovMemProto> {
public:
static constexpr char protoName[] = "MovMemProto";
MovMemProto(void *priv, RW_FLAG flag):
IOProtocol(protoName, flag, priv) {}
int read_packet_internal(void *priv, uint8_t *buf, int bufSize);
int write_packet_internal(void *priv, uint8_t *buf, int bufSize);
/* MovMemProto temporarily only support Stream so seek function
* is ignored. */
int64_t seek_packet_internal(void *opaque, int64_t offset, int whence);
void close_internal();
void push(MemPiece data) {
s.push(data);
}
private:
Stream s;
TransContext trans;
};
}}
#endif /* MOVMEMPROTO_H */
......@@ -27,8 +27,8 @@ protected:
}
void TearDown() override {
//std::string cmd = "rm -f " + outFilePath;
//system(cmd.c_str());
std::string cmd = "rm -f " + outFilePath;
system(cmd.c_str());
}
std::string inFilePath = "./resources/small_bunny_1080p_60fps.mp4";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment