-
Notifications
You must be signed in to change notification settings - Fork 7
Add local video track support #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gongxinheng
wants to merge
13
commits into
main
Choose a base branch
from
add_local_video_track_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6de5dd1
Add local video track support
gongxinheng 4babece
Update client-sdk-rust and add sample video reading
gongxinheng 78e1f6f
Add hue video publish example
gongxinheng 3eed933
Fix livekit_ffi_drop_handle being called multiple times on the same h…
gongxinheng 79c7120
Fix room connection issue
gongxinheng a0a7706
Fix room participant usage
gongxinheng 82878aa
wip
theomonnom f3bbcbf
fixed the issue
theomonnom a00de3b
Clean up
gongxinheng 1509b8e
Update client-sdk-rust dep to v0.2.0 and fix compile issue
gongxinheng b0e563e
Add cpp reorder flag
gongxinheng b95e112
Add strict cpp flags and fix comiple errors
gongxinheng aa4af20
Fix ffi handles
gongxinheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Using the same .clang-format as libwebrtc | ||
| # https://github.com/webrtc-sdk/webrtc/blob/m104_release/.clang-format | ||
| BasedOnStyle: Chromium | ||
| --- | ||
| Language: Java | ||
| BasedOnStyle: Google | ||
| --- | ||
| Language: ObjC | ||
| BasedOnStyle: Google | ||
| BinPackParameters: false | ||
| BinPackArguments: false | ||
| ColumnLimit: 100 | ||
| ObjCBlockIndentWidth: 2 | ||
| AllowAllParametersOfDeclarationOnNextLine: true | ||
| AlignOperands: false | ||
| AlwaysBreakBeforeMultilineStrings: false | ||
| AllowShortFunctionsOnASingleLine: Inline | ||
| BreakBeforeTernaryOperators: false | ||
| IndentWrappedFunctionNames: true | ||
| ContinuationIndentWidth: 4 | ||
| ObjCSpaceBeforeProtocolList: true | ||
| --- | ||
| Language: Cpp | ||
| IncludeBlocks: Regroup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ CMakeCache.txt | |
| Makefile | ||
| cmake_install.cmake | ||
| out | ||
| build | ||
| .vscode | ||
| generated/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule client-sdk-rust
updated
from 134466 to 93ce7e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,121 @@ | ||
| /* | ||
| * Copyright 2023 LiveKit | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #include <cmath> | ||
| #include <cstdio> | ||
| #include <future> | ||
| #include <iostream> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include "livekit/livekit.h" | ||
| #include "room.pb.h" | ||
| #include "track.pb.h" | ||
|
|
||
| using namespace livekit; | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| Room room{}; | ||
| room.Connect("ws://localhost:7880", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ5MjM1OTQ4MjMsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJ3ZWIiLCJuYmYiOjE2ODM1OTQ4MjMsInN1YiI6IndlYiIsInZpZGVvIjp7InJvb20iOiJsaXZla2l0LWZmaS10ZXN0Iiwicm9vbUpvaW4iOnRydWV9fQ.voLT9RK3wNYEGdWovPLv1BzyN1v5tpJ59e0DIqIVfiU"); | ||
| const std::string URL = "ws://localhost:7880"; | ||
| const std::string TOKEN = | ||
| "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTk3MjM1MzQsImlzcyI6ImRldmtleSIsIm5hbWUiOiJIZW5nc3RhciIsIm5iZiI6MTY5MTA4MzUzNCwic3ViIjoiSGVuZ3N0YXIiLCJ2aWRlbyI6eyJyb29tIjoidGVzdFJvb20iLCJyb29tSm9pbiI6dHJ1ZX19.8y6Qr_Z9UNo3P0QPD_TuXai02s23n8o5GjyWXVHK2RM"; | ||
|
|
||
| std::vector<int> hsv_to_rgb(float H, float S, float V) { | ||
|
|
||
| std::vector<int> rgb(3); | ||
| float C = S * V; | ||
| float X = C * (1 - std::abs(fmod(H * 6, 2) - 1)); | ||
| float m = V - C; | ||
|
|
||
| float R, G, B; | ||
| if (0 <= H && H < 1 / 6.0) { | ||
| R = C, G = X, B = 0; | ||
| } else if (1 / 6.0 <= H && H < 2 / 6.0) { | ||
| R = X, G = C, B = 0; | ||
| } else if (2 / 6.0 <= H && H < 3 / 6.0) { | ||
| R = 0, G = C, B = X; | ||
| } else if (3 / 6.0 <= H && H < 4 / 6.0) { | ||
| R = 0, G = X, B = C; | ||
| } else if (4 / 6.0 <= H && H < 5 / 6.0) { | ||
| R = X, G = 0, B = C; | ||
| } else { | ||
| R = C, G = 0, B = X; | ||
| } | ||
|
|
||
| rgb[0] = (R + m) * 255; | ||
| rgb[1] = (G + m) * 255; | ||
| rgb[2] = (B + m) * 255; | ||
|
|
||
| // Should we implement a mechanism to PollEvents/WaitEvents? Like SDL2/glfw | ||
| // - So we can remove the useless loop here | ||
| // Or is it better to use callback based events? | ||
| return rgb; | ||
| } | ||
|
|
||
| void publish_frames(VideoSource* source) { | ||
| ArgbFrame frame(proto::FORMAT_ARGB, 1280, 720); | ||
| double framerate = 1.0 / 30; | ||
| double hue = 0.0; | ||
| while (true) { | ||
| std::vector<int> rgb = hsv_to_rgb(hue, 1.0, 1.0); | ||
| for (size_t i = 0; i < frame.size; i += 4) { | ||
| frame.data[i] = 255; | ||
| frame.data[i + 1] = rgb[0]; | ||
| frame.data[i + 2] = rgb[1]; | ||
| frame.data[i + 3] = rgb[2]; | ||
| } | ||
|
|
||
| while(true) { | ||
| hue += framerate / 3; | ||
|
|
||
| if (hue >= 1.0) { | ||
| hue = 0.0; | ||
| } | ||
|
|
||
| return 0; | ||
| VideoFrame i420(0, proto::VIDEO_ROTATION_0, frame.ToI420()); | ||
| source->CaptureFrame(i420); | ||
|
|
||
| std::this_thread::sleep_for( | ||
| std::chrono::milliseconds(int(framerate * 1000))); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| std::shared_ptr<Room> room = Room::Create(); | ||
| room->Connect(URL, TOKEN); | ||
|
|
||
| // TODO Non blocking ? | ||
| while (!room->GetLocalParticipant()) { | ||
| } | ||
|
|
||
| VideoSource source{}; | ||
| std::thread t(publish_frames, &source); | ||
|
|
||
| std::this_thread::sleep_for(std::chrono::seconds(2)); | ||
|
|
||
| std::shared_ptr<LocalVideoTrack> track = | ||
| LocalVideoTrack::CreateVideoTrack("hue", source); | ||
|
|
||
| proto::TrackPublishOptions options{}; | ||
| options.set_source(proto::SOURCE_CAMERA); | ||
| options.set_simulcast(true); | ||
|
|
||
| std::cout << "Publishing track" << std::endl; | ||
| room->GetLocalParticipant()->PublishTrack(track, options); | ||
|
|
||
| // Should we implement a mechanism to PollEvents/WaitEvents? Like SDL2/glfw | ||
| // - So we can remove the useless loop here | ||
| // Or is it better to use callback based events? | ||
|
|
||
| while (true) { | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright 2023 LiveKit | ||
| * | ||
| * 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 LIVEKIT_PARTICIPANT_H | ||
| #define LIVEKIT_PARTICIPANT_H | ||
|
|
||
| #include <condition_variable> | ||
|
|
||
| #include "livekit/ffi_client.h" | ||
| #include "livekit/track.h" | ||
| #include "participant.pb.h" | ||
|
|
||
| namespace livekit { | ||
| class Room; | ||
|
|
||
| class Participant { | ||
| public: | ||
| Participant(const FfiHandle& handle, const proto::ParticipantInfo& info) : handle_(handle), info_(info) {} | ||
| Participant(FfiHandle&& handle, proto::ParticipantInfo&& info) : handle_(std::move(handle)), info_(std::move(info)) {} | ||
|
|
||
| const std::string& GetSid() const { return info_.sid(); } | ||
| const std::string& GetIdentity() const { return info_.identity(); } | ||
| const std::string& GetName() const { return info_.name(); } | ||
| const std::string& GetMetadata() const { return info_.metadata(); } | ||
|
|
||
| protected: | ||
| FfiHandle handle_; | ||
| proto::ParticipantInfo info_; | ||
| }; | ||
|
|
||
| class LocalParticipant : public Participant { | ||
| public: | ||
| LocalParticipant(const FfiHandle& handle, const proto::ParticipantInfo& info) | ||
| : Participant(handle, info) {} | ||
| LocalParticipant(FfiHandle&& handle, proto::ParticipantInfo&& info) | ||
| : Participant(std::move(handle), std::move(info)) {} | ||
|
|
||
| ~LocalParticipant(); | ||
|
|
||
| void PublishTrack(std::shared_ptr<Track> track, | ||
| const proto::TrackPublishOptions& options); | ||
|
|
||
| private: | ||
| std::condition_variable cv_; // Should we block? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should have a blocking API. |
||
| uint64_t publishAsyncId_; | ||
| FfiClient::ListenerId listenerId_{0}; | ||
| std::unique_ptr<proto::PublishTrackCallback> publishCallback_; | ||
|
|
||
| void OnEvent(const proto::FfiEvent& event); | ||
| }; | ||
|
|
||
| } // namespace livekit | ||
| #endif /* LIVEKIT_PARTICIPANT_H */ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should implement a correct way to handle events.
Should we expose all events in a virtual class (observer)?