1 /*
2  * Copyright (C) 2021 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 #ifndef A2DP_DATA_SERVICE_H
17 #define A2DP_DATA_SERVICE_H
18 
19 #include <chrono>
20 #include <fstream>
21 #include <iostream>
22 #include <signal.h>
23 #include <string>
24 #include <time.h>
25 #include <cstring>
26 #include <sys/mman.h>
27 
28 namespace stub {
29 static const int PCM_HEADER_SIZE = 44;
30 
31 enum PcmChannelMode : uint8_t {
32     PCM_CHANNEL_MODE_SINGLE = 0x00, /* single channel */
33     PCM_CHANNEL_MODE_DOUBLE = 0x01, /* couple channel */
34     PCM_CHANNEL_MODE_MAX = 0x02,    /* Max count      */
35 };
36 
37 enum PcmBitsPerSample : uint8_t {
38     PCM_SAMPLE_BITS_16 = 0x0,  /* BitsPerSample:16bit */
39     PCM_SAMPLE_BITS_24 = 0x1,  /* BitsPerSample:16bit */
40     PCM_SAMPLE_BITS_32 = 0x2,  /* BitsPerSample:16bit */
41     PCM_SAMPLE_BITS_MAX = 0x3, /* Max count           */
42 };
43 
44 enum PcmCodecSampleRate : uint8_t {
45     PCM_SAMPLE_RATE_44100 = 0x0, /* SampleRate 44.1 KHz */
46     PCM_SAMPLE_RATE_48000 = 0x1, /* SampleRate 48   KHz */
47     PCM_SAMPLE_RATE_MAX = 0x2,   /* Max count           */
48 };
49 
50 enum SinkCodecType : uint8_t {
51     Sink_CODEC_TYPE_SBC = 0x00,
52     Sink_CODEC_TYPE_AAC = 0x01,
53     Sink_CODEC_TYPE_MAX = 0x02,
54 };
55 
56 enum ErrorType : uint16_t {
57     ERROR_TYPE_NONE = 0x00,
58     ERROR_TYPE_TIMEOUT = 0x01,
59     ERROR_TYPE_EXTRA_GETDATA = 0x02,
60     ERROR_TYPE_DATASIZE_ERROR = 0x04,
61     ERROR_TYPE_COMPARE_DATA_ERROR = 0x08,
62 };
63 
64 class A2dpServiceObserver {
65 public:
66     virtual ~A2dpServiceObserver() = default;
OnSourceSendPcmStreamError(uint16_t errorType,uint32_t data)67     virtual void OnSourceSendPcmStreamError(uint16_t errorType, uint32_t data)
68     {}
OnSourceSendComplete(uint8_t result)69     virtual void OnSourceSendComplete(uint8_t result)
70     {}
OnSinkGetPcmStreamError(uint16_t errorType,uint32_t data)71     virtual void OnSinkGetPcmStreamError(uint16_t errorType, uint32_t data)
72     {}
OnSinkGetComplete(uint8_t result)73     virtual void OnSinkGetComplete(uint8_t result)
74     {}
75 };
76 
77 class A2dpService {
78 public:
79     static A2dpService *GetInstance();
80 
getPCMStream(uint32_t & dataSize,bool & result)81     char *getPCMStream(uint32_t &dataSize, bool &result)
82     {
83         return nullptr;
84     }
setPCMStream(char * dataStream,uint32_t dataSize)85     void setPCMStream(char *dataStream, uint32_t dataSize)
86     {}
RegisterObserver(A2dpServiceObserver * observer)87     void RegisterObserver(A2dpServiceObserver *observer)
88     {}
DeregisterObserver(A2dpServiceObserver * observer)89     void DeregisterObserver(A2dpServiceObserver *observer)
90     {}
91     bool SourceInitPcmStream(uint8_t sampleRate, uint8_t channelMode, uint8_t bitsPerSample = PCM_SAMPLE_BITS_16)
92     {
93         return false;
94     }
95     void SourceStartSendPcmStream(uint32_t sendMSTime, bool isSbc, bool fromPCMBegin = true)
96     {}
97     bool SinkInitPcmStream(uint8_t sampleRate, uint8_t channelMode, uint8_t codecType,
98         uint8_t bitsPerSample = PCM_SAMPLE_BITS_16, bool writeFileNoCheck = false)
99     {
100         return false;
101     }
102     void SinkStartGetPcmStream(uint32_t sendMSTime, bool isSbc, bool fromPCMBegin = true)
103     {}
getSourceMemFd()104     int getSourceMemFd()
105     {
106         return -1;
107     }
getSinkMemFd()108     int getSinkMemFd()
109     {
110         return -1;
111     }
112 
113 private:
114     A2dpService() = default;
115     ~A2dpService() = default;
116 
117     A2dpService(const A2dpService &) = delete;
118     A2dpService &operator=(const A2dpService &) = delete;
119 };
120 }  // namespace stub
121 #endif  // A2DP_SERVICE_H