2017-05-03 11:36:39 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-05-03 11:36:39 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-05-03 11:36:39 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/timer.h"
|
2018-03-27 12:16:00 +00:00
|
|
|
#include "base/bytes.h"
|
2024-02-01 06:19:46 +00:00
|
|
|
#include "webrtc/webrtc_device_resolver.h"
|
2017-05-03 11:36:39 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
namespace Core {
|
|
|
|
class FileLocation;
|
|
|
|
} // namespace Core
|
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
class Instance;
|
|
|
|
|
|
|
|
class Track {
|
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
Track(not_null<Instance*> instance);
|
2017-05-03 11:36:39 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void samplePeakEach(crl::time peakDuration);
|
2017-05-07 19:09:20 +00:00
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
void fillFromData(bytes::vector &&data);
|
2020-10-13 16:43:18 +00:00
|
|
|
void fillFromFile(const Core::FileLocation &location);
|
2017-05-03 11:36:39 +00:00
|
|
|
void fillFromFile(const QString &filePath);
|
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
void playOnce() {
|
|
|
|
playWithLooping(false);
|
|
|
|
}
|
|
|
|
void playInLoop() {
|
|
|
|
playWithLooping(true);
|
|
|
|
}
|
2017-05-03 11:36:39 +00:00
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
bool isLooping() const {
|
|
|
|
return _looping;
|
|
|
|
}
|
|
|
|
bool isActive() const {
|
|
|
|
return _active;
|
|
|
|
}
|
2017-05-03 11:36:39 +00:00
|
|
|
bool failed() const {
|
|
|
|
return _failed;
|
|
|
|
}
|
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
int64 getLengthMs() const {
|
|
|
|
return _lengthMs;
|
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
float64 getPeakValue(crl::time when) const;
|
2017-05-03 13:01:15 +00:00
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
void detachFromDevice();
|
|
|
|
void reattachToDevice();
|
|
|
|
void updateState();
|
|
|
|
|
|
|
|
~Track();
|
|
|
|
|
|
|
|
private:
|
2017-05-03 13:01:15 +00:00
|
|
|
void finish();
|
|
|
|
void ensureSourceCreated();
|
|
|
|
void playWithLooping(bool looping);
|
2017-05-03 11:36:39 +00:00
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Instance*> _instance;
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
bool _failed = false;
|
|
|
|
bool _active = false;
|
|
|
|
bool _looping = false;
|
|
|
|
float64 _volume = 1.;
|
|
|
|
|
|
|
|
int64 _samplesCount = 0;
|
|
|
|
int32 _sampleRate = 0;
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector _samples;
|
2017-05-03 11:36:39 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _peakDurationMs = 0;
|
2017-05-07 19:09:20 +00:00
|
|
|
int _peakEachPosition = 0;
|
|
|
|
std::vector<uint16> _peaks;
|
|
|
|
uint16 _peakValueMin = 0;
|
|
|
|
uint16 _peakValueMax = 0;
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _lengthMs = 0;
|
|
|
|
crl::time _stateUpdatedAt = 0;
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
int32 _alFormat = 0;
|
|
|
|
int64 _alPosition = 0;
|
|
|
|
uint32 _alSource = 0;
|
|
|
|
uint32 _alBuffer = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Instance {
|
|
|
|
public:
|
|
|
|
// Thread: Main.
|
|
|
|
Instance();
|
|
|
|
|
2024-02-01 06:19:46 +00:00
|
|
|
// Thread: Any. Must be locked: AudioMutex.
|
|
|
|
[[nodiscard]] Webrtc::DeviceResolvedId playbackDeviceId() const;
|
|
|
|
|
|
|
|
// Thread: Main.
|
|
|
|
[[nodiscard]] Webrtc::DeviceResolvedId captureDeviceId() const;
|
2024-01-22 16:40:55 +00:00
|
|
|
|
|
|
|
[[nodiscard]] std::unique_ptr<Track> createTrack();
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
void detachTracks();
|
|
|
|
void reattachTracks();
|
|
|
|
bool hasActiveTracks() const;
|
|
|
|
|
|
|
|
void scheduleDetachFromDevice();
|
|
|
|
void scheduleDetachIfNotUsed();
|
|
|
|
void stopDetachIfNotUsed();
|
|
|
|
|
|
|
|
~Instance();
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Track;
|
|
|
|
void registerTrack(Track *track);
|
|
|
|
void unregisterTrack(Track *track);
|
2017-05-03 13:01:15 +00:00
|
|
|
void trackStarted(Track *track);
|
|
|
|
void trackFinished(Track *track);
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::set<Track*> _tracks;
|
2024-02-01 06:19:46 +00:00
|
|
|
Webrtc::DeviceResolver _playbackDeviceId;
|
|
|
|
Webrtc::DeviceResolver _captureDeviceId;
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
base::Timer _updateTimer;
|
|
|
|
|
|
|
|
base::Timer _detachFromDeviceTimer;
|
|
|
|
bool _detachFromDeviceForce = false;
|
|
|
|
|
2024-01-22 16:40:55 +00:00
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
};
|
|
|
|
|
2024-01-22 16:40:55 +00:00
|
|
|
[[nodiscard]] Instance &Current();
|
2017-05-03 11:36:39 +00:00
|
|
|
|
|
|
|
} // namespace Audio
|
|
|
|
} // namespace Media
|