Skip to content

Latest commit

 

History

History
389 lines (294 loc) · 15.2 KB

migration.md

File metadata and controls

389 lines (294 loc) · 15.2 KB

Migrating

Everything that changed shape, and what to write instead.

Contents


Tracks are addressed by index, not by kind

A stream is however many tracks it has: two qualities, two languages, a picture and the metadata describing what is in it. The API that named a kind resolved to the first track of that kind, so a second one was silently never reported and there was no way to ask about it.

Client

Removed Use instead
NewVideoStream, NewAudioStream NewTrack
ReceivedVideoData, ReceivedAudioData ReceivedData
ReceivedRawVideoRTP, ReceivedRawAudioRTP ReceivedRawRTP
ReceivedRawVideoRTCP, ReceivedRawAudioRTCP ReceivedRawRTCP
SendVideoRTCP(rtcp), SendAudioRTCP(rtcp) SendRTCP(trackIndex, rtcp)
VideoSSRC, AudioSSRC GetSsrc(trackIndex), SetSsrc(trackIndex, ssrc)
VideoContext, AudioContext nothing — see the SRTP keys
NewStreamEventArgs NewTrackEventArgs

Every one of the replacements carries TrackIndex, Kind and Codec, so the track a frame came from is never in doubt. TrackCount says how many there are.

Before:

client.NewVideoStream += (s, e) => Console.WriteLine(e.StreamType);
client.ReceivedVideoData += (s, e) => Save(e.Data);
client.ReceivedAudioData += (s, e) => Play(e.Data);

After:

client.NewTrack += (s, e) => Console.WriteLine($"track {e.TrackIndex}: {e.Kind} {e.Codec}");

client.ReceivedData += (s, e) =>
{
    if (e.Kind == TrackKind.Video) Save(e.TrackIndex, e.Data);
    else if (e.Kind == TrackKind.Audio) Play(e.TrackIndex, e.Data);
};

If you really do want only the first track of a kind, that is now something you say rather than something the library assumes — see the next section.

Server

Removed Use instead
RTSPStreamSource.VideoTrack, AudioTrack, MetadataTrack Tracks, TrackById(id), AddTrack(track)
RTSPStreamSource(id, video, audio) RTSPStreamSource(id, params ITrack[]) — the same two-track call still compiles
RTSPStreamSource.GetTrackControl(TrackType) GetTrackControl(ITrack)
RTSPConnection.Video, Audio Streams, StreamOrNull(trackId)
RTSPConnection.SSRC RTPStream.SSRC on the stream you mean

Before:

var source = new RTSPStreamSource("stream1", h264Track, aacTrack);
ITrack video = source.VideoTrack;

After:

var source = new RTSPStreamSource("stream1", h264Track, aacTrack);  // unchanged
ITrack video = h264Track;                                           // you already have it
ITrack byId = source.TrackById(0);                                  // or ask for one

Two of these were bugs, not just awkward

Worth knowing if you run more than one track of a kind:

  • The server matched incoming RTCP to a session by looking at streams 0 and 1 only. A UDP session on any later track had its keepalives ignored — so the idle sweep dropped sessions whose clients were talking — and its reception reports discarded.
  • The RTP-Info header of a PLAY cast a track ID to a kind. A stream with two video tracks named the second one under the first audio track's control URL, or under the session URL when there was no audio track.

Both are fixed.


Which tracks to receive is one callback

MediaRequest and ReceiveAllTracks are gone. Between them they could say which kinds and how many of each, and nothing else — not "the H265 one", not "the English audio", and the metadata tracks were reachable only by turning every other extra track on at once.

Removed Use instead
MediaRequest.VIDEO_ONLY client.AcceptTrack = t => t.Kind == TrackKind.Video;
MediaRequest.AUDIO_ONLY client.AcceptTrack = t => t.Kind == TrackKind.Audio;
MediaRequest.VIDEO_AND_AUDIO client.AcceptTrack = t => t.Kind != TrackKind.Application;
ReceiveAllTracks = true client.AcceptTrack = _ => true;
ReceiveAllTracks = false the default, or RTSPClient.FirstOfEachKind written out

The mediaRequest parameter has been dropped from every Connect overload, so positional calls need the argument removed:

// before
client.Connect(uri, RTPTransport.TCP, user, pass, MediaRequest.VIDEO_AND_AUDIO, false, null, true);

// after
client.AcceptTrack = t => t.Kind != TrackKind.Application;
client.Connect(uri, RTPTransport.TCP, user, pass, false, null, true);

AcceptTrack is asked once per track, before anything is bound for it — a track passed over costs no transport, no SETUP, and nothing on the wire. It is only asked about tracks the client could actually play. The offer carries the codec, the payload type, where the track came in the description, and how many tracks have been accepted so far:

client.AcceptTrack = _ => true;                          // everything on offer
client.AcceptTrack = t => t.Codec == "H265";             // the H265 one of two video tracks
client.AcceptTrack = t => t.AcceptedSoFar < 2;           // the first two, whatever they are
client.AcceptTrack = t => t.Kind == TrackKind.Application;  // the metadata and nothing else

Unset, it behaves as RTSPClient.FirstOfEachKind: the first video track, the first audio track and the first metadata track.

One behaviour change. The first metadata track is now set up by default, where it used to require ReceiveAllTracks. If you do not want it: client.AcceptTrack = t => RTSPClient.FirstOfEachKind(t) && t.Kind != TrackKind.Application;

MaxTracks is unchanged and still applies: it bounds what a description can make the client spend, whatever the filter says.


A track has to say what kind of media it carries

TrackBase.Kind no longer defaults to (TrackType)ID. It throws NotImplementedException naming the track, so a track written outside this library has to say:

public override TrackType Kind => TrackType.Video;

The old default read a track's place in its stream as the sort of media it carries, which is right only for a stream that is one video track followed by one audio track. A second video track has ID 1 and reported itself as audio — and the kind is acted on: it decides which frames are thrown away first when a client falls behind, whether a frame is held back waiting for a keyframe, and which media section of an overridden SDP a track's control URL and keys belong to.

Every track this library ships already overrides it, so only your own tracks are affected.


The SRTP keys are not exposed

VideoContext and AudioContext are gone and there is no per-track replacement. An SrtpSessionContext is live crypto state: protecting a packet with one outside the client advances the roll over counter and the replay state, and the far end can then no longer read what follows.

What they were wanted for is sending RTCP on a protected stream, which is now done with the keys kept inside:

// before
byte[] protectedRtcp = RTSPClient.ProtectRtcp(client.VideoContext, report);
// ...and then somehow send it

// after
client.SendRTCP(trackIndex, report);

The server takes a user repository, not one user name and password

Every RTSPServer constructor took a string userName, string password pair, so a server had exactly one user. Anything downstream that asked who a client was got the same answer every time, which left AuthorizeStream with nothing it could decide on. They now take an IUserRepository:

// before
using var server = new RTSPServer(8554, "admin", "password");

// after
using var server = new RTSPServer(8554, new InMemoryUserRepository("admin", "password"));

For more than one user, or for users that come from somewhere else:

var users = new InMemoryUserRepository()
    .Add("alice", "alice-password")
    .Add("bob", "bob-password");

using var server = new RTSPServer(8554, users);

server.AuthorizeStream += (s, e) =>
{
    if (!TenantOwns(e.UserName, e.StreamID))
    {
        e.Deny(403);
    }
};
public interface IUserRepository
{
    UserInfo GetUser(string userName);   // null refuses
}

It is asked once per request that carries an Authorization header, on that connection's own receive thread, so an implementation that goes to a database holds up one client and no other. There is no asynchronous pair: the RTSP request path is synchronous, so the server would only have blocked on the result.

A server that does not authenticate used to be one given a null or empty user name. It is now one given no repository:

// before
using var server = new RTSPServer(8554, null, null);

// after
using var server = new RTSPServer(8554, (IUserRepository)null);

UserInfo.Password is the password in the clear, because that is what verifying a digest answer needs: the answer is built from MD5(user:realm:password) and there is no checking one without being able to compute it. Storing the hash instead - which is what a user database ought to keep - needs the digest verified here rather than by the transport library, which cannot be given anything but a password.

The sample servers read a Users section from appsettings.json instead of a UserName and Password pair:

"Users": [
  {
    "UserName": "admin",
    "Password": "password"
  },
  {
    "UserName": "viewer",
    "Password": "viewer-password"
  }
]

Logging belongs to the client or server, not the process

The static SharpRTSPClient.Log and SharpRTSPServer.Logging.Log classes are gone. They were process-wide, so two clients in one program wrote to the same place and turning trace on for a noisy one turned it on for all of them.

// before
Log.SinkDebug = (m, ex) => myLog.Write(m);
Log.TraceEnabled = true;

// after
client.Logger = new DefaultLog { Sink = m => myLog.Write(m), IsTraceEnabled = true };

ILog is five methods and five switches and takes no dependency on anything. NullLog.Instance says nothing at all. Hosts that already use Microsoft.Extensions.Logging can keep passing an ILoggerFactory to the constructor, which overrides Logger.

DefaultLog and NullLog are named for their pairing with ILog; NullLogger would have collided with Microsoft.Extensions.Logging.Abstractions.NullLogger.


Multicast is off by default

RTSPServer.MulticastEnabled now defaults to false. Once a group is open the media goes onto the local segment, where anything that joins the group receives it with no authentication at the RTP layer — so a single client that authenticates could previously make the server publish the stream to everything on the link.

server.MulticastEnabled = true;   // where that is the intent

Pair it with SAVP and RTSPStreamSource.SharedSrtpKey so only clients that were given the key can read what the group carries.


A stream's configuration does not mean it has parameter sets

NewTrackEventArgs.StreamConfigurationData used to be null for H264, H265 and H266 unless the SDP carried the parameter sets, and was always null for AV1 and Opus. It is now there for every H264, H265, H266, AV1, VP9 and Opus track, because it also carries what the fmtp says about the profile, level and the like - and where the fmtp says nothing, the defaults the payload format sets.

Class New
H264StreamConfigurationData ProfileLevelId, PacketizationMode
H265StreamConfigurationData ProfileSpace, ProfileId, TierFlag, LevelId, MaxDonDiff
H266StreamConfigurationData ProfileId, TierFlag, LevelId, MaxDonDiff
AV1StreamConfigurationData new: Profile, LevelIdx, Tier
VP9StreamConfigurationData new: ProfileId
OpusStreamConfigurationData new: SpropStereo, Stereo, UseInbandFec, UseDtx, SpropMaxCaptureRate
every one of them, AACStreamConfigurationData too Fmtp

Fmtp is the fmtp's format parameters as the SDP wrote them (null where it has none), for whatever the parsed properties do not cover. It is on IStreamConfigurationData as well, so it can be read without knowing the codec - which also means a class of your own that implements the interface has to add it.

Code that took a configuration to mean the parameter sets are known has to look at them instead:

// before
if (e.StreamConfigurationData is H264StreamConfigurationData config)
    WriteParameterSets(config.SPS, config.PPS);

// after
if (e.StreamConfigurationData is H264StreamConfigurationData config && config.SPS != null && config.PPS != null)
    WriteParameterSets(config.SPS, config.PPS);

Where the fmtp is there but cannot be read, such as a profile-level-id that is not six hex digits, the configuration is an UnparsedStreamConfigurationData holding only Fmtp, rather than the codec's own class claiming defaults the SDP contradicts. It used to be null. The track itself is set up all the same.


RTCP is SharpRTCP's

Reading and writing RTCP moved into the SharpRTCP package, which the client and the server both reference. SharpRTSPServer.RTCPUtils is still there and still writes what it wrote, but it is obsolete: every member hands the work to SharpRTCP.RTCPUtilities, and the compiler says what to use instead.

Obsolete Use instead
RTCPUtils.RTCP_VERSION RTCPUtilities.Version
RTCPUtils.RTCP_PACKET_TYPE_SENDER_REPORT, RTCP_PACKET_TYPE_BYE PacketType.SenderReport, PacketType.Goodbye
RTCPUtils.WriteRTCPHeader(rtcp, version, hasPadding, count, packetType, length, ssrc) RTCPUtilities.WriteHeader(packet, count, packetType, length, ssrc, hasPadding)
RTCPUtils.WriteSenderReport(rtcp, now, rtpTimestamp, packetCount, octetCount) RTCPUtilities.WriteSenderReport(packet, report)

The new WriteSenderReport writes the whole packet, header and report blocks included, where the old one wrote only the sender information and left the header to the caller:

// before
RTCPUtils.WriteRTCPHeader(rtcp, RTCPUtils.RTCP_VERSION, false, 0,
    RTCPUtils.RTCP_PACKET_TYPE_SENDER_REPORT, (28 / 4) - 1, ssrc);
RTCPUtils.WriteSenderReport(rtcp, DateTime.UtcNow, rtpTimestamp, packetCount, octetCount);

// after
RTCPUtilities.WriteSenderReport(rtcp, new SenderReport
{
    SSRC = ssrc,
    NTPTimestamp = SenderReport.ToNTPTimestamp(DateTime.UtcNow),
    RTPTimestamp = rtpTimestamp,
    PacketCount = packetCount,
    OctetCount = octetCount
});

SenderReport.ToNTPTimestamp reads a time that is not UTC as local time and converts it, where the old method took every time as UTC whatever its kind. Pass DateTime.UtcNow, as above, and the two agree.