/*************************************************************************************** * * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. * * By downloading, copying, installing or using the software you agree to this license. * If you do not agree to this license, do not download, install, * copy or use the software. * * Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved. * * Redistribution and use in binary forms, with or without modification, are permitted. * * 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 "sys_inc.h" #include "rtmp_cln.h" #include "hqueue.h" #include "media_format.h" /**********************************************************/ HQUEUE * g_queue; int g_flag = 0; pthread_t g_tid = 0; typedef struct { int event; CRtmpClient * rtmp; } EVENT_PARAMS; /**********************************************************/ /** * @desc : rtmp notify callback * * @params : * event : event type * puser : user parameter */ int rtmp_notify_callback(int event, void * puser) { printf("%s, event = %d\r\n", __FUNCTION__, event); CRtmpClient * p_rtmp = (CRtmpClient *) puser; if (RTMP_EVE_VIDEOREADY == event) { int vcodec = p_rtmp->video_codec(); if (vcodec != VIDEO_CODEC_NONE) { char codec_str[20] = {'\0'}; switch (vcodec) { case VIDEO_CODEC_H264: strcpy(codec_str, "H264"); break; case VIDEO_CODEC_H265: strcpy(codec_str, "H265"); break; case VIDEO_CODEC_MP4: strcpy(codec_str, "MP4"); break; case VIDEO_CODEC_JPEG: strcpy(codec_str, "JPEG"); break; } printf("video codec is %s\r\n", codec_str); } } else if (RTMP_EVE_AUDIOREADY == event) { int acodec = p_rtmp->audio_codec(); if (acodec != AUDIO_CODEC_NONE) { char codec_str[20] = {'\0'}; switch (acodec) { case AUDIO_CODEC_G711A: strcpy(codec_str, "G711A"); break; case AUDIO_CODEC_G711U: strcpy(codec_str, "G711U"); break; case AUDIO_CODEC_G722: strcpy(codec_str, "G722"); break; case AUDIO_CODEC_G726: strcpy(codec_str, "G726"); break; case AUDIO_CODEC_OPUS: strcpy(codec_str, "OPUS"); break; case AUDIO_CODEC_AAC: strcpy(codec_str, "AAC"); break; } printf("audio codec is %s\r\n", codec_str); printf("audio sample rate is %d\r\n", p_rtmp->get_audio_samplerate()); printf("audio channels is %d\r\n", p_rtmp->get_audio_channels()); } } EVENT_PARAMS params; params.event = event; params.rtmp = p_rtmp; if (!hqBufPut(g_queue, (char *) ¶ms)) { printf("hqBufPut failed\r\n"); } return 0; } /** * @desc : rtmp audio data callback * * @params : * pdata : audio data buffer * len : audio data buffer length * ts : timestamp * puser : user parameter */ int rtmp_audio_callback(uint8 * pdata, int len, uint32 ts, void * puser) { CRtmpClient * p_rtmp = (CRtmpClient *) puser; printf("%s, len = %d, ts = %u\r\n", __FUNCTION__, len, ts); return 0; } /** * @desc : rtmp video data callback * * @params : * pdata : video data buffer * len : video data buffer length * ts : timestamp * puser : user parameter */ int rtmp_video_callback(uint8 * pdata, int len, uint32 ts, void * puser) { CRtmpClient * p_rtmp = (CRtmpClient *) puser; printf("%s, len = %d, ts = %u\r\n", __FUNCTION__, len, ts); return 0; } void rtmp_setup(CRtmpClient * p_rtmp) { p_rtmp->set_notify_cb(rtmp_notify_callback, p_rtmp); p_rtmp->set_audio_cb(rtmp_audio_callback); p_rtmp->set_video_cb(rtmp_video_callback); } void rtmp_reconn(CRtmpClient * p_rtmp) { char url[512], user[64], pass[64]; strcpy(url, p_rtmp->get_url()); strcpy(user, p_rtmp->get_user()); strcpy(pass, p_rtmp->get_pass()); printf("rtsp_reconn, url = %s, user = %s, pass = %s\r\n", url, user, pass); p_rtmp->rtmp_close(); rtmp_setup(p_rtmp); p_rtmp->rtmp_start(url, user, pass); } void * rtmp_notify_handler(void * argv) { EVENT_PARAMS params; while (g_flag) { if (hqBufGet(g_queue, (char *) ¶ms)) { if (params.event == -1 || params.rtmp == NULL) { break; } if (RTMP_EVE_STOPPED == params.event || RTMP_EVE_CONNFAIL == params.event || RTMP_EVE_NOSIGNAL == params.event || RTMP_EVE_NODATA == params.event) { rtmp_reconn(params.rtmp); usleep(100*1000); } } } g_tid = 0; printf("%s exit\r\n", __FUNCTION__); return NULL; } #define RTMP_CLN_NUM 1 int main(int argc, char * argv[]) { if (argc < 2) { printf("usage: %s url {user} {pass}\r\n", argv[0]); return -1; } log_init("rtmptest.log"); log_set_level(HT_LOG_DBG); network_init(); // create event queue g_queue = hqCreate(RTMP_CLN_NUM * 4, sizeof(EVENT_PARAMS), HQ_GET_WAIT | HQ_PUT_WAIT); if (NULL == g_queue) { printf("create queue failed\r\n"); return -1; } // create event handler thread g_flag = 1; g_tid = sys_os_create_thread((void *)rtmp_notify_handler, NULL); if (g_tid == 0) { printf("create rtmp notify handler thread failed\r\n"); return -1; } CRtmpClient * rtmp = new CRtmpClient[RTMP_CLN_NUM]; for (int i = 0; i < RTMP_CLN_NUM; i++) { rtmp_setup(&rtmp[i]); char * p_user = NULL; char * p_pass = NULL; if (argc >= 3) { p_user = argv[2]; } if (argc >= 4) { p_pass = argv[3]; } BOOL ret = rtmp[i].rtmp_start(argv[1], p_user, p_pass); printf("rtmp %d start ret = %d\r\n", i, ret); usleep(100 * 1000); } for (;;) { if (getchar() == 'q') { break; } usleep(1000*1000); // 1s } for (int i = 0; i < RTMP_CLN_NUM; i++) { rtmp[i].rtmp_close(); } delete[] rtmp; g_flag = 0; EVENT_PARAMS params; params.event = -1; params.rtmp = NULL; hqBufPut(g_queue, (char *) ¶ms); // waiting for event handler thread to exit while (g_tid) { usleep(10*1000); } hqDelete(g_queue); g_queue = NULL; // close log log_close(); return 0; }