-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.c
180 lines (159 loc) · 4.51 KB
/
stream.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
169
170
171
172
173
174
175
176
177
178
179
180
/*
* MP3/MPlayer plugin to VDR (C++)
*
* (C) 2001-2005 Stefan Huelswitt <s.huelswitt@gmx.de>
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "common.h"
#include "setup-karaoke.h"
#include "stream.h"
#include "version.h"
#define USE_MMAP
#define MAX_MMAP_SIZE (32*1024*1024)
#define MP3FILE_BUFSIZE (32*1024)
#ifdef USE_MMAP
#include <sys/mman.h>
#endif
#define DEFAULT_PORT 80 // default port for streaming (HTTP)
//#define DUMP_HEAD "/var/tmp/headers"
// --- cStream -----------------------------------------------------------------
cStream::cStream(const char *Filename)
:cFileInfo(Filename)
{
fd=-1; ismmap=false; buffer=0;
}
cStream::~cStream()
{
Close();
}
bool cStream::Open(bool log)
{
if(fd>=0) return Seek();
if(FileInfo(log)) {
if((fd=open(Filename,O_RDONLY))>=0) {
buffpos=readpos=0; fill=0;
#ifdef USE_MMAP
if(Filesize<=MAX_MMAP_SIZE) {
buffer=(unsigned char *)mmap(0,Filesize,PROT_READ,MAP_SHARED,fd,0);
if(buffer!=MAP_FAILED) {
ismmap=true;
return true;
}
else dsyslog("mmap() failed for %s: %s",Filename,strerror(errno));
}
#endif
buffer = new unsigned char[MP3FILE_BUFSIZE];
if(buffer) return true;
else { esyslog("[vdr-karaoke] ERROR: not enough memory for buffer: %s",Filename); }
}
else if(log) { esyslog("[vdr-karaoke] ERROR: failed to open file %s: %s",Filename,strerror(errno)); }
}
Close();
return false;
}
void cStream::Close(void)
{
#ifdef USE_MMAP
if(ismmap) {
munmap(buffer,Filesize); buffer=0; ismmap=false;
}
else {
#endif
delete buffer; buffer=0;
#ifdef USE_MMAP
}
#endif
if(fd>=0) { close(fd); fd=-1; }
}
bool cStream::Seek(unsigned long long pos)
{
if(fd>=0 && pos>=0 && pos<=Filesize) {
buffpos=0; fill=0;
if(ismmap) {
readpos=pos;
return true;
}
else {
if((readpos=lseek64(fd,pos,SEEK_SET))>=0) {
if(readpos!=pos) { dsyslog("seek mismatch in %s, wanted %lld, got %lld",Filename,pos,readpos); }
return true;
}
else { esyslog("[vdr-karaoke] ERROR: seeking failed in %s: %d,%s",Filename,errno,strerror(errno)); }
}
}
else d(printf("[karaoke] karaoke: bad seek call fd=%d pos=%lld name=%s\n",fd,pos,Filename))
return false;
}
bool cStream::Stream(unsigned char * &data, unsigned long &len, const unsigned char *rest)
{
if(fd>=0) {
if(readpos<Filesize) {
if(ismmap) {
if(rest && fill) readpos=(rest-buffer); // take care of remaining data
fill=Filesize-readpos;
data=buffer+readpos; len=fill;
buffpos=readpos; readpos+=fill;
return true;
}
else {
if(rest && fill) { // copy remaining data to start of buffer
fill-=(rest-buffer); // remaing bytes
memmove(buffer,rest,fill);
}
else fill=0;
int r;
do {
r=read(fd,buffer+fill,MP3FILE_BUFSIZE-fill);
} while(r==-1 && errno==EINTR);
if(r>=0) {
buffpos=readpos-fill; readpos+=r; fill+=r;
data=buffer; len=fill;
return true;
}
else { esyslog("[vdr-karaoke] ERROR: read failed in %s: %d,%s",Filename,errno,strerror(errno)); }
}
}
else {
len=0;
return true;
}
}
return false;
}
// -----------------------------------------------------------------------------
#ifdef DUMP_HEAD
void Dump(const char *name, char *buffer)
{
FILE *f=fopen(name,"a");
if(f) {
fprintf(f,"<<<< %s\n",buffer);
/*
int n=strlen(buffer);
for(int i=0 ; i<n ; i+=8) {
fprintf(f,"%04x: ",i);
for(int l=0 ; l<8 && i+l<n ; l++) fprintf(f,"%02x ",buffer[i+l]);
fprintf(f,"\n");
}
*/
fclose(f);
}
}
#endif