]> git.tdb.fi Git - libs/al.git/blob - source/jukebox.cpp
Fix a memory leak
[libs/al.git] / source / jukebox.cpp
1 #include <algorithm>
2 #include <cstdlib>
3 #include <stdexcept>
4 #include "jukebox.h"
5 #include "playlist.h"
6 #include "sounddecoder.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace AL {
12
13 Jukebox::Jukebox():
14         streamer(source),
15         in(0),
16         decoder(0),
17         playlist(0),
18         current(0)
19 { }
20
21 Jukebox::~Jukebox()
22 {
23         streamer.stop();
24         delete decoder;
25         delete in;
26 }
27
28 void Jukebox::set_playlist(const Playlist *p)
29 {
30         bool was_playing = decoder;
31         stop();
32         playlist = p;
33         if(playlist && was_playing)
34                 play();
35 }
36
37 void Jukebox::play()
38 {
39         if(!playlist || playlist->empty() || decoder)
40                 return;
41
42         in = playlist->open(current);
43         decoder = SoundDecoder::open_io(*in);
44         streamer.play(*decoder);
45 }
46
47 void Jukebox::next()
48 {
49         if(!playlist || playlist->empty())
50                 return;
51
52         stop();
53         current = playlist->advance(current, 1);
54         signal_track_changed.emit(current);
55         play();
56 }
57
58 void Jukebox::previous()
59 {
60         if(!playlist || playlist->empty())
61                 return;
62
63         stop();
64         current = playlist->advance(current, -1);
65         signal_track_changed.emit(current);
66         play();
67 }
68
69 void Jukebox::stop()
70 {
71         streamer.stop();
72         delete decoder;
73         decoder = 0;
74         delete in;
75         in = 0;
76 }
77
78 void Jukebox::tick()
79 {
80         streamer.tick();
81         if(decoder && decoder->eof())
82                 next();
83 }
84
85 } // namespace AL
86 } // namespace Msp