File: stream_example.c

package info (click to toggle)
ulfius 2.5.2-4%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,232 kB
  • sloc: ansic: 9,103; makefile: 511; sh: 8
file content (140 lines) | stat: -rw-r--r-- 3,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * 
 * Ulfius Framework example program
 * 
 * This example program streams some data
 * 
 * Copyright 2016 Nicolas Mora <mail@babelouest.org>
 * 
 * License MIT
 *
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <ulfius.h>
#include <u_example.h>

#define PORT 7876
#define PREFIX "/stream"

/**
 * callback functions declaration
 */
int callback_get_stream (const struct _u_request * request, struct _u_response * response, void * user_data);

int callback_get_audio_stream (const struct _u_request * request, struct _u_response * response, void * user_data);

ssize_t stream_data (void * cls, uint64_t pos, char *buf, size_t max);
ssize_t stream_audio_file (void * cls, uint64_t pos, char *buf, size_t max);

void free_stream_data(void * cls);
void free_stream_audio_file(void * cls);

int main (int argc, char **argv) {
  int ret;
  
  // Set the framework port number
  struct _u_instance instance;
  
  if (ulfius_init_instance(&instance, PORT, NULL, NULL) != U_OK) {
    printf("Error ulfius_init_instance, abort\n");
    return(1);
  }
  
  u_map_put(instance.default_headers, "Access-Control-Allow-Origin", "*");
  
  // Endpoint list declaration
  ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, NULL, 0, &callback_get_stream, NULL);
  if (argc > 1) {
    ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/audio", 0, &callback_get_audio_stream, argv[1]);
  }
  
  // Start the framework
  ret = ulfius_start_framework(&instance);
  
  if (ret == U_OK) {
    printf("Start framework on port %d\n", instance.port);
    
    // Wait for the user to press <enter> on the console to quit the application
    getchar();
  } else {
    printf("Error starting framework\n");
  }
  printf("End framework\n");
  
  ulfius_stop_framework(&instance);
  ulfius_clean_instance(&instance);
  
  return 0;
}

/**
 * Callback function
 */
int callback_get_stream (const struct _u_request * request, struct _u_response * response, void * user_data) {
  ulfius_set_stream_response(response, 200, stream_data, free_stream_data, U_STREAM_SIZE_UNKOWN, 32 * 1024, o_strdup("stream test"));
  return U_CALLBACK_CONTINUE;
}

int callback_get_audio_stream (const struct _u_request * request, struct _u_response * response, void * user_data) {
  FILE * file = fopen((char *)user_data, "rb");
  int fd;
  struct stat buf;
  printf("stream audio file\n");
  
  if (file != NULL) {

    fd = fileno (file);
    if (-1 == fd) {
      fclose (file);
      return U_ERROR; /* internal error */
    }
    if ( (0 != fstat (fd, &buf)) || (! S_ISREG (buf.st_mode)) ) {
      /* not a regular file, refuse to serve */
      fclose (file);
      file = NULL;
      return U_ERROR;
    }
    u_map_put(response->map_header, "Content-Type", "audio/mpeg");
    ulfius_set_stream_response(response, 200, stream_audio_file, free_stream_audio_file, buf.st_size, 32 * 1024, file);
    return U_CALLBACK_CONTINUE;
  } else {
    return U_CALLBACK_ERROR;
  }
}

ssize_t stream_data (void * cls, uint64_t pos, char * buf, size_t max) {
  printf("stream data %" PRIu64 " %zu\n", pos, max);
  sleep(1);
  if (pos <= 100) {
      snprintf(buf, max, "%s %" PRIu64 "\n", (char *)cls, pos);
      return o_strlen(buf);
  } else {
    return U_STREAM_END;
  }
}

ssize_t stream_audio_file (void * cls, uint64_t pos, char * buf, size_t max) {
  FILE *file = cls;

  (void) fseek (file, pos, SEEK_SET);
  return fread (buf, 1, max, file);
}

void free_stream_data(void * cls) {
  printf("clean data\n");
  o_free(cls);
}

void free_stream_audio_file(void * cls) {
  printf("clean file\n");

  FILE *file = cls;
  fclose (file);
}