-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflv_to_rush_stream.cpp
129 lines (114 loc) · 3.39 KB
/
flv_to_rush_stream.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <unistd.h>
#include "flv/Flv.h"
#include "flv/FlvIo.h"
#include "lib/RushClient.h"
using namespace rush;
class Sleeper {
public:
void sleepUntilMs(int64_t tsMs) {
if (ts_ == -1) {
ts_ = tsMs;
return;
}
auto diffMs = tsMs - ts_;
if (diffMs <= 0) {
return;
}
::usleep(diffMs * 1000);
ts_ = tsMs;
}
private:
int64_t ts_{-1};
};
std::vector<std::shared_ptr<FlvTag>> process_flv(const std::string& filename) {
std::vector<std::shared_ptr<FlvTag>> tags;
FileReader reader(filename);
FlvHeader h(&reader);
auto prevTagSize = reader.read32bit();
while (true) {
auto tag = FlvTag::parse(&reader);
if (tag == nullptr) {
break;
}
tags.emplace_back(tag);
prevTagSize = reader.read32bit();
}
return tags;
}
void generate_media_payload(
RushClient* client,
const std::vector<std::shared_ptr<FlvTag>>& tags) {
Sleeper s;
for (const auto& tag : tags) {
if (tag->getTagType() == FLVTAGTYPE::AUDIO) {
// Skipping logic to check for AAC, assuming AAC
auto audioTag = std::dynamic_pointer_cast<FlvTagAudioData>(tag);
if (audioTag->getPacketType() == 0) {
// AudioHeader, AudioSpecificConfig
client->setAACAudioSpecificConfig(
audioTag->getAudioData(), audioTag->getAudioDataSize());
}
} else if (tag->getTagType() == FLVTAGTYPE::VIDEO) {
// Skipping logic to check for H264, assuming H264
auto videoTag = std::dynamic_pointer_cast<FlvTagVideoData>(tag);
if (videoTag->getPacketType() == 0) {
// VideoHeader, AVCDecoderConfig
client->setAVCDecoderConfig(
videoTag->getVideoData(), videoTag->getVideoDataSize());
}
}
}
for (const auto& tag : tags) {
if (tag->getTagType() == FLVTAGTYPE::AUDIO) {
auto audioTag = std::dynamic_pointer_cast<FlvTagAudioData>(tag);
if (audioTag->getPacketType() == 1) {
printf("audio ts %d\n", audioTag->getTimestamp());
s.sleepUntilMs(audioTag->getTimestamp());
// AudioData
client->appendAudioPayload(
audioTag->getTimestamp(),
audioTag->getAudioData(),
audioTag->getAudioDataSize());
}
} else if (tag->getTagType() == FLVTAGTYPE::VIDEO) {
auto videoTag = std::dynamic_pointer_cast<FlvTagVideoData>(tag);
if (videoTag->getPacketType() != 0) {
// VideoData
int64_t dts = videoTag->getTimestamp();
int64_t pts = dts + videoTag->getCompositionTimestamp();
printf("pts %d\n", pts);
s.sleepUntilMs(pts);
client->appendVideoPayload(
pts, dts, videoTag->getVideoData(), videoTag->getVideoDataSize());
}
}
}
}
void print_usage() {
// TODO
printf("./flv_to_rush_stream <example.flv> <connect_payload>\n");
}
int main(int argc, char* argv[]) {
std::string flv_filename;
std::string connect_payload;
if (argc < 3) {
print_usage();
return 0;
}
flv_filename.assign(argv[1]);
connect_payload.assign(argv[2]);
auto tags = process_flv(flv_filename);
if (tags.size() == 0) {
printf("Failed to process flv: %s\n", flv_filename.c_str());
return 0;
}
// flv, so audioTimescale = 1000, videoTimescale = 1000
RushClient client(connect_payload, 1000, 1000);
if (!client.connect()) {
printf("Failed to connect\n");
return 0;
}
generate_media_payload(&client, tags);
return 0;
}