Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more refactoring for streaming, adding packet fifo for sorting packet…
…s by timestamp/stream
- Loading branch information
Showing
5 changed files
with
298 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include "packetfifo.h" | ||
#include <float.h> | ||
|
||
using namespace std; | ||
using namespace transcoding; | ||
|
||
StreamPacket::StreamPacket(AVPacket& packet, double timestamp) : | ||
packet(packet), timestamp(timestamp) { | ||
} | ||
|
||
StreamPacket::~StreamPacket() { | ||
} | ||
|
||
StreamPacketList::StreamPacketList() { | ||
} | ||
|
||
StreamPacketList::~StreamPacketList() { | ||
this->DropAllPackets(); | ||
} | ||
|
||
int StreamPacketList::GetCount() { | ||
return this->packets.size(); | ||
} | ||
|
||
void StreamPacketList::QueuePacket(AVPacket& packet, double timestamp) { | ||
// NOTE: assuming that packets in a stream come in order | ||
this->packets.push_back(new StreamPacket(packet, timestamp)); | ||
} | ||
|
||
double StreamPacketList::GetNextTimestamp() { | ||
if (this->packets.size()) { | ||
this->packets[0]->timestamp; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
bool StreamPacketList::DequeuePacket(AVPacket& packet) { | ||
if (!this->packets.size()) { | ||
return false; | ||
} | ||
StreamPacket* streamPacket = this->packets.front(); | ||
this->packets.erase(this->packets.begin()); | ||
packet = streamPacket->packet; | ||
delete streamPacket; | ||
return true; | ||
} | ||
|
||
void StreamPacketList::DropAllPackets() { | ||
AVPacket packet; | ||
while (this->DequeuePacket(packet)) { | ||
av_free_packet(&packet); | ||
} | ||
} | ||
|
||
PacketFifo::PacketFifo(int streamCount) : | ||
count(0) { | ||
for (int n = 0; n < streamCount; n++) { | ||
this->streams.push_back(new StreamPacketList()); | ||
} | ||
} | ||
|
||
PacketFifo::~PacketFifo() { | ||
while(this->streams.size()) { | ||
StreamPacketList* list = this->streams.back(); | ||
this->streams.pop_back(); | ||
delete list; | ||
} | ||
} | ||
|
||
int PacketFifo::GetCount() { | ||
return this->count; | ||
} | ||
|
||
void PacketFifo::QueuePacket(int stream, AVPacket& packet, double timestamp) { | ||
this->count++; | ||
this->streams[stream]->QueuePacket(packet, timestamp); | ||
} | ||
|
||
bool PacketFifo::DequeuePacket(AVPacket& packet) { | ||
printf("dequeue: %d\n", this->count); | ||
int stream = -1; | ||
double lowestTimestamp = DBL_MAX; | ||
for (int n = 0; n < this->streams.size(); n++) { | ||
if (this->streams[n]->GetCount()) { | ||
double timestamp = this->streams[n]->GetNextTimestamp(); | ||
if (timestamp < lowestTimestamp) { | ||
stream = n; | ||
lowestTimestamp = timestamp; | ||
} | ||
} | ||
} | ||
if (stream == -1) { | ||
printf("no stream\n"); | ||
return false; | ||
} | ||
this->streams[stream]->DequeuePacket(packet); | ||
this->count--; | ||
return true; | ||
} | ||
|
||
void PacketFifo::DropAllPackets() { | ||
for (int n = 0; n < this->streams.size(); n++) { | ||
this->streams[n]->DropAllPackets(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <node.h> | ||
#include <v8.h> | ||
#include <vector> | ||
#include "utils.h" | ||
|
||
#ifndef NODE_TRANSCODING_PACKETFIFO | ||
#define NODE_TRANSCODING_PACKETFIFO | ||
|
||
using namespace v8; | ||
|
||
namespace transcoding { | ||
|
||
class StreamPacket { | ||
public: | ||
StreamPacket(AVPacket& packet, double timestamp); | ||
~StreamPacket(); | ||
|
||
double timestamp; | ||
AVPacket packet; | ||
}; | ||
|
||
class StreamPacketList { | ||
public: | ||
StreamPacketList(); | ||
~StreamPacketList(); | ||
|
||
int GetCount(); | ||
|
||
void QueuePacket(AVPacket& packet, double timestamp); | ||
double GetNextTimestamp(); | ||
bool DequeuePacket(AVPacket& packet); | ||
void DropAllPackets(); | ||
|
||
private: | ||
std::vector<StreamPacket*> packets; | ||
}; | ||
|
||
class PacketFifo { | ||
public: | ||
PacketFifo(int streamCount); | ||
~PacketFifo(); | ||
|
||
int GetCount(); | ||
|
||
void QueuePacket(int stream, AVPacket& packet, double timestamp); | ||
bool DequeuePacket(AVPacket& packet); | ||
void DropAllPackets(); | ||
|
||
private: | ||
int count; | ||
std::vector<StreamPacketList*> streams; | ||
}; | ||
|
||
}; // transcoding | ||
|
||
#endif // NODE_TRANSCODING_PACKETFIFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.