-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppbackendtest.cpp
More file actions
150 lines (128 loc) · 4.33 KB
/
cppbackendtest.cpp
File metadata and controls
150 lines (128 loc) · 4.33 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <vector>
#include <chrono>
#include "../src/Facade/videoservice.h"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "RtAudio.h"
#include "string.h"
using namespace std;
// video service
static VideoService service;
// rtaudio
static RtAudio rtAudio;
static RtAudio::StreamParameters parameters;
// record the current pts of audio
static uint64 audioPts = 0;
// lauch a array memory for storing audio data
static int audioBufferSize = 1024*128;
static std::vector<char> audioBuffer(audioBufferSize);
// flag of read position and insert position of audioBuffer
static atomic_uint32_t playIdx = 0;
static atomic_uint32_t dataFlag = 0;
// pcm length from getPcmData function
static atomic_uint32_t pcmLength = 0;
// The max value of playIdx and dataFlag
static atomic_uint32_t maxPlayIdx = audioBufferSize;
/// Callback function for rtaudio
static int callBack( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
if(playIdx==dataFlag) return 0;
char* out = (char*)outputBuffer;
memcpy(out, audioBuffer.data() + playIdx, pcmLength);
playIdx.fetch_add(pcmLength);
if(playIdx >= maxPlayIdx) playIdx.store(0);
return 0;
}
int main(int argc, char *argv[])
{
// test url
std::string streamUrl = "https://www.rmp-streaming.com/media/big-buck-bunny-360p.mp4";
// flag for stop get audio thread
bool running = true;
// Open video service
bool ret = service.open(streamUrl.c_str());
cout << "open: " << ret <<endl;
if(!ret)
return -1;
// Open rt audio
int deviceCount = rtAudio.getDeviceCount();
if ( deviceCount== 0 ) {
std::cout << "\nNo audio devices found!\n";
return -2;
}
parameters.deviceId = 1;//rtAudio.getDefaultOutputDevice();
parameters.nChannels = 2;
parameters.firstChannel = 0;
unsigned int bufferFrame = 1024;
try {
rtAudio.openStream( ¶meters, NULL, RTAUDIO_FLOAT32,
48000, &bufferFrame, &callBack, nullptr);
}
catch ( RtAudioError& e ) {
e.printMessage();
std::cout << "open rt audio failed" << endl;
return -3;
}
// A thread for get audio data from video service
thread audioThread([&](){
while (running) {
if(playIdx>0 && dataFlag==0) continue;
auto data = service.getPcmData();
if(!data) {
continue;
}
auto dataVec = (*data).first;
audioPts = (*data).second;
auto dataSize = dataVec.size();
if(dataSize>0){
pcmLength = dataSize;
uint32_t tmp = (audioBufferSize/pcmLength) * pcmLength;
if(tmp < maxPlayIdx)
maxPlayIdx = tmp ;
memcpy(audioBuffer.data() + dataFlag, dataVec.data(), dataSize);
dataFlag.fetch_add(dataSize);
}
if(dataFlag>=maxPlayIdx) dataFlag.store(0);
}
});
// start video service
service.start();
// start audio player
try {
rtAudio.startStream();
}
catch ( RtAudioError& e ) {
e.printMessage();
std::cout << "start rt audio failed" << endl;
return -4;
}
int imageWidth = service.getParameterInt(ParamInt::VideoWidth);
int imageHeight = service.getParameterInt(ParamInt::VideoHeight);
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
// Show image with opencv
while (true) {
auto data = service.getVideoRGBFrame();
if(data) {
cv::Mat frame(imageHeight, imageWidth, CV_8UC3, (*data).first.data());
uint64_t videoPts = (*data).second;
if(videoPts>audioPts){
this_thread::sleep_for(chrono::milliseconds(videoPts-audioPts));
}
cv::imshow( "window", frame );
}
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
if(std::chrono::duration_cast<std::chrono::seconds>(t2 - t1).count() > 90){
cv::destroyWindow("window");
break;
}
cv::waitKey(30);
}
// Stop service and audio player
service.stop();
rtAudio.stopStream();
running = false;
audioThread.join();
return 0;
}