-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.c
168 lines (136 loc) · 5.42 KB
/
playlist.c
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
#include "restream.h"
#include "guide.h"
#include "playlist.h"
#include "infile.h"
#include "reader.h"
#include "writer.h"
int playlist_free(ctx_restream *restrm){
int indx;
snprintf(restrm->function_name,1024,"%s","playlist_free");
if (restrm != NULL) {
if(restrm->playlist_count > 0 ){
indx = 0;
while (indx < restrm->playlist_count) {
if (restrm->playlist[indx].movie_path != NULL){
free(restrm->playlist[indx].movie_path);
}
indx ++;
}
}
if (restrm->playlist != NULL ) free(restrm->playlist);
}
//fprintf(stderr,"Playlist freed \n");
return 0;
}
void playlist_sort_alpha(ctx_restream *restrm){
char *temp;
int indx1, indx2;
snprintf(restrm->function_name,1024,"%s","playlist_sort_alpha");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
//fprintf(stderr,"%s: alpha \n",restrm->guide_info->guide_displayname);
for(indx1=0; indx1 < restrm->playlist_count - 1 ; indx1++){
for(indx2 =indx1 + 1; indx2 < restrm->playlist_count ; indx2++)
{
if(strcasecmp(restrm->playlist[indx1].movie_path,restrm->playlist[indx2].movie_path) > 0)
{
temp = restrm->playlist[indx1].movie_path;
restrm->playlist[indx1].movie_path = restrm->playlist[indx2].movie_path;
restrm->playlist[indx2].movie_path = temp;
}
}
}
}
void playlist_sort_random(ctx_restream *restrm){
char *temp;
int indx1, indx2, tmpseq, tmplen;
snprintf(restrm->function_name,1024,"%s","playlist_sort_random");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
/*
fprintf(stderr,"%s: seed: %u\n"
,restrm->guide_info->guide_displayname
,usec );
srand(usec);
*/
for(indx1=0; indx1 <= restrm->playlist_count-1 ; indx1++){
restrm->playlist[indx1].movie_seq = rand_r(&restrm->rand_seed);
/*
fprintf(stderr," %d %s: \n"
,restrm->playlist[indx1].movie_seq
,restrm->playlist[indx1].movie_path);
*/
}
for(indx1=0; indx1 < restrm->playlist_count-1 ; indx1++){
for(indx2 =0; indx2 < restrm->playlist_count-1-indx1; indx2++) {
if(restrm->playlist[indx2].movie_seq > restrm->playlist[indx2+1].movie_seq) {
temp = restrm->playlist[indx2].movie_path;
tmpseq = restrm->playlist[indx2].movie_seq;
tmplen = restrm->playlist[indx2].path_length;
restrm->playlist[indx2].movie_path = restrm->playlist[indx2+1].movie_path;
restrm->playlist[indx2].movie_seq = restrm->playlist[indx2+1].movie_seq;
restrm->playlist[indx2].path_length = restrm->playlist[indx2+1].path_length;
restrm->playlist[indx2+1].movie_path = temp;
restrm->playlist[indx2+1].movie_seq = tmpseq;
restrm->playlist[indx2+1].path_length = tmplen;
}
}
}
/*
for(indx1=0; indx1 <= restrm->playlist_count-1 ; indx1++){
fprintf(stderr," %d %s: \n"
,restrm->playlist[indx1].movie_seq
, restrm->playlist[indx1].movie_path);
}
*/
}
int playlist_loaddir(ctx_restream *restrm){
DIR *d;
struct dirent *dir;
size_t basepath_len, totlen;
int indx, retcd;
snprintf(restrm->function_name,1024,"%s","playlist_loaddir");
restrm->watchdog_playlist = av_gettime_relative() + 5000000;
playlist_free(restrm);
restrm->playlist_count = 0;
d = opendir(restrm->playlist_dir);
if (d) {
basepath_len = strlen(restrm->playlist_dir)+1;
while ((dir=readdir(d)) != NULL){
if ((strstr(dir->d_name,".mkv") != NULL) || (strstr(dir->d_name,".mp4") != NULL)) {
restrm->playlist_count++;
}
}
if (restrm->playlist_count != 0){
restrm->playlist = malloc(sizeof(struct playlist_item) * restrm->playlist_count);
rewinddir(d);
indx = 0;
while ((dir=readdir(d)) != NULL){
if ((strstr(dir->d_name,".mkv") != NULL) || (strstr(dir->d_name,".mp4") != NULL)) {
totlen =basepath_len + strlen(dir->d_name);
restrm->playlist[indx].movie_path = calloc(totlen + 2,sizeof(char));
retcd = snprintf(restrm->playlist[indx].movie_path, totlen+1
,"%s%s",restrm->playlist_dir,dir->d_name);
if (retcd < 0){
fprintf(stderr,"Error on playlist %s\n",restrm->playlist_dir);
return -1;
}
indx ++;
}
}
if (strcasecmp(restrm->playlist_sort_method,"a") == 0) {
playlist_sort_alpha(restrm);
} else {
playlist_sort_random(restrm);
}
for(indx=0; indx < restrm->playlist_count; indx++){
restrm->playlist[indx].path_length = basepath_len + strlen(restrm->playlist[indx].movie_path) ;
//fprintf(stderr,"%d: %s \n",restrm->playlist[indx].path_length, restrm->playlist[indx].movie_path);
}
}
closedir(d);
}
if (restrm->playlist_count == 0){
fprintf(stderr,"Playlist count is zero\n");
return -1;
}
return 0;
}