MBED implementation of JAudioStream
JAudioStream.cpp@8:65d8e8882907, 2015-08-20 (annotated)
- Committer:
- ollie8
- Date:
- Thu Aug 20 07:17:51 2015 +0000
- Revision:
- 8:65d8e8882907
- Parent:
- 7:9dfba830a0c8
Add stream call back function. Internal workings of stream now contained.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ollie8 | 0:6f03883f9b43 | 1 | #include "JAudioStream.h" |
ollie8 | 7:9dfba830a0c8 | 2 | #define LOG_LEVEL_INFO |
ollie8 | 2:22ee01f24346 | 3 | #include "logger.h" |
ollie8 | 0:6f03883f9b43 | 4 | |
ollie8 | 8:65d8e8882907 | 5 | JAudioStream::JAudioStream(OnStream streamCallback, int bufferSize) { |
ollie8 | 8:65d8e8882907 | 6 | this->streamCallback = streamCallback; |
ollie8 | 8:65d8e8882907 | 7 | this->bufferSize = bufferSize; |
ollie8 | 8:65d8e8882907 | 8 | requestCount = bufferSize / MAX_PACKT; |
ollie8 | 2:22ee01f24346 | 9 | cont = "cont"; |
ollie8 | 2:22ee01f24346 | 10 | cond = "cond"; |
ollie8 | 2:22ee01f24346 | 11 | dcon = "dcon"; |
ollie8 | 4:ebdd1cf0c9f7 | 12 | dctd = "dctd"; |
ollie8 | 2:22ee01f24346 | 13 | rqst = "rqst"; |
ollie8 | 2:22ee01f24346 | 14 | begn = "begn"; |
ollie8 | 2:22ee01f24346 | 15 | comt = "comt"; |
ollie8 | 2:22ee01f24346 | 16 | hrbt = "hrbt"; |
ollie8 | 0:6f03883f9b43 | 17 | connected = false; |
ollie8 | 0:6f03883f9b43 | 18 | inTransmission = false; |
ollie8 | 0:6f03883f9b43 | 19 | } |
ollie8 | 0:6f03883f9b43 | 20 | |
ollie8 | 8:65d8e8882907 | 21 | bool JAudioStream::connect(char* ip, int port, char* name) { |
ollie8 | 1:b49a6e72f353 | 22 | eth.init(); |
ollie8 | 1:b49a6e72f353 | 23 | eth.connect(); |
ollie8 | 3:51a66c975910 | 24 | socket.init(); |
ollie8 | 7:9dfba830a0c8 | 25 | socket.set_blocking(false, 500); |
ollie8 | 0:6f03883f9b43 | 26 | remotePort = port; |
ollie8 | 0:6f03883f9b43 | 27 | remoteIp = ip; |
ollie8 | 1:b49a6e72f353 | 28 | endPoint.set_address(ip, port); |
ollie8 | 3:51a66c975910 | 29 | char message[MESSAGE_SIZE*2]; |
ollie8 | 0:6f03883f9b43 | 30 | buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); |
ollie8 | 3:51a66c975910 | 31 | send(message, MESSAGE_SIZE*2); |
ollie8 | 0:6f03883f9b43 | 32 | return connected; |
ollie8 | 0:6f03883f9b43 | 33 | } |
ollie8 | 0:6f03883f9b43 | 34 | |
ollie8 | 3:51a66c975910 | 35 | bool JAudioStream::read(char *buffer, int size) { |
ollie8 | 6:7e366261cd93 | 36 | TRACE("Received %s", buffer); |
ollie8 | 7:9dfba830a0c8 | 37 | if (socket.receiveFrom(endPoint, buffer, size)) { |
ollie8 | 7:9dfba830a0c8 | 38 | return true; |
ollie8 | 7:9dfba830a0c8 | 39 | } |
ollie8 | 7:9dfba830a0c8 | 40 | return false; |
ollie8 | 0:6f03883f9b43 | 41 | } |
ollie8 | 0:6f03883f9b43 | 42 | |
ollie8 | 0:6f03883f9b43 | 43 | bool JAudioStream::isConnected() { |
ollie8 | 0:6f03883f9b43 | 44 | return connected; |
ollie8 | 0:6f03883f9b43 | 45 | } |
ollie8 | 0:6f03883f9b43 | 46 | |
ollie8 | 0:6f03883f9b43 | 47 | void JAudioStream::request(char amount) { |
ollie8 | 0:6f03883f9b43 | 48 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 49 | buildMeassge(rqst, amount, message); |
ollie8 | 0:6f03883f9b43 | 50 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 51 | } |
ollie8 | 0:6f03883f9b43 | 52 | |
ollie8 | 0:6f03883f9b43 | 53 | void JAudioStream::disconnect() { |
ollie8 | 0:6f03883f9b43 | 54 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 55 | buildMeassge(dcon, -1, message); |
ollie8 | 0:6f03883f9b43 | 56 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 57 | } |
ollie8 | 0:6f03883f9b43 | 58 | |
ollie8 | 6:7e366261cd93 | 59 | void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { |
ollie8 | 2:22ee01f24346 | 60 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 3:51a66c975910 | 61 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 62 | char i; |
ollie8 | 2:22ee01f24346 | 63 | for (i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 64 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 65 | } |
ollie8 | 2:22ee01f24346 | 66 | for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 67 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 68 | } |
ollie8 | 0:6f03883f9b43 | 69 | } |
ollie8 | 0:6f03883f9b43 | 70 | |
ollie8 | 6:7e366261cd93 | 71 | void JAudioStream::buildMeassge(const char* cmd, int param, char* data, char* message) { |
ollie8 | 2:22ee01f24346 | 72 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 73 | char i; |
ollie8 | 2:22ee01f24346 | 74 | for (i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 75 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 76 | } |
ollie8 | 0:6f03883f9b43 | 77 | intTocharArr(param, bParam); |
ollie8 | 2:22ee01f24346 | 78 | for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 79 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 80 | } |
ollie8 | 0:6f03883f9b43 | 81 | char dataSize = MESSAGE_SIZE*2; |
ollie8 | 2:22ee01f24346 | 82 | for (i=MESSAGE_SIZE; i<dataSize; i++) { |
ollie8 | 0:6f03883f9b43 | 83 | message[i] = data[i-MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 84 | } |
ollie8 | 0:6f03883f9b43 | 85 | } |
ollie8 | 0:6f03883f9b43 | 86 | |
ollie8 | 1:b49a6e72f353 | 87 | void JAudioStream::intTocharArr(int value, char* buff) { |
ollie8 | 0:6f03883f9b43 | 88 | buff[0] = (value >> 24) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 89 | buff[1] = (value >> 16) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 90 | buff[2] = (value >> 8) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 91 | buff[3] = value; |
ollie8 | 0:6f03883f9b43 | 92 | } |
ollie8 | 0:6f03883f9b43 | 93 | |
ollie8 | 3:51a66c975910 | 94 | void JAudioStream::send(char* data, int size ) { |
ollie8 | 6:7e366261cd93 | 95 | TRACE("Sending %s", data); |
ollie8 | 3:51a66c975910 | 96 | socket.sendTo(endPoint, data, size); |
ollie8 | 0:6f03883f9b43 | 97 | } |
ollie8 | 0:6f03883f9b43 | 98 | |
ollie8 | 0:6f03883f9b43 | 99 | char* JAudioStream::getNowPlaying() { |
ollie8 | 0:6f03883f9b43 | 100 | return nowPlaying; |
ollie8 | 0:6f03883f9b43 | 101 | } |
ollie8 | 2:22ee01f24346 | 102 | |
ollie8 | 8:65d8e8882907 | 103 | int JAudioStream::loop() { |
ollie8 | 8:65d8e8882907 | 104 | int result = 0; |
ollie8 | 2:22ee01f24346 | 105 | char resp[MAX_PACKT]; |
ollie8 | 8:65d8e8882907 | 106 | if (read(resp, MAX_PACKT)) { |
ollie8 | 8:65d8e8882907 | 107 | if (memcmp(resp, cond, 4) == 0) { |
ollie8 | 8:65d8e8882907 | 108 | connected = true; |
ollie8 | 8:65d8e8882907 | 109 | } else if (memcmp(resp, dctd, 4) == 0) { |
ollie8 | 8:65d8e8882907 | 110 | connected = false; |
ollie8 | 8:65d8e8882907 | 111 | result = 1; |
ollie8 | 8:65d8e8882907 | 112 | } else if (memcmp(resp, hrbt, 4) == 0) { |
ollie8 | 8:65d8e8882907 | 113 | char message[MESSAGE_SIZE]; |
ollie8 | 8:65d8e8882907 | 114 | buildMeassge(hrbt, -1, message); |
ollie8 | 8:65d8e8882907 | 115 | send(message, MESSAGE_SIZE); |
ollie8 | 8:65d8e8882907 | 116 | } else if (memcmp(resp, begn, 4) == 0) { |
ollie8 | 8:65d8e8882907 | 117 | memmove(&nowPlaying[0], &resp[4], 20); |
ollie8 | 8:65d8e8882907 | 118 | inTransmission = true; |
ollie8 | 8:65d8e8882907 | 119 | } else if (memcmp(resp, comt, 4) == 0) { |
ollie8 | 8:65d8e8882907 | 120 | inTransmission = false; |
ollie8 | 8:65d8e8882907 | 121 | } else { |
ollie8 | 8:65d8e8882907 | 122 | char buffer[bufferSize]; |
ollie8 | 8:65d8e8882907 | 123 | char i = 0; |
ollie8 | 8:65d8e8882907 | 124 | while (i < requestCount) { |
ollie8 | 8:65d8e8882907 | 125 | stream.request(MAX_PACKT); |
ollie8 | 8:65d8e8882907 | 126 | char buff[MAX_PACKT]; |
ollie8 | 8:65d8e8882907 | 127 | if (stream.read(buff, MAX_PACKT)) { |
ollie8 | 8:65d8e8882907 | 128 | memmove(&buffer[i*MAX_PACKT], &buff[0], MAX_PACKT); |
ollie8 | 8:65d8e8882907 | 129 | i++; |
ollie8 | 8:65d8e8882907 | 130 | } else { |
ollie8 | 8:65d8e8882907 | 131 | WARN("Timed out waiting for response"); |
ollie8 | 8:65d8e8882907 | 132 | } |
ollie8 | 7:9dfba830a0c8 | 133 | } |
ollie8 | 8:65d8e8882907 | 134 | streamCallback(buffer); |
ollie8 | 8:65d8e8882907 | 135 | } |
ollie8 | 8:65d8e8882907 | 136 | return result; |
ollie8 | 8:65d8e8882907 | 137 | } |
ollie8 | 2:22ee01f24346 | 138 | } |