1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <common.h>
17 
18 using namespace OHOS::AudioStandard;
19 
Enqueue(SLOHBufferQueueItf self,const void * buffer,SLuint32 size)20 SLresult Enqueue(SLOHBufferQueueItf self, const void *buffer, SLuint32 size)
21 {
22     if (self == nullptr) {
23         return SL_RESULT_PARAMETER_INVALID;
24     }
25     IOHBufferQueue *thiz = (IOHBufferQueue *)self;
26     if (thiz->mIid == SL_IID_PLAY) {
27         AudioPlayerAdapter::GetInstance()->EnqueueAdapter(thiz->mId, buffer, size);
28     } else if (thiz->mIid == SL_IID_RECORD) {
29         AudioCapturerAdapter::GetInstance()->EnqueueAdapter(thiz->mId, buffer, size);
30     }
31 
32     return SL_RESULT_SUCCESS;
33 }
34 
Clear(SLOHBufferQueueItf self)35 SLresult Clear(SLOHBufferQueueItf self)
36 {
37     if (self == nullptr) {
38         return SL_RESULT_PARAMETER_INVALID;
39     }
40     IOHBufferQueue *thiz = (IOHBufferQueue *)self;
41     if (thiz->mIid == SL_IID_PLAY) {
42         AudioPlayerAdapter::GetInstance()->ClearAdapter(thiz->mId);
43     } else if (thiz->mIid == SL_IID_RECORD) {
44         AudioCapturerAdapter::GetInstance()->ClearAdapter(thiz->mId);
45     }
46 
47     return SL_RESULT_SUCCESS;
48 }
49 
GetState(SLOHBufferQueueItf self,SLOHBufferQueueState * state)50 SLresult GetState(SLOHBufferQueueItf self, SLOHBufferQueueState *state)
51 {
52     if (self == nullptr) {
53         return SL_RESULT_PARAMETER_INVALID;
54     }
55     IOHBufferQueue *thiz = (IOHBufferQueue *)self;
56     if (thiz->mIid == SL_IID_PLAY) {
57         AudioPlayerAdapter::GetInstance()->GetStateAdapter(thiz->mId, state);
58     } else if (thiz->mIid == SL_IID_RECORD) {
59         AudioCapturerAdapter::GetInstance()->GetStateAdapter(thiz->mId, state);
60     }
61 
62     return SL_RESULT_SUCCESS;
63 }
64 
GetBuffer(SLOHBufferQueueItf self,SLuint8 ** buffer,SLuint32 * size)65 static SLresult GetBuffer(SLOHBufferQueueItf self, SLuint8 **buffer, SLuint32 *size)
66 {
67     if (self == nullptr) {
68         return SL_RESULT_PARAMETER_INVALID;
69     }
70     IOHBufferQueue *thiz = (IOHBufferQueue *)self;
71     if (thiz->mIid == SL_IID_PLAY) {
72         AudioPlayerAdapter::GetInstance()->GetBufferAdapter(thiz->mId, buffer, size);
73     } else if (thiz->mIid == SL_IID_RECORD) {
74         AudioCapturerAdapter::GetInstance()->GetBufferAdapter(thiz->mId, buffer, size);
75     }
76     return SL_RESULT_SUCCESS;
77 }
78 
RegisterCallback(SLOHBufferQueueItf self,SlOHBufferQueueCallback callback,void * pContext)79 SLresult RegisterCallback(SLOHBufferQueueItf self, SlOHBufferQueueCallback callback, void *pContext)
80 {
81     if (self == nullptr || callback == nullptr) {
82         return SL_RESULT_PARAMETER_INVALID;
83     }
84     IOHBufferQueue *thiz = (IOHBufferQueue *)self;
85     if (thiz->mIid == SL_IID_PLAY) {
86         AudioPlayerAdapter::GetInstance()->RegisterCallbackAdapter(self, callback, pContext);
87     } else if (thiz->mIid == SL_IID_RECORD) {
88         AudioCapturerAdapter::GetInstance()->RegisterCallbackAdapter(self, callback, pContext);
89     }
90     return SL_RESULT_SUCCESS;
91 }
92 
93 static const struct SLOHBufferQueueItf_ IOHBufferQueueItf = {
94     Enqueue,
95     Clear,
96     GetState,
97     GetBuffer,
98     RegisterCallback
99 };
100 
IOHBufferQueueInit(void * self,const SLInterfaceID iid,SLuint32 id)101 void IOHBufferQueueInit(void *self, const SLInterfaceID iid, SLuint32 id)
102 {
103     IOHBufferQueue *thiz = (IOHBufferQueue *) self;
104     thiz->mItf = &IOHBufferQueueItf;
105     thiz->mIid = iid;
106     thiz->mId = id;
107     if (thiz->mIid == SL_IID_PLAY) {
108         thiz->mState = SL_PLAYSTATE_STOPPED;
109     } else if (thiz->mIid == SL_IID_RECORD) {
110         thiz->mState = SL_RECORDSTATE_STOPPED;
111     }
112 }
113