2019-02-17 11:08:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "media/streaming/media_streaming_audio_track.h"
|
|
|
|
|
|
|
|
#include "media/streaming/media_streaming_utility.h"
|
|
|
|
#include "media/audio/media_audio.h"
|
|
|
|
#include "media/audio/media_child_ffmpeg_loader.h"
|
|
|
|
#include "media/player/media_player_instance.h"
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
|
|
|
|
|
|
|
AudioTrack::AudioTrack(
|
2019-02-21 09:17:25 +00:00
|
|
|
const PlaybackOptions &options,
|
2019-02-17 11:08:29 +00:00
|
|
|
Stream &&stream,
|
2019-02-21 11:15:44 +00:00
|
|
|
AudioMsgId audioId,
|
2019-02-17 11:08:29 +00:00
|
|
|
FnMut<void(const Information &)> ready,
|
2019-03-05 13:56:27 +00:00
|
|
|
Fn<void(Error)> error)
|
2019-02-21 09:17:25 +00:00
|
|
|
: _options(options)
|
|
|
|
, _stream(std::move(stream))
|
2019-02-21 11:15:44 +00:00
|
|
|
, _audioId(audioId)
|
2019-02-17 11:08:29 +00:00
|
|
|
, _ready(std::move(ready))
|
2019-02-21 11:51:22 +00:00
|
|
|
, _error(std::move(error))
|
|
|
|
, _playPosition(options.position) {
|
2019-03-05 07:40:25 +00:00
|
|
|
Expects(_stream.duration > 1);
|
2019-02-17 11:08:29 +00:00
|
|
|
Expects(_ready != nullptr);
|
|
|
|
Expects(_error != nullptr);
|
2019-02-28 21:03:25 +00:00
|
|
|
Expects(_audioId.externalPlayId() != 0);
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AudioTrack::streamIndex() const {
|
|
|
|
// Thread-safe, because _stream.index is immutable.
|
|
|
|
return _stream.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVRational AudioTrack::streamTimeBase() const {
|
|
|
|
return _stream.timeBase;
|
|
|
|
}
|
|
|
|
|
2019-03-05 07:40:25 +00:00
|
|
|
crl::time AudioTrack::streamDuration() const {
|
|
|
|
return _stream.duration;
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
void AudioTrack::process(Packet &&packet) {
|
2019-02-20 13:28:48 +00:00
|
|
|
_noMoreData = packet.empty();
|
2019-02-21 11:15:44 +00:00
|
|
|
if (initialized()) {
|
2019-02-17 11:08:29 +00:00
|
|
|
mixerEnqueue(std::move(packet));
|
|
|
|
} else if (!tryReadFirstFrame(std::move(packet))) {
|
2019-03-05 13:56:27 +00:00
|
|
|
_error(Error::InvalidData);
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:40:09 +00:00
|
|
|
void AudioTrack::waitForData() {
|
|
|
|
if (initialized()) {
|
|
|
|
mixerForceToBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 11:15:44 +00:00
|
|
|
bool AudioTrack::initialized() const {
|
|
|
|
return !_ready;
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
bool AudioTrack::tryReadFirstFrame(Packet &&packet) {
|
|
|
|
if (ProcessPacket(_stream, std::move(packet)).failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (const auto error = ReadNextFrame(_stream)) {
|
2019-02-20 13:28:48 +00:00
|
|
|
if (error.code() == AVERROR_EOF) {
|
2019-02-22 12:39:32 +00:00
|
|
|
// Return the last valid frame if we seek too far.
|
|
|
|
return processFirstFrame();
|
2019-02-20 13:28:48 +00:00
|
|
|
} else if (error.code() != AVERROR(EAGAIN) || _noMoreData) {
|
|
|
|
return false;
|
2019-02-22 12:39:32 +00:00
|
|
|
} else {
|
|
|
|
// Waiting for more packets.
|
|
|
|
return true;
|
2019-02-20 13:28:48 +00:00
|
|
|
}
|
|
|
|
} else if (!fillStateFromFrame()) {
|
2019-02-17 11:08:29 +00:00
|
|
|
return false;
|
2019-02-22 12:39:32 +00:00
|
|
|
} else if (_startedPosition < _options.position) {
|
|
|
|
// Seek was with AVSEEK_FLAG_BACKWARD so first we get old frames.
|
|
|
|
// Try skipping frames until one is after the requested position.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return processFirstFrame();
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
2019-02-22 12:39:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioTrack::processFirstFrame() {
|
2019-02-17 11:08:29 +00:00
|
|
|
mixerInit();
|
|
|
|
callReady();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioTrack::fillStateFromFrame() {
|
2019-02-22 12:39:32 +00:00
|
|
|
const auto position = FramePosition(_stream);
|
|
|
|
if (position == kTimeUnknown) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_startedPosition = position;
|
|
|
|
return true;
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioTrack::mixerInit() {
|
2019-02-21 11:15:44 +00:00
|
|
|
Expects(!initialized());
|
2019-02-17 11:08:29 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
auto data = std::make_unique<ExternalSoundData>();
|
|
|
|
data->frame = std::move(_stream.frame);
|
|
|
|
data->codec = std::move(_stream.codec);
|
2019-02-17 11:08:29 +00:00
|
|
|
data->frequency = _stream.frequency;
|
|
|
|
data->length = (_stream.duration * data->frequency) / 1000LL;
|
2019-02-21 09:17:25 +00:00
|
|
|
data->speed = _options.speed;
|
2019-02-28 21:03:25 +00:00
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
Media::Player::mixer()->play(
|
2019-02-21 11:15:44 +00:00
|
|
|
_audioId,
|
2019-02-17 11:08:29 +00:00
|
|
|
std::move(data),
|
2019-02-20 13:28:48 +00:00
|
|
|
_startedPosition);
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioTrack::callReady() {
|
|
|
|
Expects(_ready != nullptr);
|
|
|
|
|
2019-02-20 13:28:48 +00:00
|
|
|
auto data = AudioInformation();
|
|
|
|
data.state.duration = _stream.duration;
|
|
|
|
data.state.position = _startedPosition;
|
|
|
|
data.state.receivedTill = _noMoreData
|
|
|
|
? _stream.duration
|
|
|
|
: _startedPosition;
|
|
|
|
base::take(_ready)({ VideoInformation(), data });
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioTrack::mixerEnqueue(Packet &&packet) {
|
2019-02-28 21:03:25 +00:00
|
|
|
Media::Player::mixer()->feedFromExternal({
|
|
|
|
_audioId,
|
|
|
|
std::move(packet)
|
2019-02-17 11:08:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:40:09 +00:00
|
|
|
void AudioTrack::mixerForceToBuffer() {
|
2019-02-28 21:03:25 +00:00
|
|
|
Media::Player::mixer()->forceToBufferExternal(_audioId);
|
2019-02-21 13:40:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 14:57:00 +00:00
|
|
|
void AudioTrack::pause(crl::time time) {
|
|
|
|
Expects(initialized());
|
|
|
|
|
|
|
|
Media::Player::mixer()->pause(_audioId, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioTrack::resume(crl::time time) {
|
2019-02-21 11:15:44 +00:00
|
|
|
Expects(initialized());
|
2019-02-20 13:28:48 +00:00
|
|
|
|
2019-02-21 11:15:44 +00:00
|
|
|
Media::Player::mixer()->resume(_audioId, true);
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:01:55 +00:00
|
|
|
void AudioTrack::setSpeed(float64 speed) {
|
|
|
|
_options.speed = speed;
|
2019-02-28 21:03:25 +00:00
|
|
|
Media::Player::mixer()->setSpeedFromExternal(_audioId, speed);
|
2019-02-21 16:01:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:28:10 +00:00
|
|
|
rpl::producer<> AudioTrack::waitingForData() const {
|
|
|
|
return _waitingForData.events();
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:28:48 +00:00
|
|
|
rpl::producer<crl::time> AudioTrack::playPosition() {
|
2019-02-17 11:08:29 +00:00
|
|
|
Expects(_ready == nullptr);
|
|
|
|
|
|
|
|
if (!_subscription) {
|
2019-02-20 13:28:48 +00:00
|
|
|
_subscription = Media::Player::Updated(
|
|
|
|
).add_subscription([=](const AudioMsgId &id) {
|
|
|
|
using State = Media::Player::State;
|
2019-02-21 11:15:44 +00:00
|
|
|
if (id != _audioId) {
|
2019-02-20 13:28:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-28 21:03:25 +00:00
|
|
|
const auto state = Media::Player::mixer()->currentState(
|
|
|
|
_audioId.type());
|
2019-02-21 11:15:44 +00:00
|
|
|
if (state.id != _audioId) {
|
2019-02-25 10:17:25 +00:00
|
|
|
// #TODO streaming later muted by other
|
2019-02-20 13:28:48 +00:00
|
|
|
return;
|
|
|
|
} else switch (state.state) {
|
|
|
|
case State::Stopped:
|
|
|
|
case State::StoppedAtEnd:
|
|
|
|
case State::PausedAtEnd:
|
|
|
|
_playPosition.reset();
|
|
|
|
return;
|
|
|
|
case State::StoppedAtError:
|
|
|
|
case State::StoppedAtStart:
|
2019-03-05 13:56:27 +00:00
|
|
|
_error(Error::InvalidData);
|
2019-02-20 13:28:48 +00:00
|
|
|
return;
|
|
|
|
case State::Starting:
|
|
|
|
case State::Playing:
|
|
|
|
case State::Stopping:
|
|
|
|
case State::Pausing:
|
|
|
|
case State::Resuming:
|
2019-02-22 14:28:10 +00:00
|
|
|
if (state.waitingForData) {
|
|
|
|
_waitingForData.fire({});
|
|
|
|
}
|
2019-03-05 07:40:25 +00:00
|
|
|
_playPosition = std::clamp(
|
2019-03-05 15:24:55 +00:00
|
|
|
crl::time((state.position * 1000 + (state.frequency / 2))
|
2019-03-05 07:40:25 +00:00
|
|
|
/ state.frequency),
|
|
|
|
crl::time(0),
|
|
|
|
_stream.duration - 1);
|
2019-02-20 13:28:48 +00:00
|
|
|
return;
|
|
|
|
case State::Paused:
|
|
|
|
return;
|
|
|
|
}
|
2019-02-17 11:08:29 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-20 13:28:48 +00:00
|
|
|
return _playPosition.value();
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioTrack::~AudioTrack() {
|
2019-02-28 21:03:25 +00:00
|
|
|
if (_audioId.externalPlayId()) {
|
2019-02-21 11:15:44 +00:00
|
|
|
Media::Player::mixer()->stop(_audioId);
|
2019-02-17 11:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|