概述
JSON 是轻量级的文本数据交换格式,它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。
metaRTC新版本新增了纯C版的JSON支持,不需要第三方类库,增强了信令和数据交换便宜性。
mqtt+json已经成为物联网及webrtc信令交互的首选方案。
源码下载
GitHub - metartc/metaRTC: A cross-platform WebRTC SDKA cross-platform WebRTC SDK. Contribute to metartc/metaRTC development by creating an account on GitHub.https://github.com/metartc/metaRTCmetaRTC: metaRTC为纯C版本的webrtc 可与谷歌webrtc类库互通,支持Webrtc/SRT/RTMPhttps://gitee.com/metartc/metaRTC
yang_config.h配置文件修改
#define Yang_Enable_Datachannel 1
#define Yang_Enable_Mqtt 1
#define Yang_Enable_Json 1
#define Yang_Enable_Openh264 1
修改 Yang_Enable_Json 为 1
c/c++ create and destroy
int32_t yang_create_jsonReader(YangJsonReader* reader,char* jsonStr);
void yang_destroy_jsonReader(YangJsonReader* reader);
int32_t yang_create_jsonWriter(YangJsonWriter* writer);
void yang_destroy_jsonWriter(YangJsonWriter* writer);
c/c++ reader simple sample
YangJsonReader reader;
if(yang_create_jsonReader(&reader,sdp)!=Yang_Ok){
yang_error("read srs response json error!");
return;
}
YangJson* jcode = reader.getObjectItemCaseSensitive(reader.session, "code");
YangJson* sessionid = reader.getObjectItemCaseSensitive(reader.session, "sessionid");
if (reader.isNumber(jcode))
{
srs->retcode=jcode->valueint;
}
if (reader.isString(sessionid) && (sessionid->valuestring != NULL))
{
srs->sessionid=(char*)yang_calloc(yang_strlen(sessionid->valuestring)+1,1);
yang_memcpy(srs->sessionid,sessionid->valuestring,yang_strlen(sessionid->valuestring));
}
yang_destroy_jsonReader(&reader);
c/c++ writer simple sample
YangJsonWriter writer;
yang_create_jsonWriter(&writer);
writer.addNumberToObject(writer.session,"tid",tid);
writer.addStringToObject(writer.session,"streamurl",streamurl);
writer.addNullToObject(writer.session,"clientip");
char *srsAnswer=writer.printUnformatted(writer.session);
//process srsAnswer
yang_destroy_jsonWriter(&writer);