Ansersion / myRtspClient

A RTSP client based on jrtplib
Apache License 2.0
162 stars 92 forks source link

High load #5

Closed KodeZ closed 6 years ago

KodeZ commented 6 years ago

I have been testing different rtsp clients. OpenRTSP obviously being one. Most of the ones I have tested do leave a lot to be desired, but this library is ticking a lot of boxes for me, as I need to run it on an embedded system.

As such, i have been doing some tests, and I am somewhat surprised by the result. It seems that this library is using more resources than OpenRTSP. I was expecting the opposite to be honest. In the default setup, openRTSP uses approx 0.1s user, 0.3s system on my laptop for 30s recording, while this library uses 0.3s user and 0.3s system. System seems to be a fraction lower with this library, but user time is certainly higher. By increasing the USLEEP_UNIT I can get it down to < 0.2s, but that comes with it's own set of issues too, see below.

I am also seeing some video corruption from my Reolink C1-pro camera, the lower part of the frame seems to break up. That situation seems to improve if I reduce USLEEP_UNIT and increase the buffer size, but my laptop should have more than enough umph to handle this. I have yet to figure out why. I get these errors:

[h264 @ 0x7f4a28004e80] corrupted macroblock 38 42 (total_coeff=-1) 
[h264 @ 0x7f4a28004e80] error while decoding MB 38 42
[h264 @ 0x7f4a28004e80] concealing 251 DC, 251 AC, 251 MV errors in I frame
[h264 @ 0x7f4a28004e80] out of range intra chroma pred mode f=0/0   
[h264 @ 0x7f4a28004e80] error while decoding MB 14 28
[h264 @ 0x7f4a28004e80] concealing 1395 DC, 1395 AC, 1395 MV errors in I frame
[h264 @ 0x7f4a28004e80] corrupted macroblock 23 41 (total_coeff=-1) 
[h264 @ 0x7f4a28004e80] error while decoding MB 23 41
[h264 @ 0x7f4a28004e80] concealing 346 DC, 346 AC, 346 MV errors in I frame
Invalid UE golomb code=   0 aq=    0KB vq=   33KB sq=    0B f=0/0   
[h264 @ 0x7f4a28004e80] cbp too large (3199971767) at 51 43
[h264 @ 0x7f4a28004e80] error while decoding MB 51 43
[h264 @ 0x7f4a28004e80] concealing 158 DC, 158 AC, 158 MV errors in P frame
[h264 @ 0x7f4a28069100] corrupted macroblock 26 36 (total_coeff=-1) 
[h264 @ 0x7f4a28069100] error while decoding MB 26 36
[h264 @ 0x7f4a28069100] concealing 743 DC, 743 AC, 743 MV errors in I frame
[h264 @ 0x7f4a28004e80] Invalid level prefix 48KB sq=    0B f=0/0   
[h264 @ 0x7f4a28004e80] error while decoding MB 0 25
[h264 @ 0x7f4a28004e80] concealing 1649 DC, 1649 AC, 1649 MV errors in I frame
[h264 @ 0x7f4a28043120] Invalid level prefix 34KB sq=    0B f=0/0   
[h264 @ 0x7f4a28043120] error while decoding MB 12 39
[h264 @ 0x7f4a28043120] concealing 517 DC, 517 AC, 517 MV errors in P frame

So I am wondering what I am doing wrong here. I am sure that this library can work, but I am unable to do so it seems. I would be willing to help out with this and to get it working, but I don't know where to start really.

Ansersion commented 6 years ago

In order to make the library easier for using, there is much regex using which could load much, especially for the function "GetMediaData". Thanks for your feedback, I will try to improve the problem.

Ansersion commented 6 years ago

To improve performance, there are 2 kind of way:

  1. WITHOUT WRITING HARD DISK: Just now, I committed some code to improve performance, which lowers the use of regex and provide the API to disable the function of obtaining SPS,PPS,VPS periodically, please refer to complete_example.cpp. If you don't change USLEEP_UNIT(default 10,000), in my test case, it will take about 7-8%(user time+sys time) more than openRtsp. If you change USLEEP_UNIT to 13000, it will do as good as openRtsp. If you change USLEEP_UNIT to 15000, it will do better than openRtsp.

  2. WITH WRITING HARD DISK In my old examples, it invokes "write" for every packet of data. You could lower the frequency of "write" to improve the performance. You could refer to the new "complete_example.cpp" which does well in my test case

Additional note: I assumed that there are 25 frames of video data and "1 frame 1 packet" in 1 second. So the interval between 2 frames is 1/25 second(40,000 us). According to "sampling theorem", the sampling period is 20,000us. And because the 'usleep' is not precise enough, in order to get stable data, myRtspClient set it 10,000us. That is USLEEP_UNIT.