summaryrefslogtreecommitdiffstats
path: root/modules/audio_remote_submix/audio_hw.cpp
diff options
context:
space:
mode:
authorJean-Michel Trivi <jmtrivi@google.com>2012-12-18 11:30:33 -0800
committerJean-Michel Trivi <jmtrivi@google.com>2012-12-21 12:24:58 -0800
commiteafbfa4058370f113bd94d597f54f28ba41c8a96 (patch)
tree867fde8c039e2770c06864e81e6b6e3ee85f41a7 /modules/audio_remote_submix/audio_hw.cpp
parent459afa7fb59b0a88d7e86d5544469b1c9aea7b73 (diff)
downloadhardware_libhardware-eafbfa4058370f113bd94d597f54f28ba41c8a96.zip
hardware_libhardware-eafbfa4058370f113bd94d597f54f28ba41c8a96.tar.gz
hardware_libhardware-eafbfa4058370f113bd94d597f54f28ba41c8a96.tar.bz2
Use strong pointers and scope
Rely on strong pointers and scope rather than the more error prone incStrong / decStrong approach which can cause object leaks. No change in functionality. Change-Id: I1bfc7f0cdeeac022e4120482cd3521e52ffea94e
Diffstat (limited to 'modules/audio_remote_submix/audio_hw.cpp')
-rwxr-xr-xmodules/audio_remote_submix/audio_hw.cpp97
1 files changed, 46 insertions, 51 deletions
diff --git a/modules/audio_remote_submix/audio_hw.cpp b/modules/audio_remote_submix/audio_hw.cpp
index e9759e5..5e88ef7 100755
--- a/modules/audio_remote_submix/audio_hw.cpp
+++ b/modules/audio_remote_submix/audio_hw.cpp
@@ -186,18 +186,16 @@ static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
pthread_mutex_lock(&out->dev->lock);
- MonoPipe* sink = out->dev->rsxSink.get();
- if (sink != NULL) {
- sink->incStrong(out);
- } else {
- pthread_mutex_unlock(&out->dev->lock);
- return 0;
- }
-
- ALOGI("shutdown");
- sink->shutdown(true);
+ { // using the sink
+ sp<MonoPipe> sink = out->dev->rsxSink.get();
+ if (sink == 0) {
+ pthread_mutex_unlock(&out->dev->lock);
+ return 0;
+ }
- sink->decStrong(out);
+ ALOGI("shutdown");
+ sink->shutdown(true);
+ } // done using the sink
pthread_mutex_unlock(&out->dev->lock);
}
@@ -240,16 +238,16 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
out->dev->output_standby = false;
- MonoPipe* sink = out->dev->rsxSink.get();
- if (sink != NULL) {
+ sp<MonoPipe> sink = out->dev->rsxSink.get();
+ if (sink != 0) {
if (sink->isShutdown()) {
+ sink.clear();
pthread_mutex_unlock(&out->dev->lock);
// the pipe has already been shutdown, this buffer will be lost but we must
// simulate timing so we don't drain the output faster than realtime
usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
return bytes;
}
- sink->incStrong(buffer);
} else {
pthread_mutex_unlock(&out->dev->lock);
ALOGE("out_write without a pipe!");
@@ -260,12 +258,13 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
pthread_mutex_unlock(&out->dev->lock);
written_frames = sink->write(buffer, frames);
+
if (written_frames < 0) {
if (written_frames == (ssize_t)NEGOTIATE) {
ALOGE("out_write() write to pipe returned NEGOTIATE");
pthread_mutex_lock(&out->dev->lock);
- sink->decStrong(buffer);
+ sink.clear();
pthread_mutex_unlock(&out->dev->lock);
written_frames = 0;
@@ -278,9 +277,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
}
pthread_mutex_lock(&out->dev->lock);
-
- sink->decStrong(buffer);
-
+ sink.clear();
pthread_mutex_unlock(&out->dev->lock);
if (written_frames < 0) {
@@ -414,45 +411,43 @@ static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
}
in->read_counter_frames += frames_to_read;
+ size_t remaining_frames = frames_to_read;
- MonoPipeReader* source = in->dev->rsxSource.get();
- if (source != NULL) {
- source->incStrong(buffer);
- } else {
- ALOGE("no audio pipe yet we're trying to read!");
- pthread_mutex_unlock(&in->dev->lock);
- usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
- memset(buffer, 0, bytes);
- return bytes;
- }
+ {
+ // about to read from audio source
+ sp<MonoPipeReader> source = in->dev->rsxSource.get();
+ if (source == 0) {
+ ALOGE("no audio pipe yet we're trying to read!");
+ pthread_mutex_unlock(&in->dev->lock);
+ usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
+ memset(buffer, 0, bytes);
+ return bytes;
+ }
- pthread_mutex_unlock(&in->dev->lock);
+ pthread_mutex_unlock(&in->dev->lock);
- // read the data from the pipe (it's non blocking)
- size_t remaining_frames = frames_to_read;
- int attempts = 0;
- char* buff = (char*)buffer;
- while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
- attempts++;
- frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
- if (frames_read > 0) {
- remaining_frames -= frames_read;
- buff += frames_read * frame_size;
- //ALOGV(" in_read (att=%d) got %ld frames, remaining=%u",
- // attempts, frames_read, remaining_frames);
- } else {
- //ALOGE(" in_read read returned %ld", frames_read);
- usleep(READ_ATTEMPT_SLEEP_MS * 1000);
+ // read the data from the pipe (it's non blocking)
+ int attempts = 0;
+ char* buff = (char*)buffer;
+ while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
+ attempts++;
+ frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
+ if (frames_read > 0) {
+ remaining_frames -= frames_read;
+ buff += frames_read * frame_size;
+ //ALOGV(" in_read (att=%d) got %ld frames, remaining=%u",
+ // attempts, frames_read, remaining_frames);
+ } else {
+ //ALOGE(" in_read read returned %ld", frames_read);
+ usleep(READ_ATTEMPT_SLEEP_MS * 1000);
+ }
}
+ // done using the source
+ pthread_mutex_lock(&in->dev->lock);
+ source.clear();
+ pthread_mutex_unlock(&in->dev->lock);
}
- // done using the source
- pthread_mutex_lock(&in->dev->lock);
-
- source->decStrong(buffer);
-
- pthread_mutex_unlock(&in->dev->lock);
-
if (remaining_frames > 0) {
ALOGV(" remaining_frames = %d", remaining_frames);
memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0,