• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Play a shared stream that might use MMAP.
18 // Then play a second stream at a different sample rate.
19 // Make sure the first stream is still running.
20 // See: b/73369112 | AAudio disconnects shared stream if second MMAP open fails
21 
22 #include <memory.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 
26 #include <android-base/macros.h>
27 #include <aaudio/AAudio.h>
28 
29 #include <gtest/gtest.h>
30 
31 // Callback function that fills the audio output buffer.
MyDataCallbackProc(AAudioStream * stream,void * userData,void * audioData,int32_t numFrames)32 aaudio_data_callback_result_t MyDataCallbackProc(
33         AAudioStream *stream,
34         void *userData,
35         void *audioData,
36         int32_t numFrames) {
37     (void) userData;
38     int32_t numSamples = AAudioStream_getChannelCount(stream) * numFrames;
39     aaudio_format_t format = AAudioStream_getFormat(stream);
40     if (format == AAUDIO_FORMAT_PCM_I16) {
41         memset(audioData, 0, numSamples * sizeof(int16_t));
42     } else if (format == AAUDIO_FORMAT_PCM_FLOAT) {
43         memset(audioData, 0, numSamples * sizeof(float));
44     }
45     return AAUDIO_CALLBACK_RESULT_CONTINUE;
46 }
47 
48 //void foo() { // for tricking the Android Studio formatter
TEST(test_interference,aaudio_mmap_interference)49 TEST(test_interference, aaudio_mmap_interference) {
50 
51     AAudioStreamBuilder *aaudioBuilder = nullptr;
52     AAudioStream *aaudioStream1 = nullptr;
53     AAudioStream *aaudioStream2 = nullptr;
54 
55     // Use an AAudioStreamBuilder to contain requested parameters.
56     ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
57 
58     // Request stream properties.
59     AAudioStreamBuilder_setSampleRate(aaudioBuilder, 48000);
60     AAudioStreamBuilder_setDataCallback(aaudioBuilder, MyDataCallbackProc, nullptr);
61     AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
62 
63     // Create an AAudioStream using the Builder.
64     ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream1));
65     // Start it running.
66     ASSERT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream1));
67 
68     // Verify that the stream is running.
69     sleep(1);
70     EXPECT_LT(0, AAudioStream_getFramesRead(aaudioStream1));
71     ASSERT_EQ(AAUDIO_STREAM_STATE_STARTED, AAudioStream_getState(aaudioStream1));
72 
73     // Now try to open a second stream with a different rate.
74     AAudioStreamBuilder_setSampleRate(aaudioBuilder, 44100);
75     ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream2));
76     ASSERT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream2));
77 
78     // Verify that the second stream is running.
79     sleep(1);
80     EXPECT_LT(0, AAudioStream_getFramesRead(aaudioStream2));
81 
82     EXPECT_EQ(AAUDIO_STREAM_STATE_STARTED, AAudioStream_getState(aaudioStream2));
83 
84     // Now verify that the first stream is still running.
85     EXPECT_EQ(AAUDIO_STREAM_STATE_STARTED, AAudioStream_getState(aaudioStream1));
86 
87     int32_t framesRead1_1 = AAudioStream_getFramesRead(aaudioStream1);
88     EXPECT_LT(0, framesRead1_1);
89     sleep(1);
90     int32_t framesRead1_2 = AAudioStream_getFramesRead(aaudioStream1);
91     EXPECT_LT(0, framesRead1_2);
92     EXPECT_LT(framesRead1_1, framesRead1_2); // advancing?
93 
94     AAudioStream_close(aaudioStream2);
95     AAudioStream_close(aaudioStream1);
96     AAudioStreamBuilder_delete(aaudioBuilder);
97 }
98