1 #pragma once
2 
3 #include "sources/audiosourceproxy.h"
4 #include "track/track_decl.h"
5 
6 namespace mixxx {
7 
8 class TrackPointerHolder {
9   public:
TrackPointerHolder(TrackPointer && pTrack)10     explicit TrackPointerHolder(
11             TrackPointer&& pTrack)
12             : m_pTrack(std::move(pTrack)) {
13     }
14 
15   private:
16     TrackPointer m_pTrack;
17 };
18 
19 // Keeps the Track object alive while accessing the audio data
20 // of the track. The Track object must not be deleted while
21 // accessing the corresponding file to avoid file
22 // corruption when writing metadata while the file
23 // is still in use.
24 class AudioSourceTrackProxy : private TrackPointerHolder,
25                               // The audio source must be closed BEFORE releasing the track
26                               // pointer to close any open file handles. Otherwise exporting
27                               // track metadata into the same file may not work, because the
28                               // file is still locked by the OS!
29                               public AudioSourceProxy {
30   public:
create(TrackPointer pTrack,AudioSourcePointer pAudioSource)31     static AudioSourcePointer create(
32             TrackPointer pTrack,
33             AudioSourcePointer pAudioSource) {
34         return std::make_shared<AudioSourceTrackProxy>(
35                 std::move(pTrack),
36                 std::move(pAudioSource));
37     }
38 
AudioSourceTrackProxy(TrackPointer pTrack,AudioSourcePointer pAudioSource)39     AudioSourceTrackProxy(
40             TrackPointer pTrack,
41             AudioSourcePointer pAudioSource)
42             : TrackPointerHolder(std::move(pTrack)),
43               AudioSourceProxy(std::move(pAudioSource)) {
44     }
45 };
46 
47 } // namespace mixxx
48