blob: c6028120e11eab65465702ef36e0231498d1f8dc [file] [log] [blame]
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_RENDERER_IMPL_INTERNAL_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_RENDERER_IMPL_INTERNAL_H_
#include <vector>
#include "starboard/atomic.h"
#include "starboard/audio_sink.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/log.h"
#include "starboard/media.h"
#include "starboard/shared/internal_only.h"
#include "starboard/shared/starboard/player/closure.h"
#include "starboard/shared/starboard/player/decoded_audio_internal.h"
#include "starboard/shared/starboard/player/filter/audio_decoder_internal.h"
#include "starboard/shared/starboard/player/filter/audio_frame_tracker.h"
#include "starboard/shared/starboard/player/filter/audio_renderer_internal.h"
#include "starboard/shared/starboard/player/filter/audio_resampler.h"
#include "starboard/shared/starboard/player/filter/audio_time_stretcher.h"
#include "starboard/shared/starboard/player/input_buffer_internal.h"
#include "starboard/shared/starboard/player/job_queue.h"
#include "starboard/types.h"
namespace starboard {
namespace shared {
namespace starboard {
namespace player {
namespace filter {
// A default implementation of |AudioRenderer| that only depends on the
// |AudioDecoder| interface, rather than a platform specific implementation.
class AudioRendererImpl : public AudioRenderer, private JobQueue::JobOwner {
public:
static const size_t kDefaultMaxCachedFrames = 256 * 1024;
static const size_t kDefaultMaxFramesPerAppend = 16384;
// |max_cached_frames| is a soft limit for the max audio frames this class can
// cache so it can:
// 1. Avoid using too much memory.
// 2. Have the audio cache full to simulate the state that the renderer can no
// longer accept more data.
// |max_frames_per_append| is the max number of frames that the audio renderer
// tries to append to the sink buffer at once.
AudioRendererImpl(scoped_ptr<AudioDecoder> decoder,
const SbMediaAudioHeader& audio_header,
size_t max_cached_frames = kDefaultMaxCachedFrames,
size_t max_frames_per_append = kDefaultMaxFramesPerAppend);
~AudioRendererImpl() SB_OVERRIDE;
void Initialize(const Closure& error_cb) SB_OVERRIDE;
void WriteSample(const InputBuffer& input_buffer) SB_OVERRIDE;
void WriteEndOfStream() SB_OVERRIDE;
void Play() SB_OVERRIDE;
void Pause() SB_OVERRIDE;
void SetPlaybackRate(double playback_rate) SB_OVERRIDE;
void SetVolume(double volume) SB_OVERRIDE;
void Seek(SbMediaTime seek_to_pts) SB_OVERRIDE;
bool IsEndOfStreamWritten() const SB_OVERRIDE {
return eos_state_.load() >= kEOSWrittenToDecoder;
};
bool IsEndOfStreamPlayed() const SB_OVERRIDE;
bool CanAcceptMoreData() const SB_OVERRIDE;
bool IsSeekingInProgress() const SB_OVERRIDE;
// This function can be called from any thread.
SbMediaTime GetCurrentTime() SB_OVERRIDE;
protected:
enum EOSState {
kEOSNotReceived,
kEOSWrittenToDecoder,
kEOSDecoded,
kEOSSentToSink
};
const size_t max_cached_frames_;
const size_t max_frames_per_append_;
atomic_bool paused_;
atomic_bool consume_frames_called_;
atomic_bool seeking_;
SbMediaTime seeking_to_pts_;
AudioFrameTracker audio_frame_tracker_;
atomic_int64_t frames_sent_to_sink_;
atomic_int64_t frames_consumed_by_sink_;
atomic_int32_t frames_consumed_by_sink_since_last_get_current_time_;
scoped_ptr<AudioDecoder> decoder_;
atomic_int64_t frames_consumed_set_at_;
atomic_double playback_rate_;
private:
void CreateAudioSinkAndResampler();
void UpdateSourceStatus(int* frames_in_buffer,
int* offset_in_frames,
bool* is_playing,
bool* is_eos_reached);
void ConsumeFrames(int frames_consumed);
void LogFramesConsumed();
void OnDecoderConsumed();
void OnDecoderOutput();
void ProcessAudioData();
void FillResamplerAndTimeStretcher();
bool AppendAudioToFrameBuffer(bool* is_frame_buffer_full);
// SbAudioSink callbacks
static void UpdateSourceStatusFunc(int* frames_in_buffer,
int* offset_in_frames,
bool* is_playing,
bool* is_eos_reached,
void* context);
static void ConsumeFramesFunc(int frames_consumed, void* context);
atomic_int32_t eos_state_;
const int channels_;
const SbMediaAudioSampleType sink_sample_type_;
const int bytes_per_frame_;
scoped_ptr<AudioResampler> resampler_;
AudioTimeStretcher time_stretcher_;
double volume_;
std::vector<uint8_t> frame_buffer_;
uint8_t* frame_buffers_[1];
int32_t pending_decoder_outputs_;
Closure log_frames_consumed_closure_;
SbAudioSink audio_sink_;
bool can_accept_more_data_;
bool process_audio_data_scheduled_;
Closure process_audio_data_closure_;
// Our owner will attempt to seek to pts 0 when playback begins. In
// general, seeking could require a full reset of the underlying decoder on
// some platforms, so we make an effort to improve playback startup
// performance by keeping track of whether we already have a fresh decoder,
// and can thus avoid doing a full reset.
bool decoder_needs_full_reset_;
};
} // namespace filter
} // namespace player
} // namespace starboard
} // namespace shared
} // namespace starboard
#endif // STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_RENDERER_IMPL_INTERNAL_H_