/*
* multi_player_demo.cpp
*
* Author: daniusdk.com
*
* Copyright © 2017~2024 DaniuLive. All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <poll.h>
#include <errno.h>
#include <string>
#include <sstream>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include "nt_sdk_linux_smart_log.h"
#include "nt_linux_smart_player_sdk.h"
#include "nt_player_sdk_wrapper.h"
....
const char* players_url_[]
{
"rtsp://admin:daniulive12345@192.168.0.120:554/h264/ch1/main/av_stream",
"rtsp://admin:daniulive12345@192.168.0.120:554/h264/ch1/main/av_stream",
"rtsp://admin:admin123456@192.168.0.121:554/cam/realmonitor?channel=1&subtype=0",
"rtsp://admin:admin123456@192.168.0.121:554/cam/realmonitor?channel=1&subtype=0",
};
int main(int argc, char *argv[])
{
XInitThreads(); // X支持多线程, 必须调用
NT_SDKLogInit();
// SDK初始化
SmartPlayerSDKAPI player_api;
if (!NT_PlayerSDKInit(player_api))
{
fprintf(stderr, "SDK init failed.\n");
return 0;
}
auto display = XOpenDisplay(nullptr);
if (!display)
{
fprintf(stderr, "Cannot connect to X server\n");
player_api.UnInit();
return 0;
}
auto screen = DefaultScreen(display);
auto root = XRootWindow(display, screen);
XWindowAttributes root_win_att;
if (!XGetWindowAttributes(display, root, &root_win_att))
{
fprintf(stderr, "Get Root window attri failed\n");
player_api.UnInit();
XCloseDisplay(display);
return 0;
}
if (root_win_att.width < 100 || root_win_att.height < 100)
{
fprintf(stderr, "Root window size error.\n");
player_api.UnInit();
XCloseDisplay(display);
return 0;
}
fprintf(stdout, "Root Window Size:%d*%d\n", root_win_att.width, root_win_att.height);
int main_w = root_win_att.width / 2, main_h = root_win_att.height/2;
auto black_pixel = BlackPixel(display, screen);
auto white_pixel = WhitePixel(display, screen);
auto main_wid = XCreateSimpleWindow(display, root, 0, 0, main_w, main_h, 0, white_pixel, black_pixel);
if (!main_wid)
{
player_api.UnInit();
XCloseDisplay(display);
fprintf(stderr, "Cannot create main windows\n");
return 0;
}
XSelectInput(display, main_wid, StructureNotifyMask | KeyPressMask);
XMapWindow(display, main_wid);
XStoreName(display, main_wid, win_base_title);
std::vector<std::shared_ptr<NT_PlayerSDKWrapper> > players;
for (auto url: players_url_)
{
auto i = std::make_shared<NT_PlayerSDKWrapper>(&player_api);
i->SetDisplay(display);
i->SetScreen(screen);
i->SetURL(url);
players.push_back(i);
if ( players.size() > 3 )
break;
}
auto border_w = 2;
std::vector<NT_LayoutRect> layout_rects;
SubWindowsLayout(main_w, main_h, border_w, static_cast<int>(players.size()), layout_rects);
for (auto i = 0; i < static_cast<int>(players.size()); ++i)
{
assert(players[i]);
players[i]->SetWindow(CreateSubWindow(display, screen, main_wid, layout_rects[i], border_w));
}
for (const auto& i : players)
{
assert(i);
if (i->GetWindow())
XMapWindow(display, i->GetWindow());
}
for (auto i = 0; i < static_cast<int>(players.size()); ++i)
{
assert(players[i]);
// 第一路不静音, 其他全部静音
players[i]->Start(0, i!=0, 1, false);
//players[i]->Start(0, false, 1, false);
}
while (true)
{
while (MY_X11_Pending(display, 10))
{
XEvent xev;
memset(&xev, 0, sizeof(xev));
XNextEvent(display, &xev);
if (xev.type == ConfigureNotify)
{
if (xev.xconfigure.window == main_wid)
{
if (xev.xconfigure.width != main_w || xev.xconfigure.height != main_h)
{
main_w = xev.xconfigure.width;
main_h = xev.xconfigure.height;
SubWindowsLayout(main_w, main_h, border_w, static_cast<int>(players.size()), layout_rects);
for (auto i = 0; i < static_cast<int>(players.size()); ++i)
{
if (players[i]->GetWindow())
{
XMoveResizeWindow(display, players[i]->GetWindow(), layout_rects[i].x_, layout_rects[i].y_, layout_rects[i].w_, layout_rects[i].h_);
}
}
}
}
else
{
for (const auto& i: players)
{
assert(i);
if (i->GetWindow() && i->GetWindow() == xev.xconfigure.window)
{
i->OnWindowSize(xev.xconfigure.width, xev.xconfigure.height);
}
}
}
}
else if (xev.type == KeyPress)
{
if (xev.xkey.keycode == XKeysymToKeycode(display, XK_Escape))
{
fprintf(stdout, "ESC Key Press\n");
for (const auto& i : players)
{
i->Stop();
if (i->GetWindow())
{
XDestroyWindow(display, i->GetWindow());
i->SetWindow(None);
}
}
players.clear();
XDestroyWindow(display, main_wid);
XCloseDisplay(display);
player_api.UnInit();
fprintf(stdout, "Close Players....\n");
return 0;
}
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.