我需要将Stream Dry消息发送到我的应用程序正在流式传输到的RTMP服务器。我创建了一个在avformat.h上声明并在rtmpproto.c上定义的新函数,其中包含以下内容:
int av_send_rtmp_streamdry(struct URLContext *s) {
RTMPContext *rt = s->priv_data;
PutByteContext pbc;
RTMPPacket spkt = { 0 };
int ret;
uint8_t *p;
av_log(s, AV_LOG_INFO, "%p", rt);
// Create StreamDry packet
// The packet type is the same as the PING response type. From RTMP spec,
// packet type 4 belongs to User Control Messages.
// The packet size is Event Type (16 bits / 2 bytes) + Stream ID (4 bytes)
if ((ret = ff_rtmp_packet_create(&spkt, RTMP_NETWORK_CHANNEL,
RTMP_PT_PING, 0, 6)) < 0) {
av_log(s, AV_LOG_ERROR, "Unable to create response packet\n");
return ret;
}
p = spkt.data;
bytestream2_init_writer(&pbc, spkt.data, spkt.size);
bytestream2_put_be16(&pbc, 2); // 2 -> Stream Dry
bytestream2_put_be32(&pbc, rt->stream_id);
spkt.size = p - spkt.data;
ret = ff_rtmp_packet_write(rt->stream, &spkt, rt->out_chunk_size,
&rt->prev_pkt[1], &rt->nb_prev_pkt[1]);
if(ret != 0){
av_log(NULL, AV_LOG_ERROR, "Stream Dry packet could not be sent");
return ret;
}
ff_rtmp_packet_destroy(&spkt);
return 0;}
我通过static_cast<URLContext*>(ofmt_ctx->pb->opaque)
获取URLContext,其中ofmt是我的AVFormatContext。
我直接从我的程序中调用这个方法,但问题是s-&gt; priv_data内容几乎不是一个有效的指针。
我该如何实现?
答案 0 :(得分:0)
最后我可以实现这个功能。问题是尝试从AVFormatContext获取RTMPContext的转换问题。
AVFormatContext字段&#39; pb&#39;包含AVIOContext *。
AVIOContext字段&#39; opaque&#39;包含一个空*,但在我的情况下(我不知道它是否总是这样)是一个AVIOInternal *。 AVIOInternal包含了我需要的URLContext *,但因为它是在&#39; aviobuf.c&#39;上定义的。文件我无法访问它,但它只包含一个字段。因此,解决方案是绕过AVIOInternal结构转换“不透明”。从AVIOContext到URLContext的字段**。 所以下面的代码是解决方案:
AVIOContext *io = output->pb;
URLContext *url = *((URLContext**)(io->opaque));
RTMPContext *rt =(RTMPContext*)(url->priv_data);
编辑:这是完整的工作代码。
int av_send_rtmp_streamdry(AVFormatContext *output) {
AVIOContext *io = output->pb;
URLContext *url = *((URLContext**)(io->opaque));
RTMPContext *rt =(RTMPContext*)(url->priv_data);
PutByteContext pbc;
RTMPPacket spkt = { 0 };
int ret;
uint8_t *p;
// Create StreamDry packet
// The packet type is the same as the PING response type. From RTMP spec,
// packet type 4 belongs to User Control Messages.
// The packet size is Event Type (16 bits / 2 bytes) + Stream ID (4 bytes)
if ((ret = ff_rtmp_packet_create(&spkt, RTMP_NETWORK_CHANNEL,
RTMP_PT_PING, 0, 6)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Unable to create response packet\n");
return ret;
}
bytestream2_init_writer(&pbc, spkt.data, spkt.size);
bytestream2_put_be16(&pbc, 2); // 2 -> Stream Dry
bytestream2_put_be32(&pbc, rt->stream_id);
ret = ff_rtmp_packet_write(rt->stream, &spkt, rt->out_chunk_size,
&rt->prev_pkt[1], &rt->nb_prev_pkt[1]);
ff_rtmp_packet_destroy(&spkt);
if(ret < 0){
av_log(NULL, AV_LOG_ERROR, "Stream Dry packet could not be sent");
return ret;
}
return 0;
}