You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5128 lines
138 KiB

2 years ago
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
2 years ago
2 years ago
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
2 years ago
​
​
2 years ago
​
​
2 years ago
2 years ago
​
​
​
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
  1. #include "kernelModuleEmbedVideo.h"
  2. // #include "rgooseHardwareResourceNetworkProvider.h"
  3. // #include "mojoKernel.h"
  4. #include "../alg/generic/base64.h"
  5. #include "../data/media/video/videoAVLIBSource.h"
  6. #include "../data/media/video/videoLive555AVLibSource.h"
  7. #include <arpa/inet.h>
  8. #include <ctype.h>
  9. #include <err.h>
  10. #include <errno.h>
  11. #include <getopt.h>
  12. #include <ifaddrs.h>
  13. #include <netdb.h>
  14. #include <netinet/ip.h>
  15. #include <netinet/udp.h>
  16. #include <openssl/sha.h>
  17. #include <paths.h>
  18. #include <poll.h>
  19. #include <signal.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/param.h> /* MIN() */
  25. #include <sys/socket.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. // #define DEBUG
  29. // #undef DEBUG_EXTRA
  30. // #define DEBUG_EXTRA
  31. const char * m_pVideoScratchPath = VIDEO_SCRATCHPAD_PATH;
  32. #define STORAGE_DIR_MODE 0x1FF
  33. #define STORAGE_FILE_MODE 0x1B6
  34. #define LOG(X) kernelModule::m_pLog->Log("MOD_EVIDEO", this, X)
  35. #ifdef DEBUG
  36. #define LOG_ERROR(X) \
  37. { \
  38. char _tBfrErrMSG2w34[256]; \
  39. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  40. kernelModule::m_pLog->Log("MOD_EVIDEO", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  41. }
  42. #else
  43. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_EVIDEO", this, _LOG_ERROR, X);
  44. #endif
  45. #define TLOG(X) basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, X)
  46. #ifdef DEBUG
  47. #define TLOG_ERROR(X) \
  48. { \
  49. char _tBfrErrMSG2w34[256]; \
  50. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  51. basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, _LOG_ERROR, _tBfrErrMSG2w34); \
  52. }
  53. #else
  54. #define TLOG_ERROR(X) basicLogCollection::getInstance()->Log("MOD_EVIDEO_T", CTX, _LOG_ERROR, X);
  55. #endif
  56. enum
  57. {
  58. WEBRTC_REQ_SIGNIN = 0,
  59. WEBRTC_REQ_WAIT = 1,
  60. WEBRTC_REQ_SIGNOUT = 2,
  61. WEBRTC_REQ_MESSAGE = 3
  62. };
  63. static const char * kRequestPaths[] = {"/stream", "/sign_in", "/wait", "/sign_out", "/message"};
  64. // buffer_data bd[1]={0};
  65. kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
  66. kernelModule(moduleID, KERNEL_MODULE_EMBED_VIDEO),
  67. basicDualThread()
  68. {
  69. m_pEventHeap = basicEventHeap::getInstance();
  70. if (m_pDBC)
  71. {
  72. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  73. if (tDATA)
  74. {
  75. memcpy(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  76. free(tDATA);
  77. }
  78. else
  79. {
  80. LOG_ERROR("No DB Data Error!");
  81. memset(&m_sEmbedVideoData, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  82. m_sEmbedVideoData.port = 8972;
  83. };
  84. }
  85. else
  86. {
  87. LOG_ERROR("DB Error!");
  88. };
  89. m_bDetached = true;
  90. n_pNoSignalData = NULL;
  91. m_nNoSignalSize = 0;
  92. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  93. n_pNoSignalSmallData = NULL;
  94. m_nNoSignalSmallSize = 0;
  95. #endif
  96. char tBuff[1024];
  97. sprintf(m_pBaseSavePath, "%s/data/reports", getcwd(tBuff, 1024));
  98. #ifdef DEBUG
  99. char tB[64];
  100. sprintf(tB, "Web Streams Port: %d", m_sEmbedVideoData.port);
  101. LOG(tB);
  102. sprintf(tB, "Frame: [%dx%d]", m_sEmbedVideoData.width, m_sEmbedVideoData.height);
  103. LOG(tB);
  104. if (m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES)
  105. {
  106. LOG("Output Pictures");
  107. }
  108. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO)
  109. {
  110. LOG("Output Video");
  111. }
  112. if (m_sEmbedVideoData.show_ts)
  113. {
  114. LOG("Show TimeStamp");
  115. }
  116. if (m_sEmbedVideoData.show_label)
  117. {
  118. LOG("Show Label");
  119. }
  120. if (m_sEmbedVideoData.show_logo)
  121. {
  122. LOG("Show Logo");
  123. }
  124. #endif
  125. if (m_sEmbedVideoData.show_logo)
  126. {
  127. LOG(m_sEmbedVideoData.logo_md5);
  128. char tPath[MAX_PATH];
  129. sprintf(tPath, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
  130. if (! fileExist(tPath))
  131. {
  132. LOG_ERROR("Logo File Missed!");
  133. m_sEmbedVideoData.show_logo = false;
  134. }
  135. #ifdef DEBUG
  136. else
  137. {
  138. LOG("Logo File is OK");
  139. }
  140. #endif
  141. ;
  142. };
  143. if (! fileExist(DB_SHMEM_POINT))
  144. {
  145. mkdir(DB_SHMEM_POINT, 0775); // S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  146. chmod(DB_SHMEM_POINT, 0775);
  147. mkdir(m_pVideoScratchPath, 0775);
  148. chmod(m_pVideoScratchPath, 0775);
  149. }
  150. else
  151. {
  152. if (! fileExist((CSTR) m_pVideoScratchPath))
  153. {
  154. mkdir(m_pVideoScratchPath, 0775);
  155. chmod(m_pVideoScratchPath, 0775);
  156. }
  157. else
  158. {
  159. // Clean Video Scratch
  160. char tCMD[MAX_PATH];
  161. sprintf(tCMD, "rm -r -f -d %s/* ", m_pVideoScratchPath);
  162. system(tCMD);
  163. };
  164. };
  165. }
  166. kernelModuleEmbedVideo::~kernelModuleEmbedVideo()
  167. {
  168. #ifdef DEBUG
  169. LOG("Destructor");
  170. #endif
  171. if (! m_bDetached)
  172. {
  173. stopService(true);
  174. };
  175. // Sanity check
  176. LOG("Sanity check in Destructor!");
  177. waitThreadAEnd(true);
  178. if (m_vHandlersPool.size())
  179. { // Kill-Wait Started Probes
  180. #ifdef DEBUG
  181. char tB[200];
  182. sprintf(tB, "Have to Wait Client Handlers[%d]!", m_vHandlersPool.size());
  183. LOG(tB);
  184. #endif
  185. for (vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++)
  186. {
  187. pthread_cancel((*index)->m_hThreadHandle);
  188. }
  189. m_vHandlersPool.clear();
  190. };
  191. #ifdef DEBUG
  192. LOG("No More Clients Left");
  193. #endif
  194. waitThreadBEnd(true);
  195. if (m_vViewHandlersPool.size())
  196. { // Kill-Wait Started Viewers
  197. #ifdef DEBUG
  198. char tB[200];
  199. sprintf(tB, "Have to Wait View Handlers[%d]!", m_vViewHandlersPool.size());
  200. LOG(tB);
  201. #endif
  202. for (vectorHANDLERS::iterator index = m_vViewHandlersPool.begin(); index < m_vViewHandlersPool.end(); index++)
  203. {
  204. pthread_cancel((*index)->m_hThreadHandle);
  205. }
  206. m_vViewHandlersPool.clear();
  207. };
  208. #ifdef DEBUG_EXTRA
  209. LOG("No More Viewers Left");
  210. #endif
  211. #ifdef DEBUG_EXTRA
  212. LOG("Deleting CAPS");
  213. #endif
  214. if (m_mapCAPTURES.size())
  215. {
  216. m_mapCAPTURES.clear();
  217. }
  218. #ifdef DEBUG_EXTRA
  219. LOG("Deleting MATS");
  220. #endif
  221. if (m_mapMATS.size())
  222. {
  223. m_mapMATS.clear();
  224. }
  225. #ifdef DEBUG_EXTRA
  226. LOG("Deleting No Signal");
  227. #endif
  228. if (n_pNoSignalData)
  229. {
  230. free(n_pNoSignalData);
  231. }
  232. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  233. if (n_pNoSignalSmallData)
  234. {
  235. free(n_pNoSignalSmallData);
  236. }
  237. #endif
  238. if (fileExist((CSTR) m_pVideoScratchPath))
  239. {
  240. // Clean Video Scratch
  241. char tCMD[MAX_PATH];
  242. sprintf(tCMD, "rm -r -f -d %s/* ", m_pVideoScratchPath);
  243. system(tCMD);
  244. };
  245. #ifdef DEBUG_EXTRA
  246. LOG("All Done!");
  247. #endif
  248. }
  249. static const char * ht_head =
  250. "HTTP/1.1 200 OK\r\n"
  251. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  252. "Pragma: no-cache\r\n"
  253. "Expires: 0\r\n"
  254. "MIME-version: 1.0\r\n"
  255. "Connection: close\r\n"
  256. //"Keep-Alive: timeout=5, max=1\r\n"
  257. //"Connection: Keep-Alive\r\n"
  258. //"Content-Type: video/mp4\r\n"
  259. "Content-Transfer-Encoding: BINARY\r\n"
  260. //"Transfer-Encoding: chunked\r\n"
  261. "Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n"
  262. "\r\n";
  263. static const char * ht_abort =
  264. "HTTP/1.1 404 OK\r\n"
  265. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  266. "Pragma: no-cache\r\n"
  267. "Expires: 0\r\n"
  268. "Connection: close\r\n\r\n";
  269. static const char * ht_no_signal =
  270. "HTTP/1.1 200 OK\r\n"
  271. "Cache-Control: no-store, no-cache, post-check=0, pre-check=0\r\n"
  272. "Pragma: no-cache\r\n"
  273. "Expires: 0\r\n"
  274. "MIME-version: 1.0\r\n"
  275. "Connection: close\r\n"
  276. "Content-Transfer-Encoding: BINARY\r\n"
  277. //"Content-type: multipart/x-mixed-replace;boundary=MPFrame\r\n\r\n"
  278. "Content-Type: image/jpeg\r\nContent-length: %d\r\n\r\n";
  279. void signal_pipe_handler(int SIG)
  280. {
  281. // Web Client Disconnected!
  282. #ifdef DEBUG_EXTRA
  283. basicLogCollection::getInstance()->Log("SIG_PIPE", NULL, _LOG_ERROR, "Sig Pipe Received Web Client Disconnected!");
  284. #endif
  285. }
  286. uint8_t kernelModuleEmbedVideo::getMediaMode(void)
  287. {
  288. return m_sEmbedVideoData.output_mode;
  289. }
  290. void * kernelModuleEmbedVideo::webClientProc(void * ptr)
  291. {
  292. WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
  293. kernelModuleEmbedVideo * MODULE = CTX->ModulePtr;
  294. uint32_t msgSize;
  295. uint8_t tBuf[FILESTREAMBUFFERSZ];
  296. int retVal;
  297. bool w = true;
  298. #ifdef DEBUG_EXTRA
  299. TLOG("Start WebClient");
  300. #endif
  301. if (CTX)
  302. {
  303. char mesg[1024];
  304. size_t RecvSize;
  305. struct pollfd pfd[1];
  306. char cGetURI[1024];
  307. bool m_bDeflatePerMessage = false;
  308. bool m_pInit;
  309. #ifdef DEBUG_EXTRA
  310. pid_t pid = gettid();
  311. char pidBuf[16];
  312. sprintf(pidBuf, "pid:%d", pid);
  313. TLOG(pidBuf);
  314. #endif
  315. signal(SIGPIPE, signal_pipe_handler);
  316. pfd->fd = CTX->m_iSocketFID;
  317. pfd->events = POLLIN | POLLHUP;
  318. while (CTX->bValid && w)
  319. {
  320. if (! CTX->initDone)
  321. {
  322. int RetVal;
  323. char * reqline[256];
  324. RetVal = poll(pfd, 1, 1000);
  325. if (RetVal <= 0)
  326. {
  327. if (EINTR == errno)
  328. {
  329. continue;
  330. }
  331. TLOG_ERROR("Unrecoverable error");
  332. w = false;
  333. continue;
  334. };
  335. memset(mesg, 0, sizeof(mesg));
  336. RetVal = recv(CTX->m_iSocketFID, mesg, sizeof(mesg), 0);
  337. if (RetVal <= 0)
  338. {
  339. sprintf(mesg, "web recv() error: %s", strerror(errno));
  340. TLOG_ERROR(mesg);
  341. w = false;
  342. continue;
  343. };
  344. #ifdef DEBUG_EXTRA
  345. TLOG(mesg);
  346. #endif
  347. uint32_t index = 0;
  348. reqline[index] = strtok(mesg, "\n");
  349. if (strncmp(reqline[index], "GET", 3) == 0)
  350. {
  351. char * pPartURI;
  352. char * pPartPROTO;
  353. pPartURI = strstr(reqline[index], " ");
  354. if (pPartURI != NULL)
  355. {
  356. pPartURI++;
  357. pPartPROTO = strstr(pPartURI, " ");
  358. pPartPROTO++;
  359. int URILen = pPartPROTO - pPartURI;
  360. strncpy(cGetURI, pPartURI, URILen);
  361. cGetURI[URILen] = (char) 0;
  362. if (strncmp(pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp(pPartPROTO, "HTTP/1.1", 8) != 0)
  363. {
  364. TLOG_ERROR("No HTTP version supplied in request!");
  365. if (write(CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0)
  366. {
  367. sprintf(mesg, "Failed returning status 400 to client: %s", strerror(errno));
  368. TLOG_ERROR(mesg);
  369. };
  370. w = false;
  371. continue;
  372. };
  373. };
  374. #ifdef DEBUG_EXTRA
  375. TLOG("Req URI");
  376. TLOG(cGetURI);
  377. #endif
  378. };
  379. // if( ( strncmp( cGetURI, "/stream", 7 ) == 0 ) || ( strncmp( cGetURI, "/video_feed", 11 ) == 0 ) ) {
  380. if (strncmp(cGetURI, "/video_feed", 11) == 0)
  381. {
  382. uint32_t tStreamID = 0;
  383. CSTR tP;
  384. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  385. if (tP = strstr(cGetURI, "/preview"))
  386. {
  387. CTX->bPreviewMode = true;
  388. }
  389. #endif
  390. if (tP = strstr(cGetURI, "?"))
  391. {
  392. CSTR tB;
  393. tP++;
  394. if (tB = strstr(tP, "id="))
  395. {
  396. tStreamID = atoi(tB + 3);
  397. };
  398. };
  399. if (tStreamID)
  400. {
  401. #ifdef DEBUG_EXTRA
  402. char tB[64];
  403. sprintf(tB, "StremID: %d", tStreamID);
  404. TLOG(tB);
  405. #endif
  406. if (CTX->ModulePtr->registerForMediaStream(tStreamID, CTX))
  407. {
  408. // Send Header
  409. if (send(CTX->m_iSocketFID, ht_head, strlen(ht_head), 0) < 0)
  410. {
  411. #ifdef DEBUG_EXTRA
  412. sprintf(mesg, "Failed sending file to client: %s", strerror(errno));
  413. TLOG_ERROR(mesg);
  414. #endif
  415. w = false;
  416. };
  417. CTX->initDone = true;
  418. continue;
  419. #ifdef DEBUG_EXTRA
  420. }
  421. else
  422. {
  423. TLOG("Stream id Not Online");
  424. #endif
  425. };
  426. #ifdef DEBUG_EXTRA
  427. }
  428. else
  429. {
  430. TLOG("No Stream ID");
  431. #endif
  432. };
  433. };
  434. if (CTX->ModulePtr->n_pNoSignalData)
  435. {
  436. // Send Dummy Picture
  437. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  438. if (CTX->bPreviewMode)
  439. {
  440. sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSmallSize);
  441. send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
  442. send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalSmallData, CTX->ModulePtr->m_nNoSignalSmallSize, 0);
  443. send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
  444. }
  445. else
  446. {
  447. #endif
  448. sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSize);
  449. send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
  450. send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0);
  451. send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
  452. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  453. };
  454. #endif
  455. #ifdef DEBUG_EXTRA
  456. TLOG("VFEED Sent No Signal Image!");
  457. #endif
  458. }
  459. else
  460. {
  461. // Send 404 Header
  462. send(CTX->m_iSocketFID, ht_abort, strlen(ht_abort), 0);
  463. #ifdef DEBUG_EXTRA
  464. TLOG("Aborted!");
  465. #endif
  466. };
  467. w = false;
  468. continue;
  469. }
  470. else
  471. {
  472. /*
  473. retVal = read( CTX->m_iEncoderPipe[0], &msgSize, sizeof(uint32_t));
  474. if( retVal == -1 ){
  475. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  476. continue;
  477. };
  478. TLOG_ERROR("Pipe Communication Error[1]!");
  479. CTX->bValid = false;
  480. continue;
  481. };
  482. if( msgSize < FILESTREAMBUFFERSZ ) {
  483. retVal = read( CTX->m_iEncoderPipe[0], tBuf, msgSize );
  484. } else {
  485. TLOG_ERROR("Data too Big in Pipe Transfer!");
  486. CTX->bValid = false;
  487. continue;
  488. };
  489. if( retVal == -1 ){
  490. //free( tBuf );
  491. if( errno == EAGAIN || errno == EWOULDBLOCK ){
  492. continue;
  493. };
  494. TLOG_ERROR("Pipe Communication Error[2]!");
  495. CTX->bValid = false;
  496. continue;
  497. };
  498. */
  499. /**************/
  500. CTX->ModulePtr->m_mEncodedProtector.lock();
  501. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  502. if (CTX->bPreviewMode)
  503. {
  504. if (CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].size())
  505. {
  506. msgSize = CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].size();
  507. if (msgSize < FILESTREAMBUFFERSZ)
  508. {
  509. memcpy(tBuf, CTX->ModulePtr->m_mapPREVIEWS[CTX->mediaStreamId].data(), msgSize);
  510. CTX->ModulePtr->m_mEncodedProtector.unlock();
  511. }
  512. else
  513. {
  514. CTX->ModulePtr->m_mEncodedProtector.unlock();
  515. TLOG_ERROR("Data too Big in Pipe Transfer!");
  516. w = false;
  517. continue;
  518. };
  519. }
  520. else
  521. {
  522. CTX->ModulePtr->m_mEncodedProtector.unlock();
  523. if (CTX->ModulePtr->n_pNoSignalSmallData)
  524. {
  525. msgSize = CTX->ModulePtr->m_nNoSignalSmallSize;
  526. memcpy(tBuf, CTX->ModulePtr->n_pNoSignalSmallData, msgSize);
  527. }
  528. else
  529. {
  530. TLOG_ERROR("Data too Big in Pipe Transfer!");
  531. w = false;
  532. continue;
  533. };
  534. };
  535. }
  536. else
  537. {
  538. #endif
  539. if (CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size())
  540. {
  541. msgSize = CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].size();
  542. if (msgSize < FILESTREAMBUFFERSZ)
  543. {
  544. memcpy(tBuf, CTX->ModulePtr->m_mapCAPTURES[CTX->mediaStreamId].data(), msgSize);
  545. CTX->ModulePtr->m_mEncodedProtector.unlock();
  546. }
  547. else
  548. {
  549. CTX->ModulePtr->m_mEncodedProtector.unlock();
  550. TLOG_ERROR("Data too Big in Pipe Transfer!");
  551. w = false;
  552. continue;
  553. };
  554. }
  555. else
  556. {
  557. #ifdef DEBUG_EXTRA
  558. TLOG("VFEED Sent No Signal Image!");
  559. #endif
  560. CTX->ModulePtr->m_mEncodedProtector.unlock();
  561. if (CTX->ModulePtr->n_pNoSignalData)
  562. {
  563. msgSize = CTX->ModulePtr->m_nNoSignalSize;
  564. memcpy(tBuf, CTX->ModulePtr->n_pNoSignalData, msgSize);
  565. }
  566. else
  567. {
  568. TLOG_ERROR("Data too Big in Pipe Transfer!");
  569. w = false;
  570. continue;
  571. };
  572. };
  573. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  574. };
  575. #endif
  576. /*********/
  577. sprintf(mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize);
  578. if (send(CTX->m_iSocketFID, mesg, strlen(mesg), 0) < 0)
  579. {
  580. #ifdef DEBUG_EXTRA
  581. sprintf(mesg, "[1]Failed sending file to client: %s", strerror(errno));
  582. TLOG_ERROR(mesg);
  583. #endif
  584. w = false;
  585. continue;
  586. };
  587. if (send(CTX->m_iSocketFID, tBuf, msgSize, 0) < 0)
  588. {
  589. #ifdef DEBUG_EXTRA
  590. sprintf(mesg, "[2]Failed sending file to client: %s", strerror(errno));
  591. TLOG_ERROR(mesg);
  592. #endif
  593. w = false;
  594. continue;
  595. };
  596. if (send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0) < 0)
  597. {
  598. #ifdef DEBUG_EXTRA
  599. sprintf(mesg, "[3]Failed sending file to client: %s", strerror(errno));
  600. TLOG_ERROR(mesg);
  601. #endif
  602. w = false;
  603. continue;
  604. };
  605. usleep(66666);
  606. };
  607. };
  608. };
  609. close(CTX->m_iEncoderPipe[0]);
  610. shutdown(CTX->m_iSocketFID, SHUT_RDWR);
  611. SLEEP(3);
  612. close(CTX->m_iSocketFID);
  613. CTX->bValid = false;
  614. #ifdef DEBUG_EXTRA
  615. TLOG("WEB Client Handler Ended!");
  616. #endif
  617. pthread_exit(NULL);
  618. return NULL;
  619. }
  620. void * kernelModuleEmbedVideo::viewClientProc(void * ptr)
  621. {
  622. WEB_CLIENT_CTX * CTX = (WEB_CLIENT_CTX *) ptr;
  623. kernelModuleEmbedVideo * MODULE = CTX->ModulePtr;
  624. uint32_t tVideoID = 0;
  625. long int tDTS = 0;
  626. uint32_t tETS = 0;
  627. uint32_t msgSize;
  628. uint8_t tBuf[FILESTREAMBUFFERSZ];
  629. uint8_t rBuf[8192];
  630. int rSIZE;
  631. int cSIZE;
  632. int retVal;
  633. bool w = true;
  634. char tBASE[11] = {0};
  635. char tID[33] = {0};
  636. char tFILE[PATH_MAX];
  637. uint32_t cFRAME = 0;
  638. void * sPtr = NULL;
  639. void * ePtr = NULL;
  640. uint8_t baba[2] = {0xff, 0xd8};
  641. uint8_t yaba[2] = {0xff, 0xd9};
  642. FILE * tFID = NULL;
  643. TLOG("Start ViewClient");
  644. if (CTX)
  645. {
  646. char mesg[1024];
  647. size_t RecvSize;
  648. struct pollfd pfd[1];
  649. char cGetURI[1024];
  650. bool m_bDeflatePerMessage = false;
  651. bool m_pInit;
  652. #ifdef DEBUG_EXTRA
  653. pid_t pid = gettid();
  654. char pidBuf[16];
  655. sprintf(pidBuf, "pid:%d", pid);
  656. TLOG(pidBuf);
  657. #endif
  658. signal(SIGPIPE, signal_pipe_handler);
  659. pfd->fd = CTX->m_iSocketFID;
  660. pfd->events = POLLIN | POLLHUP;
  661. while (CTX->bValid && w)
  662. {
  663. if (! CTX->initDone)
  664. {
  665. int RetVal;
  666. char * reqline[256];
  667. RetVal = poll(pfd, 1, 1000);
  668. if (RetVal <= 0)
  669. {
  670. if (EINTR == errno)
  671. {
  672. continue;
  673. }
  674. TLOG_ERROR("Unrecoverable error");
  675. w = false;
  676. continue;
  677. };
  678. memset(mesg, 0, sizeof(mesg));
  679. RetVal = recv(CTX->m_iSocketFID, mesg, sizeof(mesg), 0);
  680. if (RetVal <= 0)
  681. {
  682. sprintf(mesg, "web recv() error: %s", strerror(errno));
  683. TLOG_ERROR(mesg);
  684. w = false;
  685. continue;
  686. };
  687. #ifdef DEBUG_EXTRA
  688. TLOG(mesg);
  689. #endif
  690. uint32_t index = 0;
  691. reqline[index] = strtok(mesg, "\n");
  692. if (strncmp(reqline[index], "GET", 3) == 0)
  693. {
  694. char * pPartURI;
  695. char * pPartPROTO;
  696. pPartURI = strstr(reqline[index], " ");
  697. if (pPartURI != NULL)
  698. {
  699. pPartURI++;
  700. pPartPROTO = strstr(pPartURI, " ");
  701. pPartPROTO++;
  702. int URILen = pPartPROTO - pPartURI;
  703. strncpy(cGetURI, pPartURI, URILen);
  704. cGetURI[URILen] = (char) 0;
  705. if (strncmp(pPartPROTO, "HTTP/1.0", 8) != 0 && strncmp(pPartPROTO, "HTTP/1.1", 8) != 0)
  706. {
  707. TLOG_ERROR("No HTTP version supplied in request!");
  708. if (write(CTX->m_iSocketFID, "HTTP/1.1 400 Bad Request\r\n", 26) < 0)
  709. {
  710. sprintf(mesg, "Failed returning status 400 to client: %s", strerror(errno));
  711. TLOG_ERROR(mesg);
  712. };
  713. w = false;
  714. continue;
  715. };
  716. };
  717. #ifdef DEBUG_EXTRA
  718. TLOG("Req URI");
  719. TLOG(cGetURI);
  720. #endif
  721. };
  722. if ((strncmp(cGetURI, "/stream", 7) == 0) || (strncmp(cGetURI, "/video_view", 11) == 0))
  723. {
  724. CSTR tP;
  725. if (tP = strstr(cGetURI, "?"))
  726. {
  727. CSTR tB;
  728. tP++;
  729. if (tB = strcasestr(tP, "id="))
  730. {
  731. strncpy(tID, tB + 3, 32);
  732. tID[32] = (char) 0;
  733. };
  734. if (tB = strcasestr(tP, "ts="))
  735. {
  736. tDTS = atol(tB + 3);
  737. // struct tm * ptm = gmtime ( &tDTS );
  738. struct tm * ptm = localtime(&tDTS);
  739. if (ptm)
  740. {
  741. sprintf(tBASE, "%04d/%02d/%02d", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
  742. };
  743. };
  744. if (tB = strcasestr(tP, "sf="))
  745. {
  746. tETS = atoi(tB + 3);
  747. };
  748. sprintf(tFILE, "./data/reports/%s/%s.mjpeg", tBASE, tID);
  749. };
  750. #ifdef DEBUG_EXTRA
  751. char tB[64];
  752. sprintf(tB, "Requested Video: %s", tFILE);
  753. TLOG(tB);
  754. #endif
  755. if (fileExist(tFILE))
  756. {
  757. tFID = fopen(tFILE, "r");
  758. if (tFID)
  759. {
  760. // Send Header
  761. if (send(CTX->m_iSocketFID, ht_head, strlen(ht_head), 0) < 0)
  762. {
  763. #ifdef DEBUG
  764. sprintf(mesg, "Failed sending file to client: %s", strerror(errno));
  765. TLOG_ERROR(mesg);
  766. #endif
  767. w = false;
  768. };
  769. CTX->initDone = true;
  770. continue;
  771. #ifdef DEBUG
  772. }
  773. else
  774. {
  775. TLOG("Stream id Not Online");
  776. #endif
  777. };
  778. }
  779. else
  780. {
  781. TLOG_ERROR("VVIEW File Not Exists!");
  782. };
  783. };
  784. #ifdef DEBUG_EXTRA
  785. TLOG("VVIEW Catched in DeadEnd!");
  786. #endif
  787. if (1)
  788. {
  789. // Send Dummy Picture
  790. sprintf(mesg, ht_no_signal, CTX->ModulePtr->m_nNoSignalSize);
  791. send(CTX->m_iSocketFID, mesg, strlen(mesg), 0);
  792. send(CTX->m_iSocketFID, CTX->ModulePtr->n_pNoSignalData, CTX->ModulePtr->m_nNoSignalSize, 0);
  793. send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0);
  794. #ifdef DEBUG_EXTRA
  795. TLOG("VVIEW Sent No Signal Image!");
  796. #endif
  797. }
  798. else
  799. {
  800. // Send 404 Header
  801. send(CTX->m_iSocketFID, ht_abort, strlen(ht_abort), 0);
  802. #ifdef DEBUG_EXTRA
  803. TLOG("Aborted!");
  804. #endif
  805. };
  806. w = false;
  807. continue;
  808. }
  809. else
  810. {
  811. cSIZE = 0;
  812. while (! feof(tFID) && CTX->bValid && w)
  813. {
  814. rSIZE = fread(rBuf, 1, 8192, tFID);
  815. if (rSIZE > 0)
  816. {
  817. memcpy(tBuf + cSIZE, rBuf, rSIZE);
  818. cSIZE += rSIZE;
  819. if (sPtr == NULL)
  820. {
  821. sPtr = memmem(tBuf, cSIZE, baba, 2);
  822. }
  823. if (sPtr)
  824. {
  825. if (sPtr != tBuf)
  826. {
  827. cSIZE -= ((uint64_t) sPtr - (uint64_t) tBuf);
  828. memcpy(tBuf, sPtr, cSIZE);
  829. sPtr = tBuf;
  830. };
  831. if (ePtr == NULL)
  832. {
  833. ePtr = memmem(sPtr, cSIZE, yaba, 2);
  834. }
  835. };
  836. };
  837. if (sPtr != NULL && ePtr != NULL)
  838. {
  839. if (feof(tFID))
  840. {
  841. TLOG("LAST FRAME");
  842. }
  843. cFRAME++;
  844. ePtr += 2;
  845. msgSize = ((uint64_t) ePtr - (uint64_t) sPtr);
  846. if (cFRAME > tETS || feof(tFID))
  847. { // or last chanse to send data
  848. if (feof(tFID))
  849. {
  850. TLOG("SEND HEADER");
  851. }
  852. sprintf(mesg, "--MPFrame\r\nContent-type: image/jpeg\r\nContent-length: %d\r\n\r\n", msgSize);
  853. if (send(CTX->m_iSocketFID, mesg, strlen(mesg), 0) < 0)
  854. {
  855. #ifdef DEBUG
  856. sprintf(mesg, "[1]Failed sending file to client: %s", strerror(errno));
  857. TLOG_ERROR(mesg);
  858. #endif
  859. w = false;
  860. continue;
  861. };
  862. if (feof(tFID))
  863. {
  864. TLOG("SEND BODY");
  865. }
  866. if (send(CTX->m_iSocketFID, tBuf, msgSize, 0) < 0)
  867. {
  868. #ifdef DEBUG
  869. sprintf(mesg, "[2]Failed sending file to client: %s", strerror(errno));
  870. TLOG_ERROR(mesg);
  871. #endif
  872. w = false;
  873. continue;
  874. };
  875. if (feof(tFID))
  876. {
  877. TLOG("SEND TAIL");
  878. }
  879. if (send(CTX->m_iSocketFID, "\r\n\r\n", 4, 0) < 0)
  880. {
  881. #ifdef DEBUG
  882. sprintf(mesg, "[3]Failed sending file to client: %s", strerror(errno));
  883. TLOG_ERROR(mesg);
  884. #endif
  885. w = false;
  886. continue;
  887. };
  888. SLEEP(66);
  889. };
  890. if (! feof(tFID))
  891. {
  892. cSIZE -= msgSize;
  893. memcpy(tBuf, ePtr, cSIZE);
  894. sPtr = ePtr = NULL;
  895. }
  896. #ifdef DEBUG
  897. else
  898. {
  899. TLOG("File is Finished!");
  900. }
  901. #endif
  902. };
  903. #ifdef DEBUG
  904. if (feof(tFID))
  905. {
  906. TLOG("LAST FRAME SENT");
  907. }
  908. #endif
  909. };
  910. #ifdef DEBUG_EXTRA
  911. TLOG("Close File");
  912. #endif
  913. fclose(tFID);
  914. w = false;
  915. };
  916. };
  917. };
  918. #ifdef DEBUG_EXTRA
  919. TLOG("Shut Down Socket");
  920. #endif
  921. shutdown(CTX->m_iSocketFID, SHUT_RDWR);
  922. #ifdef DEBUG_EXTRA
  923. TLOG("Close Socket");
  924. #endif
  925. close(CTX->m_iSocketFID);
  926. CTX->bValid = false;
  927. #ifdef DEBUG_EXTRA
  928. TLOG("View Client Handler Ended!");
  929. #endif
  930. pthread_exit(NULL);
  931. TLOG("After Pthread Exit");
  932. return NULL;
  933. }
  934. // Start Thread To view Live Streams
  935. void kernelModuleEmbedVideo::m_fThreadAProc(void)
  936. {
  937. if (! m_bEnabled)
  938. {
  939. #ifdef DEBUG_EXTRA
  940. LOG("Module is Disabled!");
  941. #endif
  942. return;
  943. };
  944. #ifdef DEBUG_EXTRA
  945. LOG("[LIVE] Module Started!");
  946. #endif
  947. struct pollfd pfd[1];
  948. pfd->fd = m_nSocketDescriptor;
  949. pfd->events = POLLIN | POLLHUP;
  950. m_bThreadAStatus = TH_WORKING;
  951. while (m_bThreadAStatus < TH_ENDED)
  952. {
  953. if (! m_bEnabled)
  954. {
  955. m_bThreadAStatus = TH_ENDED;
  956. continue;
  957. };
  958. int RetVal = poll(pfd, 1, 1000);
  959. if (RetVal < 0)
  960. {
  961. if (EINTR == errno)
  962. {
  963. continue;
  964. }
  965. LOG_ERROR("Unrecoverable error");
  966. m_bThreadAStatus = TH_ENDED;
  967. continue;
  968. };
  969. // Handle Connections
  970. if (RetVal != 0)
  971. {
  972. if (pfd[0].revents & POLLIN)
  973. {
  974. #ifdef DEBUG_EXTRA
  975. LOG("Handle New Client");
  976. #endif
  977. int tSID = accept(pfd[0].fd, NULL, NULL);
  978. if (tSID < 0)
  979. {
  980. char tBuf[CTEXT];
  981. sprintf(tBuf, "Accept() error: %s", strerror(errno));
  982. LOG_ERROR(tBuf);
  983. }
  984. else
  985. {
  986. #ifdef DEBUG_EXTRA
  987. LOG("Accept OK");
  988. #endif
  989. WEB_CLIENT_CTX * tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
  990. if (tCTX)
  991. {
  992. #ifdef DEBUG_EXTRA
  993. LOG("CTX OK");
  994. #endif
  995. WEB_HANDLER * tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
  996. if (tHANDLER)
  997. {
  998. #ifdef DEBUG_EXTRA
  999. LOG("HANDLER OK");
  1000. #endif
  1001. memset(tCTX, 0, sizeof(WEB_CLIENT_CTX));
  1002. memset(tHANDLER, 0, sizeof(WEB_HANDLER));
  1003. tCTX->m_iSocketFID = tSID;
  1004. tCTX->m_iEncoderPipe[0] = -1;
  1005. tCTX->m_iEncoderPipe[1] = -1;
  1006. tCTX->mediaStreamId = 0;
  1007. tCTX->bValid = true;
  1008. tCTX->ModulePtr = this;
  1009. #ifdef DEBUG_EXTRA
  1010. LOG("Before Thread Create");
  1011. #endif
  1012. int RetVal = pthread_create(&tHANDLER->m_hThreadHandle, NULL, webClientProc, (void *) tCTX);
  1013. if (RetVal == -1)
  1014. {
  1015. free(tCTX);
  1016. free(tHANDLER);
  1017. LOG_ERROR("Thread Creation Faled.");
  1018. shutdown(tSID, SHUT_RDWR);
  1019. close(tSID);
  1020. }
  1021. else
  1022. {
  1023. tHANDLER->CTX = tCTX;
  1024. m_mWebHandlersProtector.lock();
  1025. m_vHandlersPool.push_back(tHANDLER);
  1026. m_mWebHandlersProtector.unlock();
  1027. #ifdef DEBUG_EXTRA
  1028. LOG("Handler Pushed to a Pool");
  1029. #endif
  1030. };
  1031. }
  1032. else
  1033. { // https://stackoverflow.com/questions/18545077/image-fullscreen-on-click
  1034. free(tCTX);
  1035. LOG_ERROR("CL_HANDLER Mem Allocation Failure!");
  1036. shutdown(tSID, SHUT_RDWR);
  1037. close(tSID);
  1038. };
  1039. }
  1040. else
  1041. {
  1042. LOG_ERROR("CL_CTX Mem Allocation Failure!");
  1043. shutdown(tSID, SHUT_RDWR);
  1044. close(tSID);
  1045. };
  1046. };
  1047. };
  1048. };
  1049. // Handle Dead Clients
  1050. m_mWebHandlersProtector.lock();
  1051. if (m_vHandlersPool.size())
  1052. {
  1053. for (vectorHANDLERS::iterator idx = m_vHandlersPool.begin(); idx != m_vHandlersPool.end();)
  1054. {
  1055. if (! ((*idx)->CTX->bValid))
  1056. {
  1057. #ifdef DEBUG_EXTRA
  1058. LOG("Have Dead Client!");
  1059. #endif
  1060. pthread_join((*idx)->m_hThreadHandle, NULL);
  1061. // close( (*idx)->CTX->m_iEncoderPipe[0] );
  1062. close((*idx)->CTX->m_iEncoderPipe[1]);
  1063. free((*idx)->CTX);
  1064. free(*idx);
  1065. m_vHandlersPool.erase(idx);
  1066. #ifdef DEBUG_EXTRA
  1067. LOG("Web Handler Ripped!");
  1068. #endif
  1069. }
  1070. else
  1071. {
  1072. idx++;
  1073. }
  1074. };
  1075. };
  1076. m_mWebHandlersProtector.unlock();
  1077. // validate active streams ?
  1078. SLEEP(50);
  1079. };
  1080. }
  1081. void kernelModuleEmbedVideo::m_fThreadBProc(void)
  1082. {
  1083. if (! m_bEnabled)
  1084. {
  1085. #ifdef DEBUG_EXTRA
  1086. LOG("Module is Disabled!");
  1087. #endif
  1088. return;
  1089. };
  1090. #ifdef DEBUG_EXTRA
  1091. LOG("[VIEW] Module Started!");
  1092. #endif
  1093. struct pollfd pfd[1];
  1094. pfd->fd = m_nViewSocketDescriptor;
  1095. pfd->events = POLLIN | POLLHUP;
  1096. m_bThreadBStatus = TH_WORKING;
  1097. while (m_bThreadBStatus < TH_ENDED)
  1098. {
  1099. if (! m_bEnabled)
  1100. {
  1101. m_bThreadBStatus = TH_ENDED;
  1102. continue;
  1103. };
  1104. int RetVal = poll(pfd, 1, 1000);
  1105. if (RetVal < 0)
  1106. {
  1107. if (EINTR == errno)
  1108. {
  1109. continue;
  1110. }
  1111. LOG_ERROR("Unrecoverable error");
  1112. m_bThreadBStatus = TH_ENDED;
  1113. continue;
  1114. };
  1115. // Handle Connections
  1116. if (RetVal != 0)
  1117. {
  1118. if (pfd[0].revents & POLLIN)
  1119. {
  1120. #ifdef DEBUG_EXTRA
  1121. LOG("Handle New View");
  1122. #endif
  1123. int tSID = accept(pfd[0].fd, NULL, NULL);
  1124. if (tSID < 0)
  1125. {
  1126. char tBuf[CTEXT];
  1127. sprintf(tBuf, "Accept() error: %s", strerror(errno));
  1128. LOG_ERROR(tBuf);
  1129. }
  1130. else
  1131. {
  1132. #ifdef DEBUG_EXTRA
  1133. LOG("Accept OK");
  1134. #endif
  1135. WEB_CLIENT_CTX * tCTX = (WEB_CLIENT_CTX *) malloc(sizeof(WEB_CLIENT_CTX));
  1136. if (tCTX)
  1137. {
  1138. #ifdef DEBUG_EXTRA
  1139. LOG("CTX OK");
  1140. #endif
  1141. WEB_HANDLER * tHANDLER = (WEB_HANDLER *) malloc(sizeof(WEB_HANDLER));
  1142. if (tHANDLER)
  1143. {
  1144. #ifdef DEBUG_EXTRA
  1145. LOG("HANDLER OK");
  1146. #endif
  1147. memset(tCTX, 0, sizeof(WEB_CLIENT_CTX));
  1148. memset(tHANDLER, 0, sizeof(WEB_HANDLER));
  1149. tCTX->m_iSocketFID = tSID;
  1150. tCTX->m_iEncoderPipe[0] = -1;
  1151. tCTX->m_iEncoderPipe[1] = -1;
  1152. tCTX->mediaStreamId = 0;
  1153. tCTX->bValid = true;
  1154. tCTX->ModulePtr = this;
  1155. #ifdef DEBUG_EXTRA
  1156. LOG("Before Thread Create");
  1157. #endif
  1158. int RetVal = pthread_create(&tHANDLER->m_hThreadHandle, NULL, viewClientProc, (void *) tCTX);
  1159. if (RetVal == -1)
  1160. {
  1161. free(tCTX);
  1162. free(tHANDLER);
  1163. LOG_ERROR("Thread Creation Faled.");
  1164. shutdown(tSID, SHUT_RDWR);
  1165. close(tSID);
  1166. }
  1167. else
  1168. {
  1169. tHANDLER->CTX = tCTX;
  1170. m_mViewHandlersProtector.lock();
  1171. m_vViewHandlersPool.push_back(tHANDLER);
  1172. m_mViewHandlersProtector.unlock();
  1173. #ifdef DEBUG_EXTRA
  1174. LOG("Handler Pushed to a Pool");
  1175. #endif
  1176. };
  1177. }
  1178. else
  1179. { // https://stackoverflow.com/questions/18545077/image-fullscreen-on-click
  1180. free(tCTX);
  1181. LOG_ERROR("CL_HANDLER Mem Allocation Failure!");
  1182. shutdown(tSID, SHUT_RDWR);
  1183. close(tSID);
  1184. };
  1185. }
  1186. else
  1187. {
  1188. LOG_ERROR("CL_CTX Mem Allocation Failure!");
  1189. shutdown(tSID, SHUT_RDWR);
  1190. close(tSID);
  1191. };
  1192. };
  1193. };
  1194. };
  1195. // Handle Dead Clients
  1196. m_mViewHandlersProtector.lock();
  1197. if (m_vViewHandlersPool.size())
  1198. {
  1199. for (vectorHANDLERS::iterator idx = m_vViewHandlersPool.begin(); idx != m_vViewHandlersPool.end();)
  1200. {
  1201. if (! ((*idx)->CTX->bValid))
  1202. {
  1203. #ifdef DEBUG_EXTRA
  1204. LOG("Have Dead Viewer!");
  1205. #endif
  1206. pthread_join((*idx)->m_hThreadHandle, NULL);
  1207. free((*idx)->CTX);
  1208. free(*idx);
  1209. m_vViewHandlersPool.erase(idx);
  1210. #ifdef DEBUG_EXTRA
  1211. LOG("View Handler Ripped!");
  1212. #endif
  1213. }
  1214. else
  1215. {
  1216. idx++;
  1217. }
  1218. };
  1219. };
  1220. m_mViewHandlersProtector.unlock();
  1221. // validate active streams ?
  1222. SLEEP(50);
  1223. };
  1224. }
  1225. #define ENABLE_SOCKOPT(sd, level, opt) \
  1226. do \
  1227. { \
  1228. int val = 1; \
  1229. if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
  1230. warn("Failed enabling %s for web service", #opt); \
  1231. } \
  1232. while (0);
  1233. bool kernelModuleEmbedVideo::startService()
  1234. {
  1235. m_bDetached = false;
  1236. // Create NoSignal Image
  1237. cv::Mat tMat(m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3);
  1238. cv::rectangle(tMat, cv::Point(0, 0), cv::Point(m_sEmbedVideoData.width, m_sEmbedVideoData.height), cv::Scalar(255, 0, 0), cv::FILLED);
  1239. cv::putText(tMat, "NO SIGNAL", cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 3);
  1240. if (m_sEmbedVideoData.show_logo)
  1241. {
  1242. int a_x, a_y;
  1243. if (m_sEmbedVideoData.logo_x < 0)
  1244. {
  1245. a_x = m_sEmbedVideoData.width + m_sEmbedVideoData.logo_x - m_sEmbedVideoData.logo_w;
  1246. if (a_x < 0)
  1247. {
  1248. a_x = 0;
  1249. }
  1250. }
  1251. else
  1252. {
  1253. a_x = m_sEmbedVideoData.logo_x;
  1254. }
  1255. if ((a_x + m_sEmbedVideoData.logo_w) > m_sEmbedVideoData.width)
  1256. {
  1257. a_x = m_sEmbedVideoData.width - m_sEmbedVideoData.logo_w;
  1258. }
  1259. if (m_sEmbedVideoData.logo_y < 0)
  1260. {
  1261. a_y = m_sEmbedVideoData.height + m_sEmbedVideoData.logo_y - m_sEmbedVideoData.logo_h;
  1262. if (a_y < 0)
  1263. {
  1264. a_y = 0;
  1265. }
  1266. }
  1267. else
  1268. {
  1269. a_y = m_sEmbedVideoData.logo_y;
  1270. }
  1271. if ((a_y + m_sEmbedVideoData.logo_h) > m_sEmbedVideoData.height)
  1272. {
  1273. a_y = m_sEmbedVideoData.height - m_sEmbedVideoData.logo_h;
  1274. }
  1275. if ((a_x > -1) && (a_y > -1))
  1276. {
  1277. char tName[256];
  1278. m_sEmbedVideoData.logo_x = a_x;
  1279. m_sEmbedVideoData.logo_y = a_y;
  1280. sprintf(tName, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
  1281. cv::Mat T = cv::imread(tName, cv::IMREAD_UNCHANGED);
  1282. if (T.cols > 0)
  1283. {
  1284. cv::Mat mask;
  1285. vector<cv::Mat> layers;
  1286. #ifdef DEBUG_EXTRA
  1287. LOG("Logo Loaded OK");
  1288. #endif
  1289. cv::split(T, layers); // seperate channels
  1290. cv::Mat rgb[3] = {layers[0], layers[1], layers[2]};
  1291. mask = layers[3]; // png's alpha channel used as mask
  1292. cv::merge(rgb, 3, T); // put together the RGB channels, now transp insn't transparent
  1293. cv::resize(T, m_mLogoPic, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), cv::INTER_CUBIC);
  1294. cv::resize(mask, m_mLogoMask, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), cv::INTER_CUBIC);
  1295. cv::Rect roi(cv::Point(m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y), cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h));
  1296. cv::Mat destinationROI = tMat(roi);
  1297. m_mLogoPic.copyTo(destinationROI, m_mLogoMask);
  1298. // m_sEmbedVideoData.show_logo = false;
  1299. }
  1300. else
  1301. {
  1302. #ifdef DEBUG_EXTRA
  1303. LOG("Logo Disabled - No Picture is Loaded");
  1304. #endif
  1305. m_sEmbedVideoData.show_logo = false;
  1306. };
  1307. }
  1308. else
  1309. {
  1310. #ifdef DEBUG_EXTRA
  1311. LOG("Logo Disabled - Wrong Coords");
  1312. #endif
  1313. m_sEmbedVideoData.show_logo = false;
  1314. };
  1315. #ifdef DEBUG_EXTRA
  1316. LOG("Logo Draw OK");
  1317. #endif
  1318. };
  1319. try
  1320. {
  1321. vector<int> compression_params;
  1322. // compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  1323. // compression_params.push_back(9);
  1324. compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
  1325. compression_params.push_back(m_sEmbedVideoData.compression_quality);
  1326. vector<uchar> buf;
  1327. cv::imencode(".jpg", tMat, buf, compression_params);
  1328. m_nNoSignalSize = buf.size();
  1329. n_pNoSignalData = (uint8_t *) malloc(m_nNoSignalSize);
  1330. if (n_pNoSignalData)
  1331. {
  1332. memcpy(n_pNoSignalData, buf.data(), m_nNoSignalSize);
  1333. }
  1334. else
  1335. {
  1336. m_nNoSignalSize = 0;
  1337. }
  1338. buf.clear();
  1339. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  1340. // Make Small Preview Thumb
  1341. cv::Mat dst;
  1342. cv::resize(tMat, dst, cv::Size(PREVIEW_SIZE_WIDTH, PREVIEW_SIZE_HEIGHT));
  1343. cv::imencode(".jpg", dst, buf, compression_params);
  1344. m_nNoSignalSmallSize = buf.size();
  1345. n_pNoSignalSmallData = (uint8_t *) malloc(m_nNoSignalSmallSize);
  1346. if (n_pNoSignalSmallData)
  1347. {
  1348. memcpy(n_pNoSignalSmallData, buf.data(), m_nNoSignalSmallSize);
  1349. }
  1350. else
  1351. {
  1352. m_nNoSignalSmallSize = 0;
  1353. }
  1354. buf.clear();
  1355. #endif
  1356. }
  1357. catch (cv::Exception & e)
  1358. {
  1359. LOG_ERROR(e.msg.c_str());
  1360. };
  1361. // Socket Live
  1362. struct sockaddr sa;
  1363. struct sockaddr_in * sin;
  1364. memset(&sa, 0, sizeof(struct sockaddr));
  1365. sa.sa_family = AF_INET;
  1366. sin = (struct sockaddr_in *) &sa;
  1367. sin->sin_addr.s_addr = htonl(INADDR_ANY);
  1368. sin->sin_port = htons(m_sEmbedVideoData.port);
  1369. m_nSocketDescriptor = socket(sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  1370. if (m_nSocketDescriptor == -1)
  1371. {
  1372. LOG_ERROR("Failed creating ACC socket");
  1373. return false;
  1374. };
  1375. ENABLE_SOCKOPT(m_nSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
  1376. #ifdef SO_REUSEPORT
  1377. ENABLE_SOCKOPT(m_nSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
  1378. #endif
  1379. if (bind(m_nSocketDescriptor, &sa, sizeof(sa)) < 0)
  1380. {
  1381. LOG_ERROR("Failed binding Socket");
  1382. close(m_nSocketDescriptor);
  1383. return false;
  1384. #ifdef DEBUG_EXTRA
  1385. }
  1386. else
  1387. {
  1388. LOG("Bind Socessfull");
  1389. #endif
  1390. };
  1391. if (listen(m_nSocketDescriptor, 10) != 0)
  1392. {
  1393. LOG_ERROR("Failed setting web listen backlog");
  1394. close(m_nSocketDescriptor);
  1395. return false;
  1396. #ifdef DEBUG_EXTRA
  1397. }
  1398. else
  1399. {
  1400. LOG("Listen OK");
  1401. #endif
  1402. };
  1403. // Socket Viewer
  1404. // struct sockaddr sa;
  1405. // struct sockaddr_in *sin;
  1406. memset(&sa, 0, sizeof(struct sockaddr));
  1407. sa.sa_family = AF_INET;
  1408. sin = (struct sockaddr_in *) &sa;
  1409. sin->sin_addr.s_addr = htonl(INADDR_ANY);
  1410. sin->sin_port = htons(8974);
  1411. m_nViewSocketDescriptor = socket(sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
  1412. if (m_nViewSocketDescriptor == -1)
  1413. {
  1414. LOG_ERROR("Failed creating ACC socket");
  1415. return false;
  1416. };
  1417. ENABLE_SOCKOPT(m_nViewSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
  1418. #ifdef SO_REUSEPORT
  1419. ENABLE_SOCKOPT(m_nViewSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
  1420. #endif
  1421. if (bind(m_nViewSocketDescriptor, &sa, sizeof(sa)) < 0)
  1422. {
  1423. LOG_ERROR("Failed binding Socket");
  1424. close(m_nViewSocketDescriptor);
  1425. return false;
  1426. #ifdef DEBUG_EXTRA
  1427. }
  1428. else
  1429. {
  1430. LOG("Bind Socessfull");
  1431. #endif
  1432. };
  1433. if (listen(m_nViewSocketDescriptor, 10) != 0)
  1434. {
  1435. LOG_ERROR("Failed setting web listen backlog");
  1436. close(m_nViewSocketDescriptor);
  1437. return false;
  1438. #ifdef DEBUG_EXTRA
  1439. }
  1440. else
  1441. {
  1442. LOG("Listen OK");
  1443. #endif
  1444. };
  1445. #ifdef DEBUG_EXTRA
  1446. LOG("Start Thread");
  1447. #endif
  1448. m_bEnabled = init();
  1449. return m_bEnabled;
  1450. }
  1451. bool kernelModuleEmbedVideo::stopService(bool force)
  1452. {
  1453. m_bEnabled = false;
  1454. // Stop Conveyor
  1455. m_bDetached = uninit();
  1456. #ifdef DEBUG_EXTRA
  1457. LOG("StopService!");
  1458. #endif
  1459. m_mStreamHandlersProtector.lock();
  1460. if (m_mapHANDLERS.size())
  1461. {
  1462. #ifdef DEBUG
  1463. LOG("Deleting Stream Handlers!");
  1464. #endif
  1465. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
  1466. {
  1467. #ifdef DEBUG
  1468. LOG("Deleting Stream Handler");
  1469. #endif
  1470. delete((*idx).second);
  1471. }
  1472. m_mapHANDLERS.clear();
  1473. };
  1474. m_mStreamHandlersProtector.unlock();
  1475. if (m_vHandlersPool.size())
  1476. { // Kill-Wait Started Probes
  1477. #ifdef DEBUG_EXTRA
  1478. char tB[200];
  1479. sprintf(tB, "Have to Wait Client Handlers[%d]!", m_vHandlersPool.size());
  1480. LOG(tB);
  1481. #endif
  1482. for (vectorHANDLERS::iterator index = m_vHandlersPool.begin(); index != m_vHandlersPool.end(); index++)
  1483. {
  1484. if (force)
  1485. {
  1486. #ifdef DEBUG_EXTRA
  1487. LOG("Force Client Handler to Stop!");
  1488. #endif
  1489. pthread_cancel((*index)->m_hThreadHandle);
  1490. }
  1491. else
  1492. {
  1493. #ifdef DEBUG_EXTRA
  1494. LOG("Waiting Client Handler to End!");
  1495. #endif
  1496. pthread_join((*index)->m_hThreadHandle, NULL);
  1497. }
  1498. };
  1499. m_vHandlersPool.clear();
  1500. };
  1501. #ifdef DEBUG_EXTRA
  1502. LOG("No More Client Handlers!");
  1503. #endif
  1504. if (m_mapCAPTURES.size())
  1505. {
  1506. #ifdef DEBUG_EXTRA
  1507. LOG("Some Caps!");
  1508. #endif
  1509. for (mapSTREAMCAPS::iterator idx = m_mapCAPTURES.begin(); idx != m_mapCAPTURES.end(); idx++)
  1510. {
  1511. idx->second.clear();
  1512. }
  1513. m_mapCAPTURES.clear();
  1514. #ifdef DEBUG_EXTRA
  1515. LOG("No More Caps!");
  1516. #endif
  1517. };
  1518. if (m_mapMATS.size())
  1519. {
  1520. #ifdef DEBUG_EXTRA
  1521. LOG("Some Mats!");
  1522. #endif
  1523. for (mapSTREAMMAPS::iterator idx = m_mapMATS.begin(); idx != m_mapMATS.end(); idx++)
  1524. {
  1525. #ifdef DEBUG_EXTRA
  1526. LOG("Release Mat!");
  1527. #endif
  1528. idx->second.release();
  1529. };
  1530. #ifdef DEBUG_EXTRA
  1531. LOG("Clear Map Mats!");
  1532. #endif
  1533. m_mapMATS.clear();
  1534. #ifdef DEBUG_EXTRA
  1535. LOG("No More Mats!");
  1536. #endif
  1537. };
  1538. if (n_pNoSignalData)
  1539. {
  1540. #ifdef DEBUG_EXTRA
  1541. LOG("No Sig Data!");
  1542. #endif
  1543. free(n_pNoSignalData);
  1544. n_pNoSignalData = NULL;
  1545. m_nNoSignalSize = 0;
  1546. #ifdef DEBUG_EXTRA
  1547. LOG("No No Sig Data!");
  1548. #endif
  1549. };
  1550. return m_bDetached;
  1551. }
  1552. bool kernelModuleEmbedVideo::reloadSettings()
  1553. {
  1554. #ifdef DEBUG_EXTRA
  1555. LOG("Reload Service");
  1556. #endif
  1557. if (m_pDBC)
  1558. {
  1559. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  1560. if (tDATA)
  1561. {
  1562. if (stopService())
  1563. {
  1564. #ifdef DEBUG_EXTRA
  1565. LOG("Stoped!");
  1566. #endif
  1567. memcpy(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1568. free(tDATA);
  1569. #ifdef DEBUG_EXTRA
  1570. LOG("Starting...");
  1571. #endif
  1572. return startService();
  1573. }
  1574. else
  1575. {
  1576. free(tDATA);
  1577. };
  1578. };
  1579. };
  1580. return false;
  1581. }
  1582. #include "deviceNodeCamera.h"
  1583. #include "deviceNodeFactory.h"
  1584. bool kernelModuleEmbedVideo::validate()
  1585. {
  1586. bool RetVal = true;
  1587. deviceNode ** tA = NULL;
  1588. uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
  1589. if (! tCNT)
  1590. {
  1591. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PTZ_CAMERA, tA);
  1592. if (! tCNT)
  1593. {
  1594. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_STATIC_CAMERA, tA);
  1595. if (! tCNT)
  1596. {
  1597. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PTZ_CAMERA, tA);
  1598. if (! tCNT)
  1599. {
  1600. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PRESET_CAMERA, tA);
  1601. if (! tCNT)
  1602. {
  1603. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PRESET_CAMERA, tA);
  1604. };
  1605. };
  1606. };
  1607. };
  1608. };
  1609. if (tCNT)
  1610. {
  1611. for (int i = 0; i < tCNT; i++)
  1612. {
  1613. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  1614. if (! registerStream(tCAM->getCamStream(), tCAM->getFlipMode()))
  1615. {
  1616. RetVal = false;
  1617. // tCAM->set
  1618. continue;
  1619. };
  1620. };
  1621. free(tA);
  1622. tA = NULL;
  1623. #ifdef DEBUG_EXTRA
  1624. }
  1625. else
  1626. {
  1627. LOG("No Camera Defined/Enabled In the system!");
  1628. #endif
  1629. };
  1630. if (RetVal)
  1631. {
  1632. DBC_INT_LIST tInvalidList;
  1633. m_mStreamHandlersProtector.lock();
  1634. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
  1635. {
  1636. if ((*idx).second != NULL)
  1637. {
  1638. if (! (*idx).second->validate())
  1639. {
  1640. tInvalidList.push_back((*idx).first);
  1641. }
  1642. }
  1643. };
  1644. m_mStreamHandlersProtector.unlock();
  1645. if (tInvalidList.size())
  1646. {
  1647. for (DBC_INT_LIST::iterator idx = tInvalidList.begin(); idx != tInvalidList.end(); idx++)
  1648. {
  1649. if (unregisterStream(*idx))
  1650. {
  1651. registerStream(*idx);
  1652. }
  1653. };
  1654. tInvalidList.clear();
  1655. };
  1656. };
  1657. return RetVal;
  1658. if (m_pDBC)
  1659. {
  1660. DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
  1661. if (tDATA)
  1662. {
  1663. if (memcmp(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA)) != 0)
  1664. {
  1665. free(tDATA);
  1666. #ifdef DEBUG_EXTRA
  1667. LOG("Module Settings Changed!");
  1668. #endif
  1669. return reloadSettings();
  1670. };
  1671. free(tDATA);
  1672. }
  1673. else
  1674. {
  1675. LOG_ERROR("No Fetched EVE Data");
  1676. RetVal = false;
  1677. };
  1678. if (RetVal)
  1679. {
  1680. DBC_INT_LIST * tList = m_pDBC->listMediaStreams(0, LIST_VALID_RECORDS);
  1681. if (tList != NULL)
  1682. {
  1683. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  1684. {
  1685. #ifdef DEBUG_EXTRA
  1686. LOG("REG STREAM");
  1687. #endif
  1688. if (! registerStream(*idx))
  1689. {
  1690. RetVal = false;
  1691. continue;
  1692. };
  1693. };
  1694. delete(tList);
  1695. #ifdef DEBUG_EXTRA
  1696. }
  1697. else
  1698. {
  1699. LOG("- No Valid Streams -");
  1700. #endif
  1701. };
  1702. };
  1703. if (RetVal)
  1704. {
  1705. DBC_INT_LIST tInvalidList;
  1706. m_mStreamHandlersProtector.lock();
  1707. for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++)
  1708. {
  1709. if ((*idx).second != NULL)
  1710. {
  1711. if (! (*idx).second->validate())
  1712. {
  1713. tInvalidList.push_back((*idx).first);
  1714. }
  1715. }
  1716. };
  1717. m_mStreamHandlersProtector.unlock();
  1718. if (tInvalidList.size())
  1719. {
  1720. for (DBC_INT_LIST::iterator idx = tInvalidList.begin(); idx != tInvalidList.end(); idx++)
  1721. {
  1722. if (unregisterStream(*idx))
  1723. {
  1724. registerStream(*idx);
  1725. }
  1726. };
  1727. tInvalidList.clear();
  1728. };
  1729. };
  1730. }
  1731. else
  1732. {
  1733. LOG_ERROR("DB Error!");
  1734. RetVal = false;
  1735. };
  1736. return RetVal;
  1737. }
  1738. bool kernelModuleEmbedVideo::registerForMediaStream(uint32_t id, WEB_CLIENT_CTX *& CTX)
  1739. {
  1740. if (CTX)
  1741. {
  1742. if (id)
  1743. {
  1744. // Check if its Valid!
  1745. DBC_MEDIA_STREAM_DATA * tSD = m_pDBC->getMediaStreamData(id);
  1746. if (tSD)
  1747. {
  1748. // if( !tSD->valid ) {
  1749. // free( tSD );
  1750. // return false;
  1751. // };
  1752. free(tSD);
  1753. // if( pipe( CTX->m_iEncoderPipe ) == -1 ){
  1754. // CTX->bValid = false;
  1755. // return false;
  1756. // };
  1757. CTX->mediaStreamId = id;
  1758. return true;
  1759. };
  1760. };
  1761. }
  1762. else
  1763. {
  1764. LOG_ERROR("Client Context error!");
  1765. }
  1766. return false;
  1767. }
  1768. bool kernelModuleEmbedVideo::dispatchEvent(EVENT_CONTAINER * Evt)
  1769. {
  1770. if (Evt)
  1771. {
  1772. switch (Evt->m_eClass)
  1773. {
  1774. case EVT_CLASS_IMAGE:
  1775. case EVT_CLASS_VIDEO:
  1776. {
  1777. switch (Evt->m_eType)
  1778. {
  1779. case EVT_CAPTURE_START:
  1780. {
  1781. #ifdef DEBUG
  1782. LOG("Capture Event");
  1783. #endif
  1784. if (Evt->m_ref_id && Evt->m_db_id)
  1785. {
  1786. uint32_t tPicID = saveStreamPicture(Evt->m_ref_id);
  1787. if (tPicID)
  1788. {
  1789. //
  1790. // m_pDBC->addPictureEvent( Evt->m_db_id, tPicID );
  1791. }
  1792. else
  1793. {
  1794. #ifdef DEBUG
  1795. LOG_ERROR("No Picture is Saved!");
  1796. #endif
  1797. };
  1798. }
  1799. else
  1800. {
  1801. #ifdef DEBUG
  1802. LOG_ERROR("No Stream ID or Event ID in Event Data!");
  1803. #endif
  1804. };
  1805. break;
  1806. };
  1807. };
  1808. break;
  1809. };
  1810. case EVT_CLASS_CAMERA_CTRL:
  1811. {
  1812. switch (Evt->m_eType)
  1813. {
  1814. case EVT_CAMERA_MOVE_TO:
  1815. {
  1816. #ifdef DEBUG_EXTRA
  1817. LOG("Cam OnMove After Event -> Register SourceID");
  1818. #endif
  1819. uint32_t sid = m_pDBC->getNodeAuxParam(Evt->m_nSourceNodeID);
  1820. if (sid)
  1821. {
  1822. #ifdef DEBUG_EXTRA
  1823. LOG("Source ID found!");
  1824. #endif
  1825. m_mStreamHandlersProtector.lock();
  1826. #ifdef DEBUG_EXTRA
  1827. LOG("Mutex Locked!");
  1828. #endif
  1829. mapSTREAMHANDLERS::iterator hnd = m_mapHANDLERS.find(sid);
  1830. if (hnd != m_mapHANDLERS.end())
  1831. #ifdef DEBUG_EXTRA
  1832. {
  1833. LOG("Handler found!");
  1834. #endif
  1835. (*hnd).second->triggerCamMove();
  1836. #ifdef DEBUG_EXTRA
  1837. LOG("Triggered!");
  1838. }
  1839. else
  1840. {
  1841. LOG("No Stream Found!");
  1842. }
  1843. #endif
  1844. m_mStreamHandlersProtector.unlock();
  1845. #ifdef DEBUG_EXTRA
  1846. LOG("Mutex Unlocked!");
  1847. #endif
  1848. }
  1849. else
  1850. {
  1851. LOG("No Stream ID Found!");
  1852. }
  1853. break;
  1854. };
  1855. case EVT_CAMERA_ON_POSITION:
  1856. {
  1857. #ifdef DEBUG_EXTRA
  1858. LOG("Cam On Position After Event -> Capture Picture");
  1859. #endif
  1860. DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
  1861. if (tData)
  1862. {
  1863. if (! saveStreamPicture(tData))
  1864. {
  1865. LOG_ERROR("Save Error!");
  1866. }
  1867. tData->assoc_event_ids.clear();
  1868. free(tData);
  1869. Evt->m_pEvtData = NULL;
  1870. #ifdef DEBUG_EXTRA
  1871. LOG("Event Data Saved!");
  1872. #endif
  1873. }
  1874. else
  1875. {
  1876. LOG_ERROR("No Evt Data!");
  1877. }
  1878. break;
  1879. };
  1880. };
  1881. break;
  1882. };
  1883. };
  1884. return true;
  1885. };
  1886. return false;
  1887. }
  1888. // https://www.ps2-home.com/forum/viewtopic.php?t=98
  1889. // https://www.mikekohn.net/software/playstation2.php
  1890. bool kernelModuleEmbedVideo::saveStreamPicture(DBC_SNAPSHOT_DATA * ptr)
  1891. {
  1892. if (ptr == NULL)
  1893. {
  1894. return false;
  1895. }
  1896. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES) == EVE_OUTPUT_PICTURES)
  1897. {
  1898. #ifdef DEBUG_EXTRA
  1899. LOG("Saving Picture!");
  1900. #endif
  1901. // saveStreamPicture( ptr->stream_id );
  1902. //*
  1903. if (ptr->assoc_event_ids.size())
  1904. {
  1905. if (m_mapCAPTURES.find(ptr->stream_id) != m_mapCAPTURES.end())
  1906. {
  1907. m_mPictureProtector.lock();
  1908. ptr->data = (uint8_t *) malloc(m_mapCAPTURES[ptr->stream_id].size());
  1909. if (ptr->data)
  1910. {
  1911. ptr->data_size = m_mapCAPTURES[ptr->stream_id].size();
  1912. memcpy(ptr->data, m_mapCAPTURES[ptr->stream_id].data(), ptr->data_size);
  1913. m_mPictureProtector.unlock();
  1914. CSTR MD5 = MD5Binary(ptr->data, ptr->data_size);
  1915. if (MD5)
  1916. {
  1917. strncpy(ptr->md5, MD5, 32);
  1918. free(MD5);
  1919. if (! m_pDBC->addPicture(ptr, "jpg"))
  1920. {
  1921. LOG_ERROR("Database Error to consume capture!");
  1922. }
  1923. }
  1924. else
  1925. {
  1926. LOG_ERROR("no MD5 Hash!");
  1927. }
  1928. free(ptr->data);
  1929. }
  1930. else
  1931. {
  1932. m_mPictureProtector.unlock();
  1933. LOG_ERROR("No memory to allocate!");
  1934. };
  1935. #ifdef DEBUG_EXTRA
  1936. }
  1937. else
  1938. {
  1939. LOG("No apropriate Stream Found!");
  1940. #endif
  1941. };
  1942. };
  1943. //*/
  1944. #ifdef DEBUG_EXTRA
  1945. }
  1946. else
  1947. {
  1948. LOG("No Pic Mode!");
  1949. #endif
  1950. }
  1951. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO)
  1952. {
  1953. #ifdef DEBUG_EXTRA
  1954. LOG("Saving Video!");
  1955. #endif
  1956. if (ptr->assoc_event_ids.size())
  1957. {
  1958. m_mStreamHandlersProtector.lock();
  1959. mapSTREAMHANDLERS::iterator hnd = m_mapHANDLERS.find(ptr->stream_id);
  1960. if (hnd != m_mapHANDLERS.end())
  1961. {
  1962. uint32_t db_id;
  1963. uint32_t cfrm;
  1964. for (DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++)
  1965. {
  1966. (*hnd).second->triggerEvent(*idx, db_id, cfrm);
  1967. }
  1968. #ifdef DEBUG_EXTRA
  1969. }
  1970. else
  1971. {
  1972. LOG("No Stream Found!");
  1973. #endif
  1974. };
  1975. m_mStreamHandlersProtector.unlock();
  1976. #ifdef DEBUG_EXTRA
  1977. }
  1978. else
  1979. {
  1980. LOG("No Assoc EVTIDS!");
  1981. #endif
  1982. };
  1983. #ifdef DEBUG
  1984. }
  1985. else
  1986. {
  1987. LOG("No Video Mode!");
  1988. #endif
  1989. };
  1990. return true;
  1991. }
  1992. bool kernelModuleEmbedVideo::saveStreamPicture(uint32_t id)
  1993. {
  1994. if (! id)
  1995. {
  1996. return false;
  1997. }
  1998. if (m_sEmbedVideoData.output_mode & EVE_OUTPUT_PICTURES)
  1999. {
  2000. if (m_mapCAPTURES.find(id) != m_mapCAPTURES.end())
  2001. {
  2002. FILE * tFD;
  2003. m_mPictureProtector.lock();
  2004. size_t tBufferSize = m_mapCAPTURES[id].size();
  2005. uint8_t * tBuffer = (uint8_t *) malloc(tBufferSize);
  2006. if (tBuffer)
  2007. {
  2008. memcpy(tBuffer, m_mapCAPTURES[id].data(), tBufferSize);
  2009. m_mPictureProtector.unlock();
  2010. CSTR MD5 = MD5Binary(tBuffer, tBufferSize);
  2011. if (MD5)
  2012. {
  2013. char tFileName[MAX_PATH];
  2014. if (generateSavePicPath(MD5, tFileName))
  2015. {
  2016. FILE * fd = fopen(tFileName, "wb+");
  2017. if (fd)
  2018. {
  2019. fwrite(tBuffer, 1, tBufferSize, fd);
  2020. fclose(fd);
  2021. ch_chown(tFileName, "nobody", "nogroup");
  2022. // retVal = m_pDBC->addPicture( id, MD5, "jpg" );
  2023. }
  2024. else
  2025. {
  2026. LOG_ERROR("Can not open picture file!");
  2027. }
  2028. }
  2029. else
  2030. {
  2031. LOG_ERROR("Can not generate file name!");
  2032. }
  2033. free(MD5);
  2034. }
  2035. else
  2036. {
  2037. LOG_ERROR("no MD5 Hash!");
  2038. }
  2039. free(tBuffer);
  2040. }
  2041. else
  2042. {
  2043. m_mPictureProtector.unlock();
  2044. LOG_ERROR("no memory to allocate!");
  2045. };
  2046. };
  2047. }
  2048. return true;
  2049. }
  2050. int kernelModuleEmbedVideo::getWidh(void)
  2051. {
  2052. if (m_sEmbedVideoData.width)
  2053. {
  2054. return m_sEmbedVideoData.width;
  2055. }
  2056. return -1;
  2057. }
  2058. int kernelModuleEmbedVideo::getHeight(void)
  2059. {
  2060. if (m_sEmbedVideoData.height)
  2061. {
  2062. return m_sEmbedVideoData.height;
  2063. }
  2064. return -1;
  2065. }
  2066. bool kernelModuleEmbedVideo::generateSavePicPath(CSTR MD5, CSTR Output)
  2067. {
  2068. if (MD5 == NULL)
  2069. {
  2070. return false;
  2071. }
  2072. if (Output == NULL)
  2073. {
  2074. return false;
  2075. }
  2076. if (! fileExist(m_pBaseSavePath))
  2077. {
  2078. LOG_ERROR("No Base Picture Save Path Defined!");
  2079. return false;
  2080. };
  2081. if (strlen(MD5) < 4)
  2082. {
  2083. return false;
  2084. }
  2085. char test[MAX_PATH];
  2086. sprintf(test, "%s/%c%c", m_pBaseSavePath, MD5[0], MD5[1]);
  2087. if (! fileExist(test))
  2088. {
  2089. #ifdef __linux__
  2090. mkdir(test, STORAGE_DIR_MODE);
  2091. #else
  2092. _mkdir(test);
  2093. #endif
  2094. ch_chown(test, "nobody", "nogroup");
  2095. };
  2096. sprintf(test, "%s/%c%c/%c%c", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3]);
  2097. if (! fileExist(test))
  2098. {
  2099. #ifdef __linux__
  2100. mkdir(test, STORAGE_DIR_MODE);
  2101. #else
  2102. _mkdir(test);
  2103. #endif
  2104. ch_chown(test, "nobody", "nogroup");
  2105. };
  2106. sprintf(Output, "%s/%c%c/%c%c/%s.jpg", m_pBaseSavePath, MD5[0], MD5[1], MD5[2], MD5[3], MD5);
  2107. return true;
  2108. }
  2109. bool kernelModuleEmbedVideo::registerStream(uint32_t streamID, uint8_t flip_mode)
  2110. {
  2111. // return true;
  2112. try
  2113. {
  2114. m_mStreamHandlersProtector.lock();
  2115. streamHandler * tData = m_mapHANDLERS.at(streamID);
  2116. // If SH allready exists
  2117. if (tData != NULL)
  2118. {
  2119. m_mStreamHandlersProtector.unlock();
  2120. return true;
  2121. };
  2122. // Create New One
  2123. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) != EVE_OUTPUT_VIDEO)
  2124. {
  2125. tData = new streamHandler(this, MS_MODE_DECODE_FRAMES, flip_mode);
  2126. }
  2127. else
  2128. {
  2129. tData = new streamHandler(this, MS_MODE_ENCODE_FRAMES, flip_mode);
  2130. };
  2131. if (tData != NULL)
  2132. {
  2133. if (tData->create(m_pDBC, streamID))
  2134. {
  2135. m_mapHANDLERS[streamID] = tData;
  2136. m_mStreamHandlersProtector.unlock();
  2137. return true;
  2138. }
  2139. else
  2140. {
  2141. LOG_ERROR("Error Creating new Stream Handler[t]!");
  2142. delete(tData);
  2143. m_mapHANDLERS[streamID] = NULL;
  2144. };
  2145. }
  2146. else
  2147. {
  2148. LOG_ERROR("Error Constructing new Stream Handler[t]!");
  2149. };
  2150. m_mStreamHandlersProtector.unlock();
  2151. }
  2152. catch (const std::out_of_range & oor)
  2153. {
  2154. streamHandler * tSH = NULL;
  2155. // Create New One
  2156. if ((m_sEmbedVideoData.output_mode & EVE_OUTPUT_VIDEO) != EVE_OUTPUT_VIDEO)
  2157. {
  2158. tSH = new streamHandler(this, MS_MODE_DECODE_FRAMES, flip_mode);
  2159. }
  2160. else
  2161. {
  2162. tSH = new streamHandler(this, MS_MODE_ENCODE_FRAMES, flip_mode);
  2163. };
  2164. if (tSH != NULL)
  2165. {
  2166. if (tSH->create(m_pDBC, streamID))
  2167. {
  2168. m_mapHANDLERS[streamID] = tSH;
  2169. m_mStreamHandlersProtector.unlock();
  2170. return true;
  2171. }
  2172. else
  2173. {
  2174. LOG_ERROR("Error Creating new Stream Handler[c]!");
  2175. delete(tSH);
  2176. m_mapHANDLERS[streamID] = NULL;
  2177. };
  2178. }
  2179. else
  2180. {
  2181. LOG_ERROR("Error Constructing new Stream Handler[c]!");
  2182. };
  2183. m_mStreamHandlersProtector.unlock();
  2184. };
  2185. return false;
  2186. }
  2187. bool kernelModuleEmbedVideo::unregisterStream(uint32_t streamID)
  2188. {
  2189. try
  2190. {
  2191. m_mStreamHandlersProtector.lock();
  2192. streamHandler * tData = m_mapHANDLERS.at(streamID);
  2193. if (tData != NULL)
  2194. {
  2195. delete(tData);
  2196. // ToDo: Remove Stream Mats and Clients Connected to a Stream ( or Push them No Signal Image )
  2197. m_mapHANDLERS[streamID] = NULL;
  2198. m_mStreamHandlersProtector.unlock();
  2199. return true;
  2200. };
  2201. m_mStreamHandlersProtector.unlock();
  2202. LOG_ERROR("Registered Stream is Empty!");
  2203. }
  2204. catch (const std::out_of_range & oor)
  2205. {
  2206. m_mStreamHandlersProtector.unlock();
  2207. LOG_ERROR("No Such Stream Registered!");
  2208. };
  2209. return false;
  2210. }
  2211. #undef LOG
  2212. #undef LOG_ERROR
  2213. #define LOG(X) streamHandler::m_pLog->Log("MEV_STR", this, X)
  2214. #ifdef DEBUG
  2215. #define LOG_ERROR(X) \
  2216. { \
  2217. char _tBfrErrMSG2w34[256]; \
  2218. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  2219. streamHandler::m_pLog->Log("MEV_STR", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  2220. }
  2221. #else
  2222. #define LOG_ERROR(X) streamHandler::m_pLog->Log("MEV_STR", this, _LOG_ERROR, X);
  2223. #endif
  2224. streamHandler::streamHandler(kernelModuleEmbedVideo * ptr, MS_WORK_MODE M, uint8_t flip_mode) :
  2225. m_pMaster(ptr),
  2226. m_eWM(M),
  2227. basicMediaDestination()
  2228. #ifdef EVE_ENABLE_VIDEO
  2229. ,
  2230. basicThread()
  2231. #endif
  2232. {
  2233. m_pLog = basicLogCollection::getInstance();
  2234. memset(&m_sStreamData, 0, sizeof(DBC_MEDIA_STREAM_DATA));
  2235. m_pSource = NULL;
  2236. m_bEvtMarkerCounter = 0;
  2237. m_bCamMoveCounter = 0;
  2238. m_bFlipMode = flip_mode;
  2239. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2240. #ifdef EVE_ENABLE_VIDEO
  2241. m_bAFrameSkeepDivider = 1;
  2242. m_bAFrameSkeepDividerCounter = 0;
  2243. m_bAOutputFPS = VIDEO_OUTPUT_FPS;
  2244. m_bBFrameSkeepDivider = 1;
  2245. m_bBFrameSkeepDividerCounter = 0;
  2246. m_bBOutputFPS = VIDEO_OUTPUT_FPS;
  2247. #endif
  2248. m_bWebFrameSkeepDivider = 1;
  2249. m_bWebFrameSkeepDividerCounter = 0;
  2250. m_bInitialOutputFPS = m_bCurrentOutputFPS = VIDEO_OUTPUT_FPS;
  2251. #endif
  2252. #ifdef EVE_ENABLE_VIDEO
  2253. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2254. m_nAPreAlarmFrames = 0;
  2255. m_nAPostAlarmFrames = 0;
  2256. m_nASegmentLimitFrames = 0;
  2257. m_nAHardLimitFrames = 0;
  2258. m_nBPreAlarmFrames = 0;
  2259. m_nBPostAlarmFrames = 0;
  2260. m_nBSegmentLimitFrames = 0;
  2261. m_nBHardLimitFrames = 0;
  2262. #else
  2263. m_nPreAlarmFrames = 0;
  2264. m_nPostAlarmFrames = 0;
  2265. m_nSegmentLimitFrames = 0;
  2266. m_nHardLimitFrames = 0;
  2267. #endif
  2268. m_nSegAFrameCounter = 0;
  2269. m_nSegBFrameCounter = 0;
  2270. m_nGlobalFrameCounter = 0;
  2271. // m_nVideoRegisteredDBID = 0;
  2272. m_nSegAShiftFrames = 0;
  2273. m_nSegBShiftFrames = 0;
  2274. m_nSegASkippedFrames = m_nSegBSkippedFrames = 0;
  2275. m_bActiveEncoderStream = 0;
  2276. m_bSegAActive = false;
  2277. m_bSegADelete = true;
  2278. m_bSegBActive = false;
  2279. m_bSegBDelete = true;
  2280. m_bCOP = 0;
  2281. m_cSourceType = 'A';
  2282. memset(&m_sStreamSegmentInfo[0], 0, sizeof(VIDEO_SEGMENT_INFO));
  2283. memset(&m_sStreamSegmentInfo[1], 0, sizeof(VIDEO_SEGMENT_INFO));
  2284. #ifdef DIRECT_MJPEG_COMPOSING
  2285. m_pOutputFD[0] = NULL;
  2286. m_pOutputFD[1] = NULL;
  2287. #else // !DIRECT_MJPEG_COMPOSING
  2288. #ifdef OPENCV_VIDEO_OUTPUT
  2289. m_pEncoderA = NULL;
  2290. m_pEncoderB = NULL;
  2291. #endif
  2292. #endif
  2293. #endif // EVE_ENABLE_VIDEO
  2294. // if( M == MS_MODE_DECODE_FRAMES ) 1=1;
  2295. }
  2296. streamHandler::~streamHandler()
  2297. {
  2298. if (m_sStreamData.id)
  2299. {
  2300. if (m_pSource != NULL)
  2301. {
  2302. switch (m_cSourceType)
  2303. { // m_sStreamData.source_type[0] ) {
  2304. case 'D': delete((videoLIVE555Source *) m_pSource); break;
  2305. case 'A': delete((videoAVLIBSource *) m_pSource); break;
  2306. };
  2307. };
  2308. #ifdef EVE_ENABLE_VIDEO
  2309. closeAll();
  2310. #endif // EVE_ENABLE_VIDEO
  2311. };
  2312. }
  2313. bool streamHandler::create(dataBinder * ptr, uint32_t stremID)
  2314. {
  2315. if (ptr == NULL)
  2316. {
  2317. return false;
  2318. }
  2319. m_pDBC = ptr;
  2320. if (stremID)
  2321. {
  2322. DBC_MEDIA_STREAM_DATA * tData = m_pDBC->getMediaStreamData(stremID);
  2323. if (tData)
  2324. {
  2325. memcpy(&m_sStreamData, tData, sizeof(DBC_MEDIA_STREAM_DATA));
  2326. free(tData);
  2327. DBC_CAM_DATA * tCAM = m_pDBC->getCamData(m_sStreamData.source_id);
  2328. if (tCAM)
  2329. {
  2330. m_sSourceDesc.mediaStreamId = stremID;
  2331. m_sSourceDesc.mediaSourceId = m_sStreamData.source_id;
  2332. uint32_t tNID = m_pDBC->getNodeIdByTypeAndAux(tCAM->type, m_sStreamData.source_id);
  2333. // ToDo: Check Camera Addition
  2334. // Just Work around to handle Wrong tyoe 12 instead of 13
  2335. if (tNID == 0)
  2336. {
  2337. tNID = m_pDBC->getNodeIdByTypeAndAux(13, m_sStreamData.source_id);
  2338. };
  2339. if (tNID)
  2340. {
  2341. CSTR tNAM = m_pDBC->getNodeName(tNID);
  2342. if (tNAM != NULL)
  2343. {
  2344. strncpy(m_sSourceDesc.mediaName, tNAM, 127);
  2345. m_sSourceDesc.mediaName[127] = (char) 0;
  2346. free(tNAM);
  2347. }
  2348. else
  2349. {
  2350. sprintf(m_sSourceDesc.mediaName, "No Name Cam");
  2351. };
  2352. }
  2353. else
  2354. {
  2355. sprintf(m_sSourceDesc.mediaName, "No-Name-Cam");
  2356. };
  2357. m_cSourceType = m_sStreamData.source_type[0];
  2358. switch (m_cSourceType)
  2359. {
  2360. case 'A': m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sSourceDesc); break;
  2361. case 'D':
  2362. {
  2363. // Check Auth mode if basic - Override and use AVLIB
  2364. // To Debug LIVE555 ONLY
  2365. if (1 || tCAM->auth_mode == 0)
  2366. {
  2367. m_cSourceType = 'A';
  2368. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sSourceDesc);
  2369. break;
  2370. }
  2371. else
  2372. {
  2373. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sSourceDesc);
  2374. break;
  2375. };
  2376. break;
  2377. };
  2378. };
  2379. if (m_pSource != NULL)
  2380. {
  2381. char tMRL[1024];
  2382. #ifdef DEBUG_EXTRA
  2383. LOG("Source OK");
  2384. #endif
  2385. int tW = m_pMaster->getWidh();
  2386. int tH = m_pMaster->getHeight();
  2387. int pix = -1;
  2388. bool in_16_9 = (((m_sStreamData.width * 100) / m_sStreamData.height) > 140);
  2389. bool out_16_9 = (((tW * 100) / tH) > 140);
  2390. // if( tW > 0 ){
  2391. // //m_pSource->setProportionalByWidth( tW );
  2392. // m_pSource->setSize( tW, tH );
  2393. // };
  2394. if (in_16_9 != out_16_9)
  2395. {
  2396. if (in_16_9)
  2397. {
  2398. tH = (((tW * 1000) / 16) * 9) / 1000;
  2399. }
  2400. else
  2401. {
  2402. tW = (((tH * 1000) / 3) * 4) / 1000;
  2403. };
  2404. };
  2405. // export PATH=/usr/sbin:$PATH
  2406. if (strncmp(m_sStreamData.mrl_proto, "v4l2", 4) == 0)
  2407. {
  2408. sprintf(tMRL, m_sStreamData.mrl);
  2409. pix = atoi(m_sStreamData.c_type);
  2410. }
  2411. else
  2412. {
  2413. sprintf(tMRL, "%s://%s:%s@%s:%d%s", m_sStreamData.mrl_proto, tCAM->username, tCAM->password, tCAM->ip, m_sStreamData.mrl_port, m_sStreamData.mrl);
  2414. };
  2415. if (m_pSource->_open(tMRL, false, -1.0f, pix, m_sStreamData.width, m_sStreamData.height, tW, tH))
  2416. {
  2417. if (! m_pSource->attachDestination(this))
  2418. {
  2419. switch (m_cSourceType)
  2420. {
  2421. case 'D': delete((videoLIVE555Source *) m_pSource); break;
  2422. case 'A': delete((videoAVLIBSource *) m_pSource); break;
  2423. };
  2424. m_pSource = NULL;
  2425. }
  2426. else
  2427. {
  2428. #ifdef DEBUG_EXTRA
  2429. LOG("Source is Connected to Output.");
  2430. #endif
  2431. };
  2432. }
  2433. else
  2434. {
  2435. LOG_ERROR("Can not Open Video Source!");
  2436. switch (m_cSourceType)
  2437. {
  2438. case 'D': delete((videoLIVE555Source *) m_pSource); break;
  2439. case 'A': delete((videoAVLIBSource *) m_pSource); break;
  2440. };
  2441. m_pSource = NULL;
  2442. };
  2443. }
  2444. else
  2445. {
  2446. LOG_ERROR("Video Source Stream Failure!");
  2447. };
  2448. free(tCAM);
  2449. }
  2450. else
  2451. {
  2452. LOG_ERROR("Error Getting CAM Data!");
  2453. m_pSource = NULL;
  2454. return false;
  2455. };
  2456. };
  2457. };
  2458. if (m_pSource)
  2459. {
  2460. #ifdef EVE_ENABLE_VIDEO
  2461. return initVideoOutput();
  2462. #else
  2463. return true;
  2464. #endif // EVE_ENABLE_VIDEO
  2465. };
  2466. return false;
  2467. }
  2468. bool streamHandler::pushFrame(AVFrame * Frame, void * src)
  2469. {
  2470. if (m_pMaster == NULL)
  2471. {
  2472. return false;
  2473. }
  2474. MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
  2475. if (tMD)
  2476. {
  2477. };
  2478. return true;
  2479. }
  2480. bool streamHandler::pushFrame(cv::Mat & Frame, void * src)
  2481. {
  2482. /// @todo Valgrind is reporting an important memory leak in this method.
  2483. bool SkeepW, SkeepA, SkeepB;
  2484. if (m_pMaster == NULL)
  2485. {
  2486. return false;
  2487. }
  2488. MEDIA_SOURCE_DESC * tMD = (MEDIA_SOURCE_DESC *) src;
  2489. char tBuffer[128];
  2490. if (tMD)
  2491. {
  2492. float mK, oK;
  2493. cv::Rect ROI;
  2494. m_pMaster->m_mPictureProtector.lock();
  2495. if (m_pMaster->m_mapMATS[tMD->mediaStreamId].cols == 0)
  2496. {
  2497. LOG("First Frame Enqueued");
  2498. sprintf(tBuffer, "INP: [%dx%d] SrcID:%d StreamID:%d", Frame.cols, Frame.rows, tMD->mediaSourceId, tMD->mediaStreamId);
  2499. LOG(tBuffer);
  2500. m_pMaster->m_mapMATS[tMD->mediaStreamId].create(m_pMaster->m_sEmbedVideoData.height, m_pMaster->m_sEmbedVideoData.width, CV_8UC3);
  2501. cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Point(0, 0), cv::Point(m_pMaster->m_sEmbedVideoData.height, m_pMaster->m_sEmbedVideoData.width), cv::Scalar(0, 0, 0), cv::FILLED);
  2502. };
  2503. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2504. SkeepW = ! ((m_bWebFrameSkeepDividerCounter % m_bWebFrameSkeepDivider) == 0);
  2505. m_bWebFrameSkeepDividerCounter = (++m_bWebFrameSkeepDividerCounter) % m_bWebFrameSkeepDivider;
  2506. #ifdef EVE_ENABLE_VIDEO
  2507. SkeepA = ! ((m_bAFrameSkeepDividerCounter % m_bAFrameSkeepDivider) == 0);
  2508. // m_bAFrameSkeepDividerCounter++;
  2509. m_bAFrameSkeepDividerCounter = (++m_bAFrameSkeepDividerCounter) % m_bAFrameSkeepDivider;
  2510. SkeepB = ! ((m_bBFrameSkeepDividerCounter % m_bBFrameSkeepDivider) == 0);
  2511. // m_bBFrameSkeepDividerCounter++;
  2512. m_bBFrameSkeepDividerCounter = (++m_bBFrameSkeepDividerCounter) % m_bBFrameSkeepDivider;
  2513. #ifdef DEBUG_ALL
  2514. if (SkeepW || SkeepA || SkeepB)
  2515. {
  2516. char tzB[256];
  2517. sprintf(tzB, "SkipF: %d:%d %d:%d %d:%d", m_bWebFrameSkeepDividerCounter, m_bWebFrameSkeepDivider, m_bAFrameSkeepDividerCounter, m_bAFrameSkeepDivider, m_bBFrameSkeepDividerCounter, m_bBFrameSkeepDivider);
  2518. LOG(tzB);
  2519. }
  2520. else
  2521. {
  2522. LOG("PushF");
  2523. };
  2524. #endif
  2525. // if( m_bWebFrameSkeepDivider == m_bAFrameSkeepDividerCounter ) = 1;
  2526. // if( SkeepB ) m_bBFrameSkeepDividerCounter = 1;
  2527. // if( SkeepW ) m_bWebFrameSkeepDividerCounter = 1;
  2528. #else
  2529. #ifdef DEBUG_ALL
  2530. if (SkeepW)
  2531. {
  2532. char tB[256];
  2533. sprintf(tB, "Skip: %d:%d", m_bWebFrameSkeepDividerCounter, m_bWebFrameSkeepDivider);
  2534. LOG(tB);
  2535. };
  2536. #endif
  2537. if (SkeepW)
  2538. {
  2539. m_bWebFrameSkeepDividerCounter = 1;
  2540. }
  2541. #endif
  2542. #endif
  2543. #ifdef USE_SCALING_IN
  2544. oK = ((float) m_pMaster->m_sEmbedVideoData.height) / ((float) m_pMaster->m_sEmbedVideoData.width);
  2545. mK = ((float) Frame.rows) / ((float) Frame.cols);
  2546. // sprintf( tBuffer, "INP: [%dx%d] IK:%0.3f OK:%0.3f", Frame.cols, Frame.rows, mK, oK );
  2547. if ((fabsf(mK - oK) < 0.1f) || (fabsf(oK - mK) < 0.1f))
  2548. {
  2549. ROI.width = (int) m_pMaster->m_sEmbedVideoData.width;
  2550. ROI.height = (int) m_pMaster->m_sEmbedVideoData.height;
  2551. ROI.x = (int) 0;
  2552. ROI.y = (int) 0;
  2553. }
  2554. else
  2555. {
  2556. if (mK < oK)
  2557. {
  2558. ROI.width = (int) m_pMaster->m_sEmbedVideoData.width;
  2559. ROI.height = (int) (((float) m_pMaster->m_sEmbedVideoData.width) * mK);
  2560. ROI.x = (int) 0;
  2561. ROI.y = (int) (m_pMaster->m_sEmbedVideoData.height - ROI.height) >> 1;
  2562. }
  2563. else
  2564. {
  2565. ROI.height = (int) m_pMaster->m_sEmbedVideoData.height;
  2566. ROI.width = (int) (((float) m_pMaster->m_sEmbedVideoData.height) / mK);
  2567. ROI.y = (int) 0;
  2568. ROI.x = (int) (m_pMaster->m_sEmbedVideoData.width - ROI.width) >> 1;
  2569. };
  2570. };
  2571. cv::Mat mROI = m_pMaster->m_mapMATS[tMD->mediaStreamId](ROI);
  2572. #ifndef USE_CV_DENOISE
  2573. cv::resize(Frame, mROI, mROI.size(), 0, 0, cv::INTER_LINEAR);
  2574. #else
  2575. cv::Mat mDenoiser;
  2576. cv::resize(Frame, mDenoiser, mROI.size(), 0, 0, cv::INTER_LINEAR);
  2577. cv::fastNlMeansDenoisingColored(mDenoiser, mROI, 10, 10, 7, 17);
  2578. #endif
  2579. #ifdef USE_CV_TONEMAP
  2580. cv::Mat ldrReinhard;
  2581. cv::Ptr<cv::TonemapReinhard> tonemapReinhard = cv::createTonemapReinhard(1.5, 0, 0, 0);
  2582. tonemapReinhard->process(mROI, ldrReinhard);
  2583. ldrReinhard = ldrReinhard * 255;
  2584. ldrReinhard.copyTo(mROI);
  2585. #endif
  2586. #else
  2587. if ((m_pMaster->m_sEmbedVideoData.height == Frame.rows) && (m_pMaster->m_sEmbedVideoData.width == Frame.cols))
  2588. {
  2589. Frame.copyTo(m_pMaster->m_mapMATS[tMD->mediaStreamId]);
  2590. }
  2591. else
  2592. {
  2593. // Mat dst_roi = dst(Rect(left, top, src.cols, src.rows));
  2594. // src.copyTo(dst_roi);
  2595. if (m_pMaster->m_sEmbedVideoData.width == Frame.cols)
  2596. {
  2597. ROI.width = Frame.cols;
  2598. ROI.height = Frame.rows;
  2599. ROI.x = 0;
  2600. ROI.y = (m_pMaster->m_sEmbedVideoData.height - Frame.rows) >> 1;
  2601. cv::Mat mROI = m_pMaster->m_mapMATS[tMD->mediaStreamId](ROI);
  2602. #ifdef USE_CV_DENOISE
  2603. cv::fastNlMeansDenoisingColored(Frame, mROI, 10, 10, 7, 17);
  2604. #else
  2605. Frame.copyTo(mROI);
  2606. #endif
  2607. #ifdef USE_CV_TONEMAP
  2608. cv::Mat ldrReinhard;
  2609. cv::Ptr<cv::TonemapReinhard> tonemapReinhard = cv::createTonemapReinhard(1.5, 0, 0, 0);
  2610. tonemapReinhard->process(mROI, ldrReinhard);
  2611. ldrReinhard = ldrReinhard * 255;
  2612. ldrReinhard.copyTo(mROI);
  2613. #endif
  2614. }
  2615. else
  2616. {
  2617. ROI.width = Frame.cols;
  2618. ROI.height = Frame.rows;
  2619. ROI.x = (m_pMaster->m_sEmbedVideoData.width - Frame.cols) >> 1;
  2620. ROI.y = 0;
  2621. cv::Mat mROI = m_pMaster->m_mapMATS[tMD->mediaStreamId](ROI);
  2622. #ifdef USE_CV_DENOISE
  2623. cv::fastNlMeansDenoisingColored(Frame, mROI, 10, 10, 7, 17);
  2624. #else
  2625. Frame.copyTo(mROI);
  2626. #endif
  2627. #ifdef USE_CV_TONEMAP
  2628. cv::Mat ldrReinhard;
  2629. cv::Ptr<cv::TonemapReinhard> tonemapReinhard = cv::createTonemapReinhard(1.5, 0, 0, 0);
  2630. tonemapReinhard->process(mROI, ldrReinhard);
  2631. ldrReinhard = ldrReinhard * 255;
  2632. ldrReinhard.copyTo(mROI);
  2633. #endif
  2634. };
  2635. };
  2636. #endif
  2637. // 0 - dis .. 1 - Hor ... 2 - vert 3 - both
  2638. if (m_bFlipMode)
  2639. {
  2640. // Flip Horizontally = 1 vertically = 0 both = -1
  2641. cv::Mat mirroredImage;
  2642. cv::flip(m_pMaster->m_mapMATS[tMD->mediaStreamId], mirroredImage, 2 - m_bFlipMode);
  2643. mirroredImage.copyTo(m_pMaster->m_mapMATS[tMD->mediaStreamId]);
  2644. };
  2645. #ifdef EVE_ENABLE_DROP_OUTPUT_FPS
  2646. // LOG("T");
  2647. m_mEncodingQueueProtector.lock();
  2648. if (m_qEncodingQueue.size() > 10)
  2649. {
  2650. m_mEncodingQueueProtector.unlock();
  2651. m_pMaster->m_mPictureProtector.unlock();
  2652. // LOG("D");
  2653. return true;
  2654. };
  2655. m_mEncodingQueueProtector.unlock();
  2656. // LOG("Q");
  2657. #endif
  2658. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2659. #ifdef EVE_ENABLE_VIDEO
  2660. if (SkeepW && SkeepA && SkeepB)
  2661. {
  2662. m_pMaster->m_mPictureProtector.unlock();
  2663. return true;
  2664. };
  2665. #else
  2666. if (SkeepW)
  2667. {
  2668. m_pMaster->m_mPictureProtector.unlock();
  2669. return true;
  2670. };
  2671. #endif
  2672. #endif
  2673. if (m_bCamMoveCounter > m_bEvtMarkerCounter)
  2674. {
  2675. cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Rect(10, 10, 20, 20), cv::Scalar(m_bCamMoveCounter, 0, 0), cv::FILLED);
  2676. m_bCamMoveCounter -= 4;
  2677. if (m_bCamMoveCounter < 4)
  2678. {
  2679. m_bCamMoveCounter = 0;
  2680. }
  2681. }
  2682. else
  2683. {
  2684. if (m_bEvtMarkerCounter)
  2685. {
  2686. m_bCamMoveCounter = 0;
  2687. cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Rect(10, 10, 20, 20), cv::Scalar(0, 0, m_bEvtMarkerCounter), cv::FILLED);
  2688. m_bEvtMarkerCounter -= 4;
  2689. if (m_bEvtMarkerCounter < 4)
  2690. {
  2691. m_bEvtMarkerCounter = 0;
  2692. }
  2693. };
  2694. };
  2695. if (m_pMaster->m_sEmbedVideoData.show_label)
  2696. {
  2697. time_t t = time(NULL);
  2698. // struct tm * tmp = gmtime( &t );
  2699. struct tm * tmp = localtime(&t);
  2700. if (tmp)
  2701. {
  2702. char tsb[64];
  2703. // if( strftime( tsb, sizeof(tsb), "%F %T GMT", tmp )!=0 ){
  2704. if (strftime(tsb, sizeof(tsb), "%F %T", tmp) != 0)
  2705. {
  2706. // sprintf( tBuffer, ">>%d>%d>%s>%s>", tMD->mediaSourceId, tMD->mediaStreamId, tMD->mediaName, tsb );
  2707. sprintf(tBuffer, ">>>%s>%s>", tMD->mediaName, tsb);
  2708. int baseline = 0;
  2709. cv::Size textSize = cv::getTextSize(tBuffer, m_pMaster->m_sEmbedVideoData.label_font_face, m_pMaster->m_sEmbedVideoData.label_font_scale, m_pMaster->m_sEmbedVideoData.label_font_thickness, &baseline);
  2710. int x = m_pMaster->m_sEmbedVideoData.label_x;
  2711. int y = m_pMaster->m_sEmbedVideoData.label_y;
  2712. if (x < 0)
  2713. {
  2714. x = m_pMaster->m_sEmbedVideoData.width + x - textSize.width;
  2715. };
  2716. if (y < 0)
  2717. {
  2718. y = m_pMaster->m_sEmbedVideoData.height + y - baseline; //
  2719. };
  2720. if (! m_pMaster->m_sEmbedVideoData.label_transperant)
  2721. {
  2722. cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Rect(x, y - textSize.height - 4, textSize.width, textSize.height + baseline + 4), cv::Scalar(0, 0, 0), cv::FILLED);
  2723. };
  2724. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(x, y), (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.label_font_face, m_pMaster->m_sEmbedVideoData.label_font_scale, cv::Scalar(0, 255, 0),
  2725. m_pMaster->m_sEmbedVideoData.label_font_thickness, m_pMaster->m_sEmbedVideoData.label_line_type);
  2726. };
  2727. };
  2728. }
  2729. else
  2730. {
  2731. if (m_pMaster->m_sEmbedVideoData.show_ts)
  2732. {
  2733. time_t t = time(NULL);
  2734. // struct tm * tmp = gmtime( &t );
  2735. struct tm * tmp = localtime(&t);
  2736. if (tmp)
  2737. {
  2738. if (strftime(tBuffer, sizeof(tBuffer), "%F %T GMT", tmp) != 0)
  2739. {
  2740. uint8_t shift_x = 0;
  2741. uint8_t shift_y = 15;
  2742. if (! m_pMaster->m_sEmbedVideoData.ts_transperant)
  2743. {
  2744. if (m_pMaster->m_sEmbedVideoData.ts_font_face)
  2745. {
  2746. shift_y = 15;
  2747. cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Point(m_pMaster->m_sEmbedVideoData.ts_x, m_pMaster->m_sEmbedVideoData.ts_y), cv::Point(490, 60), cv::Scalar(0, 0, 0), cv::FILLED);
  2748. }
  2749. else
  2750. {
  2751. shift_y = 23;
  2752. cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Point(m_pMaster->m_sEmbedVideoData.ts_x, m_pMaster->m_sEmbedVideoData.ts_y), cv::Point(490, 60), cv::Scalar(0, 0, 0), cv::FILLED);
  2753. };
  2754. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x, m_pMaster->m_sEmbedVideoData.ts_y + shift_y),
  2755. (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 255, 255), m_pMaster->m_sEmbedVideoData.ts_font_thickness,
  2756. m_pMaster->m_sEmbedVideoData.ts_line_type);
  2757. }
  2758. else
  2759. {
  2760. if (m_pMaster->m_sEmbedVideoData.ts_font_face)
  2761. {
  2762. shift_y = 15;
  2763. }
  2764. else
  2765. {
  2766. shift_y = 23;
  2767. };
  2768. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x - 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y - 1),
  2769. (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
  2770. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x + 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y - 1),
  2771. (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
  2772. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x - 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y + 1),
  2773. (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
  2774. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x - 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y + 2),
  2775. (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
  2776. cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x, m_pMaster->m_sEmbedVideoData.ts_y + shift_y),
  2777. (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 255, 255), m_pMaster->m_sEmbedVideoData.ts_font_thickness,
  2778. m_pMaster->m_sEmbedVideoData.ts_line_type);
  2779. }
  2780. };
  2781. };
  2782. };
  2783. };
  2784. if (m_pMaster->m_sEmbedVideoData.show_logo)
  2785. {
  2786. cv::Rect roi(cv::Point(m_pMaster->m_sEmbedVideoData.logo_x, m_pMaster->m_sEmbedVideoData.logo_y), cv::Size(m_pMaster->m_sEmbedVideoData.logo_w, m_pMaster->m_sEmbedVideoData.logo_h));
  2787. cv::Mat destinationROI = m_pMaster->m_mapMATS[tMD->mediaStreamId](roi);
  2788. m_pMaster->m_mLogoPic.copyTo(destinationROI, m_pMaster->m_mLogoMask);
  2789. };
  2790. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2791. if (! SkeepW)
  2792. {
  2793. #endif
  2794. vector<int> compression_params;
  2795. // compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  2796. // compression_params.push_back(9);
  2797. compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
  2798. compression_params.push_back(50); // m_pMaster->m_sEmbedVideoData.compression_quality );
  2799. m_pMaster->m_mEncodedProtector.lock();
  2800. m_pMaster->m_mapCAPTURES[tMD->mediaStreamId].clear();
  2801. cv::imencode(".jpg", m_pMaster->m_mapMATS[tMD->mediaStreamId], m_pMaster->m_mapCAPTURES[tMD->mediaStreamId], compression_params);
  2802. // cv::Mat bw_dst;
  2803. // cv::cvtColor( m_pMaster->m_mapMATS[tMD->mediaStreamId], bw_dst, cv::COLOR_BGR2GRAY );
  2804. // cv::imencode( ".jpg", bw_dst, m_pMaster->m_mapCAPTURES[tMD->mediaStreamId], compression_params );
  2805. #ifdef HAVE_SMAL_PREVIEW_STREAMS
  2806. // Make Small Preview Thumb
  2807. cv::Mat dst;
  2808. cv::resize(m_pMaster->m_mapMATS[tMD->mediaStreamId], dst, cv::Size(PREVIEW_SIZE_WIDTH, PREVIEW_SIZE_HEIGHT));
  2809. // cv::resize( bw_dst, dst, cv::Size( PREVIEW_SIZE_WIDTH, PREVIEW_SIZE_HEIGHT ));
  2810. m_pMaster->m_mapPREVIEWS[tMD->mediaStreamId].clear();
  2811. cv::imencode(".jpg", dst, m_pMaster->m_mapPREVIEWS[tMD->mediaStreamId], compression_params);
  2812. #endif
  2813. m_pMaster->m_mEncodedProtector.unlock();
  2814. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2815. };
  2816. #endif
  2817. #ifdef EVE_ENABLE_VIDEO
  2818. // if only decode Video Stream -> Exit
  2819. if (m_eWM == MS_MODE_DECODE_FRAMES)
  2820. {
  2821. m_pMaster->m_mPictureProtector.unlock();
  2822. return true;
  2823. };
  2824. // encode Video Stream
  2825. // m_nPreAlarmFrames
  2826. // m_nPostAlarmFrames
  2827. // m_nNoAlarmSegmentFrames
  2828. // m_nSegmentLimitFrames
  2829. // m_nHardLimitFrames
  2830. if (m_bSegAActive || m_bSegBActive)
  2831. {
  2832. m_mFrameCounterProtector.lock();
  2833. if (m_bSegAActive)
  2834. {
  2835. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2836. // do {
  2837. // In Any Case Close Stream if it Long enough
  2838. if (m_nSegAFrameCounter > m_nAHardLimitFrames)
  2839. {
  2840. #ifdef DEBUG_EXTRA
  2841. LOG("HardLimit Reached!(A)");
  2842. #endif
  2843. closeSegmentA();
  2844. // break;
  2845. };
  2846. if (m_nSegAFrameCounter > (m_nASegmentLimitFrames - (m_nAPostAlarmFrames + m_nBPreAlarmFrames)))
  2847. {
  2848. if (! m_bSegBActive)
  2849. {
  2850. #ifdef DEBUG_EXTRA
  2851. LOG("Start Alt!(A)");
  2852. #endif
  2853. openSegmentB();
  2854. };
  2855. };
  2856. if (m_nSegAFrameCounter > (m_nASegmentLimitFrames - m_nAPostAlarmFrames) && ! m_bActiveEncoderStream)
  2857. {
  2858. #ifdef DEBUG_EXTRA
  2859. LOG("Swap Active!(A)");
  2860. #endif
  2861. swapActiveStreams();
  2862. // break;
  2863. };
  2864. // } while ( 0 );
  2865. #else
  2866. // do {
  2867. // In Any Case Close Stream if it Long enough
  2868. if (m_nSegAFrameCounter > m_nHardLimitFrames)
  2869. {
  2870. #ifdef DEBUG_EXTRA
  2871. LOG("HardLimit Reached!(A)");
  2872. #endif
  2873. closeSegmentA();
  2874. // break;
  2875. };
  2876. if (m_nSegAFrameCounter > (m_nSegmentLimitFrames - (m_nPostAlarmFrames + m_nPreAlarmFrames)))
  2877. {
  2878. if (! m_bSegBActive)
  2879. {
  2880. #ifdef DEBUG_EXTRA
  2881. LOG("Start Alt!(A)");
  2882. #endif
  2883. openSegmentB();
  2884. };
  2885. };
  2886. if (m_nSegAFrameCounter > (m_nSegmentLimitFrames - m_nPostAlarmFrames) && ! m_bActiveEncoderStream)
  2887. {
  2888. #ifdef DEBUG_EXTRA
  2889. LOG("Swap Active!(A)");
  2890. #endif
  2891. swapActiveStreams();
  2892. // break;
  2893. };
  2894. // } while ( 0 );
  2895. #endif // !EVE_ENABLE_EASE_OUTPUT_FPS
  2896. };
  2897. if (m_bSegBActive)
  2898. {
  2899. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2900. // do {
  2901. // In Any Case Close Stream if it Long enough
  2902. if (m_nSegBFrameCounter > m_nBHardLimitFrames)
  2903. {
  2904. #ifdef DEBUG_EXTRA
  2905. LOG("HardLimit Reached!(B)");
  2906. #endif
  2907. closeSegmentB();
  2908. // break;
  2909. };
  2910. if (m_nSegBFrameCounter > (m_nBSegmentLimitFrames - (m_nBPostAlarmFrames + m_nAPreAlarmFrames)))
  2911. {
  2912. if (! m_bSegAActive)
  2913. {
  2914. #ifdef DEBUG_EXTRA
  2915. LOG("Start Alt!(B)");
  2916. #endif
  2917. openSegmentA();
  2918. };
  2919. };
  2920. if (m_nSegBFrameCounter > (m_nBSegmentLimitFrames - m_nBPostAlarmFrames) && m_bActiveEncoderStream)
  2921. {
  2922. #ifdef DEBUG_EXTRA
  2923. LOG("Swap Active!(B)");
  2924. #endif
  2925. swapActiveStreams();
  2926. // break;
  2927. };
  2928. //} while ( 0 );
  2929. #else // !EVE_ENABLE_EASE_OUTPUT_FPS
  2930. // do {
  2931. // In Any Case Close Stream if it Long enough
  2932. if (m_nSegBFrameCounter > m_nHardLimitFrames)
  2933. {
  2934. #ifdef DEBUG_EXTRA
  2935. LOG("HardLimit Reached!(B)");
  2936. #endif
  2937. closeSegmentB();
  2938. // break;
  2939. };
  2940. if (m_nSegBFrameCounter > (m_nSegmentLimitFrames - (m_nPostAlarmFrames + m_nPreAlarmFrames)))
  2941. {
  2942. if (! m_bSegAActive)
  2943. {
  2944. #ifdef DEBUG_EXTRA
  2945. LOG("Start Alt!(B)");
  2946. #endif
  2947. openSegmentA();
  2948. };
  2949. };
  2950. if (m_nSegBFrameCounter > (m_nSegmentLimitFrames - m_nPostAlarmFrames) && m_bActiveEncoderStream)
  2951. {
  2952. #ifdef DEBUG_EXTRA
  2953. LOG("Swap Active!(B)");
  2954. #endif
  2955. swapActiveStreams();
  2956. // break;
  2957. };
  2958. //} while ( 0 );
  2959. #endif // !EVE_ENABLE_EASE_OUTPUT_FPS
  2960. };
  2961. m_mFrameCounterProtector.unlock();
  2962. ENCODING_BUFFER * tData = (ENCODING_BUFFER *) malloc(sizeof(ENCODING_BUFFER));
  2963. if (tData)
  2964. {
  2965. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  2966. tData->flag_sa = SkeepA;
  2967. tData->flag_sb = SkeepB;
  2968. #endif
  2969. #ifdef DIRECT_MJPEG_COMPOSING
  2970. tData->size = m_pMaster->m_mapCAPTURES[tMD->mediaStreamId].size();
  2971. tData->data = (uint8_t *) malloc(tData->size);
  2972. if (tData->data)
  2973. {
  2974. memcpy(tData->data, m_pMaster->m_mapCAPTURES[tMD->mediaStreamId].data(), tData->size);
  2975. // tData->data = m_pMaster->m_mapCAPTURES[tMD->mediaStreamId].data();
  2976. // if ( tData->data ) {
  2977. #else
  2978. #ifdef OPENCV_VIDEO_OUTPUT
  2979. tData->rows = m_pMaster->m_mapMATS[tMD->mediaStreamId].rows;
  2980. tData->cols = m_pMaster->m_mapMATS[tMD->mediaStreamId].cols;
  2981. tData->size = m_pMaster->m_mapMATS[tMD->mediaStreamId].cols * m_pMaster->m_mapMATS[tMD->mediaStreamId].elemSize() * tData->rows;
  2982. tData->data = (uint8_t *) malloc(tData->size);
  2983. if (tData->data)
  2984. {
  2985. memcpy(tData->data, m_pMaster->m_mapMATS[tMD->mediaStreamId].data, tData->size);
  2986. #else // OPENCV_VIDEO_OUTPUT
  2987. tData->linesize[0] = m_pMaster->m_mapMATS[tMD->mediaStreamId].cols * m_pMaster->m_mapMATS[tMD->mediaStreamId].elemSize();
  2988. tData->linesize[1] = 0;
  2989. tData->rows = m_pMaster->m_mapMATS[tMD->mediaStreamId].rows;
  2990. tData->size = tData->linesize[0] * tData->rows;
  2991. tData->data[1] = NULL;
  2992. tData->data[0] = (uint8_t *) malloc(tData->size);
  2993. if (tData->data[0])
  2994. {
  2995. memcpy(tData->data[0], m_pMaster->m_mapMATS[tMD->mediaStreamId].data, tData->size);
  2996. #endif // OPENCV_VIDEO_OUTPUT
  2997. #endif //! DIRECT_MJPEG_COMPOSING
  2998. m_mEncodingQueueProtector.lock();
  2999. m_qEncodingQueue.push(tData);
  3000. m_mEncodingQueueProtector.unlock();
  3001. }
  3002. else
  3003. {
  3004. LOG_ERROR("MAT_CPY malloc Error!");
  3005. free(tData);
  3006. };
  3007. }
  3008. else
  3009. {
  3010. LOG_ERROR("ENC_BUF malloc Error!");
  3011. }
  3012. };
  3013. #endif // EVE_ENABLE_VIDEO
  3014. m_pMaster->m_mPictureProtector.unlock();
  3015. }
  3016. else
  3017. {
  3018. LOG_ERROR("No PayLoad!");
  3019. };
  3020. return true;
  3021. }
  3022. #ifdef EVE_ENABLE_VIDEO
  3023. void streamHandler::closeSegmentA(void)
  3024. {
  3025. m_mCurrentOperationProtector.lock();
  3026. if (m_bCOP == 0)
  3027. {
  3028. m_bCOP = 0x01;
  3029. }
  3030. m_mCurrentOperationProtector.unlock();
  3031. #ifdef DEBUG_EXTRA
  3032. LOG("Closing Stream A");
  3033. #endif
  3034. }
  3035. void streamHandler::closeSegmentB(void)
  3036. {
  3037. m_mCurrentOperationProtector.lock();
  3038. if (m_bCOP == 0)
  3039. {
  3040. m_bCOP = 0x02;
  3041. }
  3042. m_mCurrentOperationProtector.unlock();
  3043. #ifdef DEBUG_EXTRA
  3044. LOG("Closing Stream B");
  3045. #endif
  3046. }
  3047. void streamHandler::closeAll()
  3048. {
  3049. waitThreadEnd(true);
  3050. #ifdef OPENCV_VIDEO_OUTPUT
  3051. if (m_pEncoderA)
  3052. {
  3053. if (m_pEncoderA->isOpened())
  3054. {
  3055. m_pEncoderA->release();
  3056. };
  3057. delete(m_pEncoderA);
  3058. };
  3059. if (m_pEncoderB)
  3060. {
  3061. if (m_pEncoderB->isOpened())
  3062. {
  3063. m_pEncoderB->release();
  3064. };
  3065. delete(m_pEncoderB);
  3066. };
  3067. #else // OPENCV_VIDEO_OUTPUT
  3068. #ifdef DIRECT_MJPEG_COMPOSING
  3069. if (m_pOutputFD[0])
  3070. {
  3071. fclose(m_pOutputFD[0]);
  3072. m_pOutputFD[0] = NULL;
  3073. };
  3074. if (m_pOutputFD[1])
  3075. {
  3076. fclose(m_pOutputFD[1]);
  3077. m_pOutputFD[1] = NULL;
  3078. };
  3079. #else // ! DIRECT_MJPEG_COMPOSING
  3080. #ifdef SEPARATED_VIDEO_ENCODERS
  3081. if ((m_pCodecACtx != NULL || m_pCodecBCtx != NULL) && (m_bSegAActive || m_bSegBActive))
  3082. {
  3083. if (m_pCodecACtx != NULL)
  3084. {
  3085. avcodec_send_frame(m_pCodecACtx, NULL);
  3086. int ret = 0;
  3087. AVPacket * avp = av_packet_alloc();
  3088. if (avp)
  3089. {
  3090. do
  3091. {
  3092. av_packet_unref(avp);
  3093. ret = avcodec_receive_packet(m_pCodecACtx, avp);
  3094. if (! ret)
  3095. {
  3096. if (m_bSegAActive)
  3097. {
  3098. av_write_frame(m_pFormatCtxA, avp);
  3099. }
  3100. };
  3101. }
  3102. while (! ret);
  3103. av_packet_unref(avp);
  3104. av_packet_free(&avp);
  3105. };
  3106. };
  3107. if (m_pCodecBCtx != NULL)
  3108. {
  3109. avcodec_send_frame(m_pCodecBCtx, NULL);
  3110. int ret = 0;
  3111. AVPacket * avp = av_packet_alloc();
  3112. if (avp)
  3113. {
  3114. do
  3115. {
  3116. av_packet_unref(avp);
  3117. ret = avcodec_receive_packet(m_pCodecBCtx, avp);
  3118. if (! ret)
  3119. {
  3120. if (m_bSegBActive)
  3121. {
  3122. av_write_frame(m_pFormatCtxB, avp);
  3123. }
  3124. };
  3125. }
  3126. while (! ret);
  3127. av_packet_unref(avp);
  3128. av_packet_free(&avp);
  3129. };
  3130. };
  3131. if (m_bSegAActive)
  3132. {
  3133. av_write_trailer(m_pFormatCtxA);
  3134. avformat_close_input(&m_pFormatCtxA);
  3135. m_bSegAActive = false;
  3136. };
  3137. if (m_bSegBActive)
  3138. {
  3139. av_write_trailer(m_pFormatCtxB);
  3140. avformat_close_input(&m_pFormatCtxB);
  3141. m_bSegBActive = false;
  3142. };
  3143. };
  3144. #else // !SEPARATED_VIDEO_ENCODERS
  3145. if (m_pCodecCtx != NULL && (m_bSegAActive || m_bSegBActive))
  3146. {
  3147. avcodec_send_frame(m_pCodecCtx, NULL);
  3148. int ret = 0;
  3149. AVPacket * avp = av_packet_alloc();
  3150. if (avp)
  3151. {
  3152. do
  3153. {
  3154. av_packet_unref(avp);
  3155. ret = avcodec_receive_packet(m_pCodecCtx, avp);
  3156. if (! ret)
  3157. {
  3158. if (m_bSegAActive)
  3159. {
  3160. av_write_frame(m_pFormatCtxA, avp);
  3161. }
  3162. if (m_bSegBActive)
  3163. {
  3164. av_write_frame(m_pFormatCtxB, avp);
  3165. }
  3166. };
  3167. }
  3168. while (! ret);
  3169. av_packet_unref(avp);
  3170. av_packet_free(&avp);
  3171. };
  3172. if (m_bSegAActive)
  3173. {
  3174. av_write_trailer(m_pFormatCtxA);
  3175. avformat_close_input(&m_pFormatCtxA);
  3176. m_bSegAActive = false;
  3177. };
  3178. if (m_bSegBActive)
  3179. {
  3180. av_write_trailer(m_pFormatCtxB);
  3181. avformat_close_input(&m_pFormatCtxB);
  3182. m_bSegBActive = false;
  3183. };
  3184. };
  3185. #endif // !SEPARATED_VIDEO_ENCODERS
  3186. #endif // !DIRECT_MJPEG_COMPOSING
  3187. // if( m_pFormat != NULL ) {
  3188. // av_free( m_pFormat );
  3189. //};
  3190. #endif // OPENCV_VIDEO_OUTPUT
  3191. }
  3192. bool streamHandler::openSegmentA(void)
  3193. {
  3194. if (! m_bSegAActive)
  3195. {
  3196. CSTR tAM = getUniqID_MD5();
  3197. if (tAM)
  3198. {
  3199. sprintf(m_sStreamSegmentInfo[0].scratchPath, "%s/%s.%s", m_pVideoScratchPath, tAM, VIDEO_OUTPUT_CONTAINER);
  3200. strncpy(m_sStreamSegmentInfo[0].recordMD5, tAM, 32);
  3201. m_sStreamSegmentInfo[0].recordMD5[32] = (char) 0;
  3202. free(tAM);
  3203. m_sStreamSegmentInfo[0].stream_id = m_sStreamData.id;
  3204. PF_TIMESTAMP ts;
  3205. getPFTS(&ts);
  3206. m_sStreamSegmentInfo[0].streamTSS = ts.seconds;
  3207. m_sStreamSegmentInfo[0].streamTMS = ts.milliseconds;
  3208. #ifdef DEBUG_EXTRA
  3209. char tB[128];
  3210. sprintf(tB, "A opened %d.%d", m_sStreamSegmentInfo[0].streamTSS, m_sStreamSegmentInfo[0].streamTMS);
  3211. LOG(tB);
  3212. #endif
  3213. strncpy(m_sStreamSegmentInfo[0].recordEXT, VIDEO_OUTPUT_CONTAINER, 6);
  3214. m_sStreamSegmentInfo[0].streamFPS = m_sStreamData.fps;
  3215. m_sStreamSegmentInfo[0].streamWidth = m_pMaster->getWidh();
  3216. m_sStreamSegmentInfo[0].streamHeight = m_pMaster->getHeight();
  3217. m_sStreamSegmentInfo[0].eventIDS.clear();
  3218. m_sStreamSegmentInfo[0].eventTSS.clear();
  3219. m_sStreamSegmentInfo[0].eventTMS.clear();
  3220. m_sStreamSegmentInfo[0].eventFrames.clear();
  3221. m_sStreamSegmentInfo[0].dataSize = 0;
  3222. m_sStreamSegmentInfo[0].db_id = 0;
  3223. #ifdef DIRECT_MJPEG_COMPOSING
  3224. m_pOutputFD[0] = fopen(m_sStreamSegmentInfo[0].scratchPath, "wb+");
  3225. m_nSegAFrameCounter = 0;
  3226. m_nSegAShiftFrames = 0;
  3227. m_bSegAActive = true;
  3228. m_bSegADelete = true;
  3229. #else // !DIRECT_MJPEG_COMPOSING
  3230. #ifdef OPENCV_VIDEO_OUTPUT
  3231. if (m_pEncoderA)
  3232. {
  3233. // int fourcc = cv::VideoWriter::fourcc('H', '2', '6', '4');
  3234. int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
  3235. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  3236. m_bAFrameSkeepDivider = m_bWebFrameSkeepDivider;
  3237. m_bAOutputFPS = m_bInitialOutputFPS / m_bAFrameSkeepDivider;
  3238. double fps = m_bAOutputFPS;
  3239. #else
  3240. double fps = m_sStreamData.fps;
  3241. #endif
  3242. cv::String mn(m_sStreamSegmentInfo[0].scratchPath);
  3243. cv::Size fz(m_sStreamData.width, m_sStreamData.height);
  3244. // open (const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
  3245. if (! m_pEncoderA->open(mn, fourcc, fps, fz))
  3246. {
  3247. LOG_ERROR("Cannot Open Output Segment A!");
  3248. return false;
  3249. #ifdef DEBUG_EXTRA
  3250. }
  3251. else
  3252. {
  3253. char tB[256];
  3254. sprintf(tB, "VideoStream A is Created [%s] %.2f fps", m_sStreamSegmentInfo[0].scratchPath, fps);
  3255. LOG(tB);
  3256. #endif
  3257. };
  3258. m_bSegAActive = true;
  3259. m_bSegADelete = true;
  3260. m_nSegAFrameCounter = 0;
  3261. m_nSegAShiftFrames = 0;
  3262. }
  3263. else
  3264. {
  3265. LOG_ERROR("No Encoder(A) Object!");
  3266. return false;
  3267. };
  3268. #else // !OPENCV_VIDEO_OUTPUT
  3269. // Check if Encoder is Ready
  3270. if (m_pFormat == NULL)
  3271. {
  3272. return false;
  3273. }
  3274. #ifdef SEPARATED_VIDEO_ENCODERS
  3275. if (avformat_alloc_output_context2(&m_pFormatCtxA, m_pFormat, NULL, "test." VIDEO_OUTPUT_CONTAINER) < 0)
  3276. {
  3277. LOG_ERROR("Format CTX A alloc error!");
  3278. return false;
  3279. }
  3280. else
  3281. {
  3282. av_dict_set(&m_pFormatCtxA->metadata, "Author", "Argus Sec. System", 0);
  3283. m_pFormatCtxA->flags |= AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT;
  3284. };
  3285. m_pVideoStrA = avformat_new_stream(m_pFormatCtxA, m_pVideoACodec);
  3286. if (m_pVideoStrA == NULL)
  3287. {
  3288. avformat_free_context(m_pFormatCtxA);
  3289. return false;
  3290. #ifdef DEBUG_EXTRA
  3291. }
  3292. else
  3293. {
  3294. LOG("VideoStream A is Created");
  3295. #endif
  3296. };
  3297. const AVRational dst_fps = {m_sStreamData.fps, 1};
  3298. m_pVideoStrA->r_frame_rate = m_pVideoStrA->avg_frame_rate = dst_fps;
  3299. m_pVideoStrA->time_base = m_pCodecACtx->time_base;
  3300. avcodec_parameters_from_context(m_pVideoStrA->codecpar, m_pCodecACtx);
  3301. #ifdef DEBUG_EXTRA
  3302. LOG("Opening Stream A");
  3303. LOG(m_cSegNameA);
  3304. char tB[128];
  3305. sprintf(tB, "FFPTS:%llu FNUM:%llu S2KF:%llu", m_pVideoStrA->start_time, m_pVideoStrA->nb_frames, m_pVideoStrA->first_dts);
  3306. LOG(tB);
  3307. sprintf(tB, "CON ST:%llu DUR:%llu", m_pFormatCtxA->start_time, m_pFormatCtxA->duration);
  3308. LOG(tB);
  3309. #endif
  3310. // if (avio_open(&m_pFormatCtxA->pb, m_cSegNameA, AVIO_FLAG_WRITE) < 0) {
  3311. if (avio_open(&m_pFormatCtxA->pb, m_sStreamSegmentInfo[0].scratchPath, AVIO_FLAG_WRITE) < 0)
  3312. {
  3313. LOG_ERROR("Could not open output file.");
  3314. return m_bSegAActive;
  3315. };
  3316. if (avformat_write_header(m_pFormatCtxA, &m_pFormatOpts) < 0)
  3317. {
  3318. LOG_ERROR("Error occurred when writing header to output file.");
  3319. return m_bSegAActive;
  3320. };
  3321. // m_mFrameCounterProtector.lock();
  3322. m_nSegAFrameCounter = 0;
  3323. m_nSegAShiftFrames = 0;
  3324. m_bSegAActive = true;
  3325. m_bSegADelete = true;
  3326. // m_mFrameCounterProtector.unlock();
  3327. #else // SEPARATED_VIDEO_ENCODERS
  3328. m_nSegASkippedFrames = m_pVideoStrA->nb_frames + 1;
  3329. if (m_pVideoStrA->nb_frames)
  3330. {
  3331. m_pVideoStrA->nb_frames = 0;
  3332. m_pVideoStrA->start_time = 0;
  3333. m_pVideoStrA->first_dts = 0;
  3334. m_pFormatCtxA->start_time = 0;
  3335. };
  3336. #ifdef DEBUG_EXTRA
  3337. LOG("Opening Stream A");
  3338. LOG(m_cSegNameA);
  3339. char tB[128];
  3340. sprintf(tB, "FFPTS:%llu FNUM:%llu S2KF:%llu", m_pVideoStrA->start_time, m_pVideoStrA->nb_frames, m_pVideoStrA->first_dts);
  3341. LOG(tB);
  3342. sprintf(tB, "CON ST:%llu DUR:%llu", m_pFormatCtxA->start_time, m_pFormatCtxA->duration);
  3343. LOG(tB);
  3344. #endif
  3345. if (avio_open(&m_pFormatCtxA->pb, m_cSegNameA, AVIO_FLAG_WRITE) < 0)
  3346. {
  3347. LOG_ERROR("Could not open output file.");
  3348. return m_bSegAActive;
  3349. };
  3350. if (avformat_write_header(m_pFormatCtxA, &m_pFormatOpts) < 0)
  3351. {
  3352. LOG_ERROR("Error occurred when writing header to output file.");
  3353. return m_bSegAActive;
  3354. };
  3355. // m_mFrameCounterProtector.lock();
  3356. m_nSegAFrameCounter = 0;
  3357. m_nSegAShiftFrames = 0;
  3358. m_bSegAActive = true;
  3359. m_bSegADelete = true;
  3360. // m_mFrameCounterProtector.unlock();
  3361. #endif // SEPARATED_VIDEO_ENCODERS
  3362. #ifdef DEBUG_EXTRA
  3363. LOG("Stream A Opened");
  3364. #endif
  3365. #endif // !OPENCV_VIDEO_OUTPUT
  3366. #endif // !DIRECT_MJPEG_COMPOSING
  3367. };
  3368. };
  3369. return m_bSegAActive;
  3370. }
  3371. bool streamHandler::openSegmentB(void)
  3372. {
  3373. if (! m_bSegBActive)
  3374. {
  3375. CSTR tAM = getUniqID_MD5();
  3376. if (tAM)
  3377. {
  3378. sprintf(m_sStreamSegmentInfo[1].scratchPath, "%s/%s.%s", m_pVideoScratchPath, tAM, VIDEO_OUTPUT_CONTAINER);
  3379. strncpy(m_sStreamSegmentInfo[1].recordMD5, tAM, 32);
  3380. m_sStreamSegmentInfo[1].recordMD5[32] = (char) 0;
  3381. free(tAM);
  3382. m_sStreamSegmentInfo[1].stream_id = m_sStreamData.id;
  3383. PF_TIMESTAMP ts;
  3384. getPFTS(&ts);
  3385. m_sStreamSegmentInfo[1].streamTSS = ts.seconds;
  3386. m_sStreamSegmentInfo[1].streamTMS = ts.milliseconds;
  3387. #ifdef DEBUG_EXTRA
  3388. char tB[128];
  3389. sprintf(tB, "B opened %d.%d", m_sStreamSegmentInfo[1].streamTSS, m_sStreamSegmentInfo[1].streamTMS);
  3390. LOG(tB);
  3391. #endif
  3392. strncpy(m_sStreamSegmentInfo[1].recordEXT, VIDEO_OUTPUT_CONTAINER, 6);
  3393. m_sStreamSegmentInfo[1].streamFPS = m_sStreamData.fps;
  3394. m_sStreamSegmentInfo[1].streamWidth = m_pMaster->getWidh();
  3395. m_sStreamSegmentInfo[1].streamHeight = m_pMaster->getHeight();
  3396. m_sStreamSegmentInfo[1].eventIDS.clear();
  3397. m_sStreamSegmentInfo[1].eventTSS.clear();
  3398. m_sStreamSegmentInfo[1].eventTMS.clear();
  3399. m_sStreamSegmentInfo[1].eventFrames.clear();
  3400. m_sStreamSegmentInfo[1].dataSize = 0;
  3401. m_sStreamSegmentInfo[1].db_id = 0;
  3402. #ifdef DIRECT_MJPEG_COMPOSING
  3403. m_pOutputFD[1] = fopen(m_sStreamSegmentInfo[1].scratchPath, "wb+");
  3404. m_nSegBFrameCounter = 0;
  3405. m_nSegBShiftFrames = 0;
  3406. m_bSegBActive = true;
  3407. m_bSegBDelete = true;
  3408. #else // !DIRECT_MJPEG_COMPOSING
  3409. #ifdef OPENCV_VIDEO_OUTPUT
  3410. if (m_pEncoderB)
  3411. {
  3412. // int fourcc = cv::VideoWriter::fourcc('H', '2', '6', '4');
  3413. int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
  3414. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  3415. m_bBFrameSkeepDivider = m_bWebFrameSkeepDivider;
  3416. m_bBOutputFPS = m_bInitialOutputFPS / m_bBFrameSkeepDivider;
  3417. double fps = m_bBOutputFPS;
  3418. #else
  3419. double fps = m_sStreamData.fps;
  3420. #endif
  3421. cv::String mn(m_sStreamSegmentInfo[1].scratchPath);
  3422. cv::Size fz(m_sStreamData.width, m_sStreamData.height);
  3423. // open (const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
  3424. if (! m_pEncoderB->open(mn, fourcc, fps, fz))
  3425. {
  3426. LOG_ERROR("Cannot Open Output Segment B!");
  3427. return false;
  3428. #ifdef DEBUG_EXTRA
  3429. }
  3430. else
  3431. {
  3432. char tB[256];
  3433. sprintf(tB, "VideoStream B is Created [%s] %.2f fps", m_sStreamSegmentInfo[1].scratchPath, fps);
  3434. LOG(tB);
  3435. #endif
  3436. };
  3437. m_bSegBActive = true;
  3438. m_bSegBDelete = true;
  3439. m_nSegBFrameCounter = 0;
  3440. m_nSegBShiftFrames = 0;
  3441. }
  3442. else
  3443. {
  3444. LOG_ERROR("No Encoder(B) Object!");
  3445. return false;
  3446. };
  3447. #else // !OPENCV
  3448. // Check if Encoder is Ready
  3449. if (m_pFormat == NULL)
  3450. {
  3451. return false;
  3452. }
  3453. #ifdef SEPARATED_VIDEO_ENCODERS
  3454. if (avformat_alloc_output_context2(&m_pFormatCtxB, m_pFormat, NULL, "test." VIDEO_OUTPUT_CONTAINER) < 0)
  3455. {
  3456. LOG_ERROR("Format CTX B alloc error!");
  3457. return false;
  3458. }
  3459. else
  3460. {
  3461. av_dict_set(&m_pFormatCtxB->metadata, "Author", "Argus Sec. System", 0);
  3462. m_pFormatCtxB->flags |= AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT;
  3463. };
  3464. m_pVideoStrB = avformat_new_stream(m_pFormatCtxB, m_pVideoBCodec);
  3465. if (m_pVideoStrB == NULL)
  3466. {
  3467. avformat_free_context(m_pFormatCtxB);
  3468. return false;
  3469. #ifdef DEBUG_EXTRA
  3470. }
  3471. else
  3472. {
  3473. LOG("VideoStream B is Created");
  3474. #endif
  3475. };
  3476. const AVRational dst_fps = {m_sStreamData.fps, 1};
  3477. m_pVideoStrB->r_frame_rate = m_pVideoStrB->avg_frame_rate = dst_fps;
  3478. m_pVideoStrB->time_base = m_pCodecBCtx->time_base;
  3479. avcodec_parameters_from_context(m_pVideoStrB->codecpar, m_pCodecBCtx);
  3480. #ifdef DEBUG_EXTRA
  3481. LOG("Opening Stream B");
  3482. LOG(m_cSegNameB);
  3483. char tB[128];
  3484. sprintf(tB, "FFPTS:%llu FNUM:%llu S2KF:%llu", m_pVideoStrB->start_time, m_pVideoStrB->nb_frames, m_pVideoStrB->first_dts);
  3485. LOG(tB);
  3486. sprintf(tB, "CON ST:%llu DUR:%llu", m_pFormatCtxB->start_time, m_pFormatCtxB->duration);
  3487. LOG(tB);
  3488. #endif
  3489. // if ( avio_open( &m_pFormatCtxB->pb, m_cSegNameB, AVIO_FLAG_WRITE ) < 0 ){
  3490. if (avio_open(&m_pFormatCtxB->pb, m_sStreamSegmentInfo[1].scratchPath, AVIO_FLAG_WRITE) < 0)
  3491. {
  3492. LOG_ERROR("Could not open output file.");
  3493. avformat_free_context(m_pFormatCtxB);
  3494. return false;
  3495. };
  3496. if (avformat_write_header(m_pFormatCtxB, &m_pFormatOpts) < 0)
  3497. {
  3498. LOG_ERROR("Error occurred when writing header to output file.");
  3499. avio_close(m_pFormatCtxB->pb);
  3500. avformat_free_context(m_pFormatCtxB);
  3501. return false;
  3502. };
  3503. // m_mFrameCounterProtector.lock();
  3504. m_nSegBFrameCounter = 0;
  3505. m_nSegBShiftFrames = 0;
  3506. m_bSegBActive = true;
  3507. m_bSegBDelete = true;
  3508. // m_mFrameCounterProtector.unlock();
  3509. #else // SEPARATED_VIDEO_ENCODERS
  3510. m_nSegBSkippedFrames = m_pVideoStrB->nb_frames + 1;
  3511. if (m_pVideoStrB->nb_frames)
  3512. {
  3513. m_pVideoStrB->nb_frames = 0;
  3514. m_pVideoStrB->start_time = 0;
  3515. m_pVideoStrB->first_dts = 0;
  3516. m_pFormatCtxB->start_time = 0;
  3517. };
  3518. if (avio_open(&m_pFormatCtxB->pb, m_cSegNameB, AVIO_FLAG_WRITE) < 0)
  3519. {
  3520. LOG_ERROR("Could not open output file.");
  3521. return m_bSegBActive;
  3522. };
  3523. if (avformat_write_header(m_pFormatCtxB, &m_pFormatOpts) < 0)
  3524. {
  3525. LOG_ERROR("Error occurred when writing header to output file.");
  3526. return m_bSegBActive;
  3527. };
  3528. // m_mFrameCounterProtector.lock();
  3529. m_nSegBFrameCounter = 0;
  3530. m_nSegBShiftFrames = 0;
  3531. m_bSegBActive = true;
  3532. m_bSegBDelete = true;
  3533. // m_mFrameCounterProtector.unlock();
  3534. #endif // SEPARATED_VIDEO_ENCODERS
  3535. #ifdef DEBUG_EXTRA
  3536. LOG("Stream B Opened");
  3537. #endif
  3538. #endif // !OPENCV_VIDEO_OUTPUT
  3539. #endif // !DIRECT_MJPEG_COMPOSING
  3540. };
  3541. };
  3542. return m_bSegBActive;
  3543. }
  3544. bool streamHandler::initVideoOutput()
  3545. {
  3546. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  3547. m_bAFrameSkeepDivider = m_bBFrameSkeepDivider = 1;
  3548. m_bAFrameSkeepDividerCounter = m_bBFrameSkeepDividerCounter = 0;
  3549. m_bCurrentOutputFPS = m_bInitialOutputFPS = m_sStreamData.fps;
  3550. m_nAPreAlarmFrames = (PRE_ALARM_TIME_MILSECONDS * m_bAOutputFPS) / 1000;
  3551. m_nAPostAlarmFrames = (POST_ALARM_TIME_MILSECONDS * m_bAOutputFPS) / 1000;
  3552. m_nASegmentLimitFrames = (ENCODING_SEGMENT_MILSECONDS * m_bAOutputFPS) / 1000;
  3553. m_nAHardLimitFrames = (ENCODING_SEGMENT_LIMIT_MILSECONDS * m_bAOutputFPS) / 1000;
  3554. m_nBPreAlarmFrames = (PRE_ALARM_TIME_MILSECONDS * m_bBOutputFPS) / 1000;
  3555. m_nBPostAlarmFrames = (POST_ALARM_TIME_MILSECONDS * m_bBOutputFPS) / 1000;
  3556. m_nBSegmentLimitFrames = (ENCODING_SEGMENT_MILSECONDS * m_bBOutputFPS) / 1000;
  3557. m_nBHardLimitFrames = (ENCODING_SEGMENT_LIMIT_MILSECONDS * m_bBOutputFPS) / 1000;
  3558. #else
  3559. m_nPreAlarmFrames = (PRE_ALARM_TIME_MILSECONDS * m_sStreamData.fps) / 1000;
  3560. m_nPostAlarmFrames = (POST_ALARM_TIME_MILSECONDS * m_sStreamData.fps) / 1000;
  3561. m_nSegmentLimitFrames = (ENCODING_SEGMENT_MILSECONDS * m_sStreamData.fps) / 1000;
  3562. m_nHardLimitFrames = (ENCODING_SEGMENT_LIMIT_MILSECONDS * m_sStreamData.fps) / 1000;
  3563. #endif
  3564. #ifndef DIRECT_MJPEG_COMPOSING
  3565. #ifdef OPENCV_VIDEO_OUTPUT
  3566. m_pEncoderA = new cv::VideoWriter();
  3567. if (m_pEncoderA == NULL)
  3568. {
  3569. LOG_ERROR("Encoder A Create Failure!");
  3570. return false;
  3571. };
  3572. m_pEncoderA->set(cv::VIDEOWRITER_PROP_QUALITY, 10.0);
  3573. m_pEncoderA->set(cv::VIDEOWRITER_PROP_NSTRIPES, -1.0);
  3574. m_pEncoderB = new cv::VideoWriter();
  3575. if (m_pEncoderA == NULL)
  3576. {
  3577. LOG_ERROR("Encoder B Create Failure!");
  3578. return false;
  3579. };
  3580. m_pEncoderB->set(cv::VIDEOWRITER_PROP_QUALITY, 10.0);
  3581. m_pEncoderB->set(cv::VIDEOWRITER_PROP_NSTRIPES, -1.0);
  3582. #else
  3583. if (! initLibAV())
  3584. {
  3585. return false;
  3586. }
  3587. m_pFormat = av_guess_format(VIDEO_OUTPUT_CONTAINER, "test." VIDEO_OUTPUT_CONTAINER, NULL);
  3588. if (m_pFormat == NULL)
  3589. {
  3590. LOG_ERROR("Out VideoFormat Not Found!");
  3591. return false;
  3592. #ifdef DEBUG_EXTRA
  3593. }
  3594. else
  3595. {
  3596. LOG("Output video Format");
  3597. LOG(VIDEO_OUTPUT_CONTAINER);
  3598. #endif
  3599. };
  3600. #ifdef SEPARATED_VIDEO_ENCODERS
  3601. /* Stream A */
  3602. m_pVideoACodec = avcodec_find_encoder(VIDEO_OUTPUT_CODEC);
  3603. if (m_pVideoACodec == NULL)
  3604. {
  3605. LOG_ERROR("Encoder Video Codec Not Found!");
  3606. return false;
  3607. #ifdef DEBUG_EXTRA
  3608. }
  3609. else
  3610. {
  3611. LOG("Video Encoder A Created");
  3612. #endif
  3613. };
  3614. m_pCodecACtx = avcodec_alloc_context3(m_pVideoACodec);
  3615. if (m_pCodecACtx == NULL)
  3616. {
  3617. LOG_ERROR("Video Encoder A Context Memory Allocation Error!");
  3618. av_free(m_pVideoACodec);
  3619. avformat_free_context(m_pFormatCtxA);
  3620. return false;
  3621. #ifdef DEBUG_EXTRA
  3622. }
  3623. else
  3624. {
  3625. LOG("Video Encoder Context A Allocated");
  3626. #endif
  3627. };
  3628. /* Stream B */
  3629. m_pVideoBCodec = avcodec_find_encoder(VIDEO_OUTPUT_CODEC);
  3630. if (m_pVideoBCodec == NULL)
  3631. {
  3632. LOG_ERROR("Out VideoCodec Not Found!");
  3633. av_free(m_pVideoACodec);
  3634. return false;
  3635. #ifdef DEBUG_EXTRA
  3636. }
  3637. else
  3638. {
  3639. LOG("Video Encoder B Created");
  3640. #endif
  3641. };
  3642. m_pCodecBCtx = avcodec_alloc_context3(m_pVideoBCodec);
  3643. if (m_pCodecBCtx == NULL)
  3644. {
  3645. LOG_ERROR("Video Encoder B Context Memory Allocation Error!");
  3646. av_free(m_pVideoACodec);
  3647. av_free(m_pVideoBCodec);
  3648. avformat_free_context(m_pFormatCtxA);
  3649. avformat_free_context(m_pFormatCtxB);
  3650. return false;
  3651. #ifdef DEBUG_EXTRA
  3652. }
  3653. else
  3654. {
  3655. LOG("Video Encoder Context B Allocated");
  3656. #endif
  3657. };
  3658. m_pCodecACtx->codec_id = VIDEO_OUTPUT_CODEC;
  3659. m_pCodecACtx->codec_type = AVMEDIA_TYPE_VIDEO;
  3660. m_pCodecACtx->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  3661. m_pCodecACtx->bit_rate = VIDEO_OUTPUT_BITRATE;
  3662. m_pCodecACtx->max_b_frames = 1;
  3663. m_pCodecACtx->width = m_pMaster->m_sEmbedVideoData.width;
  3664. m_pCodecACtx->height = m_pMaster->m_sEmbedVideoData.height;
  3665. m_pCodecACtx->time_base.num = 1;
  3666. m_pCodecACtx->time_base.den = m_sStreamData.fps;
  3667. m_pCodecACtx->framerate.num = m_sStreamData.fps;
  3668. m_pCodecACtx->framerate.den = 1;
  3669. m_pCodecBCtx->codec_id = VIDEO_OUTPUT_CODEC;
  3670. m_pCodecBCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  3671. m_pCodecBCtx->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  3672. m_pCodecBCtx->bit_rate = VIDEO_OUTPUT_BITRATE;
  3673. m_pCodecBCtx->max_b_frames = 1;
  3674. m_pCodecBCtx->width = m_pMaster->m_sEmbedVideoData.width;
  3675. m_pCodecBCtx->height = m_pMaster->m_sEmbedVideoData.height;
  3676. m_pCodecBCtx->time_base.num = 1;
  3677. m_pCodecBCtx->time_base.den = m_sStreamData.fps;
  3678. m_pCodecBCtx->framerate.num = m_sStreamData.fps;
  3679. m_pCodecBCtx->framerate.den = 1;
  3680. // https://pastebin.com/yG37YhWW
  3681. // https://stackoverflow.com/questions/49276997/raw-h264-frames-in-mp4-container-using-libavcodec
  3682. // G@ avcodec encode NALU mp4
  3683. #if (LIBAVCODEC_VERSION_MAJOR > 54)
  3684. ////m_pCodecACtx->codec_tag = av_codec_get_tag( m_pFormatCtxA->oformat->codec_tag, VIDEO_OUTPUT_CODEC );
  3685. #endif
  3686. #if (LIBAVCODEC_VERSION_MAJOR > 54)
  3687. ////m_pCodecBCtx->codec_tag = av_codec_get_tag( m_pFormatCtxB->oformat->codec_tag, VIDEO_OUTPUT_CODEC );
  3688. #endif
  3689. m_pFormatOpts = NULL;
  3690. // https://stackoverflow.com/questions/70060148/libav-producing-mp4-file-with-extremely-high-frame-rate
  3691. if ((m_pCodecACtx->codec_id == AV_CODEC_ID_H264) || (m_pCodecACtx->codec_id == AV_CODEC_ID_H265))
  3692. {
  3693. #ifdef DEBUG_EXTRA
  3694. LOG("Begin Options!");
  3695. #endif
  3696. // Reference for AvFormatContext options : https://ffmpeg.org/doxygen/2.8/movenc_8c_source.html
  3697. // Set format's privater options, to be passed to avformat_write_header()
  3698. /*
  3699. mp4 muxer AVOptions:
  3700. -movflags E..... MOV muxer flags (default 0)
  3701. rtphint E..... Add RTP hint tracks
  3702. empty_moov E..... Make the initial moov atom empty (not supported by QuickTime)
  3703. frag_keyframe E..... Fragment at video keyframes
  3704. separate_moof E..... Write separate moof/mdat atoms for each track
  3705. frag_custom E..... Flush fragments on caller requests
  3706. isml E..... Create a live smooth streaming feed (for pushing to a publishing point)
  3707. faststart E..... Run a second pass to put the index (moov atom) at the beginning of the file
  3708. omit_tfhd_offset E..... Omit the base data offset in tfhd atoms
  3709. delay_moov ??
  3710. -moov_size E..... maximum moov size so it can be placed at the begin (from 0 to INT_MAX) (default 0)
  3711. -rtpflags E..... RTP muxer flags (default 0)
  3712. latm E..... Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC
  3713. rfc2190 E..... Use RFC 2190 packetization instead of RFC 4629 for H.263
  3714. skip_rtcp E..... Don't send RTCP sender reports
  3715. h264_mode0 E..... Use mode 0 for H264 in RTP
  3716. send_bye E..... Send RTCP BYE packets when finishing
  3717. -skip_iods E..... Skip writing iods atom. (from 0 to 1) (default 1)
  3718. -iods_audio_profile E..... iods audio profile atom. (from -1 to 255) (default -1)
  3719. -iods_video_profile E..... iods video profile atom. (from -1 to 255) (default -1)
  3720. -frag_duration E..... Maximum fragment duration (from 0 to INT_MAX) (default 0)
  3721. -min_frag_duration E..... Minimum fragment duration (from 0 to INT_MAX) (default 0)
  3722. -frag_size E..... Maximum fragment size (from 0 to INT_MAX) (default 0)
  3723. -ism_lookahead E..... Number of lookahead entries for ISM files (from 0 to INT_MAX) (default 0)
  3724. -use_editlist E..... use edit list (from -1 to 1) (default -1)
  3725. -video_track_timescale E..... set timescale of all video tracks (from 0 to INT_MAX) (default 0)
  3726. */
  3727. if (av_dict_set(&m_pFormatOpts, "movflags", "isml+faststart+frag_keyframe+empty_moov+omit_tfhd_offset", 0) < 0)
  3728. {
  3729. LOG_ERROR("av_dict_set movflags!");
  3730. #ifdef DEBUG_EXTRA
  3731. }
  3732. else
  3733. {
  3734. LOG("MOV FLAG SET!");
  3735. #endif
  3736. };
  3737. // if( av_dict_set( &m_pFormatOpts, "live", "1", 0 ) < 0 ){
  3738. // LOG_ERROR( "av_dict_set live!" );
  3739. // };
  3740. // default brand is "isom", which fails on some devices
  3741. if (av_dict_set(&m_pFormatOpts, "brand", "mp42", 0) < 0)
  3742. {
  3743. LOG_ERROR("av_dict_set brand!");
  3744. };
  3745. // m_pCodecCtx->max_b_frames = 1;
  3746. // m_pCodecCtx->level = 31;
  3747. m_pCodecACtx->gop_size = m_sStreamData.fps << 1; // VIDEO_OUTPUT_GOP; // emit one intra frame every X frames
  3748. /// P-frames, generated by referencing data from prev and future frames.
  3749. /// [Compression up, CPU usage up]
  3750. /// [use 3/gop]
  3751. m_pCodecACtx->max_b_frames = 5;
  3752. /// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
  3753. /// [use 3?5 ref per P]
  3754. m_pCodecACtx->refs = 3;
  3755. /// Compression efficiency (slower -> better quality + higher cpu%)
  3756. /// [ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow]
  3757. /// Set this option to "ultrafast" is critical for realtime encoding
  3758. av_opt_set(m_pCodecACtx->priv_data, "preset", "ultrafast", 0);
  3759. // av_opt_set( m_pCodecCtx->priv_data, "preset", "fast", 0 );
  3760. // av_opt_set( m_pCodecCtx->priv_data, "preset", "slow", 0 );
  3761. /// Compression rate (lower -> higher compression) compress to lower size, makes decoded image more noisy
  3762. /// Range: [0; 51], sane range: [18; 26]. I used 35 as good compression/quality compromise. This option also critical for realtime encoding
  3763. av_opt_set(m_pCodecACtx->priv_data, "crf", "32", 0);
  3764. /// Change settings based upon the specifics of input
  3765. /// [psnr, ssim, grain, zerolatency, fastdecode, animation]
  3766. /// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
  3767. av_opt_set(m_pCodecACtx->priv_data, "tune", "zerolatency", 0);
  3768. /// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
  3769. /// Because Quicktime (ie. iOS) doesn't support this option
  3770. av_opt_set(m_pCodecACtx->priv_data, "b-pyramid", "0", 0);
  3771. #ifdef DEBUG_EXTRA
  3772. LOG("End Options!");
  3773. #endif
  3774. }
  3775. else
  3776. {
  3777. if (m_pFormat->flags & AVFMT_GLOBALHEADER)
  3778. {
  3779. m_pCodecACtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  3780. }
  3781. };
  3782. if ((m_pCodecBCtx->codec_id == AV_CODEC_ID_H264) || (m_pCodecBCtx->codec_id == AV_CODEC_ID_H265))
  3783. {
  3784. #ifdef DEBUG_EXTRA
  3785. LOG("Begin Options!");
  3786. #endif
  3787. // Reference for AvFormatContext options : https://ffmpeg.org/doxygen/2.8/movenc_8c_source.html
  3788. // Set format's privater options, to be passed to avformat_write_header()
  3789. /*
  3790. mp4 muxer AVOptions:
  3791. -movflags E..... MOV muxer flags (default 0)
  3792. rtphint E..... Add RTP hint tracks
  3793. empty_moov E..... Make the initial moov atom empty (not supported by QuickTime)
  3794. frag_keyframe E..... Fragment at video keyframes
  3795. separate_moof E..... Write separate moof/mdat atoms for each track
  3796. frag_custom E..... Flush fragments on caller requests
  3797. isml E..... Create a live smooth streaming feed (for pushing to a publishing point)
  3798. faststart E..... Run a second pass to put the index (moov atom) at the beginning of the file
  3799. omit_tfhd_offset E..... Omit the base data offset in tfhd atoms
  3800. delay_moov ??
  3801. -moov_size E..... maximum moov size so it can be placed at the begin (from 0 to INT_MAX) (default 0)
  3802. -rtpflags E..... RTP muxer flags (default 0)
  3803. latm E..... Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC
  3804. rfc2190 E..... Use RFC 2190 packetization instead of RFC 4629 for H.263
  3805. skip_rtcp E..... Don't send RTCP sender reports
  3806. h264_mode0 E..... Use mode 0 for H264 in RTP
  3807. send_bye E..... Send RTCP BYE packets when finishing
  3808. -skip_iods E..... Skip writing iods atom. (from 0 to 1) (default 1)
  3809. -iods_audio_profile E..... iods audio profile atom. (from -1 to 255) (default -1)
  3810. -iods_video_profile E..... iods video profile atom. (from -1 to 255) (default -1)
  3811. -frag_duration E..... Maximum fragment duration (from 0 to INT_MAX) (default 0)
  3812. -min_frag_duration E..... Minimum fragment duration (from 0 to INT_MAX) (default 0)
  3813. -frag_size E..... Maximum fragment size (from 0 to INT_MAX) (default 0)
  3814. -ism_lookahead E..... Number of lookahead entries for ISM files (from 0 to INT_MAX) (default 0)
  3815. -use_editlist E..... use edit list (from -1 to 1) (default -1)
  3816. -video_track_timescale E..... set timescale of all video tracks (from 0 to INT_MAX) (default 0)
  3817. */
  3818. // m_pCodecCtx->max_b_frames = 1;
  3819. // m_pCodecCtx->level = 31;
  3820. m_pCodecBCtx->gop_size = m_sStreamData.fps << 1; // VIDEO_OUTPUT_GOP; // emit one intra frame every X frames
  3821. /// P-frames, generated by referencing data from prev and future frames.
  3822. /// [Compression up, CPU usage up]
  3823. /// [use 3/gop]
  3824. m_pCodecBCtx->max_b_frames = 5;
  3825. /// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
  3826. /// [use 3?5 ref per P]
  3827. m_pCodecBCtx->refs = 3;
  3828. /// Compression efficiency (slower -> better quality + higher cpu%)
  3829. /// [ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow]
  3830. /// Set this option to "ultrafast" is critical for realtime encoding
  3831. av_opt_set(m_pCodecBCtx->priv_data, "preset", "ultrafast", 0);
  3832. // av_opt_set( m_pCodecCtx->priv_data, "preset", "fast", 0 );
  3833. // av_opt_set( m_pCodecCtx->priv_data, "preset", "slow", 0 );
  3834. /// Compression rate (lower -> higher compression) compress to lower size, makes decoded image more noisy
  3835. /// Range: [0; 51], sane range: [18; 26]. I used 35 as good compression/quality compromise. This option also critical for realtime encoding
  3836. av_opt_set(m_pCodecBCtx->priv_data, "crf", "32", 0);
  3837. /// Change settings based upon the specifics of input
  3838. /// [psnr, ssim, grain, zerolatency, fastdecode, animation]
  3839. /// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
  3840. av_opt_set(m_pCodecBCtx->priv_data, "tune", "zerolatency", 0);
  3841. /// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
  3842. /// Because Quicktime (ie. iOS) doesn't support this option
  3843. av_opt_set(m_pCodecBCtx->priv_data, "b-pyramid", "0", 0);
  3844. #ifdef DEBUG_EXTRA
  3845. LOG("End Options!");
  3846. #endif
  3847. }
  3848. else
  3849. {
  3850. if (m_pFormat->flags & AVFMT_GLOBALHEADER)
  3851. {
  3852. m_pCodecBCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  3853. }
  3854. };
  3855. const AVRational dst_fps = {m_sStreamData.fps, 1};
  3856. #ifdef DEBUG_EXTRA
  3857. char TB[129];
  3858. sprintf(TB, "TimeBaseA: num[%d]/den[%d]", m_pCodecACtx->time_base.num, m_pCodecACtx->time_base.den);
  3859. LOG(TB);
  3860. sprintf(TB, "TimeBaseB: num[%d]/den[%d]", m_pCodecBCtx->time_base.num, m_pCodecBCtx->time_base.den);
  3861. LOG(TB);
  3862. #endif
  3863. ////m_pVideoStrA->time_base = m_pCodecACtx->time_base;
  3864. ////m_pVideoStrB->time_base = m_pCodecBCtx->time_base;
  3865. // avcodec_get_context_defaults3( m_pVideoStrA->codec, m_pVideoCodec );
  3866. // m_pVideoStrA->codec->width = m_pMaster->m_sEmbedVideoData.width;
  3867. // m_pVideoStrA->codec->height = m_pMaster->m_sEmbedVideoData.height;
  3868. // m_pVideoStrA->codec->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  3869. // m_pVideoStrA->codec->time_base = m_pVideoStrB->time_base = av_inv_q(dst_fps);
  3870. //// m_pVideoStrA->r_frame_rate = m_pVideoStrA->avg_frame_rate =
  3871. ////dst_fps;
  3872. // avcodec_get_context_defaults3( m_pVideoStrB->codec, m_pVideoCodec );
  3873. // m_pVideoStrB->codec->width = m_pMaster->m_sEmbedVideoData.width;
  3874. // m_pVideoStrB->codec->height = m_pMaster->m_sEmbedVideoData.height;
  3875. // m_pVideoStrB->codec->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  3876. // m_pVideoStrB->codec->time_base = m_pVideoStrB->time_base = av_inv_q(dst_fps);
  3877. //// m_pVideoStrB->r_frame_rate = m_pVideoStrB->avg_frame_rate =
  3878. ////dst_fps;
  3879. // if ( m_pFormatCtxA->oformat->flags & AVFMT_GLOBALHEADER ) m_pVideoStrA->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  3880. // if ( m_pFormatCtxB->oformat->flags & AVFMT_GLOBALHEADER ) m_pVideoStrB->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  3881. //// avcodec_parameters_from_context( m_pVideoStrA->codecpar,
  3882. ////m_pCodecACtx );
  3883. // m_pVideoStrA->codecpar->format = VIDEO_OUTPUT_PIX_FMT;
  3884. // m_pVideoStrA->codecpar->extradata = m_pCodecCtx->extradata;
  3885. // m_pVideoStrA->codecpar->extradata_size = m_pCodecCtx->extradata_size;
  3886. //// avcodec_parameters_from_context( m_pVideoStrB->codecpar,
  3887. ////m_pCodecBCtx );
  3888. // m_pVideoStrB->codecpar->format = VIDEO_OUTPUT_PIX_FMT;
  3889. // m_pVideoStrB->codecpar->extradata = m_pCodecCtx->extradata;
  3890. // m_pVideoStrB->codecpar->extradata_size = m_pCodecCtx->extradata_size;
  3891. if (avcodec_is_open(m_pCodecACtx) == 0)
  3892. {
  3893. LOG("output codec context is closed, opening");
  3894. if (avcodec_open2(m_pCodecACtx, m_pVideoACodec, NULL) < 0)
  3895. {
  3896. LOG_ERROR("Error opening2 encoder");
  3897. return false;
  3898. #ifdef DEBUG_EXTRA
  3899. }
  3900. else
  3901. {
  3902. LOG("output codec context is opened");
  3903. #endif
  3904. };
  3905. #ifdef DEBUG_EXTRA
  3906. }
  3907. else
  3908. {
  3909. LOG("output codec context is already opened");
  3910. #endif
  3911. };
  3912. if (avcodec_is_open(m_pCodecBCtx) == 0)
  3913. {
  3914. LOG("output codec context is closed, opening");
  3915. if (avcodec_open2(m_pCodecBCtx, m_pVideoBCodec, NULL) < 0)
  3916. {
  3917. LOG_ERROR("Error opening2 encoder");
  3918. return false;
  3919. #ifdef DEBUG_EXTRA
  3920. }
  3921. else
  3922. {
  3923. LOG("output codec context is opened");
  3924. #endif
  3925. };
  3926. #ifdef DEBUG_EXTRA
  3927. }
  3928. else
  3929. {
  3930. LOG("output codec context is already opened");
  3931. #endif
  3932. };
  3933. #else // SEPARATED_VIDEO_ENCODERS
  3934. if (avformat_alloc_output_context2(&m_pFormatCtxA, m_pFormat, NULL, "test." VIDEO_OUTPUT_CONTAINER) < 0)
  3935. {
  3936. LOG_ERROR("Format CTX A alloc error!");
  3937. return false;
  3938. }
  3939. else
  3940. {
  3941. av_dict_set(&m_pFormatCtxA->metadata, "Author", "Argus Sec. System", 0);
  3942. m_pFormatCtxA->flags |= AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT;
  3943. };
  3944. if (avformat_alloc_output_context2(&m_pFormatCtxB, m_pFormat, NULL, "test." VIDEO_OUTPUT_CONTAINER) < 0)
  3945. {
  3946. LOG_ERROR("Format CTX B alloc error!");
  3947. avformat_free_context(m_pFormatCtxA);
  3948. return false;
  3949. }
  3950. else
  3951. {
  3952. av_dict_set(&m_pFormatCtxB->metadata, "Author", "Argus Sec. System", 0);
  3953. m_pFormatCtxB->flags |= AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT;
  3954. };
  3955. m_pVideoCodec = avcodec_find_encoder(VIDEO_OUTPUT_CODEC);
  3956. if (m_pVideoCodec == NULL)
  3957. {
  3958. LOG_ERROR("Output VideoCodec Not Found!");
  3959. avformat_free_context(m_pFormatCtxA);
  3960. avformat_free_context(m_pFormatCtxB);
  3961. return false;
  3962. #ifdef DEBUG_EXTRA
  3963. }
  3964. else
  3965. {
  3966. LOG("Output Video Codec Found");
  3967. #endif
  3968. };
  3969. m_pCodecCtx = avcodec_alloc_context3(m_pVideoCodec);
  3970. if (m_pCodecCtx == NULL)
  3971. {
  3972. LOG_ERROR("Out VideoCodec Context Memory Allocation Error");
  3973. av_free(m_pVideoCodec);
  3974. avformat_free_context(m_pFormatCtxA);
  3975. avformat_free_context(m_pFormatCtxB);
  3976. return false;
  3977. #ifdef DEBUG_EXTRA
  3978. }
  3979. else
  3980. {
  3981. LOG("Out Codec Context3 Allocated");
  3982. #endif
  3983. };
  3984. m_pVideoStrA = avformat_new_stream(m_pFormatCtxA, m_pVideoCodec);
  3985. if (m_pVideoStrA == NULL)
  3986. {
  3987. LOG_ERROR("VideoStream A Not Created!");
  3988. avcodec_free_context(&m_pCodecCtx);
  3989. av_free(m_pCodecCtx);
  3990. av_free(m_pVideoCodec);
  3991. // avformat_close_input ( &m_pFormatCtxA );
  3992. // avformat_close_input ( &m_pFormatCtxB );
  3993. avformat_free_context(m_pFormatCtxA);
  3994. avformat_free_context(m_pFormatCtxB);
  3995. return false;
  3996. #ifdef DEBUG_EXTRA
  3997. }
  3998. else
  3999. {
  4000. LOG("VideoStream A is Created");
  4001. #endif
  4002. };
  4003. m_pVideoStrB = avformat_new_stream(m_pFormatCtxB, m_pVideoCodec);
  4004. if (m_pVideoStrB == NULL)
  4005. {
  4006. LOG_ERROR("VideoStream B Not Created!");
  4007. avcodec_free_context(&m_pCodecCtx);
  4008. av_free(m_pCodecCtx);
  4009. av_free(m_pVideoCodec);
  4010. // avformat_close_input ( &m_pFormatCtxA );
  4011. // avformat_close_input ( &m_pFormatCtxB );
  4012. avformat_free_context(m_pFormatCtxA);
  4013. avformat_free_context(m_pFormatCtxB);
  4014. return false;
  4015. #ifdef DEBUG_EXTRA
  4016. }
  4017. else
  4018. {
  4019. LOG("VideoStream B is Created");
  4020. #endif
  4021. };
  4022. m_pCodecCtx->codec_id = VIDEO_OUTPUT_CODEC;
  4023. m_pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  4024. m_pCodecCtx->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  4025. m_pCodecCtx->bit_rate = VIDEO_OUTPUT_BITRATE;
  4026. // m_pCodecCtx->thread_count = 1;
  4027. // m_pCodecCtx->bit_rate_tolerance = 1;
  4028. // m_pCodecCtx->global_quality = 100;
  4029. // m_pCodecCtx->compression_level = 2;
  4030. m_pCodecCtx->max_b_frames = 1;
  4031. m_pCodecCtx->width = m_pMaster->m_sEmbedVideoData.width;
  4032. m_pCodecCtx->height = m_pMaster->m_sEmbedVideoData.height;
  4033. m_pCodecCtx->time_base.num = 1;
  4034. m_pCodecCtx->time_base.den = m_sStreamData.fps;
  4035. m_pCodecCtx->framerate.num = m_sStreamData.fps;
  4036. m_pCodecCtx->framerate.den = 1;
  4037. // https://pastebin.com/yG37YhWW
  4038. // https://stackoverflow.com/questions/49276997/raw-h264-frames-in-mp4-container-using-libavcodec
  4039. // G@ avcodec encode NALU mp4
  4040. #if (LIBAVCODEC_VERSION_MAJOR > 54)
  4041. m_pCodecCtx->codec_tag = av_codec_get_tag(m_pFormatCtxA->oformat->codec_tag, VIDEO_OUTPUT_CODEC);
  4042. #endif
  4043. m_pFormatOpts = NULL;
  4044. // https://stackoverflow.com/questions/70060148/libav-producing-mp4-file-with-extremely-high-frame-rate
  4045. if ((m_pCodecCtx->codec_id == AV_CODEC_ID_H264) || (m_pCodecCtx->codec_id == AV_CODEC_ID_H265))
  4046. {
  4047. #ifdef DEBUG_EXTRA
  4048. LOG("Begin Options!");
  4049. #endif
  4050. // Reference for AvFormatContext options : https://ffmpeg.org/doxygen/2.8/movenc_8c_source.html
  4051. // Set format's privater options, to be passed to avformat_write_header()
  4052. /*
  4053. mp4 muxer AVOptions:
  4054. -movflags E..... MOV muxer flags (default 0)
  4055. rtphint E..... Add RTP hint tracks
  4056. empty_moov E..... Make the initial moov atom empty (not supported by QuickTime)
  4057. frag_keyframe E..... Fragment at video keyframes
  4058. separate_moof E..... Write separate moof/mdat atoms for each track
  4059. frag_custom E..... Flush fragments on caller requests
  4060. isml E..... Create a live smooth streaming feed (for pushing to a publishing point)
  4061. faststart E..... Run a second pass to put the index (moov atom) at the beginning of the file
  4062. omit_tfhd_offset E..... Omit the base data offset in tfhd atoms
  4063. delay_moov ??
  4064. -moov_size E..... maximum moov size so it can be placed at the begin (from 0 to INT_MAX) (default 0)
  4065. -rtpflags E..... RTP muxer flags (default 0)
  4066. latm E..... Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC
  4067. rfc2190 E..... Use RFC 2190 packetization instead of RFC 4629 for H.263
  4068. skip_rtcp E..... Don't send RTCP sender reports
  4069. h264_mode0 E..... Use mode 0 for H264 in RTP
  4070. send_bye E..... Send RTCP BYE packets when finishing
  4071. -skip_iods E..... Skip writing iods atom. (from 0 to 1) (default 1)
  4072. -iods_audio_profile E..... iods audio profile atom. (from -1 to 255) (default -1)
  4073. -iods_video_profile E..... iods video profile atom. (from -1 to 255) (default -1)
  4074. -frag_duration E..... Maximum fragment duration (from 0 to INT_MAX) (default 0)
  4075. -min_frag_duration E..... Minimum fragment duration (from 0 to INT_MAX) (default 0)
  4076. -frag_size E..... Maximum fragment size (from 0 to INT_MAX) (default 0)
  4077. -ism_lookahead E..... Number of lookahead entries for ISM files (from 0 to INT_MAX) (default 0)
  4078. -use_editlist E..... use edit list (from -1 to 1) (default -1)
  4079. -video_track_timescale E..... set timescale of all video tracks (from 0 to INT_MAX) (default 0)
  4080. */
  4081. if (av_dict_set(&m_pFormatOpts, "movflags", "isml+faststart+frag_keyframe+empty_moov+omit_tfhd_offset", 0) < 0)
  4082. {
  4083. LOG_ERROR("av_dict_set movflags!");
  4084. #ifdef DEBUG_EXTRA
  4085. }
  4086. else
  4087. {
  4088. LOG("MOV FLAG SET!");
  4089. #endif
  4090. };
  4091. // if( av_dict_set( &m_pFormatOpts, "live", "1", 0 ) < 0 ){
  4092. // LOG_ERROR( "av_dict_set live!" );
  4093. // };
  4094. // default brand is "isom", which fails on some devices
  4095. if (av_dict_set(&m_pFormatOpts, "brand", "mp42", 0) < 0)
  4096. {
  4097. LOG_ERROR("av_dict_set brand!");
  4098. };
  4099. // m_pCodecCtx->max_b_frames = 1;
  4100. // m_pCodecCtx->level = 31;
  4101. m_pCodecCtx->gop_size = m_sStreamData.fps << 1; // VIDEO_OUTPUT_GOP; // emit one intra frame every X frames
  4102. /// P-frames, generated by referencing data from prev and future frames.
  4103. /// [Compression up, CPU usage up]
  4104. /// [use 3/gop]
  4105. m_pCodecCtx->max_b_frames = 5;
  4106. /// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
  4107. /// [use 3?5 ref per P]
  4108. m_pCodecCtx->refs = 3;
  4109. /// Compression efficiency (slower -> better quality + higher cpu%)
  4110. /// [ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow]
  4111. /// Set this option to "ultrafast" is critical for realtime encoding
  4112. av_opt_set(m_pCodecCtx->priv_data, "preset", "ultrafast", 0);
  4113. // av_opt_set( m_pCodecCtx->priv_data, "preset", "fast", 0 );
  4114. // av_opt_set( m_pCodecCtx->priv_data, "preset", "slow", 0 );
  4115. /// Compression rate (lower -> higher compression) compress to lower size, makes decoded image more noisy
  4116. /// Range: [0; 51], sane range: [18; 26]. I used 35 as good compression/quality compromise. This option also critical for realtime encoding
  4117. av_opt_set(m_pCodecCtx->priv_data, "crf", "32", 0);
  4118. /// Change settings based upon the specifics of input
  4119. /// [psnr, ssim, grain, zerolatency, fastdecode, animation]
  4120. /// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
  4121. av_opt_set(m_pCodecCtx->priv_data, "tune", "zerolatency", 0);
  4122. /// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
  4123. /// Because Quicktime (ie. iOS) doesn't support this option
  4124. av_opt_set(m_pCodecCtx->priv_data, "b-pyramid", "0", 0);
  4125. #ifdef DEBUG_EXTRA
  4126. LOG("End Options!");
  4127. #endif
  4128. }
  4129. else
  4130. {
  4131. if (m_pFormat->flags & AVFMT_GLOBALHEADER)
  4132. {
  4133. m_pCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  4134. }
  4135. };
  4136. const AVRational dst_fps = {m_sStreamData.fps, 1};
  4137. #ifdef DEBUG_EXTRA
  4138. char TB[129];
  4139. sprintf(TB, "TimeBase: num[%d]/den[%d]", m_pCodecCtx->time_base.num, m_pCodecCtx->time_base.den);
  4140. LOG(TB);
  4141. #endif
  4142. m_pVideoStrA->time_base = m_pCodecCtx->time_base;
  4143. m_pVideoStrB->time_base = m_pCodecCtx->time_base;
  4144. // avcodec_get_context_defaults3( m_pVideoStrA->codec, m_pVideoCodec );
  4145. // m_pVideoStrA->codec->width = m_pMaster->m_sEmbedVideoData.width;
  4146. // m_pVideoStrA->codec->height = m_pMaster->m_sEmbedVideoData.height;
  4147. // m_pVideoStrA->codec->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  4148. // m_pVideoStrA->codec->time_base = m_pVideoStrB->time_base = av_inv_q(dst_fps);
  4149. m_pVideoStrA->r_frame_rate = m_pVideoStrB->avg_frame_rate = dst_fps;
  4150. // avcodec_get_context_defaults3( m_pVideoStrB->codec, m_pVideoCodec );
  4151. // m_pVideoStrB->codec->width = m_pMaster->m_sEmbedVideoData.width;
  4152. // m_pVideoStrB->codec->height = m_pMaster->m_sEmbedVideoData.height;
  4153. // m_pVideoStrB->codec->pix_fmt = VIDEO_OUTPUT_PIX_FMT;
  4154. // m_pVideoStrB->codec->time_base = m_pVideoStrB->time_base = av_inv_q(dst_fps);
  4155. m_pVideoStrB->r_frame_rate = m_pVideoStrB->avg_frame_rate = dst_fps;
  4156. // if ( m_pFormatCtxA->oformat->flags & AVFMT_GLOBALHEADER ) m_pVideoStrA->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  4157. // if ( m_pFormatCtxB->oformat->flags & AVFMT_GLOBALHEADER ) m_pVideoStrB->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  4158. avcodec_parameters_from_context(m_pVideoStrA->codecpar, m_pCodecCtx);
  4159. // m_pVideoStrA->codecpar->format = VIDEO_OUTPUT_PIX_FMT;
  4160. // m_pVideoStrA->codecpar->extradata = m_pCodecCtx->extradata;
  4161. // m_pVideoStrA->codecpar->extradata_size = m_pCodecCtx->extradata_size;
  4162. avcodec_parameters_from_context(m_pVideoStrB->codecpar, m_pCodecCtx);
  4163. // m_pVideoStrB->codecpar->format = VIDEO_OUTPUT_PIX_FMT;
  4164. // m_pVideoStrB->codecpar->extradata = m_pCodecCtx->extradata;
  4165. // m_pVideoStrB->codecpar->extradata_size = m_pCodecCtx->extradata_size;
  4166. if (avcodec_is_open(m_pCodecCtx) == 0)
  4167. {
  4168. LOG("output codec context is closed, opening");
  4169. if (avcodec_open2(m_pCodecCtx, m_pVideoCodec, NULL) < 0)
  4170. {
  4171. LOG_ERROR("Error opening2 encoder");
  4172. return false;
  4173. #ifdef DEBUG_EXTRA
  4174. }
  4175. else
  4176. {
  4177. LOG("output codec context is opened");
  4178. #endif
  4179. };
  4180. #ifdef DEBUG_EXTRA
  4181. }
  4182. else
  4183. {
  4184. LOG("output codec context is already opened");
  4185. #endif
  4186. };
  4187. #endif // SEPARATED_VIDEO_ENCODERS
  4188. m_pConvertCtx =
  4189. sws_getContext(m_pMaster->m_sEmbedVideoData.width, m_pMaster->m_sEmbedVideoData.height, AV_PIX_FMT_BGR24, m_pMaster->m_sEmbedVideoData.width, m_pMaster->m_sEmbedVideoData.height, VIDEO_OUTPUT_PIX_FMT, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  4190. if (m_pConvertCtx == NULL)
  4191. {
  4192. return false;
  4193. }
  4194. m_pAVFrame = av_frame_alloc();
  4195. if (m_pAVFrame == NULL)
  4196. {
  4197. return false;
  4198. };
  4199. m_pOutBuffer = (uint8_t *) av_malloc(av_image_get_buffer_size(VIDEO_OUTPUT_PIX_FMT, m_pMaster->m_sEmbedVideoData.width, m_pMaster->m_sEmbedVideoData.height, BUFFER_ALIGN));
  4200. if (m_pOutBuffer == NULL)
  4201. {
  4202. return false;
  4203. };
  4204. m_pAVFrame->width = m_pMaster->m_sEmbedVideoData.width;
  4205. m_pAVFrame->height = m_pMaster->m_sEmbedVideoData.height;
  4206. m_pAVFrame->format = VIDEO_OUTPUT_PIX_FMT;
  4207. // av_frame_get_buffer( m_pAVFrame, 0 );
  4208. av_image_fill_arrays(m_pAVFrame->data, m_pAVFrame->linesize, m_pOutBuffer, VIDEO_OUTPUT_PIX_FMT, m_pMaster->m_sEmbedVideoData.width, m_pMaster->m_sEmbedVideoData.height, 1);
  4209. // av_frame_make_writable( m_pAVFrame );
  4210. #endif // !OPENCV_VIDEO_OUTPUT
  4211. #endif // !DIRECT_MJPEG_COMPOSING
  4212. return init();
  4213. }
  4214. // encoding thread
  4215. void streamHandler::m_fThreadProc(void)
  4216. {
  4217. char tB[128];
  4218. // #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4219. size_t nQueueSize;
  4220. // #endif
  4221. #ifndef DIRECT_MJPEG_COMPOSING
  4222. #ifndef OPENCV_VIDEO_OUTPUT
  4223. AVPacket mPacket;
  4224. int bGotPacket;
  4225. int rVal = 0;
  4226. av_init_packet(&mPacket);
  4227. #endif
  4228. #endif
  4229. while (m_bThreadStatus != TH_ENDED)
  4230. {
  4231. // At the begining All streams are closed --> try to Open
  4232. if (m_bThreadStatus < TH_WORKING)
  4233. {
  4234. if (! openSegmentA())
  4235. {
  4236. m_bThreadStatus = TH_ENDED;
  4237. LOG_ERROR("Can not Open Seg A for first time!");
  4238. continue;
  4239. };
  4240. // if( !openSegmentB() ){
  4241. // m_bThreadStatus = TH_ENDED;
  4242. // continue;
  4243. // };
  4244. m_bThreadStatus = TH_WORKING;
  4245. continue;
  4246. };
  4247. m_mEncodingQueueProtector.lock();
  4248. nQueueSize = m_qEncodingQueue.size();
  4249. m_mEncodingQueueProtector.unlock();
  4250. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4251. if (nQueueSize > EVE_EASE_FPS_QUEUE_SIZE_MAXIMUM)
  4252. {
  4253. // Just in case allredy applied to a streams
  4254. if (m_bWebFrameSkeepDivider == m_bAFrameSkeepDivider && m_bWebFrameSkeepDivider == m_bBFrameSkeepDivider)
  4255. {
  4256. m_bWebFrameSkeepDivider++;
  4257. if (m_bWebFrameSkeepDivider > EVE_EASE_FPS_DIVIDER_MAXIMUM)
  4258. {
  4259. m_bWebFrameSkeepDivider = EVE_EASE_FPS_DIVIDER_MAXIMUM;
  4260. }
  4261. else
  4262. {
  4263. #ifdef DEBUG_EXTRA
  4264. LOG("Video Quality Degraded!");
  4265. #endif
  4266. EVENT_CONTAINER * bEVT = createBasicEvent(m_sStreamData.source_id, EVT_CLASS_VIDEO, EVT_VIDEO_FPS_DEGRADED, NULL, NULL, NULL);
  4267. if (bEVT)
  4268. {
  4269. basicEventHeap::getInstance()->broadcast(bEVT);
  4270. }
  4271. else
  4272. {
  4273. LOG_ERROR("Event Creation failure!");
  4274. }
  4275. };
  4276. };
  4277. }
  4278. else if (nQueueSize < EVE_EASE_FPS_QUEUE_SIZE_MINIMUM)
  4279. {
  4280. // Just in case allredy applied to a streams
  4281. if (m_bWebFrameSkeepDivider == m_bAFrameSkeepDivider && m_bWebFrameSkeepDivider == m_bBFrameSkeepDivider)
  4282. {
  4283. m_bWebFrameSkeepDivider--;
  4284. if (m_bWebFrameSkeepDivider < EVE_EASE_FPS_DIVIDER_MINIMUM)
  4285. {
  4286. m_bWebFrameSkeepDivider = EVE_EASE_FPS_DIVIDER_MINIMUM;
  4287. }
  4288. else
  4289. {
  4290. #ifdef DEBUG_EXTRA
  4291. LOG("Video Quality Upgraded!");
  4292. #endif
  4293. EVENT_CONTAINER * bEVT = createBasicEvent(m_sStreamData.source_id, EVT_CLASS_VIDEO, EVT_VIDEO_FPS_RAISED, NULL, NULL, NULL);
  4294. if (bEVT)
  4295. {
  4296. basicEventHeap::getInstance()->broadcast(bEVT);
  4297. }
  4298. else
  4299. {
  4300. LOG_ERROR("Event Creation failure!");
  4301. }
  4302. };
  4303. };
  4304. };
  4305. #endif // EVE_ENABLE_EASE_OUTPUT_FPS
  4306. if (nQueueSize)
  4307. {
  4308. m_mEncodingQueueProtector.lock();
  4309. ENCODING_BUFFER * tData = m_qEncodingQueue.front();
  4310. if (tData)
  4311. {
  4312. m_qEncodingQueue.pop();
  4313. m_mEncodingQueueProtector.unlock();
  4314. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4315. bool bSkeepA = tData->flag_sa;
  4316. bool bSkeepB = tData->flag_sb;
  4317. #endif
  4318. #ifdef DIRECT_MJPEG_COMPOSING
  4319. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4320. if (! bSkeepA)
  4321. #endif
  4322. if (m_bSegAActive)
  4323. {
  4324. if (m_pOutputFD[0])
  4325. {
  4326. fwrite(tData->data, 1, tData->size, m_pOutputFD[0]);
  4327. m_mFrameCounterProtector.lock();
  4328. m_nSegAFrameCounter++;
  4329. m_mFrameCounterProtector.unlock();
  4330. };
  4331. }
  4332. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4333. if (! bSkeepB)
  4334. #endif
  4335. if (m_bSegBActive)
  4336. {
  4337. if (m_pOutputFD[1])
  4338. {
  4339. fwrite(tData->data, 1, tData->size, m_pOutputFD[1]);
  4340. m_mFrameCounterProtector.lock();
  4341. m_nSegBFrameCounter++;
  4342. m_mFrameCounterProtector.unlock();
  4343. };
  4344. }
  4345. // Don't free tData->data since is pointer on static data
  4346. free(tData->data);
  4347. #else // !DIRECT_MJPEG_COMPOSING
  4348. #ifdef OPENCV_VIDEO_OUTPUT
  4349. cv::Mat image(tData->rows, tData->cols, CV_8UC3);
  4350. memcpy(image.data, tData->data, tData->size);
  4351. free(tData->data);
  4352. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4353. if (! bSkeepA)
  4354. #endif
  4355. if (m_bSegAActive)
  4356. {
  4357. if (m_pEncoderA->isOpened())
  4358. {
  4359. m_pEncoderA->write(image);
  4360. m_mFrameCounterProtector.lock();
  4361. m_nSegAFrameCounter++;
  4362. m_mFrameCounterProtector.unlock();
  4363. };
  4364. }
  4365. #ifdef EVE_ENABLE_EASE_OUTPUT_FPS
  4366. if (! bSkeepB)
  4367. #endif
  4368. if (m_bSegBActive)
  4369. {
  4370. if (m_pEncoderB->isOpened())
  4371. {
  4372. m_pEncoderB->write(image);
  4373. m_mFrameCounterProtector.lock();
  4374. m_nSegBFrameCounter++;
  4375. m_mFrameCounterProtector.unlock();
  4376. };
  4377. }
  4378. #else //! OPENCV_VIDEO_OUTPUT
  4379. // av_frame_make_writable( m_pAVFrame );
  4380. // m_qEncodingQueue.pop();
  4381. // m_mEncodingQueueProtector.unlock();
  4382. sws_scale(m_pConvertCtx, tData->data, tData->linesize, 0, tData->rows, m_pAVFrame->data, m_pAVFrame->linesize);
  4383. free(tData->data[0]);
  4384. /*
  4385. AV_PICTURE_TYPE_NONE Undefined.
  4386. AV_PICTURE_TYPE_I Intra.
  4387. AV_PICTURE_TYPE_P Predicted.
  4388. AV_PICTURE_TYPE_B Bi-dir predicted.
  4389. AV_PICTURE_TYPE_S S(GMC)-VOP MPEG4.
  4390. AV_PICTURE_TYPE_SI Switching Intra.
  4391. AV_PICTURE_TYPE_SP Switching Predicted.
  4392. AV_PICTURE_TYPE_BI BI type.
  4393. */
  4394. if ((m_nGlobalFrameCounter % m_sStreamData.fps) == 0)
  4395. {
  4396. m_pAVFrame->key_frame = 1;
  4397. m_pAVFrame->pict_type = AVPictureType::AV_PICTURE_TYPE_I;
  4398. }
  4399. else
  4400. {
  4401. m_pAVFrame->key_frame = 0;
  4402. m_pAVFrame->pict_type = AVPictureType::AV_PICTURE_TYPE_NONE;
  4403. };
  4404. m_pAVFrame->pts = m_nGlobalFrameCounter + 1;
  4405. #ifdef SEPARATED_VIDEO_ENCODERS
  4406. if (m_bSegAActive)
  4407. {
  4408. rVal = avcodec_send_frame(m_pCodecACtx, m_pAVFrame);
  4409. }
  4410. if (m_bSegBActive)
  4411. {
  4412. rVal = avcodec_send_frame(m_pCodecBCtx, m_pAVFrame);
  4413. }
  4414. #else //! SEPARATED_VIDEO_ENCODERS
  4415. rVal = avcodec_send_frame(m_pCodecCtx, m_pAVFrame);
  4416. #endif //! SEPARATED_VIDEO_ENCODERS
  4417. #endif //! OPENCV_VIDEO_OUTPUT
  4418. #endif //! DIRECT_MJPEG_COMPOSING
  4419. free(tData);
  4420. m_mFrameCounterProtector.lock();
  4421. m_nGlobalFrameCounter++;
  4422. m_mFrameCounterProtector.unlock();
  4423. }
  4424. else
  4425. {
  4426. m_mEncodingQueueProtector.unlock();
  4427. }
  4428. };
  4429. // Test Commands!
  4430. m_mCurrentOperationProtector.lock();
  4431. switch (m_bCOP)
  4432. {
  4433. case 0x01:
  4434. {
  4435. #ifdef DEBUG_EXTRA
  4436. LOG("Actual Closing Video Stream A!");
  4437. LOG(m_sStreamSegmentInfo[0].scratchPath);
  4438. #endif
  4439. m_bSegAActive = false;
  4440. if (! m_bSegADelete)
  4441. {
  4442. #ifdef DIRECT_MJPEG_COMPOSING
  4443. if (m_pOutputFD[0])
  4444. {
  4445. fclose(m_pOutputFD[0]);
  4446. m_pOutputFD[0] = NULL;
  4447. };
  4448. #else //! DIRECT_MJPEG_COMPOSING
  4449. #ifdef OPENCV_VIDEO_OUTPUT
  4450. if (m_pEncoderA->isOpened())
  4451. {
  4452. m_pEncoderA->release();
  4453. #ifdef DEBUG_EXTRA
  4454. }
  4455. else
  4456. {
  4457. LOG(" Encoder A is NOT Opened!");
  4458. #endif
  4459. };
  4460. #else //! OPENCV_VIDEO_OUTPUT
  4461. av_write_trailer(m_pFormatCtxA);
  4462. avio_close(m_pFormatCtxA->pb);
  4463. #endif //! OPENCV_VIDEO_OUTPUT
  4464. #endif //! DIRECT_MJPEG_COMPOSING
  4465. EVENT_CONTAINER * bEVT = createBasicEvent(m_sStreamData.source_id, EVT_CLASS_VIDEO, EVT_CAPTURE_DONE, NULL, NULL, &m_sStreamSegmentInfo[0]);
  4466. if (bEVT)
  4467. {
  4468. basicEventHeap::getInstance()->broadcast(bEVT);
  4469. }
  4470. else
  4471. {
  4472. LOG_ERROR("Event Creation failure!");
  4473. }
  4474. #ifdef DEBUG_EXTRA
  4475. LOG("Stream A is Saved");
  4476. #endif
  4477. }
  4478. else
  4479. {
  4480. #ifdef DIRECT_MJPEG_COMPOSING
  4481. if (m_pOutputFD[0])
  4482. {
  4483. fclose(m_pOutputFD[0]);
  4484. m_pOutputFD[0] = NULL;
  4485. };
  4486. #else // !DIRECT_MJPEG_COMPOSING
  4487. #ifdef OPENCV_VIDEO_OUTPUT
  4488. if (m_pEncoderA->isOpened())
  4489. {
  4490. m_pEncoderA->release();
  4491. #ifdef DEBUG_EXTRA
  4492. }
  4493. else
  4494. {
  4495. LOG(" Encoder A is NOT Opened!");
  4496. #endif
  4497. };
  4498. #else // !OPENCV
  4499. #ifdef EVE_DONT_DELETE_SEGMENTS
  4500. av_write_trailer(m_pFormatCtxA);
  4501. #endif
  4502. avio_close(m_pFormatCtxA->pb);
  4503. #endif // !OPENCV_VIDEO_OUTPUT
  4504. #endif // !DIRECT_MJPEG_COMPOSING
  4505. #ifndef EVE_DONT_DELETE_SEGMENTS
  4506. unlink(m_sStreamSegmentInfo[0].scratchPath);
  4507. #endif
  4508. #ifdef DEBUG_EXTRA
  4509. LOG("Stream A is Closed!");
  4510. #endif
  4511. };
  4512. #ifndef DIRECT_MJPEG_COMPOSING
  4513. #ifndef OPENCV_VIDEO_OUTPUT
  4514. #ifdef SEPARATED_VIDEO_ENCODERS
  4515. #ifdef DEBUG_EXTRA
  4516. LOG("Del FCTX");
  4517. #endif
  4518. avformat_free_context(m_pFormatCtxA);
  4519. m_pVideoStrA = NULL;
  4520. #ifdef DEBUG_EXTRA
  4521. LOG("Del FCTX DONE");
  4522. #endif
  4523. #endif // SEPARATED_VIDEO_ENCODERS
  4524. #endif // !OPENCV
  4525. #endif // !DIRECT_MJPEG_COMPOSING
  4526. break;
  4527. };
  4528. case 0x02:
  4529. {
  4530. #ifdef DEBUG_EXTRA
  4531. LOG("Actual Closing Video Stream B!");
  4532. LOG(m_sStreamSegmentInfo[1].scratchPath);
  4533. #endif
  4534. m_bSegBActive = false;
  4535. if (! m_bSegBDelete)
  4536. {
  4537. #ifdef DIRECT_MJPEG_COMPOSING
  4538. if (m_pOutputFD[1])
  4539. {
  4540. fclose(m_pOutputFD[1]);
  4541. m_pOutputFD[1] = NULL;
  4542. };
  4543. #else // !DIRECT_MJPEG_COMPOSING
  4544. #ifdef OPENCV_VIDEO_OUTPUT
  4545. if (m_pEncoderB->isOpened())
  4546. {
  4547. m_pEncoderB->release();
  4548. #ifdef DEBUG_EXTRA
  4549. }
  4550. else
  4551. {
  4552. LOG(" Encoder B is NOT Opened!");
  4553. #endif
  4554. };
  4555. #else // !OpenCV
  4556. av_write_trailer(m_pFormatCtxB);
  4557. avio_close(m_pFormatCtxB->pb);
  4558. #endif // !OPENCV_VIDEO_OUTPUT
  4559. #endif // !DIRECT_MJPEG_COMPOSING
  4560. EVENT_CONTAINER * bEVT = createBasicEvent(m_sStreamData.source_id, EVT_CLASS_VIDEO, EVT_CAPTURE_DONE, NULL, NULL, &m_sStreamSegmentInfo[1]);
  4561. if (bEVT)
  4562. {
  4563. basicEventHeap::getInstance()->broadcast(bEVT);
  4564. }
  4565. else
  4566. {
  4567. LOG_ERROR("Event Creation failure!");
  4568. }
  4569. #ifdef DEBUG_EXTRA
  4570. LOG("Stream B is Saved");
  4571. #endif
  4572. }
  4573. else
  4574. {
  4575. #ifdef DIRECT_MJPEG_COMPOSING
  4576. if (m_pOutputFD[1])
  4577. {
  4578. fclose(m_pOutputFD[1]);
  4579. m_pOutputFD[1] = NULL;
  4580. };
  4581. #else
  4582. #ifdef OPENCV_VIDEO_OUTPUT
  4583. if (m_pEncoderB->isOpened())
  4584. {
  4585. m_pEncoderB->release();
  4586. #ifdef DEBUG_EXTRA
  4587. }
  4588. else
  4589. {
  4590. LOG(" Encoder B is NOT Opened!");
  4591. #endif
  4592. };
  4593. #else // !OPenCV
  4594. #ifdef EVE_DONT_DELETE_SEGMENTS
  4595. av_write_trailer(m_pFormatCtxB);
  4596. #endif
  4597. avio_close(m_pFormatCtxB->pb);
  4598. #endif // !OPENCV_VIDEO_OUTPUT
  4599. #endif // !DIRECT_MJPEG_COMPOSING
  4600. #ifndef EVE_DONT_DELETE_SEGMENTS
  4601. unlink(m_sStreamSegmentInfo[1].scratchPath);
  4602. #endif
  4603. #ifdef DEBUG_EXTRA
  4604. LOG("Stream B is Closed!");
  4605. #endif
  4606. };
  4607. #ifndef DIRECT_MJPEG_COMPOSING
  4608. #ifndef OPENCV_VIDEO_OUTPUT
  4609. #ifdef SEPARATED_VIDEO_ENCODERS
  4610. avformat_free_context(m_pFormatCtxB);
  4611. m_pVideoStrB = NULL;
  4612. #endif // SEPARATED_VIDEO_ENCODERS
  4613. #endif // !OPENCV
  4614. #endif // !DIRECT_MJPEG_COMPOSING
  4615. break;
  4616. };
  4617. };
  4618. m_bCOP = 0;
  4619. m_mCurrentOperationProtector.unlock();
  4620. //};
  4621. #ifndef DIRECT_MJPEG_COMPOSING
  4622. #ifndef OPENCV_VIDEO_OUTPUT
  4623. #ifdef SEPARATED_VIDEO_ENCODERS
  4624. do
  4625. {
  4626. mPacket.data = NULL;
  4627. mPacket.size = 0;
  4628. rVal = avcodec_receive_packet(m_pCodecACtx, &mPacket);
  4629. if (rVal == AVERROR(EAGAIN) || rVal == AVERROR_EOF)
  4630. {
  4631. break;
  4632. }
  4633. else if (rVal < 0)
  4634. {
  4635. LOG_ERROR("Error during encoding ");
  4636. m_bThreadStatus = TH_ENDED;
  4637. break;
  4638. };
  4639. if (! rVal)
  4640. {
  4641. if (m_bSegAActive)
  4642. {
  4643. mPacket.stream_index = m_pVideoStrA->index;
  4644. mPacket.duration = 1;
  4645. m_mFrameCounterProtector.lock();
  4646. m_nSegAFrameCounter++;
  4647. m_mFrameCounterProtector.unlock();
  4648. mPacket.pts = av_rescale_q(m_nSegAFrameCounter, m_pCodecACtx->time_base, m_pVideoStrA->time_base);
  4649. // mPacket.pts = AV_NOPTS_VALUE;
  4650. // mPacket.pts = av_rescale_q( m_nSegASkippedFrames + m_nSegAFrameCounter, m_pCodecCtx->time_base, m_pVideoStrA->time_base );
  4651. mPacket.dts = mPacket.pts;
  4652. int rc = av_write_frame(m_pFormatCtxA, &mPacket);
  4653. if (rc < 0)
  4654. {
  4655. // if( av_interleaved_write_frame( m_pFormatCtxA, &mPacket) < 0 ) {
  4656. m_bThreadStatus = TH_ENDED;
  4657. LOG_ERROR("Trouble with Video Output A!");
  4658. av_strerror(rc, tB, 128);
  4659. LOG_ERROR(tB);
  4660. break; // continue (changed since embed into do-while)
  4661. };
  4662. };
  4663. if (m_bSegBActive)
  4664. {
  4665. mPacket.stream_index = m_pVideoStrB->index;
  4666. mPacket.duration = 1;
  4667. m_mFrameCounterProtector.lock();
  4668. m_nSegBFrameCounter++;
  4669. m_mFrameCounterProtector.unlock();
  4670. mPacket.pts = av_rescale_q(m_nSegBFrameCounter, m_pCodecBCtx->time_base, m_pVideoStrB->time_base);
  4671. // mPacket.pts = AV_NOPTS_VALUE;
  4672. // mPacket.pts = av_rescale_q( m_nSegBSkippedFrames + m_nSegBFrameCounter, m_pCodecCtx->time_base, m_pVideoStrB->time_base );
  4673. mPacket.dts = mPacket.pts;
  4674. int rc = av_write_frame(m_pFormatCtxB, &mPacket);
  4675. if (rc < 0)
  4676. {
  4677. // if( av_interleaved_write_frame( m_pFormatCtxB, &mPacket) < 0 ) {
  4678. m_bThreadStatus = TH_ENDED;
  4679. LOG_ERROR("Trouble with Video Output B!");
  4680. av_strerror(rc, tB, 128);
  4681. LOG_ERROR(tB);
  4682. break; // continue (changed since embed into do-while)
  4683. };
  4684. };
  4685. av_packet_unref(&mPacket);
  4686. };
  4687. }
  4688. while (rVal >= 0 && m_bThreadStatus != TH_ENDED);
  4689. #else // ! SEPARATED_VIDEO_ENCODERS
  4690. do
  4691. {
  4692. mPacket.data = NULL;
  4693. mPacket.size = 0;
  4694. rVal = avcodec_receive_packet(m_pCodecCtx, &mPacket);
  4695. if (rVal == AVERROR(EAGAIN) || rVal == AVERROR_EOF)
  4696. {
  4697. break;
  4698. }
  4699. else if (rVal < 0)
  4700. {
  4701. LOG_ERROR("Error during encoding ");
  4702. m_bThreadStatus = TH_ENDED;
  4703. break;
  4704. };
  4705. if (! rVal)
  4706. {
  4707. if (m_bSegAActive)
  4708. {
  4709. mPacket.stream_index = m_pVideoStrA->index;
  4710. mPacket.duration = 1;
  4711. mPacket.pts = av_rescale_q(m_nGlobalFrameCounter, m_pCodecCtx->time_base, m_pVideoStrA->time_base);
  4712. // mPacket.pts = AV_NOPTS_VALUE;
  4713. // mPacket.pts = av_rescale_q( m_nSegASkippedFrames + m_nSegAFrameCounter, m_pCodecCtx->time_base, m_pVideoStrA->time_base );
  4714. mPacket.dts = mPacket.pts;
  4715. int rc = av_write_frame(m_pFormatCtxA, &mPacket);
  4716. if (rc < 0)
  4717. {
  4718. // if( av_interleaved_write_frame( m_pFormatCtxA, &mPacket) < 0 ) {
  4719. m_bThreadStatus = TH_ENDED;
  4720. LOG_ERROR("Trouble with Video Output A!");
  4721. av_strerror(rc, tB, 128);
  4722. LOG_ERROR(tB);
  4723. break; // continue (changed since embed into do-while)
  4724. };
  4725. };
  4726. if (m_bSegBActive)
  4727. {
  4728. mPacket.stream_index = m_pVideoStrB->index;
  4729. mPacket.duration = 1;
  4730. mPacket.pts = av_rescale_q(m_nGlobalFrameCounter, m_pCodecCtx->time_base, m_pVideoStrA->time_base);
  4731. // mPacket.pts = AV_NOPTS_VALUE;
  4732. // mPacket.pts = av_rescale_q( m_nSegBSkippedFrames + m_nSegBFrameCounter, m_pCodecCtx->time_base, m_pVideoStrB->time_base );
  4733. mPacket.dts = mPacket.pts;
  4734. int rc = av_write_frame(m_pFormatCtxB, &mPacket);
  4735. if (rc < 0)
  4736. {
  4737. // if( av_interleaved_write_frame( m_pFormatCtxB, &mPacket) < 0 ) {
  4738. m_bThreadStatus = TH_ENDED;
  4739. LOG_ERROR("Trouble with Video Output B!");
  4740. av_strerror(rc, tB, 128);
  4741. LOG_ERROR(tB);
  4742. break; // continue (changed since embed into do-while)
  4743. };
  4744. };
  4745. av_packet_unref(&mPacket);
  4746. };
  4747. }
  4748. while (rVal >= 0 && m_bThreadStatus != TH_ENDED);
  4749. #endif // ! SEPARATED_VIDEO_ENCODERS
  4750. #endif // !OPENCV_VIDEO_OUTPUT
  4751. #endif // !DIRECT_MJPEG_COMPOSING
  4752. SLEEP(10);
  4753. };
  4754. #ifdef DEBUG_EXTRA
  4755. LOG("Encoder Loop Ended!");
  4756. #endif
  4757. }
  4758. void streamHandler::swapActiveStreams(void)
  4759. {
  4760. // m_mFrameCounterProtector.lock();
  4761. m_bActiveEncoderStream = 1 - m_bActiveEncoderStream;
  4762. if (m_bActiveEncoderStream)
  4763. {
  4764. if (m_bSegADelete)
  4765. {
  4766. closeSegmentA();
  4767. }
  4768. }
  4769. else
  4770. {
  4771. if (m_bSegBDelete)
  4772. {
  4773. closeSegmentB();
  4774. }
  4775. };
  4776. // m_mFrameCounterProtector.unlock();
  4777. }
  4778. #endif // EVE_ENABLE_VIDEO
  4779. bool streamHandler::triggerEvent(uint32_t eid, uint32_t & dbid, uint32_t & frame)
  4780. {
  4781. #ifdef DEBUG_EXTRA
  4782. LOG("Trig Evt");
  4783. #endif
  4784. #ifdef EVE_ENABLE_VIDEO
  4785. m_mFrameCounterProtector.lock();
  4786. // To Mark Events Frames
  4787. m_bEvtMarkerCounter = 255;
  4788. PF_TIMESTAMP ts;
  4789. getPFTS(&ts);
  4790. /*
  4791. if ( m_nVideoRegisteredDBID == 0 ) {
  4792. LOG("New Event!");
  4793. if ( !m_bActiveEncoderStream ) { // Stream A is Active
  4794. m_nVideoRegisteredDBID = m_pDBC->addVideoRecord(
  4795. m_sStreamData.id,
  4796. m_cSegMD5A,
  4797. VIDEO_OUTPUT_CONTAINER,
  4798. ts.seconds,
  4799. ts.milliseconds,
  4800. m_sStreamData.fps
  4801. );
  4802. m_bSegADelete = false;
  4803. m_nSegAShiftFrames = frame = m_nSegAFrameCounter;
  4804. } else { // Stream B is Active
  4805. m_nVideoRegisteredDBID = m_pDBC->addVideoRecord(
  4806. m_sStreamData.id,
  4807. m_cSegMD5B,
  4808. VIDEO_OUTPUT_CONTAINER,
  4809. ts.seconds,
  4810. ts.milliseconds,
  4811. m_sStreamData.fps
  4812. );
  4813. m_bSegBDelete = false;
  4814. m_nSegBShiftFrames = frame = m_nSegBFrameCounter;
  4815. };
  4816. };
  4817. */
  4818. if (! m_bActiveEncoderStream)
  4819. { // Stream A is Active
  4820. m_bSegADelete = false;
  4821. m_nSegAShiftFrames = frame = m_nSegAFrameCounter;
  4822. }
  4823. else
  4824. { // Stream B is Active
  4825. m_bSegBDelete = false;
  4826. m_nSegBShiftFrames = frame = m_nSegBFrameCounter;
  4827. };
  4828. m_sStreamSegmentInfo[m_bActiveEncoderStream].eventIDS.push_back(eid);
  4829. m_sStreamSegmentInfo[m_bActiveEncoderStream].eventTSS.push_back(ts.seconds);
  4830. m_sStreamSegmentInfo[m_bActiveEncoderStream].eventTMS.push_back(ts.milliseconds);
  4831. m_sStreamSegmentInfo[m_bActiveEncoderStream].eventFrames.push_back(frame);
  4832. // dbid = m_sStreamSegmentInfo[m_bActiveEncoderStream].db_id;// = m_nVideoRegisteredDBID;
  4833. m_mFrameCounterProtector.unlock();
  4834. // if ( dbid == 0 ) return false;
  4835. // dbid = m_nVideoRegisteredDBID;
  4836. #ifdef DEBUG_EXTRA
  4837. LOG("Event Registered!");
  4838. #endif
  4839. #endif // EVE_ENABLE_VIDEO
  4840. return true;
  4841. }
  4842. bool streamHandler::triggerCamMove(void)
  4843. {
  4844. #ifdef DEBUG_EXTRA
  4845. LOG("Trig Cammove");
  4846. #endif
  4847. m_bCamMoveCounter = 255;
  4848. return true;
  4849. }
  4850. bool streamHandler::validate(void)
  4851. {
  4852. if (m_pSource)
  4853. {
  4854. if (m_pSource->getStatus() < MS_CLOSING)
  4855. {
  4856. #ifdef EVE_ENABLE_VIDEO
  4857. if (m_nGlobalFrameCounter)
  4858. { // Frames Received from a source
  4859. if (m_nGlobalFrameCounter == m_nValidatorFrameCounter)
  4860. {
  4861. // Video Stalled!
  4862. LOG_ERROR("Video Stalled");
  4863. return false; // Try use System to recuperate
  4864. }
  4865. else
  4866. {
  4867. m_nValidatorFrameCounter = m_nGlobalFrameCounter;
  4868. }
  4869. };
  4870. if (m_bThreadStatus < TH_ENDED)
  4871. {
  4872. return true;
  4873. }
  4874. else
  4875. {
  4876. LOG_ERROR("Thread Ended!");
  4877. }
  4878. #endif
  4879. }
  4880. else
  4881. {
  4882. LOG_ERROR("Video Ended");
  4883. }
  4884. };
  4885. return false;
  4886. }
  4887. // #undef DEBUG_EXTRA
  4888. // #undef DEBUG
  4889. #undef LOG
  4890. #undef LOG_ERROR
  4891. #undef TLOG
  4892. #undef TLOG_ERROR