Skip to content

Commit 03f2fb3

Browse files
committed
AAH_RX: Add the ability to control volume and stream type.
DO NOT MERGE Change the AAH_RX player so that it implements MediaPlayerHWInterface (instead of just MediaPlayerInterface) so the app level can control the rendering volume of individual RX player instances as well as the stream type of the audio tracks created by the RX player. Change-Id: Ieff1ea774f7981227546744883ee4b4e87a2cd2a Signed-off-by: John Grossman <johngro@google.com>
1 parent 9d98a08 commit 03f2fb3

File tree

6 files changed

+157
-22
lines changed

6 files changed

+157
-22
lines changed

media/libaah_rtp/aah_decoder_pump.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2011 The Android Open Source Project
2+
* Copyright (C) 2012 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,9 @@ AAH_DecoderPump::AAH_DecoderPump(OMXClient& omx)
4747
, last_queued_pts_valid_(false)
4848
, last_queued_pts_(0)
4949
, last_ts_transform_valid_(false)
50-
, last_volume_(0xFF) {
50+
, last_left_volume_(1.0f)
51+
, last_right_volume_(1.0f)
52+
, last_stream_type_(AUDIO_STREAM_DEFAULT) {
5153
thread_ = new ThreadWrapper(this);
5254
}
5355

@@ -102,13 +104,13 @@ void AAH_DecoderPump::queueToRenderer(MediaBuffer* decoded_sample) {
102104
if (NULL != renderer_) {
103105
int frameCount;
104106
AudioTrack::getMinFrameCount(&frameCount,
105-
AUDIO_STREAM_DEFAULT,
107+
last_stream_type_,
106108
static_cast<int>(format_sample_rate_));
107109
int ch_format = (format_channels_ == 1)
108110
? AUDIO_CHANNEL_OUT_MONO
109111
: AUDIO_CHANNEL_OUT_STEREO;
110112

111-
res = renderer_->set(AUDIO_STREAM_DEFAULT,
113+
res = renderer_->set(last_stream_type_,
112114
format_sample_rate_,
113115
AUDIO_FORMAT_PCM_16_BIT,
114116
ch_format,
@@ -128,9 +130,8 @@ void AAH_DecoderPump::queueToRenderer(MediaBuffer* decoded_sample) {
128130
delete renderer_;
129131
renderer_ = NULL;
130132
} else {
131-
float volume = static_cast<float>(last_volume_)
132-
/ 255.0f;
133-
if (renderer_->setVolume(volume, volume) != OK) {
133+
if (renderer_->setVolume(last_left_volume_,
134+
last_right_volume_) != OK) {
134135
LOGW("%s: setVolume failed", __FUNCTION__);
135136
}
136137

@@ -204,22 +205,41 @@ void AAH_DecoderPump::setRenderTSTransform(const LinearTransform& trans) {
204205
}
205206
}
206207

207-
void AAH_DecoderPump::setRenderVolume(uint8_t volume) {
208+
void AAH_DecoderPump::setRenderVolume(float left, float right) {
208209
Mutex::Autolock lock(&render_lock_);
209210

210-
if (volume == last_volume_) {
211+
if ((left == last_left_volume_) && (right == last_right_volume_)) {
211212
return;
212213
}
213214

214-
last_volume_ = volume;
215+
last_left_volume_ = left;
216+
last_right_volume_ = right;
217+
215218
if (renderer_ != NULL) {
216-
float volume = static_cast<float>(last_volume_) / 255.0f;
217-
if (renderer_->setVolume(volume, volume) != OK) {
219+
if (renderer_->setVolume(left, right) != OK) {
218220
LOGW("%s: setVolume failed", __FUNCTION__);
219221
}
220222
}
221223
}
222224

225+
void AAH_DecoderPump::setRenderStreamType(int stream_type) {
226+
Mutex::Autolock lock(&render_lock_);
227+
228+
if (last_stream_type_ == stream_type) {
229+
return;
230+
}
231+
232+
// TODO: figure out if changing stream type dynamically will ever be a
233+
// requirement. If it is not, we probably want to refactor this such that
234+
// stream type is only passed during construction.
235+
if (renderer_ != NULL) {
236+
LOGW("Attempting to change stream type (%d -> %d) after rendering has"
237+
" started", last_stream_type_, stream_type);
238+
}
239+
240+
last_stream_type_ = stream_type;
241+
}
242+
223243
// isAboutToUnderflow is something of a hack used to figure out when it might be
224244
// time to give up on trying to fill in a gap in the RTP sequence and simply
225245
// move on with a discontinuity. If we had perfect knowledge of when we were
@@ -509,7 +529,8 @@ status_t AAH_DecoderPump::shutdown_l() {
509529

510530
last_queued_pts_valid_ = false;
511531
last_ts_transform_valid_ = false;
512-
last_volume_ = 0xFF;
532+
last_left_volume_ = 1.0f;
533+
last_right_volume_ = 1.0f;
513534
thread_status_ = OK;
514535

515536
decoder_ = NULL;

media/libaah_rtp/aah_decoder_pump.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class AAH_DecoderPump : public MediaSource {
4242
status_t shutdown();
4343

4444
void setRenderTSTransform(const LinearTransform& trans);
45-
void setRenderVolume(uint8_t volume);
45+
void setRenderVolume(float left, float right);
46+
void setRenderStreamType(int stream_type);
4647
bool isAboutToUnderflow(int64_t threshold);
4748
bool getStatus() const { return thread_status_; }
4849

@@ -93,7 +94,9 @@ class AAH_DecoderPump : public MediaSource {
9394
int64_t last_queued_pts_;
9495
bool last_ts_transform_valid_;
9596
LinearTransform last_ts_transform_;
96-
uint8_t last_volume_;
97+
float last_left_volume_;
98+
float last_right_volume_;
99+
int last_stream_type_;
97100
CCHelper cc_helper_;
98101

99102
// protected by the thread_lock_

media/libaah_rtp/aah_rx_player.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ AAH_RXPlayer::AAH_RXPlayer()
4646
current_epoch_known_ = false;
4747
data_source_set_ = false;
4848
sock_fd_ = -1;
49+
audio_volume_left_ = 1.0;
50+
audio_volume_right_ = 1.0;
51+
audio_stream_type_ = AUDIO_STREAM_DEFAULT;
52+
audio_params_dirty_ = false;
4953

5054
substreams_.setCapacity(4);
5155

@@ -298,4 +302,38 @@ void AAH_RXPlayer::fetchAudioFlinger() {
298302
}
299303
}
300304

305+
status_t AAH_RXPlayer::setVolume(float leftVolume, float rightVolume) {
306+
AutoMutex api_lock(&api_lock_);
307+
308+
{ // explicit scope for autolock pattern
309+
AutoMutex api_lock(&audio_param_lock_);
310+
if ((leftVolume == audio_volume_left_) &&
311+
(rightVolume == audio_volume_right_)) {
312+
return OK;
313+
}
314+
315+
audio_volume_left_ = leftVolume;
316+
audio_volume_right_ = rightVolume;
317+
audio_params_dirty_ = true;
318+
}
319+
320+
signalEventFD(wakeup_work_thread_evt_fd_);
321+
322+
return OK;
323+
}
324+
325+
status_t AAH_RXPlayer::setAudioStreamType(int streamType) {
326+
AutoMutex api_lock(&api_lock_);
327+
328+
{ // explicit scope for autolock pattern
329+
AutoMutex api_lock(&audio_param_lock_);
330+
audio_stream_type_ = streamType;
331+
audio_params_dirty_ = true;
332+
}
333+
334+
signalEventFD(wakeup_work_thread_evt_fd_);
335+
336+
return OK;
337+
}
338+
301339
} // namespace android

media/libaah_rtp/aah_rx_player.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
namespace android {
3636

37-
class AAH_RXPlayer : public MediaPlayerInterface {
37+
class AAH_RXPlayer : public MediaPlayerHWInterface {
3838
public:
3939
AAH_RXPlayer();
4040

@@ -62,6 +62,9 @@ class AAH_RXPlayer : public MediaPlayerInterface {
6262
virtual status_t getParameter(int key, Parcel *reply);
6363
virtual status_t invoke(const Parcel& request, Parcel *reply);
6464

65+
virtual status_t setVolume(float leftVolume, float rightVolume);
66+
virtual status_t setAudioStreamType(int streamType);
67+
6568
protected:
6669
virtual ~AAH_RXPlayer();
6770

@@ -196,6 +199,9 @@ class AAH_RXPlayer : public MediaPlayerInterface {
196199
uint32_t getSSRC() const { return ssrc_; }
197200
uint8_t getProgramID() const { return (ssrc_ >> 5) & 0x1F; }
198201
status_t getStatus() const { return status_; }
202+
void setAudioSpecificParams(float left_vol,
203+
float right_vol,
204+
int stream_type);
199205

200206
void clearInactivityTimeout() {
201207
inactivity_timeout_.setTimeout(-1);
@@ -226,6 +232,7 @@ class AAH_RXPlayer : public MediaPlayerInterface {
226232
bool setupAACSubstreamMeta();
227233
bool setupSubstreamType(uint8_t substream_type,
228234
uint8_t codec_type);
235+
void applyVolume();
229236

230237
uint32_t ssrc_;
231238
bool waiting_for_rap_;
@@ -248,6 +255,11 @@ class AAH_RXPlayer : public MediaPlayerInterface {
248255
Timeout inactivity_timeout_;
249256
bool eos_reached_;
250257

258+
float audio_volume_local_left_;
259+
float audio_volume_local_right_;
260+
uint8_t audio_volume_remote_;
261+
int audio_stream_type_;
262+
251263
static const int64_t kAboutToUnderflowThreshold;
252264
static const int kInactivityTimeoutMsec;
253265

@@ -303,6 +315,12 @@ class AAH_RXPlayer : public MediaPlayerInterface {
303315
OMXClient omx_;
304316
CCHelper cc_helper_;
305317

318+
Mutex audio_param_lock_;
319+
float audio_volume_left_;
320+
float audio_volume_right_;
321+
int audio_stream_type_;
322+
bool audio_params_dirty_;
323+
306324
// Connection to audio flinger used to hack a path to setMasterVolume.
307325
sp<IAudioFlinger> audio_flinger_;
308326

media/libaah_rtp/aah_rx_player_core.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,30 @@ bool AAH_RXPlayer::threadLoop() {
316316
clearEventFD(wakeup_work_thread_evt_fd_);
317317
process_more_right_now = false;
318318

319-
// Step 2: Do we have data waiting in the socket? If so, drain the
319+
// Step 2: Do we have a change of audio parameters (volume/stream_type)
320+
// to apply to our current substreams? If so, go ahead and take care of
321+
// it.
322+
if (audio_params_dirty_) {
323+
float latched_left_volume, latched_right_volume;
324+
int latched_stream_type;
325+
{ // explicit scope for autolock pattern
326+
AutoMutex api_lock(&audio_param_lock_);
327+
latched_left_volume = audio_volume_left_;
328+
latched_right_volume = audio_volume_right_;
329+
latched_stream_type = audio_stream_type_;
330+
audio_params_dirty_ = false;
331+
}
332+
333+
for (size_t i = 0; i < substreams_.size(); ++i) {
334+
CHECK(substreams_.valueAt(i) != NULL);
335+
substreams_.valueAt(i)->setAudioSpecificParams(
336+
latched_left_volume,
337+
latched_right_volume,
338+
latched_stream_type);
339+
}
340+
}
341+
342+
// Step 3: Do we have data waiting in the socket? If so, drain the
320343
// socket moving valid RTP information into the ring buffer to be
321344
// processed.
322345
if (poll_fds[1].revents) {
@@ -403,13 +426,13 @@ bool AAH_RXPlayer::threadLoop() {
403426
}
404427
}
405428

406-
// Step 3: Process any data we mave have accumulated in the ring buffer
429+
// Step 4: Process any data we mave have accumulated in the ring buffer
407430
// so far.
408431
if (!thread_wrapper_->exitPending()) {
409432
processRingBuffer();
410433
}
411434

412-
// Step 4: At this point in time, the ring buffer should either be
435+
// Step 5: At this point in time, the ring buffer should either be
413436
// empty, or stalled in front of a gap caused by some dropped packets.
414437
// Check on the current gap situation and deal with it in an appropriate
415438
// fashion. If processGaps returns true, it means that it has given up
@@ -419,7 +442,7 @@ bool AAH_RXPlayer::threadLoop() {
419442
process_more_right_now = processGaps();
420443
}
421444

422-
// Step 5: Check for fatal errors. If any of our substreams has
445+
// Step 6: Check for fatal errors. If any of our substreams has
423446
// encountered a fatal, unrecoverable, error, then propagate the error
424447
// up to user level and shut down.
425448
for (size_t i = 0; i < substreams_.size(); ++i) {

media/libaah_rtp/aah_rx_player_substream.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ AAH_RXPlayer::Substream::Substream(uint32_t ssrc, OMXClient& omx) {
4444
status_ = OK;
4545
codec_mime_type_ = "";
4646
eos_reached_ = false;
47+
audio_volume_local_left_ = 1.0f;
48+
audio_volume_local_right_ = 1.0f;
49+
audio_volume_remote_ = 0xFF;
50+
audio_stream_type_ = AUDIO_STREAM_DEFAULT;
4751

4852
decoder_ = new AAH_DecoderPump(omx);
4953
if (decoder_ == NULL) {
@@ -242,8 +246,9 @@ void AAH_RXPlayer::Substream::processPayloadStart(uint8_t* buf,
242246
return;
243247
}
244248

245-
if (decoder_ != NULL) {
246-
decoder_->setRenderVolume(volume);
249+
if (audio_volume_remote_ != volume) {
250+
audio_volume_remote_ = volume;
251+
applyVolume();
247252
}
248253

249254
// TODO : move all of the constant flag and offset definitions for TRTP up
@@ -699,4 +704,31 @@ bool AAH_RXPlayer::Substream::setupSubstreamType(uint8_t substream_type,
699704
return true;
700705
}
701706

707+
void AAH_RXPlayer::Substream::setAudioSpecificParams(float left_vol,
708+
float right_vol,
709+
int stream_type) {
710+
if ((audio_volume_local_left_ != left_vol) ||
711+
(audio_volume_local_right_ != right_vol)) {
712+
audio_volume_local_left_ = left_vol;
713+
audio_volume_local_right_ = right_vol;
714+
applyVolume();
715+
}
716+
717+
if (audio_stream_type_ != stream_type) {
718+
audio_stream_type_ = stream_type;
719+
if (decoder_ != NULL) {
720+
decoder_->setRenderStreamType(audio_stream_type_);
721+
}
722+
}
723+
}
724+
725+
void AAH_RXPlayer::Substream::applyVolume() {
726+
if (decoder_ != NULL) {
727+
float remote_vol = static_cast<float>(audio_volume_remote_) /
728+
255.0f;
729+
decoder_->setRenderVolume(audio_volume_local_left_ * remote_vol,
730+
audio_volume_local_right_ * remote_vol);
731+
}
732+
}
733+
702734
} // namespace android

0 commit comments

Comments
 (0)