1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CAST_STANDALONE_RECEIVER_SDL_AUDIO_PLAYER_H_ 6 #define CAST_STANDALONE_RECEIVER_SDL_AUDIO_PLAYER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "cast/standalone_receiver/sdl_player_base.h" 12 13 namespace openscreen { 14 namespace cast { 15 16 // Consumes frames from a Receiver, decodes them, and renders them to an 17 // internally-owned SDL audio device. 18 class SDLAudioPlayer final : public SDLPlayerBase { 19 public: 20 // |error_callback| is run only if a fatal error occurs, at which point the 21 // player has halted and set |error_status()|. 22 SDLAudioPlayer(ClockNowFunctionPtr now_function, 23 TaskRunner* task_runner, 24 Receiver* receiver, 25 AudioCodec codec, 26 std::function<void()> error_callback); 27 28 ~SDLAudioPlayer() final; 29 30 private: 31 // SDLPlayerBase implementation. 32 ErrorOr<Clock::time_point> RenderNextFrame( 33 const SDLPlayerBase::PresentableFrame& frame) final; 34 bool RenderWhileIdle(const SDLPlayerBase::PresentableFrame* frame) final; 35 void Present() final; 36 37 // Maps an AVSampleFormat enum to the SDL_AudioFormat equivalent. 38 static SDL_AudioFormat GetSDLAudioFormat(AVSampleFormat format); 39 40 // The audio format determined by the last call to RenderCurrentFrame(). 41 SDL_AudioSpec pending_audio_spec_{}; 42 43 // The amount of time before a target presentation time to call Present(), to 44 // account for audio buffering (the latency until samples reach the hardware). 45 Clock::duration approximate_lead_time_{}; 46 47 // When the decoder provides planar data, this buffer is used for storing the 48 // interleaved conversion. 49 std::vector<uint8_t> interleaved_audio_buffer_; 50 51 // Points to the memory containing the next chunk of interleaved audio. 52 absl::Span<const uint8_t> pending_audio_; 53 54 // The currently-open SDL audio device (or zero, if not open). 55 SDL_AudioDeviceID device_ = 0; 56 57 // The audio format being used by the currently-open SDL audio device. 58 SDL_AudioSpec device_spec_{}; 59 }; 60 61 } // namespace cast 62 } // namespace openscreen 63 64 #endif // CAST_STANDALONE_RECEIVER_SDL_AUDIO_PLAYER_H_ 65