fix last commits

This commit is contained in:
martii
2013-11-21 23:15:35 +01:00
parent 2d652aab01
commit 3e42b225f0
2 changed files with 76 additions and 178 deletions

View File

@@ -95,7 +95,7 @@ static const char *FILENAME = "container_ffmpeg.c";
/* ***************************** */
/* ***************************** */
/* Varaibles */
/* Variables */
/* ***************************** */
@@ -108,7 +108,7 @@ static unsigned char isContainerRunning = 0;
static long long int latestPts = 0;
float seek_sec_abs = 0.0, seek_sec_rel = 0.0;
float seek_sec_abs = -1.0, seek_sec_rel = 0.0;
/* ***************************** */
/* Prototypes */
@@ -225,9 +225,9 @@ long long int calcPts(AVStream * stream, int64_t pts)
else if (avContext->start_time == AV_NOPTS_VALUE)
pts = 90000.0 * (double) pts *av_q2d(stream->time_base);
else
pts =
90000.0 * (double) pts *av_q2d(stream->time_base) -
90000.0 * avContext->start_time / AV_TIME_BASE;
pts =
90000.0 * (double) pts *av_q2d(stream->time_base) -
90000.0 * avContext->start_time / AV_TIME_BASE;
if (pts & 0x8000000000000000ull)
pts = INVALID_PTS_VALUE;
@@ -303,9 +303,8 @@ static void FFMPEGThread(Context_t * context)
hasPlayThreadStarted = 1;
int64_t lastPts = -1, currentVideoPts = -1, currentAudioPts =
int64_t currentVideoPts = -1, currentAudioPts =
-1, showtime = 0, bofcount = 0;
off_t lastPos = -1;
AudioVideoOut_t avOut;
SwrContext *swr = NULL;
@@ -334,9 +333,6 @@ static void FFMPEGThread(Context_t * context)
continue;
}
if (!context->playback || !context->playback->isPlaying)
continue;
int seek_target_flag = 0;
int64_t seek_target = INT64_MIN;
@@ -347,19 +343,13 @@ static void FFMPEGThread(Context_t * context)
seek_target_flag = AVSEEK_FLAG_BYTE;
seek_target = avio_tell(avContext->pb) + seek_sec_rel * br;
} else {
lastPts =
(currentVideoPts >
0) ? currentVideoPts : currentAudioPts;
if (lastPts > 0)
seek_target =
((currentVideoPts >
0) ? currentVideoPts : currentAudioPts) +
seek_sec_rel * AV_TIME_BASE;
seek_target =
((((currentVideoPts >
0) ? currentVideoPts : currentAudioPts) /
90000.0) + seek_sec_rel) * AV_TIME_BASE;
}
seek_sec_rel = 0.0;
lastPts = -1;
lastPos = -1;
} else if (seek_sec_abs != 0.0) {
} else if (seek_sec_abs >= 0.0) {
if (avContext->iformat->flags & AVFMT_TS_DISCONT) {
float br = (avContext->bit_rate) ? br =
avContext->bit_rate / 8.0 : 180000.0;
@@ -368,9 +358,7 @@ static void FFMPEGThread(Context_t * context)
} else {
seek_target = seek_sec_abs * AV_TIME_BASE;
}
seek_sec_abs = 0.0;
lastPts = -1;
lastPos = -1;
seek_sec_abs = -1.0;
} else if (context->playback->BackWard && av_gettime() >= showtime) {
context->output->Command(context, OUTPUT_CLEAR, "video");
@@ -381,35 +369,27 @@ static void FFMPEGThread(Context_t * context)
}
if (avContext->iformat->flags & AVFMT_TS_DISCONT) {
if (lastPos < 0)
lastPos = avio_tell(avContext->pb);
off_t pos = avio_tell(avContext->pb);
if (lastPos > 0) {
if (pos > 0) {
float br;
if (avContext->bit_rate)
br = avContext->bit_rate / 8.0;
else
br = 180000.0;
lastPos += context->playback->Speed * 8 * br;
seek_target = pos + context->playback->Speed * 8 * br;
seek_target_flag = AVSEEK_FLAG_BYTE;
seek_target = lastPos;
}
} else {
if (lastPts < 0)
lastPts =
(currentVideoPts >
0) ? currentVideoPts : currentAudioPts;
if (lastPts > 0) {
lastPts += context->playback->Speed * 8 * AV_TIME_BASE;
seek_target = lastPts;
}
seek_target =
((((currentVideoPts >
0) ? currentVideoPts : currentAudioPts) /
90000.0) +
context->playback->Speed * 8) * AV_TIME_BASE;;
}
showtime = av_gettime() + 300000; //jump back every 300ms
} else {
bofcount = 0;
if (!context->playback->BackWard)
lastPts = -1, lastPos = -1;
}
if (seek_target > INT64_MIN) {
@@ -466,16 +446,16 @@ static void FFMPEGThread(Context_t * context)
Command(context, MANAGER_GET_TRACK, &audioTrack) < 0)
ffmpeg_err("error getting audio track\n");
if (context->manager->subtitle->
Command(context, MANAGER_GET_TRACK, &subtitleTrack) < 0)
if (context->manager->subtitle->Command(context, MANAGER_GET_TRACK,
&subtitleTrack) < 0)
ffmpeg_err("error getting subtitle track\n");
if (context->manager->dvbsubtitle->
Command(context, MANAGER_GET_TRACK, &dvbsubtitleTrack) < 0)
ffmpeg_err("error getting dvb subtitle track\n");
if (context->manager->teletext->
Command(context, MANAGER_GET_TRACK, &teletextTrack) < 0)
if (context->manager->teletext->Command(context, MANAGER_GET_TRACK,
&teletextTrack) < 0)
ffmpeg_err("error getting teletext track\n");
ffmpeg_printf(200, "packet.size %d - index %d\n", packet.size,
@@ -588,7 +568,8 @@ static void FFMPEGThread(Context_t * context)
if (!swr) {
int rates[] =
{ 48000, 96000, 192000, 44100, 88200, 176400,
0 };
0
};
int *rate = rates;
int in_rate = c->sample_rate;
while (*rate
@@ -670,8 +651,8 @@ static void FFMPEGThread(Context_t * context)
calcPts(audioTrack->stream, next_out_pts);
out_samples =
swr_convert(swr, &output, out_samples,
(const uint8_t **) &decoded_frame->
data[0], in_samples);
(const uint8_t **)
&decoded_frame->data[0], in_samples);
pcmPrivateData_t extradata;
@@ -912,11 +893,9 @@ static void FFMPEGThread(Context_t * context)
if (decoded_frame)
avcodec_free_frame(&decoded_frame);
avformat_close_input(&avContext);
hasPlayThreadStarted = 0;
if (context->playback)
context->playback->isPlaying = 0;
context->playback->abortRequested = 1;
hasPlayThreadStarted = 0;
ffmpeg_printf(10, "terminating\n");
}
@@ -957,6 +936,7 @@ int container_ffmpeg_init(Context_t * context, char *filename)
ffmpeg_err("ups already running?\n");
return cERR_CONTAINER_FFMPEG_RUNNING;
}
isContainerRunning = 1;
/* initialize ffmpeg */
avcodec_register_all();
@@ -976,6 +956,7 @@ int container_ffmpeg_init(Context_t * context, char *filename)
av_strerror(err, error, 512);
ffmpeg_err("Cause: %s\n", error);
isContainerRunning = 0;
return cERR_CONTAINER_FFMPEG_OPEN;
}
@@ -994,14 +975,13 @@ int container_ffmpeg_init(Context_t * context, char *filename)
* until other works are done and we can prove this.
*/
avformat_close_input(&avContext);
//for buffered io (end)
isContainerRunning = 0;
return cERR_CONTAINER_FFMPEG_STREAM;
#endif
}
terminating = 0;
latestPts = 0;
isContainerRunning = 1;
int res = container_ffmpeg_update_tracks(context, filename, 1);
return res;
}
@@ -1264,7 +1244,8 @@ int container_ffmpeg_update_tracks(Context_t * context, char *filename,
memset(track.aacbuf, 0, track.aacbuflen);
unsigned char ASF_Stream_Properties_Object[16] =
{ 0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11,
0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 };
0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65
};
memcpy(track.aacbuf + 0, ASF_Stream_Properties_Object, 16); // ASF_Stream_Properties_Object
memcpy(track.aacbuf + 16, &track.aacbuflen, 4); //FrameDateLength
@@ -1273,12 +1254,14 @@ int container_ffmpeg_update_tracks(Context_t * context, char *filename,
unsigned char ASF_Audio_Media[16] =
{ 0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11,
0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B };
0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B
};
memcpy(track.aacbuf + 24, ASF_Audio_Media, 16); //ASF_Audio_Media
unsigned char ASF_Audio_Spread[16] =
{ 0x50, 0xCD, 0xC3, 0xBF, 0x8F, 0x61, 0xCF, 0x11,
0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20 };
0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20
};
memcpy(track.aacbuf + 40, ASF_Audio_Spread, 16); //ASF_Audio_Spread
memset(track.aacbuf + 56, 0, 4); // time_offset (not used)
@@ -1547,10 +1530,8 @@ static int container_ffmpeg_stop(Context_t * context)
ffmpeg_err("Container not running\n");
return cERR_CONTAINER_FFMPEG_ERR;
}
if (context->playback) {
context->playback->isPlaying = 0;
if (context->playback)
context->playback->abortRequested = 1;
}
while (hasPlayThreadStarted != 0)
usleep(100000);
@@ -1574,7 +1555,7 @@ static int container_ffmpeg_seek(Context_t * context
if (absolute)
seek_sec_abs = sec, seek_sec_rel = 0.0;
else
seek_sec_abs = 0.0, seek_sec_rel = sec;
seek_sec_abs = -1.0, seek_sec_rel = sec;
return cERR_CONTAINER_FFMPEG_NO_ERROR;
}
@@ -1809,8 +1790,9 @@ static int Command(void *_context, ContainerCmd_t command, void *argument)
static char *FFMPEG_Capabilities[] =
{ "avi", "mkv", "mp4", "ts", "mov", "flv", "flac", "mp3", "mpg",
"m2ts", "vob", "wmv", "wma", "asf", "mp2", "m4v", "m4a", "divx", "dat", "mpeg", "trp", "mts", "vdr",
"ogg", "wav", "wtv", "ogm", "3gp", NULL };
"m2ts", "vob", "wmv", "wma", "asf", "mp2", "m4v", "m4a", "divx", "dat",
"mpeg", "trp", "mts", "vdr", "ogg", "wav", "wtv", "ogm", "3gp", NULL
};
Container_t FFMPEGContainer = {
"FFMPEG",

View File

@@ -29,7 +29,7 @@
#define PLAYBACK_DEBUG
static short debug_level = 0;
static short debug_level = 50;
static const char *FILENAME = "playback.c";
#ifdef PLAYBACK_DEBUG
@@ -73,100 +73,21 @@ static int PlaybackTerminate(Context_t * context);
static void SupervisorThread(Context_t * context)
{
int status = 0, lastStatus = 0;
long long int playPts = -1;
long long int lastPts = -1;
int dieNow = 0;
int count = 0;
hasThreadStarted = 1;
playback_printf(10, ">\n");
while (context && context->playback && context->playback->isPlaying) {
if (context->container->selectedContainer != NULL)
context->container->selectedContainer->Command(context,
CONTAINER_STATUS,
&status);
else
dieNow = 1;
if (context->container->selectedContainer != NULL)
context->container->selectedContainer->Command(context,
CONTAINER_LAST_PTS,
&lastPts);
#ifdef FRAMECOUNT_WORKS
// This is a good place to implement buffer managment
long long int frameCount = -1;
int ret =
context->playback->Command(context, PLAYBACK_GET_FRAME_COUNT,
&frameCount);
playback_printf(1, "Framecount = %ull\n", frameCount);
status = 1;
#endif
if ((status == 0) && (status != lastStatus)) {
playback_printf(1,
"container has ended, syncing to playback pts ...\n");
#define FLUSH_AFTER_CONTAINER_ENDED
#ifdef FLUSH_AFTER_CONTAINER_ENDED
// These means that we have injected everything we got, so flush it.
// As this is a thread, the call should block the thread as long as frames are beeing played.
// The main thread should not be blocked by this.
// This also helps for files which dont have any pts at all
if (context->output->Command(context, OUTPUT_FLUSH, NULL) < 0) {
playback_err("failed to flush output.\n");
}
#endif
while (!dieNow) {
if (context && context->playback
&& context->playback->abortRequested)
dieNow = 1;
else if (context && context->playback
&& context->playback->isPlaying) {
int ret =
context->playback->Command(context, PLAYBACK_PTS,
&playPts);
playback_err
("playbackPts %lld ->lastPts %lld ret %d\n",
playPts, lastPts, ret);
if (ret != cERR_PLAYBACK_NO_ERROR
|| playPts + (2 * 90000) >= lastPts)
dieNow = 1;
} else {
playback_err("playback already died ?\n");
dieNow = 1;
}
count++;
if (count == 200) {
// playback_err("something went wrong, expect end but never reached?\n");
dieNow = 1;
}
usleep(10000);
}
}
lastStatus = status;
if (dieNow)
break;
usleep(10000);
} /* while */
while (context && context->playback && context->playback->isPlaying
&& !context->playback->abortRequested)
usleep(100000);
playback_printf(10, "<\n");
hasThreadStarted = 0;
hasThreadStarted = 2;
PlaybackTerminate(context);
playback_printf(0, "terminating\n");
hasThreadStarted = 0;
}
/* ***************************** */
@@ -210,16 +131,14 @@ static int PlaybackOpen(Context_t * context, char *uri)
if (context->container && context->container->textSrtContainer)
context->container->textSrtContainer->Command(context,
CONTAINER_INIT,
context->
playback->uri +
7);
context->playback->
uri + 7);
if (context->container && context->container->textSsaContainer)
context->container->textSsaContainer->Command(context,
CONTAINER_INIT,
context->
playback->uri +
7);
context->playback->
uri + 7);
if (context->container && context->container->assContainer)
context->container->assContainer->Command(context,
@@ -245,8 +164,9 @@ static int PlaybackOpen(Context_t * context, char *uri)
if ((context->container->Command(context, CONTAINER_ADD, extension) <
0)
|| (!context->container->selectedContainer)
|| (context->container->selectedContainer->
Command(context, CONTAINER_INIT, context->playback->uri) < 0))
|| (context->container->
selectedContainer->Command(context, CONTAINER_INIT,
context->playback->uri) < 0))
return cERR_PLAYBACK_ERROR;
playback_printf(10, "exiting with value 0\n");
@@ -338,12 +258,9 @@ static int PlaybackPlay(Context_t * context)
"Error creating thread, error:%d:%s\n",
error, strerror(error));
hasThreadStarted = 0;
ret = cERR_PLAYBACK_ERROR;
} else {
playback_printf(10, "Created thread\n");
hasThreadStarted = 1;
}
}
@@ -467,7 +384,7 @@ static int PlaybackStop(Context_t * context)
ret = cERR_PLAYBACK_ERROR;
}
while ((hasThreadStarted != 0) && (--wait_time) > 0) {
while ((hasThreadStarted == 1) && (--wait_time) > 0) {
playback_printf(10,
"Waiting for supervisor thread to terminate itself, will try another %d times\n",
wait_time);
@@ -504,7 +421,6 @@ static int PlaybackTerminate(Context_t * context)
context->container->selectedContainer->Command(context,
CONTAINER_STOP,
NULL);
context->playback->isPaused = 0;
context->playback->isPlaying = 0;
context->playback->isForwarding = 0;
@@ -522,7 +438,7 @@ static int PlaybackTerminate(Context_t * context)
*/
}
while ((hasThreadStarted != 0) && (--wait_time) > 0) {
while ((hasThreadStarted == 1) && (--wait_time) > 0) {
playback_printf(10,
"Waiting for supervisor thread to terminate itself, will try another %d times\n",
wait_time);
@@ -550,8 +466,8 @@ static int PlaybackFastForward(Context_t * context, int *speed)
/* Audio only forwarding not supported */
if (context->playback->isVideo && !context->playback->isHttp
&& !context->playback->BackWard && (!context->playback->isPaused
|| context->playback->
isPlaying)) {
|| context->
playback->isPlaying)) {
if ((*speed <= 0) || (*speed > cMaxSpeed_ff)) {
playback_err("speed %d out of range (1 - %d) \n", *speed,
@@ -817,8 +733,8 @@ static int PlaybackSwitchSubtitle(Context_t * context, int *track)
if (context->manager && context->manager->subtitle) {
int trackid;
if (context->manager->subtitle->
Command(context, MANAGER_SET, track) < 0) {
if (context->manager->
subtitle->Command(context, MANAGER_SET, track) < 0) {
playback_err("manager set track failed\n");
}
#if 0
@@ -828,17 +744,15 @@ static int PlaybackSwitchSubtitle(Context_t * context, int *track)
&& context->container->textSrtContainer)
context->container->textSrtContainer->Command(context,
CONTAINER_INIT,
context->
playback->
uri + 7);
context->playback->uri
+ 7);
if (context->container
&& context->container->textSsaContainer)
context->container->textSsaContainer->Command(context,
CONTAINER_INIT,
context->
playback->
uri + 7);
context->playback->uri
+ 7);
if (context->container && context->container->assContainer)
context->container->assContainer->Command(context,
@@ -895,9 +809,10 @@ static int PlaybackSwitchDVBSubtitle(Context_t * context, int *pid)
playback_printf(10, "Track: %d\n", *pid);
if (context && context->manager && context->manager->dvbsubtitle) {
if (context->manager->dvbsubtitle->
Command(context, *pid < 0 ? MANAGER_DEL : MANAGER_SET,
pid) < 0) {
if (context->manager->
dvbsubtitle->Command(context,
*pid < 0 ? MANAGER_DEL : MANAGER_SET,
pid) < 0) {
playback_err("dvbsub manager set track failed\n");
ret = cERR_PLAYBACK_ERROR;
}
@@ -919,8 +834,9 @@ static int PlaybackSwitchTeletext(Context_t * context, int *pid)
playback_printf(10, "Track: %d\n", *pid);
if (context && context->manager && context->manager->teletext) {
if (context->manager->teletext->
Command(context, *pid < 0 ? MANAGER_DEL : MANAGER_SET, pid)) {
if (context->manager->
teletext->Command(context,
*pid < 0 ? MANAGER_DEL : MANAGER_SET, pid)) {
playback_err("ttxsub manager set track failed\n");
ret = cERR_PLAYBACK_ERROR;
}
@@ -1036,7 +952,7 @@ static int Command(void *_context, PlaybackCmd_t command, void *argument)
break;
}
case PLAYBACK_GET_FRAME_COUNT:{
// 10
// 10
ret =
PlaybackGetFrameCount(context,
(unsigned long long int *) argument);