1 #include "IpStreamer.h"
2
IpStreamer()3 IpStreamer::IpStreamer() {}
4
~IpStreamer()5 IpStreamer::~IpStreamer() {}
6
startIpStream()7 void IpStreamer::startIpStream() {
8 ALOGI("Starting IP Stream thread");
9 mFp = fopen(mFilePath.c_str(), "rb");
10 if (mFp == nullptr) {
11 ALOGE("Failed to open file at path: %s", mFilePath.c_str());
12 return;
13 }
14 mIpStreamerThread = std::thread(&IpStreamer::ipStreamThreadLoop, this, mFp);
15 }
16
stopIpStream()17 void IpStreamer::stopIpStream() {
18 ALOGI("Stopping IP Stream thread");
19 close(mSockfd);
20 if (mFp != nullptr) fclose(mFp);
21 if (mIpStreamerThread.joinable()) {
22 mIpStreamerThread.join();
23 }
24 }
25
ipStreamThreadLoop(FILE * fp)26 void IpStreamer::ipStreamThreadLoop(FILE* fp) {
27 mSockfd = socket(AF_INET, SOCK_DGRAM, 0);
28 if (mSockfd < 0) {
29 ALOGE("IpStreamer::ipStreamThreadLoop: Socket creation failed (%s)", strerror(errno));
30 exit(1);
31 }
32
33 if (mFp == NULL) {
34 ALOGE("IpStreamer::ipStreamThreadLoop: Cannot open file %s: (%s)", mFilePath.c_str(),
35 strerror(errno));
36 exit(1);
37 }
38
39 struct sockaddr_in destaddr;
40 memset(&destaddr, 0, sizeof(destaddr));
41 destaddr.sin_family = mIsIpV4 ? AF_INET : AF_INET6;
42 destaddr.sin_port = htons(mPort);
43 destaddr.sin_addr.s_addr = inet_addr(mIpAddress.c_str());
44
45 char buf[mBufferSize];
46 int n;
47 while (1) {
48 if (fp == nullptr) break;
49 n = fread(buf, 1, mBufferSize, fp);
50 ALOGI("IpStreamer::ipStreamThreadLoop: Bytes read from fread(): %d\n", n);
51 if (n <= 0) {
52 break;
53 }
54 sendto(mSockfd, buf, n, 0, (struct sockaddr*)&destaddr, sizeof(destaddr));
55 sleep(mSleepTime);
56 }
57 }
58