Skip to content

Commit

Permalink
Add and use Starboard extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jellefoks committed Oct 30, 2024
1 parent aba7e2c commit 968b7ad
Show file tree
Hide file tree
Showing 12 changed files with 424 additions and 203 deletions.
21 changes: 18 additions & 3 deletions net/socket/udp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "net/log/net_log_source_type.h"
#include "net/socket/udp_net_log_parameters.h"
#include "starboard/common/socket.h"
#include "starboard/extension/socket_receive_multi_msg.h"
#include "starboard/system.h"

namespace net {
Expand All @@ -39,6 +40,8 @@ namespace {
// Read in larger batches to minimize recvmmsg overhead.
inline constexpr int kNumPacketsPerReadMmsgCall = 64;
inline constexpr size_t kDefaultUdpPacketControlBufferSize = 512;

const CobaltExtensionSocketReceiveMultiMsgApi* g_socket_extension = nullptr;
} // namespace

UDPSocketStarboard::UDPSocketStarboard(DatagramSocket::BindType bind_type,
Expand All @@ -56,6 +59,15 @@ UDPSocketStarboard::UDPSocketStarboard(DatagramSocket::BindType bind_type,
write_buf_len_(0),
net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::UDP_SOCKET)),
weak_factory_(this) {
if (g_socket_extension == nullptr) {
// Note that this initializes a constant pointer, so even if two threads run
// this in parallel, they will only do redundant work without overwriting
// previous work. Therefore, there is no need for atomics, locks or memory
// barriers here.
g_socket_extension =
static_cast<const CobaltExtensionSocketReceiveMultiMsgApi*>(
SbSystemGetExtension(kCobaltExtensionSocketReceiveMultiMsgName));
}
net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE,
source);
}
Expand Down Expand Up @@ -187,6 +199,9 @@ int UDPSocketStarboard::StartWatchingSocketForReading() {
int UDPSocketStarboard::ReadMultiplePackets(Socket::ReadPacketResults* results,
int packet_buffer_size,
CompletionOnceCallback callback) {
if (!g_socket_extension) {
return ERR_NOT_IMPLEMENTED;
}
if (!results || packet_buffer_size <= 0) {
return ERR_INVALID_ARGUMENT;
}
Expand All @@ -201,7 +216,7 @@ int UDPSocketStarboard::ReadMultiplePackets(Socket::ReadPacketResults* results,
results->packets == nullptr) {
// Request how much memory to allocate for the needed buffers and data
// structures from the platform.
int buffer_size = SbSocketReceiveMultiMsgBufferSize(
int buffer_size = g_socket_extension->ReceiveMultiMsgBufferSize(
kNumPacketsPerReadMmsgCall, packet_buffer_size,
kDefaultUdpPacketControlBufferSize);
// Calculate the space for our ReadPacketResult array.
Expand All @@ -216,7 +231,7 @@ int UDPSocketStarboard::ReadMultiplePackets(Socket::ReadPacketResults* results,
// Our packets array is at the end of the buffer.
results->packets = reinterpret_cast<Socket::ReadPacketResult*>(
results->buffer->data() + buffer_size);
SbSocketReceiveMultiMsgBufferInitialize(
g_socket_extension->ReceiveMultiMsgBufferInitialize(
kNumPacketsPerReadMmsgCall, packet_buffer_size,
kDefaultUdpPacketControlBufferSize, results->buffer->data());
}
Expand Down Expand Up @@ -582,7 +597,7 @@ int UDPSocketStarboard::InternalRecvFrom(IOBuffer* buf,
int UDPSocketStarboard::InternalReadMultiplePackets(
Socket::ReadPacketResults* results) {
SbSocketReceiveMultiMsgResult* msgresult =
SbSocketReceiveMultiMsg(socket_, results->buffer->data());
g_socket_extension->ReceiveMultiMsg(socket_, results->buffer->data());
CHECK(msgresult);
results->result = msgresult ? msgresult->result : -1;
if (results->result < 0) {
Expand Down
3 changes: 3 additions & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ static_library("starboard_platform") {
"//starboard/shared/posix/socket_join_multicast_group.cc",
"//starboard/shared/posix/socket_listen.cc",
"//starboard/shared/posix/socket_receive_from.cc",
"//starboard/shared/posix/socket_receive_multi_msg_internal.cc",
"//starboard/shared/posix/socket_receive_multi_msg_internal.h",
"//starboard/shared/posix/socket_receive_multi_msg.cc",
"//starboard/shared/posix/socket_receive_multi_msg.h",
"//starboard/shared/posix/socket_resolve.cc",
"//starboard/shared/posix/socket_send_to.cc",
"//starboard/shared/posix/socket_set_broadcast.cc",
Expand Down
5 changes: 5 additions & 0 deletions starboard/android/shared/system_get_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
#include "starboard/extension/platform_info.h"
#include "starboard/extension/platform_service.h"
#include "starboard/extension/player_set_max_video_input_size.h"
#include "starboard/extension/socket_receive_multi_msg.h"
#include "starboard/extension/system_info.h"
#include "starboard/shared/posix/socket_receive_multi_msg.h"

const void* SbSystemGetExtension(const char* name) {
#if SB_IS(EVERGREEN_COMPATIBLE)
Expand Down Expand Up @@ -98,5 +100,8 @@ const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kStarboardExtensionSystemInfoName) == 0) {
return starboard::android::shared::GetSystemInfoApi();
}
if (strcmp(name, kCobaltExtensionSocketReceiveMultiMsgName) == 0) {
return starboard::shared::posix::GetSocketReceiveMultiMsgApi();
}
return NULL;
}
21 changes: 21 additions & 0 deletions starboard/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "starboard/extension/platform_service.h"
#include "starboard/extension/player_configuration.h"
#include "starboard/extension/player_set_max_video_input_size.h"
#include "starboard/extension/socket_receive_multiple_messages.h"
#include "starboard/extension/system_info.h"
#include "starboard/extension/time_zone.h"
#include "starboard/extension/updater_notification.h"
Expand Down Expand Up @@ -623,6 +624,26 @@ TEST(ExtensionTest, CobaltAccessibilityExtension) {
<< "Extension struct should be a singleton";
}

TEST(ExtensionTest, SocketReceiveMultiMsg) {
typedef CobaltExtensionSocketReceiveMultiMsgApi ExtensionApi;
const char* kExtensionName = kCobaltExtensionSocketReceiveMultiMsgName;

const ExtensionApi* extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
if (!extension_api) {
return;
}

EXPECT_STREQ(extension_api->name, kExtensionName);
EXPECT_EQ(extension_api->version, 1u);
EXPECT_NE(extension_api->MemoryMapFile, nullptr);

const ExtensionApi* second_extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
EXPECT_EQ(second_extension_api, extension_api)
<< "Extension struct should be a singleton";
}

TEST(ExtensionTest, StarboardSystemInfoExtension) {
typedef StarboardExtensionSystemInfoApi ExtensionApi;
const char* kExtensionName = kStarboardExtensionSystemInfoName;
Expand Down
70 changes: 70 additions & 0 deletions starboard/extension/socket_receive_multi_msg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_EXTENSION_SOCKET_RECEIVE_MULTI_MSG_H_
#define STARBOARD_EXTENSION_SOCKET_RECEIVE_MULTI_MSG_H_

#include <stdint.h>

#include "starboard/socket.h"

#ifdef __cplusplus
extern "C" {
#endif

struct SbSocketReceiveMultiMsgPacket {
char* buffer = nullptr;
int result = 0;
};

struct SbSocketReceiveMultiMsgResult {
int result = 0;
SbSocketReceiveMultiMsgPacket* packets = nullptr;
};

SbSocketReceiveMultiMsgResult* SbSocketReceiveMultiMsg(SbSocket socket,
char* buffer);

#define kCobaltExtensionSocketReceiveMultiMsgName \
"dev.cobalt.extension.SocketReceiveMultiMsg"

typedef struct CobaltExtensionSocketReceiveMultiMsgApi {
// Name should be the string |kCobaltExtensionSocketReceiveMultiMsgName|.
// This helps to validate that the extension API is correct.
const char* name;

// This specifies the version of the API that is implemented.
uint32_t version;

// The fields below this point were added in version 1 or later.

unsigned int (*ReceiveMultiMsgBufferSize)(unsigned int num_messages,
unsigned int message_size,
unsigned int control_size);

void (*ReceiveMultiMsgBufferInitialize)(unsigned int num_messages,
unsigned int message_size,
unsigned int control_size,
char* data);

SbSocketReceiveMultiMsgResult* (*ReceiveMultiMsg)(SbSocket socket,
char* buffer);

} CobaltExtensionSocketReceiveMultiMsgApi;

#ifdef __cplusplus
} // extern "C"
#endif

#endif // STARBOARD_EXTENSION_SOCKET_RECEIVE_MULTI_MSG_H_
3 changes: 3 additions & 0 deletions starboard/linux/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ static_library("starboard_platform_sources") {
"//starboard/shared/posix/socket_join_multicast_group.cc",
"//starboard/shared/posix/socket_listen.cc",
"//starboard/shared/posix/socket_receive_from.cc",
"//starboard/shared/posix/socket_receive_multi_msg_internal.cc",
"//starboard/shared/posix/socket_receive_multi_msg_internal.h",
"//starboard/shared/posix/socket_receive_multi_msg.cc",
"//starboard/shared/posix/socket_receive_multi_msg.h",
"//starboard/shared/posix/socket_resolve.cc",
"//starboard/shared/posix/socket_send_to.cc",
"//starboard/shared/posix/socket_set_broadcast.cc",
Expand Down
5 changes: 5 additions & 0 deletions starboard/linux/shared/system_get_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#endif
#include "starboard/extension/memory_mapped_file.h"
#include "starboard/extension/platform_service.h"
#include "starboard/extension/socket_receive_multi_msg.h"
#include "starboard/extension/time_zone.h"
#include "starboard/linux/shared/configuration.h"
#include "starboard/linux/shared/ifa.h"
Expand All @@ -38,6 +39,7 @@
#include "starboard/shared/ffmpeg/ffmpeg_demuxer.h"
#include "starboard/shared/posix/free_space.h"
#include "starboard/shared/posix/memory_mapped_file.h"
#include "starboard/shared/posix/socket_receive_multi_msg.h"
#include "starboard/shared/starboard/application.h"
#include "starboard/shared/starboard/crash_handler.h"
#if SB_IS(EVERGREEN_COMPATIBLE)
Expand Down Expand Up @@ -71,6 +73,9 @@ const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kCobaltExtensionFreeSpaceName) == 0) {
return starboard::shared::posix::GetFreeSpaceApi();
}
if (strcmp(name, kCobaltExtensionSocketReceiveMultiMsgName) == 0) {
return starboard::shared::posix::GetSocketReceiveMultiMsgApi();
}
#if SB_API_VERSION < 15
if (strcmp(name, kCobaltExtensionEnhancedAudioName) == 0) {
return starboard::shared::enhanced_audio::GetEnhancedAudioApi();
Expand Down
Loading

0 comments on commit 968b7ad

Please sign in to comment.