summaryrefslogtreecommitdiff
path: root/streamer.c
blob: 7ba83acac943103175457ffd69d89e27546e3bbd (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <unistd.h>
#include "common.h"
#include "playlist.h"
#include "streamer.h"
#include "transmuxer.h"

/* ===== Definitions ===== */
#define PRIORITY -10
struct Streamer {
	pthread_t       worker_thread;
	pthread_mutex_t worker_ready_mutex;
	pthread_cond_t  worker_ready_cond;
	int             worker_ready_flag;

	enum State      state;
	pthread_mutex_t state_mutex;

	HLSRemuxer     *remuxer;
	Playlist       *playlist;
	char           *current_song;
};

/* ====== Streamer initialisation ===== */
static void *streamer_worker       (void *t);

Streamer *streamer_init (const struct StreamerOpt *opts)
{
	Streamer   *s = NULL;
	Playlist   *p = NULL;
	HLSRemuxer *r = NULL;

	if ((s = malloc(sizeof(*s))) == NULL)
		goto error;
	if ((p = playlist_init()) == NULL)
		goto error;
	if ((r = hls_remuxer_init(opts)) == NULL)
		goto error;

	if (pthread_create(&s->worker_thread, NULL, streamer_worker, s))
		goto error;

	s->playlist    = p;
	s->remuxer     = r;
	s->state       = STATE_STANDBY;
	s->state_mutex = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
	return s;

error:
	hls_remuxer_free(r);
	playlist_free(p);
	free(s);
	return NULL;
}

int streamer_push(Streamer *s, const char *url, unsigned int index)
{
	return (index == PLAYLIST_END)
		? playlist_enqueue(s->playlist, url)
		: playlist_insert(s->playlist, index, url);
}

int streamer_pop(Streamer *s, unsigned int id)
{
	int err;
	struct Entry entry;

	if ((err = playlist_remove(s->playlist, id, &entry)))
		return err;
	free(entry.file);
	return 0;
}

void streamer_skip(Streamer *s)
{
	/* 'skip' only halt the current song, which is then replaced by the streamer
	 * worker, either immediately, or as soon as a song gets pushed.
	 */
	hls_remuxer_halt(s->remuxer);
}

void streamer_pause(Streamer *s)
{
	pthread_mutex_lock(&s->state_mutex);
	if (s->state == STATE_PLAYING) {
		hls_remuxer_pause(s->remuxer);
		s->state = STATE_PAUSED;
	}
	pthread_mutex_unlock(&s->state_mutex);
}

void streamer_resume(Streamer *s)
{
	pthread_mutex_lock(&s->state_mutex);
	if (s->state == STATE_PAUSED) {
		hls_remuxer_resume(s->remuxer);
		s->state = STATE_PLAYING;
	}
	pthread_mutex_unlock(&s->state_mutex);
}

int streamer_info(Streamer *streamer, struct StateInfo *info)
{
	pthread_mutex_lock(&streamer->state_mutex);
	info->state = streamer->state;

	if ((info->current_song = strdup(streamer->current_song)) == NULL) {
		pthread_mutex_unlock(&streamer->state_mutex);
		return -1;
	}

	if (playlist_list(streamer->playlist, info)) {
		pthread_mutex_unlock(&streamer->state_mutex);
		free(info->current_song);
		return -1;
	}

	pthread_mutex_unlock(&streamer->state_mutex);
	return 0;
}

void streamer_free (Streamer *s)
{
	pthread_kill(s->worker_thread, SIGTERM);
	hls_remuxer_halt(s->remuxer);
	pthread_join(s->worker_thread, NULL);
	playlist_free(s->playlist);
	free(s);
}

static inline int streamer_worker_conf_priority(void)
{
	errno = 0;
	setpriority(PRIO_PROCESS, 0, PRIORITY);
	return errno ? -1 : 0;
}

static void *streamer_worker(void *arg)
{
	Streamer *s;
	struct Entry next;


	/* Configure worker */
	pthread_mutex_lock(&s->worker_ready_mutex);
	streamer_worker_conf_priority();
	s->worker_ready_flag = 1;
	pthread_cond_broadcast(&s->worker_ready_cond);
	pthread_mutex_unlock(&s->worker_ready_mutex);

	/* Main loop */
	while (s->state != ) {
		/* Attempt to fetch the next song, or go into standby mode */
		if (playlist_try_dequeue(s->playlist, &next)) {
			puts("The playlist is empty, going into standby mode.");
			pthread_mutex_lock(&s->state_mutex);
			s->state = STATE_STANDBY;
			pthread_mutex_unlock(&s->state_mutex);
			playlist_dequeue(s->playlist, &next);
		}

		/* Update the current state */
		pthread_mutex_lock(&s->state_mutex);
		printf("Next song: [%i] %s\n", next.id, next.file);
		s->current_song = next.file;
		s->state = STATE_PLAYING;
		pthread_mutex_unlock(&s->state_mutex);

		/* Play the song entirely */
		hls_remuxer_play(s->remuxer, next.file);

		/* Discard current_song, but keep the current 'playing' state. */
		pthread_mutex_lock(&s->state_mutex);
		free(s->current_song);
		s->current_song = NULL;
		pthread_mutex_unlock(&s->state_mutex);
	}

	return NULL;
}