adapt to liblinphone 5
Stefan Schuermans

Stefan Schuermans commited on 2023-08-22 13:31:00
Showing 3 changed files, with 48 additions and 20 deletions.

... ...
@@ -8,7 +8,6 @@
8 8
 #include <iostream>
9 9
 #include <mutex>
10 10
 #include <sstream>
11
-#include <stdarg.h>
12 11
 #include <stdio.h>
13 12
 #include <string>
14 13
 #include <string.h>
... ...
@@ -42,16 +41,15 @@ namespace linphone {
42 41
 /// interlock for liblinphone, higher in mutex hierarchy than shared data mutex
43 42
 static std::mutex g_mtx;
44 43
 
45
-static char const *loglevel2str(OrtpLogLevel lev)
44
+static char const *loglevel2str(LinphoneLogLevel lev)
46 45
 {
47 46
   switch(lev) {
48
-  case ORTP_DEBUG: return "DEBUG";
49
-  case ORTP_MESSAGE: return "MESSAGE";
50
-  case ORTP_WARNING: return "WARNING";
51
-  case ORTP_ERROR: return "ERROR";
52
-  case ORTP_FATAL: return "FATAL";
53
-  case ORTP_TRACE: return "TRACE";
54
-  case ORTP_LOGLEV_END: return "LOGLEV_END";
47
+  case LinphoneLogLevelDebug: return "DEBUG";
48
+  case LinphoneLogLevelTrace: return "TRACE";
49
+  case LinphoneLogLevelMessage: return "MESSAGE";
50
+  case LinphoneLogLevelWarning: return "WARNING";
51
+  case LinphoneLogLevelError: return "ERROR";
52
+  case LinphoneLogLevelFatal: return "FATAL";
55 53
   default: return "?";
56 54
   }
57 55
 }
... ...
@@ -105,19 +103,21 @@ static char const *cstate2str(LinphoneCallState cstate)
105 103
   }
106 104
 }
107 105
 
106
+LinphoneLoggingService *glLogSrv; ///< logging service
107
+LinphoneLoggingServiceCbs *glLogCbs; ///< logging callbacks
108 108
 std::string glLogFileName; ///< name of global log file
109 109
 std::ofstream glLogStream; ///< stream to open global log file
110 110
 unsigned int glLogLineCnt; ///< number of lines already logged
111 111
 
112 112
 /**
113 113
  * @brief log information from linphone library
114
+ * @param[in] log_srv logging service - ignored
114 115
  * @param[in] domain log domain - ignored
115 116
  * @param[in] lev log level
116
- * @param[in] fmt format string
117
- * @param[in] args arguments to insert into format string
117
+ * @param[in] msg message string
118 118
  */
119
-void log_handler(const char *domain, OrtpLogLevel lev, const char *fmt,
120
-                 va_list args)
119
+static void log_msg_wr(LinphoneLoggingService *log_srv, const char *domain,
120
+                       LinphoneLogLevel lev, const char *msg)
121 121
 {
122 122
   // (re-)open logfile on first line
123 123
   if (glLogLineCnt == 0) {
... ...
@@ -130,11 +130,8 @@ void log_handler(const char *domain, OrtpLogLevel lev, const char *fmt,
130 130
     glLogStream.open(glLogFileName.c_str(), std::ios::out);
131 131
   }
132 132
   // write log line
133
-  char buffer[4096];
134
-  vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
135
-  buffer[sizeof(buffer) - 1] = 0;
136 133
   glLogStream << Time::now().toStr() << " "
137
-              << loglevel2str(lev) << " " << buffer << std::endl;
134
+              << loglevel2str(lev) << " " << msg << std::endl;
138 135
   // count lines
139 136
   ++glLogLineCnt;
140 137
   // close file if maximum line count reached
... ...
@@ -145,6 +142,7 @@ void log_handler(const char *domain, OrtpLogLevel lev, const char *fmt,
145 142
     // reset line count, so new file is opened on next line
146 143
     glLogLineCnt = 0;
147 144
   }
145
+  (void)log_srv;
148 146
   (void)domain;
149 147
 }
150 148
 
... ...
@@ -153,7 +151,18 @@ void init(std::string const &logFileName)
153 151
 {
154 152
   glLogFileName = logFileName;
155 153
   glLogLineCnt = 0;
156
-  linphone_core_set_log_handler(log_handler);
154
+  glLogSrv = linphone_logging_service_get();
155
+  linphone_logging_service_ref(glLogSrv);
156
+  LinphoneFactory *factory = linphone_factory_get();
157
+  glLogCbs = linphone_factory_create_logging_service_cbs(factory);
158
+  linphone_logging_service_cbs_set_log_message_written(glLogCbs, log_msg_wr);
159
+}
160
+
161
+/// global cleanup
162
+void exit()
163
+{
164
+  linphone_logging_service_cbs_unref(glLogCbs);
165
+  linphone_logging_service_unref(glLogSrv);
157 166
 }
158 167
 
159 168
 /// data of SIP client worker thread
... ...
@@ -423,13 +432,14 @@ void do_register(Data &data, std::string const &server,
423 432
   linphone_core_cbs_set_dtmf_received(data.callbacks, dtmf_received);
424 433
 
425 434
   // create linphone core object
426
-  data.lc = linphone_factory_create_core(factory, data.callbacks, NULL, NULL);
435
+  data.lc = linphone_factory_create_core_3(factory, NULL, NULL, NULL);
427 436
   if (! data.lc) {
428 437
     log(data, "failed to create linphone core");
429 438
     linphone_core_cbs_unref(data.callbacks);
430 439
     data.callbacks = nullptr;
431 440
     return;
432 441
   }
442
+  linphone_core_add_callbacks(data.lc, data.callbacks);
433 443
   linphone_core_set_user_data(data.lc, &data);
434 444
   log(data, "linphone core created");
435 445
 
... ...
@@ -490,7 +500,7 @@ void do_register(Data &data, std::string const &server,
490 500
 
491 501
   // tell linphone not to rind and to use files instead of sound card
492 502
   linphone_core_set_ring(data.lc, nullptr);
493
-  linphone_core_use_files(data.lc, TRUE);
503
+  linphone_core_set_use_files(data.lc, TRUE);
494 504
   linphone_core_set_play_file(data.lc, nullptr);
495 505
 }
496 506
 
... ...
@@ -505,6 +515,7 @@ void do_deregister(Data &data)
505 515
   }
506 516
 
507 517
   log(data, "unreferencing...");
518
+  linphone_core_remove_callbacks(data.lc, data.callbacks);
508 519
   linphone_core_unref(data.lc);
509 520
   data.lc = nullptr;
510 521
   linphone_core_cbs_unref(data.callbacks);
... ...
@@ -648,6 +659,16 @@ void SipPhone::init(const Directory &globalLogDir)
648 659
 #endif
649 660
 }
650 661
 
662
+/**
663
+ * @brief global cleanup (call once after finishing using this class)
664
+ */
665
+void SipPhone::exit()
666
+{
667
+#ifdef BLINKER_CFG_LINPHONE
668
+  linphone::exit();
669
+#endif
670
+}
671
+
651 672
 /**
652 673
  * @brief constructor
653 674
  * @param[in] name module name
... ...
@@ -64,6 +64,11 @@ public:
64 64
    */
65 65
   static void init(const Directory &globalLogDir);
66 66
 
67
+  /**
68
+   * @brief global cleanup (call once after finishing using this class)
69
+   */
70
+  static void exit();
71
+
67 72
 public:
68 73
   /**
69 74
    * @brief constructor
... ...
@@ -85,6 +85,8 @@ void run(const std::string &dirConfig)
85 85
 #undef MODULEMGR
86 86
 
87 87
   mgrs.m_callMgr.run();
88
+
89
+  SipPhone::exit();
88 90
 }
89 91
 
90 92
 int main(int argc, const char *argv[])
91 93