summaryrefslogtreecommitdiffstats
path: root/src/sources/audiosourcetrackproxy.h
blob: dca3f58b494d1aaa7c5c759e175b848fcde66f07 (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
#pragma once

#include "sources/audiosourceproxy.h"
#include "track/track_decl.h"

namespace mixxx {

class TrackPointerHolder {
  public:
    explicit TrackPointerHolder(
            TrackPointer&& pTrack)
            : m_pTrack(std::move(pTrack)) {
    }

  private:
    TrackPointer m_pTrack;
};

// Keeps the Track object alive while accessing the audio data
// of the track. The Track object must not be deleted while
// accessing the corresponding file to avoid file
// corruption when writing metadata while the file
// is still in use.
class AudioSourceTrackProxy : private TrackPointerHolder,
                              // The audio source must be closed BEFORE releasing the track
                              // pointer to close any open file handles. Otherwise exporting
                              // track metadata into the same file may not work, because the
                              // file is still locked by the OS!
                              public AudioSourceProxy {
  public:
    static AudioSourcePointer create(
            TrackPointer pTrack,
            AudioSourcePointer pAudioSource) {
        return std::make_shared<AudioSourceTrackProxy>(
                std::move(pTrack),
                std::move(pAudioSource));
    }

    AudioSourceTrackProxy(
            TrackPointer pTrack,
            AudioSourcePointer pAudioSource)
            : TrackPointerHolder(std::move(pTrack)),
              AudioSourceProxy(std::move(pAudioSource)) {
    }
};

} // namespace mixxx