|
| 1 | +#include <esp_timer.h> |
| 2 | +#include <esp_http_server.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <stdint.h> |
| 6 | + |
| 7 | +#include <freertos/FreeRTOS.h> |
| 8 | +#include <freertos/queue.h> |
| 9 | +#include <freertos/task.h> |
| 10 | + |
| 11 | +#include "cam_streamer.h" |
| 12 | + |
| 13 | +#define PART_BOUNDARY "123456789000000000000987654321" |
| 14 | + |
| 15 | +#define _STREAM_HEADERS "HTTP/1.1 200 OK\r\n"\ |
| 16 | + "Access-Control-Allow-Origin: *\r\n"\ |
| 17 | + "Connection: Keep-Alive\r\n"\ |
| 18 | + "Keep-Alive: timeout=15\r\n"\ |
| 19 | + "Content-Type: multipart/x-mixed-replace;boundary=" PART_BOUNDARY "\r\n" |
| 20 | + |
| 21 | +static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n"; |
| 22 | +static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n"; |
| 23 | + |
| 24 | +static uint8_t is_send_error(int r) { |
| 25 | + switch(r) { |
| 26 | + case HTTPD_SOCK_ERR_INVALID: |
| 27 | +#ifdef DEBUG_DEFAULT_ON |
| 28 | + printf("[cam_streamer] invalid argument occured!\n"); |
| 29 | +#endif |
| 30 | + return 1; |
| 31 | + case HTTPD_SOCK_ERR_TIMEOUT: |
| 32 | +#ifdef DEBUG_DEFAULT_ON |
| 33 | + printf("[cam_streamer] timeout/interrupt occured!\n"); |
| 34 | +#endif |
| 35 | + return 1; |
| 36 | + case HTTPD_SOCK_ERR_FAIL: |
| 37 | +#ifdef DEBUG_DEFAULT_ON |
| 38 | + printf("[cam_streamer] unrecoverable error while send()!\n"); |
| 39 | +#endif |
| 40 | + return 1; |
| 41 | + case ESP_ERR_INVALID_ARG: |
| 42 | +#ifdef DEBUG_DEFAULT_ON |
| 43 | + printf("[text-streamer] session closed!\n"); |
| 44 | +#endif |
| 45 | + return 1; |
| 46 | + default: |
| 47 | +#ifdef DEBUG_DEFAULT_ON |
| 48 | + printf("[cam_streamer] sent %d bytes!\n", r); |
| 49 | +#endif |
| 50 | + return 0; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void cam_streamer_init(cam_streamer_t *s, httpd_handle_t server, uint16_t fps) { |
| 55 | + memset(s, 0, sizeof(cam_streamer_t)); |
| 56 | + s->frame_delay=1000000/fps; |
| 57 | + s->clients=xQueueCreate(CAM_STREAMER_MAX_CLIENTS*2, sizeof(int)); |
| 58 | + s->server=server; |
| 59 | +} |
| 60 | + |
| 61 | +static void cam_streamer_update_frame(cam_streamer_t *s) { |
| 62 | + uint8_t l=0; |
| 63 | + while(!__atomic_compare_exchange_n(&s->buf_lock, &l, 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { |
| 64 | + l=0; |
| 65 | + vTaskDelay(1/portTICK_PERIOD_MS); |
| 66 | + } |
| 67 | + |
| 68 | + if(s->buf) |
| 69 | + esp_camera_fb_return(s->buf); |
| 70 | + |
| 71 | + s->buf=esp_camera_fb_get(); |
| 72 | + |
| 73 | + s->last_updated=esp_timer_get_time(); |
| 74 | + s->part_len=snprintf(s->part_buf, 64, _STREAM_PART, s->buf->len); |
| 75 | + __atomic_store_n(&s->buf_lock, 0, __ATOMIC_RELAXED); |
| 76 | +#ifdef DEBUG_DEFAULT_ON |
| 77 | + printf("[cam_streamer] fetched new frame\n"); |
| 78 | +#endif |
| 79 | +} |
| 80 | + |
| 81 | +void cam_streamer_task(void *p) { |
| 82 | + cam_streamer_t *s=(cam_streamer_t *) p; |
| 83 | + |
| 84 | + uint8_t res; |
| 85 | + uint64_t last_time=0, current_time; |
| 86 | + int fd; |
| 87 | + unsigned int n_entries; |
| 88 | + for(;;) { |
| 89 | + while(!(n_entries=uxQueueMessagesWaiting(s->clients))) |
| 90 | + vTaskSuspend(NULL); |
| 91 | + |
| 92 | + current_time=esp_timer_get_time(); |
| 93 | + if((current_time-last_time)<s->frame_delay) |
| 94 | + vTaskDelay((s->frame_delay-(current_time-last_time))/(1000*portTICK_PERIOD_MS)); |
| 95 | + last_time=current_time; |
| 96 | + |
| 97 | + cam_streamer_update_frame(s); |
| 98 | + |
| 99 | + for(unsigned int i=0; i<n_entries; ++i) { |
| 100 | + if(xQueueReceive(s->clients, &fd, 10/portTICK_PERIOD_MS)==pdFALSE) { |
| 101 | +#ifdef DEBUG_DEFAULT_ON |
| 102 | + printf("[cam_streamer] failed to dequeue fd!\n"); |
| 103 | +#endif |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | +#ifdef DEBUG_DEFAULT_ON |
| 108 | + printf("[cam_streamer] dequeued fd %d\n", fd); |
| 109 | + printf("[cam_streamer] sending part: \"%.*s\"\n", (int) s->part_len, s->part_buf); |
| 110 | +#endif |
| 111 | + if((res=is_send_error(httpd_socket_send(s->server, fd, s->part_buf, s->part_len, 0)))) |
| 112 | + continue; |
| 113 | + |
| 114 | + if((res|=is_send_error(httpd_socket_send(s->server, fd, s->buf->buf, s->buf->len, 0)))) |
| 115 | + continue; |
| 116 | + |
| 117 | + if((res|=is_send_error(httpd_socket_send(s->server, fd, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY), 0)))) |
| 118 | + continue; |
| 119 | + |
| 120 | + if(!res) { |
| 121 | +#ifdef DEBUG_DEFAULT_ON |
| 122 | + printf("[cam_streamer] fd %d requeued\n", fd); |
| 123 | +#endif |
| 124 | + xQueueSend(s->clients, (void *) &fd, 10/portTICK_PERIOD_MS); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +void cam_streamer_start(cam_streamer_t *s) { |
| 131 | + BaseType_t r=xTaskCreate(cam_streamer_task, "cam_streamer", 10*1024, (void *) s, tskIDLE_PRIORITY+3, &s->task); |
| 132 | + |
| 133 | +#ifdef DEBUG_DEFAULT_ON |
| 134 | + if(r!=pdPASS) |
| 135 | + printf("[cam_streamer] failed to create task!\n"); |
| 136 | +#endif |
| 137 | +} |
| 138 | + |
| 139 | +void cam_streamer_stop(cam_streamer_t *s) { |
| 140 | + vTaskDelete(s->task); |
| 141 | +} |
| 142 | + |
| 143 | +bool cam_streamer_enqueue_client(cam_streamer_t *s, int fd) { |
| 144 | +#ifdef DEBUG_DEFAULT_ON |
| 145 | + printf("sending stream headers:\n%s\nLength: %d\n", _STREAM_HEADERS, strlen(_STREAM_HEADERS)); |
| 146 | +#endif |
| 147 | + if(is_send_error(httpd_socket_send(s->server, fd, _STREAM_HEADERS, strlen(_STREAM_HEADERS), 0))) { |
| 148 | +#ifdef DEBUG_DEFAULT_ON |
| 149 | + printf("failed sending headers!\n"); |
| 150 | +#endif |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + if(is_send_error(httpd_socket_send(s->server, fd, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY), 0))) { |
| 155 | +#ifdef DEBUG_DEFAULT_ON |
| 156 | + printf("failed sending boundary!\n"); |
| 157 | +#endif |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + const BaseType_t r=xQueueSend(s->clients, (void *) &fd, 10*portTICK_PERIOD_MS); |
| 162 | + if(r!=pdTRUE) { |
| 163 | +#ifdef DEBUG_DEFAULT_ON |
| 164 | + printf("[cam_streamer] failed to enqueue fd %d\n", fd); |
| 165 | +#endif |
| 166 | +#define EMSG "failed to enqueue" |
| 167 | + httpd_socket_send(s->server, fd, EMSG, strlen(EMSG), 0); |
| 168 | +#undef EMSG |
| 169 | + } else { |
| 170 | +#ifdef DEBUG_DEFAULT_ON |
| 171 | + printf("[cam_streamer] socket %d enqueued\n", fd); |
| 172 | +#endif |
| 173 | + vTaskResume(s->task); |
| 174 | + } |
| 175 | + |
| 176 | + return r==pdTRUE?true:false; |
| 177 | +} |
| 178 | + |
0 commit comments