• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 
18 #ifndef CTSAUDIO_AUDIOPROTOCOL_H
19 #define CTSAUDIO_AUDIOPROTOCOL_H
20 
21 #include <stdint.h>
22 
23 #include <utils/StrongPointer.h>
24 #include "Log.h"
25 #include "audio/Buffer.h"
26 #include "ClientSocket.h"
27 
28 #define U32_ENDIAN_SWAP(x) ( ((x) & 0x000000ff)<<24 | ((x) & 0x0000ff00)<<8 | \
29         ((x) & 0x00ff0000)>>8 | ((x) & 0xff000000)>>24 )
30 
31 class AudioParam {
32 public:
33     bool mStereo;
34     uint32_t mSamplingF;
35     uint32_t mMode;
36     uint32_t mNumberRepetition; // only for playback
37     uint32_t mVolume;
38     uint32_t mId;
39     android::sp<Buffer> mBuffer;
40 };
41 
42 class AudioProtocol {
43 public:
44     enum CommandId {
45         ECmdStart               = 0x12340001, //not actual command
46         ECmdDownload            = 0x12340001,
47         ECmdStartPlayback       = 0x12340002,
48         ECmdStopPlayback        = 0x12340003,
49         ECmdStartRecording      = 0x12340004,
50         ECmdStopRecording       = 0x12340005,
51         ECmdLast                = 0x12340006, // not actual command
52     };
53 
54     static const uint32_t REPLY_HEADER_SIZE = 12;
55     // up to 5 parameters for command / reply
56     class ProtocolParam {
57     public:
58         void* param[5];
59     };
60 
~AudioProtocol()61     virtual ~AudioProtocol() {
62         //LOGD("~AudioProtocol %x", this);
63     };
64 
65     /// default implementation, no param, no payload
66     virtual bool sendCommand(AudioParam& param);
67     /// default implementation, no param, no payload
68     virtual bool handleReply(const uint32_t* data, AudioParam* param);
69 
70     /**
71      * read header of reply and returns CommandId of reply.
72      * @param socket socket to read
73      * @param data pointer to buffer to store header, it should be uint32_t[3]
74      * @param id types of reply
75      * @return true if everything OK
76      */
77     static bool handleReplyHeader(ClientSocket& socket, uint32_t* data, CommandId& id);
78 
79 protected:
AudioProtocol(ClientSocket & socket,uint32_t command)80     AudioProtocol(ClientSocket& socket, uint32_t command)
81         : mCommand(command),
82           mSocket(socket) {};
83 
sendData(const char * data,int len)84     bool sendData(const char* data, int len) {
85         return mSocket.sendData(data, len);
86     };
87 
88     bool checkHeaderId(const uint32_t* data, uint32_t command);
readData(char * data,int len)89     bool readData(char* data, int len) {
90         return mSocket.readData(data, len);
91     };
92 
93 protected:
94     int mBuffer[8];
95 private:
96     uint32_t mCommand;
97     ClientSocket& mSocket;
98 
99 };
100 
101 class CmdDownload: public AudioProtocol {
102 public:
CmdDownload(ClientSocket & socket)103     CmdDownload(ClientSocket& socket)
104         : AudioProtocol(socket, ECmdDownload) {};
~CmdDownload()105     virtual ~CmdDownload() {};
106     virtual bool sendCommand(AudioParam& param);
107 };
108 
109 
110 class CmdStartPlayback: public AudioProtocol {
111 public:
CmdStartPlayback(ClientSocket & socket)112     CmdStartPlayback(ClientSocket& socket)
113         : AudioProtocol(socket, ECmdStartPlayback) {};
~CmdStartPlayback()114     virtual ~CmdStartPlayback() {};
115     virtual bool sendCommand(AudioParam& param);
116 };
117 
118 class CmdStopPlayback: public AudioProtocol {
119 public:
CmdStopPlayback(ClientSocket & socket)120     CmdStopPlayback(ClientSocket& socket)
121         : AudioProtocol(socket, ECmdStopPlayback) {};
~CmdStopPlayback()122     virtual ~CmdStopPlayback() {};
123 };
124 
125 class CmdStartRecording: public AudioProtocol {
126 public:
CmdStartRecording(ClientSocket & socket)127     CmdStartRecording(ClientSocket& socket)
128         : AudioProtocol(socket, ECmdStartRecording) {};
~CmdStartRecording()129     virtual ~CmdStartRecording() {};
130 
131     virtual bool sendCommand(AudioParam& param);
132 
133     virtual bool handleReply(const uint32_t* data, AudioParam* param);
134 };
135 
136 class CmdStopRecording: public AudioProtocol {
137 public:
CmdStopRecording(ClientSocket & socket)138     CmdStopRecording(ClientSocket& socket)
139         : AudioProtocol(socket, ECmdStopRecording) {};
~CmdStopRecording()140     virtual ~CmdStopRecording() {};
141 };
142 
143 
144 
145 
146 #endif // CTSAUDIO_AUDIOPROTOCOL_H
147