jpeg.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. struct channelConfigSt {
  7. uint dummy[4];
  8. int state;
  9. int encoder;
  10. };
  11. extern struct channelConfigSt *get_enc_chn_config(int ch);
  12. extern int get_video_run_state(int ch);
  13. extern void video_param_set_mutex_lock();
  14. extern int IMP_Encoder_StartRecvPic(int ch);
  15. extern int IMP_Encoder_PollingStream(int ch, int timeoutMSec);
  16. extern int IMP_Encoder_GetStream(int ch, uint *stream, int);
  17. extern int IMP_Encoder_ReleaseStream(int ch, int *stream);
  18. extern int IMP_Encoder_StopRecvPic(int ch);
  19. extern int save_jpeg(int fd, int *stream);
  20. extern void video_param_set_mutex_unlock();
  21. extern void CommandResponse(int fd, const char *res);
  22. static const char *HttpResHeader = "Cache-Control: no-cache\nContent-Type: image/jpeg\n\n";
  23. static const char *HttpErrorHeader = "Cache-Control: no-cache\nStatus: 503\n\n";
  24. static pthread_mutex_t JpegDataMutex = PTHREAD_MUTEX_INITIALIZER;
  25. static int JpegCaptureFd = -1;
  26. char *JpegCapture(int fd, char *tokenPtr) {
  27. if(JpegCaptureFd >= 0) {
  28. fprintf(stderr, "[command] jpeg capture error %d %d\n", JpegCaptureFd, fd);
  29. write(fd, HttpErrorHeader, strlen(HttpErrorHeader));
  30. CommandResponse(fd, "error : jpeg capture error");
  31. return NULL;
  32. }
  33. JpegCaptureFd = fd;
  34. pthread_mutex_unlock(&JpegDataMutex);
  35. return NULL;
  36. }
  37. static int GetJpegData(int fd) {
  38. struct channelConfigSt *chConfig = get_enc_chn_config(0);
  39. if (!chConfig->state) {
  40. fprintf(stderr, "[command] jpeg err: ch0 is not enable jpeg!\n");
  41. return -1;
  42. }
  43. int state = get_video_run_state(0);
  44. if (state < 5) {
  45. fprintf(stderr, "[command] jpeg err: U should call 'video_run' before this func\n");
  46. return -1;
  47. }
  48. video_param_set_mutex_lock(1);
  49. int encoder = chConfig->encoder;
  50. int ret = 0;
  51. if(IMP_Encoder_StartRecvPic(encoder) < 0) {
  52. fprintf(stderr, "[command] jpeg err: IMP_Encoder_StartRecvPic(%d) failed\n", encoder);
  53. ret = -1;
  54. goto error1;
  55. }
  56. if(IMP_Encoder_PollingStream(encoder, 2000) < 0) {
  57. fprintf(stderr, "[command] jpeg err: Polling stream(chn%d) timeout\n", encoder);
  58. ret = -1;
  59. goto error2;
  60. }
  61. uint stream[17];
  62. memset(stream, 0, 60);
  63. if(IMP_Encoder_GetStream(encoder, stream, 1) < 0) {
  64. fprintf(stderr, "[command] jpeg err: IMP_Encoder_GetStream(chn%d) failed\n", encoder);
  65. ret = -1;
  66. goto error2;
  67. }
  68. write(JpegCaptureFd, HttpResHeader, strlen(HttpResHeader));
  69. if(save_jpeg(fd, stream) < 0) {
  70. fprintf(stderr, "[command] jpeg err: save_jpeg(%d) failed\n", fd);
  71. ret = -2;
  72. }
  73. IMP_Encoder_ReleaseStream(encoder, stream);
  74. error2:
  75. if(IMP_Encoder_StopRecvPic(encoder) < 0) {
  76. fprintf(stderr, "[command] jpeg err: IMP_Encoder_StopRecvPic(chn%d) failed\n", encoder);
  77. }
  78. error1:
  79. video_param_set_mutex_unlock(1);
  80. if(ret == -1) write(JpegCaptureFd, HttpErrorHeader, strlen(HttpErrorHeader));
  81. return ret;
  82. }
  83. static void *JpegCaptureThread() {
  84. while(1) {
  85. pthread_mutex_lock(&JpegDataMutex);
  86. if(JpegCaptureFd >= 0) {
  87. int res = GetJpegData(JpegCaptureFd);
  88. CommandResponse(JpegCaptureFd, res >= 0 ? "" : "error : buffer size error");
  89. }
  90. JpegCaptureFd = -1;
  91. }
  92. }
  93. static void __attribute ((constructor)) JpegInit(void) {
  94. pthread_mutex_lock(&JpegDataMutex);
  95. pthread_t thread;
  96. if(pthread_create(&thread, NULL, JpegCaptureThread, NULL)) {
  97. fprintf(stderr, "pthread_create error\n");
  98. pthread_mutex_unlock(&JpegDataMutex);
  99. return;
  100. }
  101. }