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.

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