media_streaming_audio_track.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "media/streaming/media_streaming_audio_track.h"
  8. #include "media/streaming/media_streaming_utility.h"
  9. #include "media/audio/media_audio.h"
  10. #include "media/audio/media_child_ffmpeg_loader.h"
  11. #include "media/player/media_player_instance.h"
  12. namespace Media {
  13. namespace Streaming {
  14. AudioTrack::AudioTrack(
  15. const PlaybackOptions &options,
  16. Stream &&stream,
  17. AudioMsgId audioId,
  18. FnMut<void(const Information &)> ready,
  19. Fn<void(Error)> error)
  20. : _options(options)
  21. , _stream(std::move(stream))
  22. , _audioId(audioId)
  23. , _ready(std::move(ready))
  24. , _error(std::move(error))
  25. , _playPosition(options.position) {
  26. Expects(_stream.duration > 1);
  27. Expects(_stream.duration != kDurationUnavailable); // Not supported.
  28. Expects(_ready != nullptr);
  29. Expects(_error != nullptr);
  30. Expects(_audioId.externalPlayId() != 0);
  31. }
  32. int AudioTrack::streamIndex() const {
  33. // Thread-safe, because _stream.index is immutable.
  34. return _stream.index;
  35. }
  36. AVRational AudioTrack::streamTimeBase() const {
  37. return _stream.timeBase;
  38. }
  39. crl::time AudioTrack::streamDuration() const {
  40. return _stream.duration;
  41. }
  42. void AudioTrack::process(std::vector<FFmpeg::Packet> &&packets) {
  43. if (packets.empty()) {
  44. return;
  45. } else if (packets.front().empty()) {
  46. Assert(packets.size() == 1);
  47. _readTillEnd = true;
  48. }
  49. for (auto i = begin(packets), e = end(packets); i != e; ++i) {
  50. if (initialized()) {
  51. mixerEnqueue(gsl::make_span(&*i, (e - i)));
  52. break;
  53. } else if (!tryReadFirstFrame(std::move(*i))) {
  54. _error(Error::InvalidData);
  55. break;
  56. }
  57. }
  58. }
  59. void AudioTrack::waitForData() {
  60. if (initialized()) {
  61. mixerForceToBuffer();
  62. }
  63. }
  64. bool AudioTrack::initialized() const {
  65. return !_ready;
  66. }
  67. bool AudioTrack::tryReadFirstFrame(FFmpeg::Packet &&packet) {
  68. if (ProcessPacket(_stream, std::move(packet)).failed()) {
  69. return false;
  70. }
  71. while (true) {
  72. if (const auto error = ReadNextFrame(_stream)) {
  73. if (error.code() == AVERROR_EOF) {
  74. if (!_initialSkippingFrame) {
  75. return false;
  76. }
  77. // Return the last valid frame if we seek too far.
  78. _stream.decodedFrame = std::move(_initialSkippingFrame);
  79. return processFirstFrame();
  80. } else if (error.code() != AVERROR(EAGAIN) || _readTillEnd) {
  81. return false;
  82. } else {
  83. // Waiting for more packets.
  84. return true;
  85. }
  86. } else if (!fillStateFromFrame()) {
  87. return false;
  88. } else if (_startedPosition >= _options.position) {
  89. return processFirstFrame();
  90. }
  91. // Seek was with AVSEEK_FLAG_BACKWARD so first we get old frames.
  92. // Try skipping frames until one is after the requested position.
  93. std::swap(_initialSkippingFrame, _stream.decodedFrame);
  94. if (!_stream.decodedFrame) {
  95. _stream.decodedFrame = FFmpeg::MakeFramePointer();
  96. }
  97. }
  98. }
  99. bool AudioTrack::processFirstFrame() {
  100. if (!FFmpeg::FrameHasData(_stream.decodedFrame.get())) {
  101. return false;
  102. }
  103. mixerInit();
  104. callReady();
  105. return true;
  106. }
  107. bool AudioTrack::fillStateFromFrame() {
  108. const auto position = FramePosition(_stream);
  109. if (position == kTimeUnknown) {
  110. return false;
  111. }
  112. _startedPosition = position;
  113. return true;
  114. }
  115. void AudioTrack::mixerInit() {
  116. Expects(!initialized());
  117. auto data = std::make_unique<ExternalSoundData>();
  118. data->frame = std::move(_stream.decodedFrame);
  119. data->codec = std::move(_stream.codec);
  120. data->duration = _stream.duration;
  121. data->speed = _options.speed;
  122. Media::Player::mixer()->play(
  123. _audioId,
  124. std::move(data),
  125. _startedPosition);
  126. }
  127. void AudioTrack::callReady() {
  128. Expects(_ready != nullptr);
  129. auto data = AudioInformation();
  130. data.state.duration = _stream.duration;
  131. data.state.position = _startedPosition;
  132. data.state.receivedTill = _readTillEnd
  133. ? _stream.duration
  134. : _startedPosition;
  135. base::take(_ready)({ VideoInformation(), data });
  136. }
  137. void AudioTrack::mixerEnqueue(gsl::span<FFmpeg::Packet> packets) {
  138. Media::Player::mixer()->feedFromExternal({
  139. _audioId,
  140. packets
  141. });
  142. }
  143. void AudioTrack::mixerForceToBuffer() {
  144. Media::Player::mixer()->forceToBufferExternal(_audioId);
  145. }
  146. void AudioTrack::pause(crl::time time) {
  147. Expects(initialized());
  148. Media::Player::mixer()->pause(_audioId, true);
  149. }
  150. void AudioTrack::resume(crl::time time) {
  151. Expects(initialized());
  152. Media::Player::mixer()->resume(_audioId, true);
  153. }
  154. void AudioTrack::stop() {
  155. if (_audioId.externalPlayId()) {
  156. Media::Player::mixer()->stop(_audioId);
  157. }
  158. }
  159. void AudioTrack::setSpeed(float64 speed) {
  160. _options.speed = speed;
  161. Media::Player::mixer()->setSpeedFromExternal(_audioId, speed);
  162. }
  163. rpl::producer<> AudioTrack::waitingForData() const {
  164. return _waitingForData.events();
  165. }
  166. rpl::producer<crl::time> AudioTrack::playPosition() {
  167. Expects(_ready == nullptr);
  168. if (!_subscription) {
  169. _subscription = Media::Player::Updated(
  170. ) | rpl::start_with_next([=](const AudioMsgId &id) {
  171. using State = Media::Player::State;
  172. if (id != _audioId) {
  173. return;
  174. }
  175. const auto state = Media::Player::mixer()->currentState(
  176. _audioId.type());
  177. if (state.id != _audioId) {
  178. // #TODO streaming later muted by other
  179. return;
  180. } else switch (state.state) {
  181. case State::Stopped:
  182. case State::StoppedAtEnd:
  183. case State::PausedAtEnd:
  184. _playPosition.reset();
  185. return;
  186. case State::StoppedAtError:
  187. case State::StoppedAtStart:
  188. _error(Error::InvalidData);
  189. return;
  190. case State::Starting:
  191. case State::Playing:
  192. case State::Stopping:
  193. case State::Pausing:
  194. case State::Resuming:
  195. if (state.waitingForData) {
  196. _waitingForData.fire({});
  197. }
  198. _playPosition = std::clamp(
  199. crl::time((state.position * 1000 + (state.frequency / 2))
  200. / state.frequency),
  201. crl::time(0),
  202. _stream.duration - 1);
  203. return;
  204. case State::Paused:
  205. return;
  206. }
  207. });
  208. }
  209. return _playPosition.value();
  210. }
  211. AudioTrack::~AudioTrack() {
  212. stop();
  213. }
  214. } // namespace Streaming
  215. } // namespace Media