Browse Source

Merge pull request #579 from paullouisageneau/capi-track-getters

Add getters for track mid and direction to C API
Paul-Louis Ageneau 3 years ago
parent
commit
988756ec18
5 changed files with 130 additions and 12 deletions
  1. 35 2
      DOC.md
  2. 2 0
      include/rtc/rtc.h
  3. 35 2
      pages/content/pages/reference.md
  4. 22 3
      src/capi.cpp
  5. 36 5
      test/capi_track.cpp

+ 35 - 2
DOC.md

@@ -682,7 +682,7 @@ Retrieves the SDP media description of a Track.
 
 
 Arguments:
 Arguments:
 
 
-- `dc`: the Track identifier
+- `tr`: the Track identifier
 - `buffer`: a user-supplied buffer to store the description
 - `buffer`: a user-supplied buffer to store the description
 - `size`: the size of `buffer`
 - `size`: the size of `buffer`
 
 
@@ -690,7 +690,40 @@ Return value: the length of the string copied in buffer (including the terminati
 
 
 If `buffer` is `NULL`, the description is not copied but the size is still returned.
 If `buffer` is `NULL`, the description is not copied but the size is still returned.
 
 
-### Media
+#### rtcGetTrackMid
+
+```
+int rtcGetTrackMid(int tr, char *buffer, int size)
+```
+
+Retrieves the mid (media indentifier) of a Track.
+
+Arguments:
+
+- `tr`: the Track identifier
+- `buffer`: a user-supplied buffer to store the mid
+- `size`: the size of `buffer`
+
+Return value: the length of the string copied in buffer (including the terminating null character) or a negative error code
+
+If `buffer` is `NULL`, the mid is not copied but the size is still returned.
+
+#### rtcGetTrackDirection
+
+```
+int rtcGetTrackDirection(int tr, rtcDirection *direction)
+```
+
+Retrieves the direction of a Track.
+
+Arguments:
+
+- `tr`: the Track identifier
+- `direction`: a pointer to a rtcDescription enum to store the result
+
+On success, the value pointed by `direction` will be set to one of the following: `RTC_DIRECTION_SENDONLY`, `RTC_DIRECTION_RECVONLY`, `RTC_DIRECTION_SENDRECV`, `RTC_DIRECTION_INACTIVE`, or `RTC_DIRECTION_UNKNOWN`.
+
+### Media handling
 
 
 TODO
 TODO
 
 

+ 2 - 0
include/rtc/rtc.h

@@ -255,6 +255,8 @@ RTC_EXPORT int rtcAddTrackEx(int pc, const rtcTrackInit *init);      // returns
 RTC_EXPORT int rtcDeleteTrack(int tr);
 RTC_EXPORT int rtcDeleteTrack(int tr);
 
 
 RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
 RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
+RTC_EXPORT int rtcGetTrackMid(int tr, char *buffer, int size);
+RTC_EXPORT int rtcGetTrackDirection(int tr, rtcDirection *direction);
 
 
 #if RTC_ENABLE_MEDIA
 #if RTC_ENABLE_MEDIA
 
 

+ 35 - 2
pages/content/pages/reference.md

@@ -685,7 +685,7 @@ Retrieves the SDP media description of a Track.
 
 
 Arguments:
 Arguments:
 
 
-- `dc`: the Track identifier
+- `tr`: the Track identifier
 - `buffer`: a user-supplied buffer to store the description
 - `buffer`: a user-supplied buffer to store the description
 - `size`: the size of `buffer`
 - `size`: the size of `buffer`
 
 
@@ -693,7 +693,40 @@ Return value: the length of the string copied in buffer (including the terminati
 
 
 If `buffer` is `NULL`, the description is not copied but the size is still returned.
 If `buffer` is `NULL`, the description is not copied but the size is still returned.
 
 
-### Media
+#### rtcGetTrackMid
+
+```
+int rtcGetTrackMid(int tr, char *buffer, int size)
+```
+
+Retrieves the mid (media indentifier) of a Track.
+
+Arguments:
+
+- `tr`: the Track identifier
+- `buffer`: a user-supplied buffer to store the mid
+- `size`: the size of `buffer`
+
+Return value: the length of the string copied in buffer (including the terminating null character) or a negative error code
+
+If `buffer` is `NULL`, the mid is not copied but the size is still returned.
+
+#### rtcGetTrackDirection
+
+```
+int rtcGetTrackDirection(int tr, rtcDirection *direction)
+```
+
+Retrieves the direction of a Track.
+
+Arguments:
+
+- `tr`: the Track identifier
+- `direction`: a pointer to a rtcDescription enum to store the result
+
+On success, the value pointed by `direction` will be set to one of the following: `RTC_DIRECTION_SENDONLY`, `RTC_DIRECTION_RECVONLY`, `RTC_DIRECTION_SENDRECV`, `RTC_DIRECTION_INACTIVE`, or `RTC_DIRECTION_UNKNOWN`.
+
+### Media handling
 
 
 TODO
 TODO
 
 

+ 22 - 3
src/capi.cpp

@@ -1012,6 +1012,24 @@ int rtcGetTrackDescription(int tr, char *buffer, int size) {
 	});
 	});
 }
 }
 
 
+int rtcGetTrackMid(int tr, char *buffer, int size) {
+	return wrap([&] {
+		auto track = getTrack(tr);
+		return copyAndReturn(track->mid(), buffer, size);
+	});
+}
+
+int rtcGetTrackDirection(int tr, rtcDirection *direction) {
+	return wrap([&] {
+		if (!direction)
+			throw std::invalid_argument("Unexpected null pointer for track direction");
+
+		auto track = getTrack(tr);
+		*direction = static_cast<rtcDirection>(track->direction());
+		return RTC_ERR_SUCCESS;
+	});
+}
+
 #if RTC_ENABLE_MEDIA
 #if RTC_ENABLE_MEDIA
 
 
 void setSSRC(Description::Media *description, uint32_t ssrc, const char *_name, const char *_msid,
 void setSSRC(Description::Media *description, uint32_t ssrc, const char *_name, const char *_msid,
@@ -1373,12 +1391,13 @@ void rtcPreload() {
 void rtcCleanup() {
 void rtcCleanup() {
 	try {
 	try {
 		size_t count = eraseAll();
 		size_t count = eraseAll();
-		if(count != 0) {
+		if (count != 0) {
 			PLOG_INFO << count << " objects were not properly destroyed before cleanup";
 			PLOG_INFO << count << " objects were not properly destroyed before cleanup";
 		}
 		}
 
 
-		if(rtc::Cleanup().wait_for(10s) == std::future_status::timeout)
-			throw std::runtime_error("Cleanup timeout (possible deadlock or undestructible object)");
+		if (rtc::Cleanup().wait_for(10s) == std::future_status::timeout)
+			throw std::runtime_error(
+			    "Cleanup timeout (possible deadlock or undestructible object)");
 
 
 	} catch (const std::exception &e) {
 	} catch (const std::exception &e) {
 		PLOG_ERROR << e.what();
 		PLOG_ERROR << e.what();

+ 36 - 5
test/capi_track.cpp

@@ -41,7 +41,8 @@ static Peer *peer1 = NULL;
 static Peer *peer2 = NULL;
 static Peer *peer2 = NULL;
 
 
 static const char *mediaDescription = "video 9 UDP/TLS/RTP/SAVPF\r\n"
 static const char *mediaDescription = "video 9 UDP/TLS/RTP/SAVPF\r\n"
-                                      "a=mid:video\r\n";
+                                      "a=mid:video\r\n"
+                                      "a=sendonly\r\n";
 
 
 static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
 static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
 	Peer *peer = (Peer *)ptr;
 	Peer *peer = (Peer *)ptr;
@@ -82,13 +83,31 @@ static void RTC_API closedCallback(int id, void *ptr) {
 
 
 static void RTC_API trackCallback(int pc, int tr, void *ptr) {
 static void RTC_API trackCallback(int pc, int tr, void *ptr) {
 	Peer *peer = (Peer *)ptr;
 	Peer *peer = (Peer *)ptr;
+
+	char buffer[1024];
+	if (rtcGetTrackDescription(tr, buffer, 1024) < 0) {
+		fprintf(stderr, "rtcGetTrackDescription failed\n");
+		return;
+	}
+
+	printf("Track %d: Received with media description: \n%s\n", peer == peer1 ? 1 : 2, buffer);
+
+	char mid[256];
+	if (rtcGetTrackMid(tr, mid, 256) < 0 || strcmp(mid, "video") != 0) {
+		fprintf(stderr, "rtcGetTrackMid failed\n");
+		return;
+	}
+
+	// Description is reversed here
+	rtcDirection direction;
+	if (rtcGetTrackDirection(tr, &direction) < 0 || direction != RTC_DIRECTION_RECVONLY) {
+		fprintf(stderr, "rtcGetTrackDirection failed\n");
+		return;
+	}
+
 	peer->tr = tr;
 	peer->tr = tr;
 	rtcSetOpenCallback(tr, openCallback);
 	rtcSetOpenCallback(tr, openCallback);
 	rtcSetClosedCallback(tr, closedCallback);
 	rtcSetClosedCallback(tr, closedCallback);
-
-	char buffer[1024];
-	if (rtcGetTrackDescription(tr, buffer, 1024) >= 0)
-		printf("Track %d: Received with media description: \n%s\n", peer == peer1 ? 1 : 2, buffer);
 }
 }
 
 
 static Peer *createPeer(const rtcConfiguration *config) {
 static Peer *createPeer(const rtcConfiguration *config) {
@@ -155,6 +174,18 @@ int test_capi_track_main() {
 	rtcSetOpenCallback(peer1->tr, openCallback);
 	rtcSetOpenCallback(peer1->tr, openCallback);
 	rtcSetClosedCallback(peer1->tr, closedCallback);
 	rtcSetClosedCallback(peer1->tr, closedCallback);
 
 
+	char mid[256];
+	if (rtcGetTrackMid(peer1->tr, mid, 256) < 0 || strcmp(mid, "video") != 0) {
+		fprintf(stderr, "rtcGetTrackMid failed\n");
+		goto error;
+	}
+
+	rtcDirection direction;
+	if (rtcGetTrackDirection(peer1->tr, &direction) < 0 || direction != RTC_DIRECTION_SENDONLY) {
+		fprintf(stderr, "rtcGetTrackDirection failed\n");
+		goto error;
+	}
+
 	// Initiate the handshake
 	// Initiate the handshake
 	rtcSetLocalDescription(peer1->pc, NULL);
 	rtcSetLocalDescription(peer1->pc, NULL);