int32_t sessionID, const void *data, ssize_t size = -1,
bool timeValid = false, int64_t timeUs = -1ll);
+ status_t switchToWebSocketMode(int32_t sessionID);
+
enum NotificationReason {
kWhatError,
kWhatConnected,
kWhatData,
kWhatDatagram,
kWhatBinaryData,
+ kWhatWebSocketMessage,
kWhatNetworkStall,
};
const char *getContent() const;
- void getRequestField(size_t index, AString *field) const;
+ bool getRequestField(size_t index, AString *field) const;
bool getStatusCode(int32_t *statusCode) const;
AString debugString() const;
#include "RemoteDisplay.h"
-#include "ANetworkSession.h"
#include "source/WifiDisplaySource.h"
#include <media/IRemoteDisplayClient.h>
+#include <media/stagefright/foundation/ADebug.h>
+#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
namespace android {
RemoteDisplay::RemoteDisplay(
- const sp<IRemoteDisplayClient> &client, const char *iface)
+ const sp<IRemoteDisplayClient> &client,
+ const char *iface)
: mLooper(new ALooper),
- mNetSession(new ANetworkSession),
- mSource(new WifiDisplaySource(mNetSession, client)) {
+ mNetSession(new ANetworkSession) {
mLooper->setName("wfd_looper");
+
+ mSource = new WifiDisplaySource(mNetSession, client);
mLooper->registerHandler(mSource);
mNetSession->start();
status_t RemoteDisplay::dispose() {
mSource->stop();
+ mSource.clear();
mLooper->stop();
mNetSession->stop();
#define REMOTE_DISPLAY_H_
+#include <media/IMediaPlayerService.h>
#include <media/IRemoteDisplay.h>
#include <media/stagefright/foundation/ABase.h>
#include <utils/Errors.h>
struct WifiDisplaySource;
struct RemoteDisplay : public BnRemoteDisplay {
- RemoteDisplay(const sp<IRemoteDisplayClient> &client, const char *iface);
+ RemoteDisplay(
+ const sp<IRemoteDisplayClient> &client,
+ const char *iface);
virtual status_t pause();
virtual status_t resume();
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/foundation/hexdump.h>
-#include <media/stagefright/Utils.h>
namespace android {
+static uint16_t U16_AT(const uint8_t *ptr) {
+ return ptr[0] << 8 | ptr[1];
+}
+
+static uint32_t U32_AT(const uint8_t *ptr) {
+ return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
+}
+
+static uint64_t U64_AT(const uint8_t *ptr) {
+ return ((uint64_t)U32_AT(ptr)) << 32 | U32_AT(ptr + 4);
+}
+
static const size_t kMaxUDPSize = 1500;
static const int32_t kMaxUDPRetries = 200;
};
struct ANetworkSession::Session : public RefBase {
+ enum Mode {
+ MODE_RTSP,
+ MODE_DATAGRAM,
+ MODE_WEBSOCKET,
+ };
+
enum State {
CONNECTING,
CONNECTED,
status_t sendRequest(
const void *data, ssize_t size, bool timeValid, int64_t timeUs);
- void setIsRTSPConnection(bool yesno);
+ void setMode(Mode mode);
+
+ status_t switchToWebSocketMode();
protected:
virtual ~Session();
int32_t mSessionID;
State mState;
- bool mIsRTSPConnection;
+ Mode mMode;
int mSocket;
sp<AMessage> mNotify;
bool mSawReceiveFailure, mSawSendFailure;
const sp<AMessage> ¬ify)
: mSessionID(sessionID),
mState(state),
- mIsRTSPConnection(false),
+ mMode(MODE_DATAGRAM),
mSocket(s),
mNotify(notify),
mSawReceiveFailure(false),
return mSocket;
}
-void ANetworkSession::Session::setIsRTSPConnection(bool yesno) {
- mIsRTSPConnection = yesno;
+void ANetworkSession::Session::setMode(Mode mode) {
+ mMode = mode;
+}
+
+status_t ANetworkSession::Session::switchToWebSocketMode() {
+ if (mState != CONNECTED || mMode != MODE_RTSP) {
+ return INVALID_OPERATION;
+ }
+
+ mMode = MODE_WEBSOCKET;
+
+ return OK;
}
sp<AMessage> ANetworkSession::Session::getNotificationMessage() const {
status_t ANetworkSession::Session::readMore() {
if (mState == DATAGRAM) {
+ CHECK_EQ(mMode, MODE_DATAGRAM);
+
status_t err;
do {
sp<ABuffer> buf = new ABuffer(kMaxUDPSize);
err = -ECONNRESET;
}
- if (!mIsRTSPConnection) {
+ if (mMode == MODE_DATAGRAM) {
// TCP stream carrying 16-bit length-prefixed datagrams.
while (mInBuffer.size() >= 2) {
mInBuffer.erase(0, packetSize + 2);
}
- } else {
+ } else if (mMode == MODE_RTSP) {
for (;;) {
size_t length;
break;
}
}
+ } else {
+ CHECK_EQ(mMode, MODE_WEBSOCKET);
+
+ const uint8_t *data = (const uint8_t *)mInBuffer.c_str();
+ // hexdump(data, mInBuffer.size());
+
+ while (mInBuffer.size() >= 2) {
+ size_t offset = 2;
+
+ unsigned payloadLen = data[1] & 0x7f;
+ if (payloadLen == 126) {
+ if (offset + 2 > mInBuffer.size()) {
+ break;
+ }
+
+ payloadLen = U16_AT(&data[offset]);
+ offset += 2;
+ } else if (payloadLen == 127) {
+ if (offset + 8 > mInBuffer.size()) {
+ break;
+ }
+
+ payloadLen = U64_AT(&data[offset]);
+ offset += 8;
+ }
+
+ uint32_t mask = 0;
+ if (data[1] & 0x80) {
+ // MASK==1
+ if (offset + 4 > mInBuffer.size()) {
+ break;
+ }
+
+ mask = U32_AT(&data[offset]);
+ offset += 4;
+ }
+
+ if (offset + payloadLen > mInBuffer.size()) {
+ break;
+ }
+
+ // We have the full message.
+
+ sp<ABuffer> packet = new ABuffer(payloadLen);
+ memcpy(packet->data(), &data[offset], payloadLen);
+
+ if (mask != 0) {
+ for (size_t i = 0; i < payloadLen; ++i) {
+ packet->data()[i] =
+ data[offset + i]
+ ^ ((mask >> (8 * (3 - (i % 4)))) & 0xff);
+ }
+ }
+
+ sp<AMessage> notify = mNotify->dup();
+ notify->setInt32("sessionID", mSessionID);
+ notify->setInt32("reason", kWhatWebSocketMessage);
+ notify->setBuffer("data", packet);
+ notify->setInt32("headerByte", data[0]);
+ notify->post();
+
+ mInBuffer.erase(0, offset + payloadLen);
+ }
}
if (err != OK) {
sp<ABuffer> buffer;
- if (mState == CONNECTED && !mIsRTSPConnection) {
+ if (mState == CONNECTED && mMode == MODE_DATAGRAM) {
CHECK_LE(size, 65535);
buffer = new ABuffer(size + 2);
buffer->data()[0] = size >> 8;
buffer->data()[1] = size & 0xff;
memcpy(buffer->data() + 2, data, size);
+ } else if (mState == CONNECTED && mMode == MODE_WEBSOCKET) {
+ static const bool kUseMask = false; // Chromium doesn't like it.
+
+ size_t numHeaderBytes = 2 + (kUseMask ? 4 : 0);
+ if (size > 65535) {
+ numHeaderBytes += 8;
+ } else if (size > 125) {
+ numHeaderBytes += 2;
+ }
+
+ buffer = new ABuffer(numHeaderBytes + size);
+ buffer->data()[0] = 0x81; // FIN==1 | opcode=1 (text)
+ buffer->data()[1] = kUseMask ? 0x80 : 0x00;
+
+ if (size > 65535) {
+ buffer->data()[1] |= 127;
+ buffer->data()[2] = 0x00;
+ buffer->data()[3] = 0x00;
+ buffer->data()[4] = 0x00;
+ buffer->data()[5] = 0x00;
+ buffer->data()[6] = (size >> 24) & 0xff;
+ buffer->data()[7] = (size >> 16) & 0xff;
+ buffer->data()[8] = (size >> 8) & 0xff;
+ buffer->data()[9] = size & 0xff;
+ } else if (size > 125) {
+ buffer->data()[1] |= 126;
+ buffer->data()[2] = (size >> 8) & 0xff;
+ buffer->data()[3] = size & 0xff;
+ } else {
+ buffer->data()[1] |= size;
+ }
+
+ if (kUseMask) {
+ uint32_t mask = rand();
+
+ buffer->data()[numHeaderBytes - 4] = (mask >> 24) & 0xff;
+ buffer->data()[numHeaderBytes - 3] = (mask >> 16) & 0xff;
+ buffer->data()[numHeaderBytes - 2] = (mask >> 8) & 0xff;
+ buffer->data()[numHeaderBytes - 1] = mask & 0xff;
+
+ for (size_t i = 0; i < (size_t)size; ++i) {
+ buffer->data()[numHeaderBytes + i] =
+ ((const uint8_t *)data)[i]
+ ^ ((mask >> (8 * (3 - (i % 4)))) & 0xff);
+ }
+ } else {
+ memcpy(buffer->data() + numHeaderBytes, data, size);
+ }
} else {
buffer = new ABuffer(size);
memcpy(buffer->data(), data, size);
notify);
if (mode == kModeCreateTCPDatagramSessionActive) {
- session->setIsRTSPConnection(false);
+ session->setMode(Session::MODE_DATAGRAM);
} else if (mode == kModeCreateRTSPClient) {
- session->setIsRTSPConnection(true);
+ session->setMode(Session::MODE_RTSP);
}
mSessions.add(session->sessionID(), session);
return err;
}
+status_t ANetworkSession::switchToWebSocketMode(int32_t sessionID) {
+ Mutex::Autolock autoLock(mLock);
+
+ ssize_t index = mSessions.indexOfKey(sessionID);
+
+ if (index < 0) {
+ return -ENOENT;
+ }
+
+ const sp<Session> session = mSessions.valueAt(index);
+ return session->switchToWebSocketMode();
+}
+
void ANetworkSession::interrupt() {
static const char dummy = 0;
clientSocket,
session->getNotificationMessage());
- clientSession->setIsRTSPConnection(
- session->isRTSPServer());
+ clientSession->setMode(
+ session->isRTSPServer()
+ ? Session::MODE_RTSP
+ : Session::MODE_DATAGRAM);
sessionsToAdd.push_back(clientSession);
}
ALooper.cpp \
ALooperRoster.cpp \
AMessage.cpp \
+ ANetworkSession.cpp \
AString.cpp \
+ ParsedMessage.cpp \
base64.cpp \
hexdump.cpp
#include <ctype.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
+#include <media/stagefright/foundation/hexdump.h>
namespace android {
ssize_t lastDictIndex = -1;
size_t offset = 0;
+ bool headersComplete = false;
while (offset < size) {
size_t lineEndOffset = offset;
while (lineEndOffset + 1 < size
}
if (lineEndOffset == offset) {
+ // An empty line separates headers from body.
+ headersComplete = true;
offset += 2;
break;
}
offset = lineEndOffset + 2;
}
+ if (!headersComplete && (!noMoreData || offset == 0)) {
+ // We either saw the empty line separating headers from body
+ // or we saw at least the status line and know that no more data
+ // is going to follow.
+ return -1;
+ }
+
for (size_t i = 0; i < mDict.size(); ++i) {
mDict.editValueAt(i).trim();
}
- // Found the end of headers.
-
int32_t contentLength;
if (!findInt32("content-length", &contentLength) || contentLength < 0) {
contentLength = 0;
return totalLength;
}
-void ParsedMessage::getRequestField(size_t index, AString *field) const {
+bool ParsedMessage::getRequestField(size_t index, AString *field) const {
AString line;
CHECK(findString("_", &line));
size_t prevOffset = 0;
size_t offset = 0;
for (size_t i = 0; i <= index; ++i) {
+ if (offset >= line.size()) {
+ return false;
+ }
+
ssize_t spacePos = line.find(" ", offset);
if (spacePos < 0) {
}
field->setTo(line, prevOffset, offset - prevOffset - 1);
+
+ return true;
}
bool ParsedMessage::getStatusCode(int32_t *statusCode) const {
AString statusCodeString;
- getRequestField(1, &statusCodeString);
+ if (!getRequestField(1, &statusCodeString)) {
+ *statusCode = 0;
+ return false;
+ }
char *end;
*statusCode = strtol(statusCodeString.c_str(), &end, 10);
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
- ANetworkSession.cpp \
MediaReceiver.cpp \
MediaSender.cpp \
Parameters.cpp \
- ParsedMessage.cpp \
rtp/RTPAssembler.cpp \
rtp/RTPReceiver.cpp \
rtp/RTPSender.cpp \
#include "MediaReceiver.h"
-#include "ANetworkSession.h"
#include "AnotherPacketSource.h"
#include "rtp/RTPReceiver.h"
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/Utils.h>
#include "MediaSender.h"
-#include "ANetworkSession.h"
#include "rtp/RTPSender.h"
#include "source/TSPacketizer.h"
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <ui/GraphicBuffer.h>
namespace android {
#include "TimeSyncer.h"
-#include "ANetworkSession.h"
-
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AHandler.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <media/stagefright/Utils.h>
namespace android {
#define LOG_TAG "nettest"
#include <utils/Log.h>
-#include "ANetworkSession.h"
#include "TimeSyncer.h"
#include <binder/ProcessState.h>
#include <media/stagefright/foundation/AHandler.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/DataSource.h>
#include <media/stagefright/MediaDefs.h>
#include "RTPAssembler.h"
#include "RTPReceiver.h"
-#include "ANetworkSession.h"
-
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/Utils.h>
#include "RTPSender.h"
-#include "ANetworkSession.h"
-
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/Utils.h>
#define LOG_TAG "rtptest"
#include <utils/Log.h>
-#include "ANetworkSession.h"
#include "rtp/RTPSender.h"
#include "rtp/RTPReceiver.h"
#include "TimeSyncer.h"
#include <media/stagefright/foundation/AHandler.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/DataSource.h>
#include <media/stagefright/MediaDefs.h>
#include "DirectRenderer.h"
#include "MediaReceiver.h"
-#include "ParsedMessage.h"
#include "TimeSyncer.h"
#include <cutils/properties.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ParsedMessage.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/Utils.h>
#define WIFI_DISPLAY_SINK_H_
-#include "ANetworkSession.h"
-
#include "VideoFormats.h"
#include <gui/Surface.h>
#include <media/stagefright/foundation/AHandler.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
namespace android {
err = mSource->start(params.get());
} else {
err = mSource->start();
+ if (err != OK) {
+ ALOGE("source failed to start w/ err %d", err);
+ }
}
if (err == OK) {
#include "WifiDisplaySource.h"
#include "PlaybackSession.h"
#include "Parameters.h"
-#include "ParsedMessage.h"
#include "rtp/RTPSender.h"
#include "TimeSyncer.h"
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ParsedMessage.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/Utils.h>
#define WIFI_DISPLAY_SOURCE_H_
-#include "ANetworkSession.h"
#include "VideoFormats.h"
#include <media/stagefright/foundation/AHandler.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
#include <netinet/in.h>
#define LOG_TAG "udptest"
#include <utils/Log.h>
-#include "ANetworkSession.h"
#include "TimeSyncer.h"
#include <binder/ProcessState.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/ANetworkSession.h>
namespace android {
iface.append(StringPrintf(":%d", port).c_str());
sp<RemoteDisplayClient> client = new RemoteDisplayClient;
- sp<IRemoteDisplay> display = service->listenForRemoteDisplay(client, iface);
+ sp<IRemoteDisplay> display =
+ service->listenForRemoteDisplay(client, iface);
client->waitUntilDone();