Skip to content

Commit

Permalink
Added hasBframes and keyFrameInterval to the GetStreamInfo API
Browse files Browse the repository at this point in the history
  • Loading branch information
choigilhoon committed Mar 24, 2023
1 parent 3059bef commit ef94cc3
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
8 changes: 8 additions & 0 deletions docs/rest-api/v1/virtualhost/application/stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ Content-Type: application/json
"bypass": false,
"codec": "H264",
"framerate": 30.0,
"hasBframes": false,
"keyFrameInterval": 30,
"height": 720,
"width": 1280
}
Expand Down Expand Up @@ -376,6 +378,12 @@ Content-Type: application/json
Details of the stream
```

{% code title="Note" overflow="wrap" lineNumbers="false" %}
```
keyFrameInterval is GOP size
```
{% endcode %}

</details>

<details>
Expand Down
18 changes: 17 additions & 1 deletion src/projects/base/info/media_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,15 @@ bool MediaTrack::HasQualityMeasured()
return _has_quality_measured;
}

void MediaTrack::OnFrameAdded(uint64_t bytes)
void MediaTrack::OnFrameAdded(const std::shared_ptr<MediaPacket> &media_packet)
{
if (_clock_from_first_frame_received.IsStart() == false)
{
_clock_from_first_frame_received.Start();
}

size_t bytes = media_packet->GetDataLength();

_total_frame_count++;
_total_frame_bytes += bytes;

Expand All @@ -468,6 +470,19 @@ void MediaTrack::OnFrameAdded(uint64_t bytes)

logtd("Track(%u) FPS(%f)", GetId(), framerate);
}

if (GetMediaType() == cmn::MediaType::Video)
{
if (media_packet->GetFlag() == MediaPacketFlag::Key)
{
SetKeyFrameIntervalByMeasured(_key_frame_interval_count);
_key_frame_interval_count = 1;
}
else if (_key_frame_interval_count > 0)
{
_key_frame_interval_count++;
}
}
}

int64_t MediaTrack::GetTotalFrameCount() const
Expand Down Expand Up @@ -562,6 +577,7 @@ std::shared_ptr<MediaTrack> MediaTrack::Clone()
track->_height = _height;
track->_height_conf = _height_conf;
track->_key_frame_interval = _key_frame_interval;
track->_key_frame_interval_conf = _key_frame_interval_conf;
track->_b_frames = _b_frames;
track->_has_bframe = _has_bframe;
track->_preset = _preset;
Expand Down
3 changes: 2 additions & 1 deletion src/projects/base/info/media_track.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MediaTrack : public VideoTrack, public AudioTrack
std::shared_ptr<ov::Data> &GetCodecExtradata();

// For statistics
void OnFrameAdded(uint64_t bytes);
void OnFrameAdded(const std::shared_ptr<MediaPacket> &media_packet);

int64_t GetTotalFrameCount() const;
int64_t GetTotalFrameBytes() const;
Expand Down Expand Up @@ -159,6 +159,7 @@ class MediaTrack : public VideoTrack, public AudioTrack
// Statistics
uint64_t _total_frame_count = 0;
uint64_t _total_frame_bytes = 0;
int32_t _key_frame_interval_count = 0;

// Validity
bool _is_valid = false;
Expand Down
29 changes: 25 additions & 4 deletions src/projects/base/info/video_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ VideoTrack::VideoTrack()
_video_timescale(0),
_width(0),
_height(0),
_key_frame_interval(0),
_key_frame_interval(0),
_key_frame_interval_conf(0),
_b_frames(0),
_has_bframe(false),
_preset(""),
Expand Down Expand Up @@ -116,7 +117,7 @@ void VideoTrack::SetHasBframes(bool has_bframe)
_has_bframe = has_bframe;
}

bool VideoTrack::HasBframes()
bool VideoTrack::HasBframes() const
{
return _has_bframe;
}
Expand All @@ -131,16 +132,36 @@ int VideoTrack::GetThreadCount()
return _thread_count;
}

void VideoTrack::SetKeyFrameInterval(int32_t key_frame_interval)
int32_t VideoTrack::GetKeyFrameInterval() const
{
if(_key_frame_interval_conf > 0)
{
return _key_frame_interval_conf;
}

return _key_frame_interval;
}

void VideoTrack::SetKeyFrameIntervalByMeasured(int32_t key_frame_interval)
{
_key_frame_interval = key_frame_interval;
}

int32_t VideoTrack::GetKeyFrameInterval()
int32_t VideoTrack::GetKeyFrameIntervalByMeasured() const
{
return _key_frame_interval;
}

void VideoTrack::SetKeyFrameIntervalByConfig(int32_t key_frame_interval)
{
_key_frame_interval_conf = key_frame_interval;
}

int32_t VideoTrack::GetKeyFrameIntervalByConfig() const
{
return _key_frame_interval_conf;
}

void VideoTrack::SetBFrames(int32_t b_frames)
{
_b_frames = b_frames;
Expand Down
17 changes: 13 additions & 4 deletions src/projects/base/info/video_track.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class VideoTrack
double GetVideoTimestampScale() const;

void SetHasBframes(bool has_bframe);
bool HasBframes();
bool HasBframes() const;

void SetColorspace(int colorspace);
int GetColorspace() const;
Expand All @@ -62,8 +62,15 @@ class VideoTrack
void SetThreadCount(int thread_count);
int GetThreadCount();

void SetKeyFrameInterval(int32_t key_frame_interval);
int32_t GetKeyFrameInterval();
// Return the proper key_frame_interval for this track.
// If there is a key_frame_interval set by the user, it is returned. If not, the automatically measured key_frame_interval is returned
int32_t GetKeyFrameInterval() const;

void SetKeyFrameIntervalByMeasured(int32_t key_frame_interval);
int32_t GetKeyFrameIntervalByMeasured() const;

void SetKeyFrameIntervalByConfig(int32_t key_frame_interval);
int32_t GetKeyFrameIntervalByConfig() const;

void SetBFrames(int32_t b_frames);
int32_t GetBFrames();
Expand Down Expand Up @@ -99,8 +106,10 @@ class VideoTrack
int32_t _width_conf;
int32_t _height_conf;

// Key Frame Interval (set by user)
// Key Frame Interval (measurement)
int32_t _key_frame_interval;
// Key Frame Interval (set by user)
int32_t _key_frame_interval_conf;

// Number of B-frame (set by user)
int32_t _b_frames;
Expand Down
4 changes: 2 additions & 2 deletions src/projects/mediarouter/mediarouter_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,8 +1077,6 @@ std::shared_ptr<MediaPacket> MediaRouteStream::Pop()
return nullptr;
}

media_track->OnFrameAdded(pop_media_packet->GetDataLength());

switch (GetInoutType())
{
case MediaRouterStreamType::INBOUND: {
Expand Down Expand Up @@ -1122,6 +1120,8 @@ std::shared_ptr<MediaPacket> MediaRouteStream::Pop()
break;
}

media_track->OnFrameAdded(pop_media_packet);

////////////////////////////////////////////////////////////////////////////////////
// Detect abnormal increases in PTS.
if (GetInoutType() == MediaRouterStreamType::INBOUND)
Expand Down
2 changes: 2 additions & 0 deletions src/projects/modules/json_serdes/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace serdes
SetString(object, "bitrate", ov::Converter::ToString(track->GetBitrate()), Optional::False);
SetFloat(object, "framerate", track->GetFrameRate());
SetTimebase(object, "timebase", track->GetTimeBase(), Optional::False);
SetBool(object, "hasBframes", track->HasBframes());
SetInt(object, "keyFrameInterval", track->GetKeyFrameInterval());
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/projects/transcoder/transcoder_stream_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ std::shared_ptr<MediaTrack> TranscoderStreamInternal::CreateOutputTrack(const st
output_track->SetFrameRateByConfig(profile.GetFramerate());
}

profile.GetKeyFrameInterval(&is_parsed);
if (is_parsed == true)
{
output_track->SetKeyFrameIntervalByConfig(profile.GetKeyFrameInterval());
}

output_track->SetMediaType(cmn::MediaType::Video);
output_track->SetId(NewTrackId());
output_track->SetVariantName(profile.GetName());
Expand Down Expand Up @@ -177,7 +183,6 @@ std::shared_ptr<MediaTrack> TranscoderStreamInternal::CreateOutputTrack(const st
output_track->SetTimeBase(GetDefaultTimebaseByCodecId(output_track->GetCodecId()));
output_track->SetPreset(profile.GetPreset());
output_track->SetThreadCount(profile.GetThreadCount());
output_track->SetKeyFrameInterval(profile.GetKeyFrameInterval());
output_track->SetBFrames(profile.GetBFrames());
output_track->SetProfile(profile.GetProfile());
}
Expand Down

0 comments on commit ef94cc3

Please sign in to comment.