~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/decoder/FlacIOHandle.cxx

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "FlacIOHandle.hxx"
 
22
#include "util/Error.hxx"
 
23
#include "Compiler.h"
 
24
 
 
25
#include <errno.h>
 
26
 
 
27
static size_t
 
28
FlacIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle)
 
29
{
 
30
        InputStream *is = (InputStream *)handle;
 
31
 
 
32
        uint8_t *const p0 = (uint8_t *)ptr, *p = p0,
 
33
                *const end = p0 + size * nmemb;
 
34
 
 
35
        /* libFLAC is very picky about short reads, and expects the IO
 
36
           callback to fill the whole buffer (undocumented!) */
 
37
 
 
38
        Error error;
 
39
        while (p < end) {
 
40
                size_t nbytes = is->LockRead(p, end - p, error);
 
41
                if (nbytes == 0) {
 
42
                        if (!error.IsDefined())
 
43
                                /* end of file */
 
44
                                break;
 
45
 
 
46
                        if (error.IsDomain(errno_domain))
 
47
                                errno = error.GetCode();
 
48
                        else
 
49
                                /* just some random non-zero
 
50
                                   errno value */
 
51
                                errno = EINVAL;
 
52
                        return 0;
 
53
                }
 
54
 
 
55
                p += nbytes;
 
56
        }
 
57
 
 
58
        /* libFLAC expects a clean errno after returning from the IO
 
59
           callbacks (undocumented!) */
 
60
        errno = 0;
 
61
        return (p - p0) / size;
 
62
}
 
63
 
 
64
static int
 
65
FlacIOSeek(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
 
66
{
 
67
        InputStream *is = (InputStream *)handle;
 
68
 
 
69
        Error error;
 
70
        return is->LockSeek(offset, whence, error) ? 0 : -1;
 
71
}
 
72
 
 
73
static FLAC__int64
 
74
FlacIOTell(FLAC__IOHandle handle)
 
75
{
 
76
        InputStream *is = (InputStream *)handle;
 
77
 
 
78
        return is->offset;
 
79
}
 
80
 
 
81
static int
 
82
FlacIOEof(FLAC__IOHandle handle)
 
83
{
 
84
        InputStream *is = (InputStream *)handle;
 
85
 
 
86
        return is->LockIsEOF();
 
87
}
 
88
 
 
89
static int
 
90
FlacIOClose(gcc_unused FLAC__IOHandle handle)
 
91
{
 
92
        /* no-op because the libFLAC caller is repsonsible for closing
 
93
           the #InputStream */
 
94
 
 
95
        return 0;
 
96
}
 
97
 
 
98
const FLAC__IOCallbacks flac_io_callbacks = {
 
99
        FlacIORead,
 
100
        nullptr,
 
101
        nullptr,
 
102
        nullptr,
 
103
        FlacIOEof,
 
104
        FlacIOClose,
 
105
};
 
106
 
 
107
const FLAC__IOCallbacks flac_io_callbacks_seekable = {
 
108
        FlacIORead,
 
109
        nullptr,
 
110
        FlacIOSeek,
 
111
        FlacIOTell,
 
112
        FlacIOEof,
 
113
        FlacIOClose,
 
114
};