Skip to content

Commit 663a557

Browse files
committed
add rpc methods
- jamulus/getAvailableMethods - jamulusclient/setMuted - jamulusserver/broadcastChatMessage - jamulusserver/getClientDetails - jamulusserver/chatMessageReceived - jamulusserver/clientConnected - jamulusserver/clientDisconnected
1 parent 76fc757 commit 663a557

8 files changed

Lines changed: 380 additions & 3 deletions

File tree

docs/JSON-RPC.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ Results:
9595
| result | string | "ok" on success |
9696

9797

98+
### jamulus/getAvailableMethods
99+
100+
Returns all available rpc methods.
101+
102+
Parameters:
103+
104+
| Name | Type | Description |
105+
| --- | --- | --- |
106+
| params | object | No parameters (empty object). |
107+
108+
Results:
109+
110+
| Name | Type | Description |
111+
| --- | --- | --- |
112+
| result.methods | array | All available methods. |
113+
114+
98115
### jamulus/getMode
99116

100117
Returns the current mode, i.e. whether Jamulus is running as a server or client.
@@ -291,6 +308,23 @@ Results:
291308
| result | string | Always "ok". |
292309

293310

311+
### jamulusclient/setMuted
312+
313+
Mutes or unmutes the client.
314+
315+
Parameters:
316+
317+
| Name | Type | Description |
318+
| --- | --- | --- |
319+
| params.muted | boolean | muted (true or false). |
320+
321+
Results:
322+
323+
| Name | Type | Description |
324+
| --- | --- | --- |
325+
| result | string | Always "ok". |
326+
327+
294328
### jamulusclient/setName
295329

296330
Sets your name.
@@ -325,6 +359,50 @@ Results:
325359
| result | string | Always "ok". |
326360

327361

362+
### jamulusserver/broadcastChatMessage
363+
364+
Sends a chat message to all connected clients.
365+
366+
Parameters:
367+
368+
| Name | Type | Description |
369+
| --- | --- | --- |
370+
| params.chatMessage | string | The chat message text. |
371+
372+
Results:
373+
374+
| Name | Type | Description |
375+
| --- | --- | --- |
376+
| result | string | Always "ok". |
377+
378+
379+
### jamulusserver/getClientDetails
380+
381+
Returns the list of connected clients along with complete details about them.
382+
383+
Parameters:
384+
385+
| Name | Type | Description |
386+
| --- | --- | --- |
387+
| params | object | No parameters (empty object). |
388+
389+
Results:
390+
391+
| Name | Type | Description |
392+
| --- | --- | --- |
393+
| result.clients | array | The list of connected clients. |
394+
| result.clients[*].id | number | The client’s channel id. |
395+
| result.clients[*].address | string | The client’s address (ip:port). |
396+
| result.clients[*].name | string | The client’s name. |
397+
| result.clients[*].city | string | The client’s city. |
398+
| result.clients[*].country | string | The client’s country. |
399+
| result.clients[*].instr | string | The client’s instrument. |
400+
| result.clients[*].instrpic | string | The client’s instrument picture. |
401+
| result.clients[*].skill | string | The client’s skill. |
402+
| result.clients[*].jitterBufferSize | number | The client’s jitter buffer size. |
403+
| result.clients[*].channels | number | The number of audio channels of the client. |
404+
405+
328406
### jamulusserver/getClients
329407

330408
Returns the list of connected clients along with details about them.
@@ -621,3 +699,38 @@ Parameters:
621699
| params.servers[*].city | string | Server city. |
622700

623701

702+
### jamulusserver/chatMessageReceived
703+
704+
Emitted when a chat message is received.
705+
706+
Parameters:
707+
708+
| Name | Type | Description |
709+
| --- | --- | --- |
710+
| params.chatMessage | string | Chat message text. |
711+
712+
713+
### jamulusserver/clientConnected
714+
715+
Emitted when a client has connected to the server.
716+
717+
Parameters:
718+
719+
| Name | Type | Description |
720+
| --- | --- | --- |
721+
| params.id | number | The channel ID assigned to the client. |
722+
| params.address | string | The client's address. |
723+
| params.totalChannels | number | Number of total channels connected to the server. |
724+
725+
726+
### jamulusserver/clientDisconnected
727+
728+
Emitted when a client has disconnected from the server.
729+
730+
Parameters:
731+
732+
| Name | Type | Description |
733+
| --- | --- | --- |
734+
| params.id | number | The channel ID assigned to the client. |
735+
736+

src/client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ class CClient : public QObject
329329
// settings
330330
CChannelCoreInfo ChannelInfo;
331331
QString strClientName;
332+
void OnRPCInMuteMyself ( bool bMute ) { OnControllerInMuteMyself ( bMute ); }
332333

333334
public:
334335
void SetSettings ( CClientSettings* settings );

src/clientrpc.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe
497497
response["result"] = jsonDevices;
498498
Q_UNUSED ( params );
499499
} );
500+
501+
/// @rpc_method jamulusclient/setMuted
502+
/// @brief Mutes or unmutes the client.
503+
/// @param {boolean} params.muted - muted (true or false).
504+
/// @result {string} result - Always "ok".
505+
pRpcServer->HandleMethod ( "jamulusclient/setMuted", [=] ( const QJsonObject& params, QJsonObject& response ) {
506+
auto muted = params["muted"];
507+
if ( !muted.isBool() )
508+
{
509+
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: muted is not a boolean" );
510+
return;
511+
}
512+
513+
pClient->OnRPCInMuteMyself ( muted.toBool() );
514+
515+
response["result"] = "ok";
516+
} );
500517
}
501518

502519
QJsonValue CClientRpc::SerializeSkillLevel ( ESkillLevel eSkillLevel )

src/rpcserver.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ CRpcServer::CRpcServer ( QObject* parent, QString strBindIP, int iPort, QString
6666
response["result"] = result;
6767
Q_UNUSED ( params );
6868
} );
69+
70+
/// @rpc_method jamulus/getAvailableMethods
71+
/// @brief Returns all available rpc methods.
72+
/// @param {object} params - No parameters (empty object).
73+
/// @result {array} result.methods - All available methods.
74+
HandleMethod ( "jamulus/getAvailableMethods", [=] ( const QJsonObject& params, QJsonObject& response ) {
75+
QJsonObject result = getAvailableMethods();
76+
response["result"] = result;
77+
Q_UNUSED ( params );
78+
} );
6979
}
7080

7181
CRpcServer::~CRpcServer()
@@ -224,6 +234,22 @@ void CRpcServer::HandleApiAuth ( QTcpSocket* pSocket, const QJsonObject& params,
224234

225235
void CRpcServer::HandleMethod ( const QString& strMethod, CRpcHandler pHandler ) { mapMethodHandlers[strMethod] = pHandler; }
226236

237+
QJsonObject CRpcServer::getAvailableMethods()
238+
{
239+
QJsonArray methods;
240+
QMapIterator<QString, CRpcHandler> i ( mapMethodHandlers );
241+
242+
while ( i.hasNext() )
243+
{
244+
i.next();
245+
methods.append ( i.key() );
246+
}
247+
QJsonObject result{
248+
{ "methods", methods },
249+
};
250+
return result;
251+
}
252+
227253
void CRpcServer::ProcessMessage ( QTcpSocket* pSocket, QJsonObject message, QJsonObject& response )
228254
{
229255
if ( !message["method"].isString() )

src/rpcserver.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ class CRpcServer : public QObject
6969
CRpcServer ( QObject* parent, QString strBindIP, int iPort, QString secret );
7070
virtual ~CRpcServer();
7171

72-
bool Start();
73-
void HandleMethod ( const QString& strMethod, CRpcHandler pHandler );
74-
void BroadcastNotification ( const QString& strMethod, const QJsonObject& aParams );
72+
bool Start();
73+
void HandleMethod ( const QString& strMethod, CRpcHandler pHandler );
74+
void BroadcastNotification ( const QString& strMethod, const QJsonObject& aParams );
75+
QJsonObject getAvailableMethods();
7576

7677
static QJsonObject CreateJsonRpcError ( int code, QString message );
7778

src/server.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ void CServer::OnNewConnection ( int iChID, int iTotChans, CHostAddress RecHostAd
470470

471471
// logging of new connected channel
472472
Logging.AddNewConnection ( RecHostAddr.InetAddr, iTotChans );
473+
474+
emit ClientConnected ( iChID, RecHostAddr.InetAddr, iTotChans );
473475
}
474476

475477
void CServer::OnServerFull ( CHostAddress RecHostAddr )
@@ -876,6 +878,8 @@ void CServer::DecodeReceiveData ( const int iChanCnt, const int iNumClients )
876878
{
877879
emit ClientDisconnected ( iCurChanID ); // TODO do this outside the mutex lock?
878880
}
881+
// notify RPC server of disconnection
882+
emit ClientDisconnectedRPC ( iCurChanID );
879883

880884
FreeChannel ( iCurChanID ); // note that the channel is now not in use
881885

@@ -1306,6 +1310,22 @@ void CServer::CreateAndSendChatTextForAllConChannels ( const int iCurChanID, con
13061310
vecChannels[i].CreateChatTextMes ( strActualMessageText );
13071311
}
13081312
}
1313+
emit receivedChatMessage ( strActualMessageText );
1314+
}
1315+
1316+
// external chat
1317+
void CServer::CreateAndSendChatTextForAllConChannels ( const QString& strChatText )
1318+
{
1319+
// Send chat text to all connected clients ---------------------------------
1320+
for ( int i = 0; i < iMaxNumChannels; i++ )
1321+
{
1322+
if ( vecChannels[i].IsConnected() )
1323+
{
1324+
// send message
1325+
vecChannels[i].CreateChatTextMes ( strChatText );
1326+
}
1327+
}
1328+
emit receivedChatMessage ( strChatText );
13091329
}
13101330

13111331
void CServer::CreateAndSendRecorderStateForAllConChannels()
@@ -1571,6 +1591,68 @@ void CServer::SetWelcomeMessage ( const QString& strNWelcMess )
15711591
strWelcomeMessage = strWelcomeMessage.left ( MAX_LEN_CHAT_TEXT );
15721592
}
15731593

1594+
void CServer::GetCompleteClientInfos ( CVector<CHostAddress>& vecHostAddresses,
1595+
CVector<QString>& vecsName,
1596+
CVector<QString>& vecsCity,
1597+
CVector<QString>& vecsCountry,
1598+
CVector<QString>& vecsInstr,
1599+
CVector<QString>& vecsInstrPic,
1600+
CVector<QString>& vecsSkill,
1601+
CVector<int>& veciJitBufNumFrames,
1602+
CVector<int>& veciNetwFrameSizeFact )
1603+
{
1604+
// init return values
1605+
vecHostAddresses.Init ( iMaxNumChannels );
1606+
vecsName.Init ( iMaxNumChannels );
1607+
vecsCity.Init ( iMaxNumChannels );
1608+
vecsCountry.Init ( iMaxNumChannels );
1609+
vecsInstr.Init ( iMaxNumChannels );
1610+
vecsInstrPic.Init ( iMaxNumChannels );
1611+
vecsSkill.Init ( iMaxNumChannels );
1612+
veciJitBufNumFrames.Init ( iMaxNumChannels );
1613+
veciNetwFrameSizeFact.Init ( iMaxNumChannels );
1614+
1615+
// check all possible channels
1616+
for ( int i = 0; i < iMaxNumChannels; i++ )
1617+
{
1618+
if ( vecChannels[i].IsConnected() )
1619+
{
1620+
// get requested data
1621+
vecHostAddresses[i] = vecChannels[i].GetAddress();
1622+
vecsName[i] = vecChannels[i].GetName();
1623+
vecsCity[i] = vecChannels[i].GetChanInfo().strCity;
1624+
vecsCountry[i] = QLocale::countryToString ( vecChannels[i].GetChanInfo().eCountry );
1625+
vecsInstr[i] = CInstPictures::GetName ( vecChannels[i].GetChanInfo().iInstrument );
1626+
vecsInstrPic[i] = CInstPictures::GetResourceReference ( vecChannels[i].GetChanInfo().iInstrument );
1627+
vecsSkill[i] = "";
1628+
veciJitBufNumFrames[i] = vecChannels[i].GetSockBufNumFrames();
1629+
veciNetwFrameSizeFact[i] = vecChannels[i].GetNetwFrameSizeFact();
1630+
switch ( vecChannels[i].GetChanInfo().eSkillLevel )
1631+
{
1632+
case SL_BEGINNER:
1633+
vecsSkill[i] = "\"Beginner\"";
1634+
break;
1635+
1636+
case SL_INTERMEDIATE:
1637+
vecsSkill[i] = "\"Intermediate\"";
1638+
break;
1639+
1640+
case SL_PROFESSIONAL:
1641+
vecsSkill[i] = "\"Expert\"";
1642+
break;
1643+
1644+
case SL_NOT_SET:
1645+
vecsSkill[i] = "";
1646+
break;
1647+
1648+
default:
1649+
vecsSkill[i] = "";
1650+
break;
1651+
}
1652+
}
1653+
}
1654+
}
1655+
15741656
void CServer::customEvent ( QEvent* pEvent )
15751657
{
15761658
if ( pEvent->type() == QEvent::User + 11 )

src/server.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
191191
void SetEnableDelayPanning ( bool bDelayPanningOn ) { bDelayPan = bDelayPanningOn; }
192192
bool IsDelayPanningEnabled() { return bDelayPan; }
193193

194+
virtual void CreateAndSendChatTextForAllConChannels ( const QString& strChatText );
195+
void GetCompleteClientInfos ( CVector<CHostAddress>& vecHostAddresses,
196+
CVector<QString>& vecsName,
197+
CVector<QString>& vecsCity,
198+
CVector<QString>& vecsCountry,
199+
CVector<QString>& vecsInstr,
200+
CVector<QString>& vecsInstrPic,
201+
CVector<QString>& vecsSkill,
202+
CVector<int>& veciJitBufNumFrames,
203+
CVector<int>& veciNetwFrameSizeFact );
204+
194205
protected:
195206
// access functions for actual channels
196207
bool IsConnected ( const int iChanNum ) { return vecChannels[iChanNum].IsConnected(); }
@@ -328,6 +339,9 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
328339
void Started();
329340
void Stopped();
330341
void ClientDisconnected ( const int iChID );
342+
void ClientDisconnectedRPC ( const int iChID );
343+
void ClientConnected ( const int iChID, const QHostAddress RecHostAddr, const int iTotChans );
344+
void receivedChatMessage ( const QString& strChatText );
331345
void SvrRegStatusChanged();
332346
void AudioFrame ( const int iChID,
333347
const QString stChName,

0 commit comments

Comments
 (0)