-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[media] Create AudioDiscardDurationTracker
1. Creates a class AudioDiscardDurationTracker to store the discard durations of audio buffers sent to pass-through audio renderers. The stored durations can then be used during media time calculation. This is implemented for the Android pass-through audio renderer. b/313719729
- Loading branch information
Showing
9 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
starboard/shared/starboard/player/filter/audio_discard_duration_tracker.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 The Cobalt Authors. 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. | ||
|
||
#include "starboard/shared/starboard/player/filter/audio_discard_duration_tracker.h" | ||
|
||
#include <algorithm> | ||
#include <unordered_map> | ||
|
||
#include "starboard/common/log.h" | ||
#include "starboard/media.h" | ||
#include "starboard/shared/starboard/media/media_util.h" | ||
|
||
namespace starboard { | ||
namespace shared { | ||
namespace starboard { | ||
namespace player { | ||
namespace filter { | ||
using shared::starboard::media::AudioSampleInfo; | ||
|
||
void AudioDiscardDurationTracker::CacheDiscardDuration( | ||
const InputBuffer& input_buffer) { | ||
SB_DCHECK(input_buffer.sample_type() == kSbMediaTypeAudio); | ||
const AudioSampleInfo audio_sample_info = input_buffer.audio_sample_info(); | ||
discard_durations_by_timestamp_[input_buffer.timestamp()] = | ||
audio_sample_info.discarded_duration_from_front + | ||
audio_sample_info.discarded_duration_from_back; | ||
} | ||
|
||
void AudioDiscardDurationTracker::CacheMultipleDiscardDurations( | ||
const InputBuffers& input_buffers) { | ||
for (const auto& input_buffer : input_buffers) { | ||
CacheDiscardDuration(*input_buffer.get()); | ||
} | ||
} | ||
|
||
void AudioDiscardDurationTracker::AddCachedDiscardDurationToTotal( | ||
SbTime timestamp, | ||
bool remove_timestamp_from_cache) { | ||
if (discard_durations_by_timestamp_.find(timestamp) == | ||
discard_durations_by_timestamp_.end()) { | ||
SB_LOG(INFO) << "Discarded duration for timestamp: " << timestamp | ||
<< " is not cached."; | ||
return; | ||
} | ||
|
||
if (discard_durations_by_timestamp_.find(timestamp) != | ||
discard_durations_by_timestamp_.end()) { | ||
total_discard_duration_ += discard_durations_by_timestamp_[timestamp]; | ||
if (remove_timestamp_from_cache) { | ||
discard_durations_by_timestamp_.erase(timestamp); | ||
} | ||
} | ||
} | ||
|
||
SbTime AudioDiscardDurationTracker::AdjustTimeForTotalDiscardDuration( | ||
SbTime timestamp) { | ||
SbTime adjusted_timestamp = timestamp - total_discard_duration_; | ||
return std::max(SbTime(0), adjusted_timestamp); | ||
} | ||
|
||
} // namespace filter | ||
} // namespace player | ||
} // namespace starboard | ||
} // namespace shared | ||
} // namespace starboard |
59 changes: 59 additions & 0 deletions
59
starboard/shared/starboard/player/filter/audio_discard_duration_tracker.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 The Cobalt Authors. 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_DISCARD_DURATION_TRACKER_H_ | ||
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_DISCARD_DURATION_TRACKER_H_ | ||
|
||
#include <unordered_map> | ||
|
||
#include "starboard/shared/internal_only.h" | ||
#include "starboard/shared/starboard/player/input_buffer_internal.h" | ||
#include "starboard/time.h" | ||
|
||
namespace starboard { | ||
namespace shared { | ||
namespace starboard { | ||
namespace player { | ||
namespace filter { | ||
|
||
// This class caches the discard durations of audio buffers to maintain a | ||
// running total discard duration. This total can be used to adjust the media | ||
// time of the playback when the audio buffers do not support partial audio, | ||
// and prevent an A/V desync. | ||
class AudioDiscardDurationTracker { | ||
public: | ||
void CacheDiscardDuration(const InputBuffer& input_buffer); | ||
void CacheMultipleDiscardDurations(const InputBuffers& input_buffers); | ||
void AddCachedDiscardDurationToTotal(SbTime timestamp, | ||
bool remove_timestamp_from_cache = true); | ||
SbTime AdjustTimeForTotalDiscardDuration(SbTime timestamp); | ||
void Reset() { | ||
discard_durations_by_timestamp_.clear(); | ||
total_discard_duration_ = 0; | ||
} | ||
|
||
SbTime total_discard_duration() const { return total_discard_duration_; } | ||
|
||
private: | ||
std::unordered_map<SbTime, SbTime> discard_durations_by_timestamp_; | ||
SbTime total_discard_duration_ = 0; | ||
}; | ||
|
||
} // namespace filter | ||
} // namespace player | ||
} // namespace starboard | ||
} // namespace shared | ||
} // namespace starboard | ||
|
||
#endif // STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_DISCARD_DURATION_TRACKER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters