• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 #include <stdlib.h>
18 
19 #include "common/OboeDebug.h"
20 #include "TestErrorCallback.h"
21 
22 using namespace oboe;
23 
open()24 oboe::Result TestErrorCallback::open() {
25     mCallbackMagic = 0;
26     mDataCallback = std::make_shared<MyDataCallback>();
27     mErrorCallback = std::make_shared<MyErrorCallback>(this);
28     AudioStreamBuilder builder;
29     oboe::Result result = builder.setSharingMode(oboe::SharingMode::Exclusive)
30             ->setPerformanceMode(oboe::PerformanceMode::LowLatency)
31             ->setFormat(oboe::AudioFormat::Float)
32             ->setChannelCount(kChannelCount)
33 #if 0
34             ->setDataCallback(mDataCallback.get())
35             ->setErrorCallback(mErrorCallback.get()) // This can lead to a crash or FAIL.
36 #else
37             ->setDataCallback(mDataCallback)
38             ->setErrorCallback(mErrorCallback) // shared_ptr avoids a crash
39 #endif
40             ->openStream(mStream);
41     return result;
42 }
43 
start()44 oboe::Result TestErrorCallback::start() {
45     return mStream->requestStart();
46 }
47 
stop()48 oboe::Result TestErrorCallback::stop() {
49     return mStream->requestStop();
50 }
51 
close()52 oboe::Result TestErrorCallback::close() {
53     return mStream->close();
54 }
55 
test()56 int TestErrorCallback::test() {
57     oboe::Result result = open();
58     if (result != oboe::Result::OK) {
59         return (int) result;
60     }
61     return (int) start();
62 }
63 
onAudioReady(AudioStream * audioStream,void * audioData,int32_t numFrames)64 DataCallbackResult TestErrorCallback::MyDataCallback::onAudioReady(
65         AudioStream *audioStream,
66         void *audioData,
67         int32_t numFrames) {
68     float *output = (float *) audioData;
69     // Fill buffer with random numbers to create "white noise".
70     int numSamples = numFrames * kChannelCount;
71     for (int i = 0; i < numSamples; i++) {
72         *output++ = (float)((drand48() - 0.5) * 0.2);
73     }
74     return oboe::DataCallbackResult::Continue;
75 }
76