forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh264fileparser.cpp
71 lines (63 loc) · 2.19 KB
/
h264fileparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* libdatachannel streamer example
* Copyright (c) 2020 Filip Klembara (in2core)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "h264fileparser.hpp"
#include "rtc/rtc.hpp"
#include <fstream>
#include <cstring>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
using namespace std;
H264FileParser::H264FileParser(string directory, uint32_t fps, bool loop): FileParser(directory, ".h264", fps, loop) { }
void H264FileParser::loadNextSample() {
FileParser::loadNextSample();
size_t i = 0;
while (i < sample.size()) {
assert(i + 4 < sample.size());
auto lengthPtr = (uint32_t *) (sample.data() + i);
uint32_t length;
std::memcpy(&length, lengthPtr, sizeof(uint32_t));
length = ntohl(length);
auto naluStartIndex = i + 4;
auto naluEndIndex = naluStartIndex + length;
assert(naluEndIndex <= sample.size());
auto header = reinterpret_cast<rtc::NalUnitHeader *>(sample.data() + naluStartIndex);
auto type = header->unitType();
switch (type) {
case 7:
previousUnitType7 = {sample.begin() + i, sample.begin() + naluEndIndex};
break;
case 8:
previousUnitType8 = {sample.begin() + i, sample.begin() + naluEndIndex};;
break;
case 5:
previousUnitType5 = {sample.begin() + i, sample.begin() + naluEndIndex};;
break;
}
i = naluEndIndex;
}
}
vector<byte> H264FileParser::initialNALUS() {
vector<byte> units{};
if (previousUnitType7.has_value()) {
auto nalu = previousUnitType7.value();
units.insert(units.end(), nalu.begin(), nalu.end());
}
if (previousUnitType8.has_value()) {
auto nalu = previousUnitType8.value();
units.insert(units.end(), nalu.begin(), nalu.end());
}
if (previousUnitType5.has_value()) {
auto nalu = previousUnitType5.value();
units.insert(units.end(), nalu.begin(), nalu.end());
}
return units;
}