|
| 1 | +#include "rtpStream.h" |
| 2 | + |
| 3 | +typedef struct float4 { |
| 4 | + float x; |
| 5 | + float y; |
| 6 | + float z; |
| 7 | + float w; |
| 8 | +} float4; |
| 9 | + |
| 10 | +/* |
| 11 | + * error - wrapper for perror |
| 12 | + */ |
| 13 | +void error(char *msg) { |
| 14 | + perror(msg); |
| 15 | + exit(0); |
| 16 | +} |
| 17 | + |
| 18 | +void rgbtoyuv(int y, int x, char* yuv, char* rgb) |
| 19 | +{ |
| 20 | + int c,cc,R,G,B,Y,U,V; |
| 21 | + int size; |
| 22 | + |
| 23 | + cc=0; |
| 24 | + size = x*3; |
| 25 | + for (c=0;c<size;c+=3) |
| 26 | + { |
| 27 | + R=rgb[c]; |
| 28 | + G=rgb[c+1]; |
| 29 | + B=rgb[c+2]; |
| 30 | + /* sample luma for every pixel */ |
| 31 | + Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16; |
| 32 | + yuv[cc]=Y; |
| 33 | + if (c % 2 == 0) |
| 34 | + { |
| 35 | + V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128; |
| 36 | + yuv[cc+1]=V; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128; |
| 41 | + yuv[cc+1]=U; |
| 42 | + } |
| 43 | + cc+=2; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/* Broadcast the stream to port 5004 */ |
| 48 | +rtpStream::rtpStream(int height, int width, char* hostname, int portno) : |
| 49 | + camera(height, width) |
| 50 | +{ |
| 51 | + strcpy(mHostname, hostname); |
| 52 | + mPortNo = portno; |
| 53 | + mFrame = 0; |
| 54 | +} |
| 55 | + |
| 56 | +bool rtpStream::Open() |
| 57 | +{ |
| 58 | + /* socket: create the socket */ |
| 59 | + mSockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 60 | + if (mSockfd < 0) |
| 61 | + { |
| 62 | + printf("ERROR opening socket"); |
| 63 | + return error; |
| 64 | + } |
| 65 | + |
| 66 | + /* gethostbyname: get the server's DNS entry */ |
| 67 | + mServer = gethostbyname(mHostname); |
| 68 | + if (mServer == NULL) { |
| 69 | + fprintf(stderr,"ERROR, no such host as %s\n", mHostname); |
| 70 | + exit(0); |
| 71 | + } |
| 72 | + |
| 73 | + /* build the server's Internet address */ |
| 74 | + bzero((char *) &mServeraddr, sizeof(mServeraddr)); |
| 75 | + mServeraddr.sin_family = AF_INET; |
| 76 | + bcopy((char *)mServer->h_addr, |
| 77 | + (char *)&mServeraddr.sin_addr.s_addr, mServer->h_length); |
| 78 | + mServeraddr.sin_port = htons(mPortNo); |
| 79 | + |
| 80 | + /* send the message to the server */ |
| 81 | + mServerlen = sizeof(mServeraddr); |
| 82 | + |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +void rtpStream::Close() |
| 87 | +{ |
| 88 | + close(mSockfd); |
| 89 | +} |
| 90 | + |
| 91 | +#if ARM |
| 92 | +void rtpStream::endianswap32(uint32_t *data, int length) |
| 93 | +{ |
| 94 | + int c = 0; |
| 95 | + for (c=0;c<length;c++) |
| 96 | + data[c] = __bswap_32 (data[c]); |
| 97 | +} |
| 98 | + |
| 99 | +void rtpStream::endianswap16(uint16_t *data, int length) |
| 100 | +{ |
| 101 | + int c = 0; |
| 102 | + for (c=0;c<length;c++) |
| 103 | + data[c] = __bswap_16 (data[c]); |
| 104 | +} |
| 105 | +#endif |
| 106 | + |
| 107 | +void rtpStream::update_header(header *packet, int line, int last, int32_t timestamp, int32_t source) |
| 108 | +{ |
| 109 | + bzero((char *)packet, sizeof(header)); |
| 110 | + packet->rtp.protocol = RTP_VERSION << 30; |
| 111 | + packet->rtp.protocol = packet->rtp.protocol | RTP_PAYLOAD_TYPE << 16; |
| 112 | + packet->rtp.protocol = packet->rtp.protocol | sequence_number++; |
| 113 | + /* leaving other fields as zero TODO Fix*/ |
| 114 | + packet->rtp.timestamp = timestamp += (Hz90 / RTP_FRAMERATE); |
| 115 | + packet->rtp.source = source; |
| 116 | + packet->payload.extended_sequence_number = 0; /* TODO : Fix extended seq numbers */ |
| 117 | + packet->payload.line[0].length = MAX_BUFSIZE; |
| 118 | + packet->payload.line[0].line_number = line; |
| 119 | + packet->payload.line[0].offset = 0; |
| 120 | + if (last==1) |
| 121 | + { |
| 122 | + packet->rtp.protocol = packet->rtp.protocol | 1 << 23; |
| 123 | + } |
| 124 | +#if 0 |
| 125 | + printf("0x%x, 0x%x, 0x%x \n", packet->rtp.protocol, packet->rtp.timestamp, packet->rtp.source); |
| 126 | + printf("0x%x, 0x%x, 0x%x \n", packet->payload.line[0].length, packet->payload.line[0].line_number, packet->payload.line[0].offset); |
| 127 | +#endif |
| 128 | +} |
| 129 | + |
| 130 | +int rtpStream::Transmit(char* rgbframe) |
| 131 | +{ |
| 132 | + rtp_packet packet; |
| 133 | + char *yuv; |
| 134 | + int c=0; |
| 135 | + int n=0; |
| 136 | + |
| 137 | + |
| 138 | + sequence_number=0; |
| 139 | + |
| 140 | +#if 0 |
| 141 | +#if 0 |
| 142 | + float4 *RGBAf = (float4*)rgbframe; |
| 143 | + |
| 144 | + // RGBAf to RGB |
| 145 | + for( int y=0; y < 100; y++ ) |
| 146 | + { |
| 147 | + for( int x=0; x < mWidth; x++ ) |
| 148 | + { |
| 149 | + rgbframe[(y*(mWidth*3))+(x*3)] = (char)RGBAf[(y*mWidth)+x].x; |
| 150 | + rgbframe[(y*(mWidth*3))+(x*3)+1] = (char)RGBAf[(y*mWidth)+x].y; |
| 151 | + rgbframe[(y*(mWidth*3))+(x*3)+2] = (char)RGBAf[(y*mWidth)+x].z; |
| 152 | + } |
| 153 | + } |
| 154 | +#else |
| 155 | + for( int y=0; y < mHeight; y++ ) |
| 156 | + { |
| 157 | + for( int x=0; x < mWidth; x+=3 ) |
| 158 | + { |
| 159 | + rgbframe[(y*(mWidth*3))+(x*3)] = (char)0xff; |
| 160 | + rgbframe[(y*(mWidth*3))+(x*3)+1] = (char)0; |
| 161 | + rgbframe[(y*(mWidth*3))+(x*3)+2] = (char)0; |
| 162 | + } |
| 163 | + } |
| 164 | +#endif |
| 165 | +#endif |
| 166 | + |
| 167 | + |
| 168 | + /* get a message from the user */ |
| 169 | + bzero(packet.data, MAX_BUFSIZE); |
| 170 | + |
| 171 | + /* send a frame */ |
| 172 | + { |
| 173 | + struct timeval NTP_value; |
| 174 | + int32_t time = 10000; |
| 175 | + |
| 176 | + for (c=0;c<(mHeight);c++) |
| 177 | + { |
| 178 | + int x,last = 0; |
| 179 | + if (c==mHeight-1) last=1; |
| 180 | + update_header((header*)&packet, c, last, time, RTP_SOURCE); |
| 181 | + x = c * (mWidth * 3); |
| 182 | + |
| 183 | +#if 1 |
| 184 | + rgbtoyuv(mHeight, mWidth, packet.data, (char*)&rgbframe[x]); |
| 185 | +#else |
| 186 | + memcpy((char*)packet.data, (char*)&rgbframe[x], mWidth* 3); |
| 187 | +#endif |
| 188 | + |
| 189 | +#if ARM |
| 190 | + endianswap32((uint32_t *)&packet, sizeof(rtp_header)/4); |
| 191 | + endianswap16((uint16_t *)&packet.head.payload, sizeof(payload_header)/2); |
| 192 | +#endif |
| 193 | + n = sendto(mSockfd, (char *)&packet, sizeof(rtp_packet), 0, (const sockaddr*)&mServeraddr, mServerlen); |
| 194 | + if (n < 0) |
| 195 | + fprintf(stderr, "ERROR in sendto"); |
| 196 | + } |
| 197 | + |
| 198 | + printf("Sent frame %d\n", mFrame++); |
| 199 | + } |
| 200 | + |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
0 commit comments